strapi-client v1.0.0
strapi-client
An http client to easily access Strapi.io API.
Usage
Create an instance
const StrapiClient = require('strapi-client')
const strapi = new StrapiClient('http://localhost:1337')
Login to the API
login(identifier, password)
const { jwt, user } = await strapi.login('uname', 'pword')
console.log('Token:', jwt)
console.log('User:', user)
If the login succeeds, the token will automatically be applied to the Authorization
header.
Get currently authenticated user
currentUser(token)
const user = await strapi.currentUser()
token
is optional. If a user has recently logged in, the token is automatically applied to the Authorization
header.
Persistence of the token
should be managed by the developer. To reauthenticate a user, just pass the token to the currentUser()
method.
const user = await strapi.currentUser('efghijk1234567...')
Register an account
register(data)
let newUser = {
email: 'email@email.com',
username: 'uname',
password: 'pword'
}
const { jwt, user } = strapi.register(newUser)
console.log('Token:', jwt)
console.log('User:', user)
Count the entries of a content-type
count(contentType, query)
const numberOfUsers = await strapi.count('users')
query
is optional. It accepts an object that is passed as HTTP params along with the GET
request. See Strapi's documentation about Parameters.
Create an entry
create(contentType, data)
const article = await strapi.create('articles', {
title: 'Welcome to Strapi Client!',
content: 'Strapi-client helps us do CRUD easily.'
})
Returns the created entry.
Get a list or a specific entry
get(contentType, id | query)
// Get a specific user
const user = await strapi.get('users', 2)
// Get a list of users
const users = await strapi.get('users')
query
is optional. See the definition of this argument above. If a number
is passed instead of an object
, the method will return an object containing the entry of the given ID
. Otherwise, will return an array
of entries of the content-type.
Update an entry
update(contentType, id, updatedData)
const updatedArticle = await strapi.update('articles', 12, {
title: 'Strapi Client - CRUD made simple!'
})
Returns the updated entry.
Delete an entry
delete(contentType, id)
const deletedArticle = await strapi.delete('articles', 12)
Returns the deleted entry.
Note:
- All method returns a
Promise.reject
if a server or connectivity issue occurs. - If the api fails to perform a request, it will return an object containing the error information.
TODO:
- Improve the documentation.
- Add other necessary methods.
- Find support and contributions. :P
4 years ago