Javascript client for Strapi Rest API.
npm i @kmariappan/strapi-client-js
npm i axios qs
or
yarn add @kmariappan/strapi-client-js
yarn add axios qs
This package uses axios as a http client and qs for parsing and stringifying the Query.
// Typescript
import { createClient, StrapiClientOptions } from '@kmariappan/strapi-client-js';
const options: StrapiClientOptions = {
url: 'http://localhost:1337/api',
apiToken: '', // Built in API token,
normalizeData: true, // Normalize Unified response Format. default - true
headers: {}, // Custom Headers
persistSession: false, // Persist authenticated token in browser local storage. default -false
};
const strapiClient = createClient(options);
import { createClient } from '@kmariappan/strapi-client-js';
const strapiClient = createClient({ url: 'http://localhost:1337/api' });
const run = async () => {
const { data, error, meta } = await strapiClient
.from('students')
// .from<Student>("students") ** typescript **
.select(['firstname', 'lastname']) // Select only specific fields.
// .select(["firstname", "lastname"]) ** typescript **
.get();
if (error) {
console.log(error);
} else {
console.log(data);
console.log(meta);
}
};
run();
Retrieve many entries by ids
const { data, error, meta } = await strapiClient.from('students').selectManyByID([200, 240]).get();
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.equalTo('id', 2)
.get();
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.between('id', [40, 45])
.get();
equalTo();
notEqualTo();
lessThan();
lessThanOrEqualTo();
greaterThan();
greaterThanOrEqualTo();
containsCaseSensitive();
notContainsCaseSensitive();
contains();
notContains();
isNull();
isNotNull();
between();
startsWith();
endsWith();
@param path - as string by relation
@param Operator "eq" | "ne" | "lt" | "gt" | "lte" | "gte" | "in" | "notIn" | "contains" | "notContains" | "startsWith" | "endsWith"
@param values can be string, number or array
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.filterDeep('address.city', 'eq', 'Munich')
.get();
Expects an array with the field and order example - [{ field: 'id', order: 'asc' }]
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.between('id', [40, 45])
.sortBy([{ field: 'id', order: 'desc' }])
.get();
Returns both draft entries & published entries.
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.withDraft()
.get();
Returns only draft entries.
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.onlyDraft()
.get();
Get entries from a specific locale.
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.setLocale('de')
.get();
To paginate results by page
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.paginate(1, 15)
.get();
To paginate results by offset
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.paginateByOffset(0, 25)
.get();
Populate 1 level for all relations
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.populate()
.get();
Populate 2 levels
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.populateWith<Address>('address', ['id', 'city'], true)
.get();
Populate Deep
const { data, error, meta } = await strapiClient
.from<Student>('students')
.select(['firstname', 'lastname'])
.populateDeep([
{
path: 'address',
fields: ['id', 'string'],
children: [{ key: 'country', fields: ['id', 'name'] }],
},
])
.get();
Create single record
const { data, error, meta } = await strapiClient
.from('students')
.create({ firstname: 'Vorname', lastname: 'Nachname' });
Create Many records
const { success } = await strapiClient.from('students').createMany([
{ firstname: 'muster', lastname: 'muster' },
{ firstname: 'muster1', lastname: 'muster1' },
]);
update();
updateMany();
deleteOne();
deleteMany();
signup new user
const { data, error } = await strapiClient.auth.signUp({
username: 'username',
email: 'name@gmail.com',
password: '12345678',
});
signin user
const { data, error } = await strapiClient.auth.signIn({
email: 'name@gmail.com',
password: '12345678',
});
signout user - removes the authentication token if saved in localstorage
const { error } = await strapiClient.auth.signOut();
Get url from the client Object
const url = strapiClient.getApiUrl();
console.log(url);