npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@iamsamwen/strapi-api

v1.1.6

Published

A wrapper of strapi api calls util class to simplify the code to manipulate strapi data and media files

Downloads

3

Readme

strapi-api for strap v4

A wrapper of strapi api calls util class to simplify the code to manipulate strapi data and media files.

Install

     npm install @iamsamwen/strapi-api

OR

     yarn add @iamsamwen/strapi-api

Setup

create .env file in your project root directory with following name and values:

STRAPI_BASE_URL=http://localhost:1337
STRAPI_API_TOKEN=xxx
STRAPI_ADMIN_EMAIL=xxx
STRAPI_ADMIN_PASSWORD=xxx

With STRAPI_ADMIN_EMAIL and STRAPI_ADMIN_PASSWORD setup, strapi-api can call APIs used by strapi admin frontend.

Examples

if the .env file is setup correctly, strapi-api can create content type tests:

created content type tests with field name title

'use strict';

require('dotenv').config();

const StrapiApi = require('@iamsamwen/strapi-api');

(async () => {

    const strapi = new StrapiApi();

    const data = {
        contentType: {
            draftAndPublish: true,
            singularName: 'test',
            pluralName: 'tests',
            displayName: 'test',
            kind: 'collectionType',
            attributes: { title: { type: 'string' } }
        }
    };
    const result = await strapi.post('/content-type-builder/content-types', data);
    
    console.log(result);

})();

The rest of examples are assuming that you have setup .env file and created a content type tests with field name title.

example 1 - simple operations

'use strict';

require('dotenv').config();

const StrapiApi = require('@iamsamwen/strapi-api');

(async () => {

    const strapi = new StrapiApi();

    // create an entry
    //
    let result = await strapi.post('/api/tests', {data: {title: 'example'}});
    console.log(result);

    const id = result.data.id;

    // get the created entry
    //
    result = await strapi.get('/api/tests', id);
    console.log(result);

    // get all entries of tests
    //
    result = await strapi.get_all('/api/tests');
    console.log(result);

    // update the entry
    //
    result = await strapi.put('/api/tests', id, {data: {title: 'update example'}});
    console.log(result);

    // delete the entry
    //
    result = await strapi.del('/api/tests', id);
    console.log(result);

})();

example 2 - upload a media file

'use strict';

require('dotenv').config();

const StrapiApi = require('@iamsamwen/strapi-api');

(async () => {

    const strapi = new StrapiApi();

    const result = await strapi.upload_file(__dirname + '/image.png', {path: 't-shirts', name: 'color blue', caption: 't-shirts', alternativeText: 'color blue'});

    console.log(result);

})()

example 3 - get all content types (an api used by admin frontend)

'use strict';

require('dotenv').config();

const StrapiApi = require('@iamsamwen/strapi-api');

(async () => {

    const strapi = new StrapiApi();

    const result = await strapi.get('/content-manager/content-types');

    console.log(result);

})();

You may noticed that the data layout of results from api call and admin call are different.

list of methods

all methods are async. For how to use query, please read strapi doc for REST API: Filtering, Locale, and Publication State.

| name | arguments | comments | | -------------- | ------------------------------------------------------------ |------------------------------------------------------------| | constructor| {base_url, api_token, admin_email, admin_password, page_size, batch_size, api_log, api_debug} | set with environment variable, prefix with STRAPI_ and capitalized name. for example: base_url => STRAPI_BASE_URL | |get|path, id, query| path is an api path, i.e., /api/tests| |post|path, data|{data: {title: 'hello'}}| |put|path, id, data|id is the strapi data id| |del|path, id|| |search|path, query|query: {title: {$contains: 'llo'}}| |count|path, query|| |get_ids|path, query|| |get_all|path, query|| |get_page|path, query, page, page_size|| |del_all|path, query|| |upload_file|filepath, {ref, id, field, path, name, caption, alternativeText}|