1.0.14 • Published 2 years ago

@casbah/strapi-fetch v1.0.14

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

Strapi-Fetch

A cross-fetch wrapper for Strapi's API.

Install

npm i @casbah/strapi-fetch -S

Usage

Import strapi-fetch:

import * as strapi from '@casbah/strapi-fetch';

Get Users

const response = await strapi.get<User[]>(
  'https://www.project.com/users?confirmed=true'
);

if (response.ok) {
  const users = response.data;
} else {
  const error = response.error;
}

Create User

const response = await strapi.post('https://www.project.com/users', {
  email: 'someother@email.com',
});

if (response.ok) {
  console.log(response.data);
} else {
  console.log(response.error);
}

Update User

const response = await strapi.put('https://www.project.com/users/1', {
  email: 'someother@email.com',
});

if (response.ok) {
  console.log(response.data);
} else {
  console.log(response.error);
}

Delete User

const response = await strapi.del('https://www.project.com/users/1');

if (response.ok) {
  console.log(response.data);
} else {
  console.log(response.error);
}

Authentication

const auth = await strapi.auth(
  'https://www.project.com/auth/local',
  'some@email.com',
  'somepassword'
);

if (auth.ok) {
  const response = await strapi.get<User[]>(
    'https://www.project.com/users',
    auth.data.jwt // Use JWT in other methods
  );
} else {
  console.log(auth.messages);
}

The response object

Every strapi-fetch method returns the same type of object, whether the request is successful or not.

interface StrapiFetchResponse<T> {
  ok: boolean;
  status?: number;
  error?: string;
  messages: string[];
  data?: T;
}

When the request is successful, the ok property will be true, and the data property will hold Strapi's data.

When the request is not successful, the ok property will be false, an the error property will contain the details of the error or exception. For more user-friendly error messages (for instance, "Username or password incorrect") use the messages property.

1.0.14

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

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.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago