1.0.0-alpha • Published 2 years ago

@thi3rry/ohmystrapi v1.0.0-alpha

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

ohmystrapi

Lib based on ohmyfetch to consume strapi API

Install

npm i ohmystrapi

Usage

Basic init

import ohMyStrapi from 'ohmystrapi';

const strapi = ohMyStrapi.useStrapi({
    baseUrl: 'http://localhost:1337',
});

You can fetch content with :

const articlesResponse = await strapi.createFetch()('/articles')

BUT! You can also use the ohmystrapi content-manager plugin

Plugins

ContentManager

import ohMyStrapi from 'ohmystrapi';
import {useSingleTypeEntityApi} from 'ohmystrapi';

strapi.use(ohMyStrapi.useEntityApi('articles'));
strapi.use(useSingleTypeEntityApi('settings'));

// get articles
const articles = await strapi.articles.find();
articles.forEach(article => console.log(article.title));

// get articles with "strapi" in content 
const articles = await strapi.articles.search({content: {$contains: 'strapi'}});
articles.data.forEach(article => console.log(article.title));

// get the first article with title contains strapi
const article = await strapi.articles.searchOne({title: {$contains: 'strapi'}});


// Toogle an `online` boolean on an single entity 'settings'
const {data: {online}} = await strapi.settings.find();
await strapi.settings.createOrUpdate({online: !online});
EntityMapper

You can pass as a second argument of the useEntityApi a callback that will be used to map every entity returns by find/findOne/search/searchOne etc...

// Put all attributes next to the id of the entity
strapi.use(useEntityApi('articles', (obj) => ({id: obj.id, ...obj.attributes})));

Auth user

import ohMyStrapi from 'ohmystrapi';

strapi.use(ohMyStrapi.useUsersPermissionsApi());

// Login a user
await strapi.user.login({identifier: 'login@example.com', password: 'password'});
const personnalData = await strapi.user.me();

Tests

It uses vitest to create test and fetch a local strapi. You need to start a local strapi instance to run tests