1.1.6 • Published 2 years ago

@iamsamwen/strapi-api v1.1.6

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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.

nameargumentscomments
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
getpath, id, querypath is an api path, i.e., /api/tests
postpath, data{data: {title: 'hello'}}
putpath, id, dataid is the strapi data id
delpath, id
searchpath, queryquery: {title: {$contains: 'llo'}}
countpath, query
get_idspath, query
get_allpath, query
get_pagepath, query, page, page_size
del_allpath, query
upload_filefilepath, {ref, id, field, path, name, caption, alternativeText}
1.1.6

2 years ago

1.1.5

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago