2.1.0 • Published 1 year ago
strapi-js v2.1.0
strapi-js
A nodejs/browser typescript/javascript sdk for Strapi headless CMS. You can use it to communicate with Strapi-CMS in client-side applications (such as a Vue.js SPA) or server-side applications (such as an SSR server like Nuxt or other NodeJs based applications).
Features
- 📦 TypeScript & Fully Typed APIs
- ✔️ based on Ofetch and Works with Node.js / Browser / Edge
- ⚡️ Fast Build System Vite, pnpm, esbuild
- ⚙️ Unit Testing with Vitest and high test coverage.
🚀 Quick Start
Install:
# npm
npm i strapi-js
# pnpm
pnpm add strapi-js
# yarn
yarn add strapi-jsImport & Simple Usage:
import { newStrapiSdk } from 'strapi-js'
// CommonJS: const { newStrapiSdk } = require('strapi-js')
// Usage: 1) provide config (the default configuration)
const config = {
url: 'http://localhost:1337/',
prefix: '/api',
version: 'v4',
logType: 'auto',
retry: 0, // Number of auto retries for requests that have encountered an error
}
// Usage: 2) obtain sdk instance
const sdk = newStrapiSdk (config)
// Usage: 3) use it
const { user, jwt } = sdk.auth.login ({
identifier: 'mgh@gmail.com',
password: 'pass123'
})
sdk.auth.changePassword({
currentPassword: 'pass123',
password: 'pass123456',
passwordConfirmation: 'pass123456'
})Docs
Auth Sdk
This sdk i.e. sdk.auth contains APIs of Strapi related to path /api/auth for login, register, change-pass etc:
login: login into strapi providing identifier (username/email) and passwordregister: registers new user providing username, email and passwordemailConfirmation: confirms user's account with confirmation codesendEmailConfirmation: resends the activation emailchangePassword: changes user's password providing jwt-token, current-password and new-passwordforgotPassword: initiates a password reset request for a user's account using a registered email addressresetPassword: sets new password using a code received with emailutilGetCurrentUser: a utility function to get logged-in user info using the jwt-token (equal tosdk.users.me())
Users Sdk
This sdk i.e. sdk.users contains APIs of Strapi related to path /api/users for getting and setting users:
me: gets logged-in user's info using the jwt-tokengetUsers: gets a list of registered usersgetUserById: gets user's data by user's idupdateUserById: updates user's data by user's iddeleteUserById: deletes a user by user's id
Upload Sdk
This sdk i.e. sdk.upload contains APIs of Strapi related to path /api/upload for working with files/media:
upload: uploads a file and simultaneously links it to a field in an entitygetFiles: gets a list of uploaded filesgetFileById: gets details of a file using the file's iddeleteFileById: deletes a file using the file's id
Content Sdk (Collection Type)
This sdk i.e. sdk.content contains APIs of Strapi related to path /api/:pluralApiId and /api/:pluralApiId/:documentId for working with collection type:
find: gets a list of entries stored in thispluralApiId.create: creates a new entry.fineOne: gets details of an entry using the entry's idupdate: updates details of an entry using the entry's iddeleteFileById: deletes an entry using the entry's id