@smile/strapi-client
Version:
An HTTP client for Strapi focused on content and media creation (for mass data migration from another CMS/datasource)
129 lines (128 loc) • 4.82 kB
JavaScript
import { basename } from 'node:path';
import * as path from 'path';
import * as mime from 'mime-types';
const fetch = (await import('node-fetch')).default;
import { blobFromSync, FormData } from 'node-fetch';
export class StrapiClient {
constructor(strapiBaseUrl, token, adminToken) {
this.strapiBaseUrl = strapiBaseUrl;
this.token = token;
this.adminToken = adminToken;
this.apiBaseUrl = path.join(strapiBaseUrl, 'api');
}
configureScheduler(plugin) {
this.schedulerPlugin = plugin;
}
async createEntry(apiId, data) {
return await (await fetch(`${this.apiBaseUrl}/${apiId}`, {
method: 'post',
headers: {
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
data: data
})
})).json();
}
async updateEntry(apiId, id, data) {
return await (await fetch(`${this.apiBaseUrl}/${apiId}/${id}`, {
method: 'put',
headers: {
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
data: data
})
})).json();
}
async addMediaAsset(assetPathOrUrl, alt, caption) {
const blob = /^http(s)?:\/\//.test(assetPathOrUrl)
? await (await fetch(assetPathOrUrl)).blob()
: blobFromSync(assetPathOrUrl, mime.lookup(assetPathOrUrl));
const form = new FormData();
form.append('files', blob, basename(assetPathOrUrl));
form.append('fileInfo', JSON.stringify({
alternativeText: alt,
caption: caption
}));
return await (await fetch(`${this.apiBaseUrl}/upload`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.token}`,
},
body: form
})).json();
}
async createMediaFolder(folderName, parentId = null) {
if (!this.adminToken)
throw new Error('You must specify an admin token for media folder creation');
return await (await fetch(`${this.strapiBaseUrl}/upload/folders/`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.adminToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: folderName,
parent: parentId
})
})).json();
}
async moveMedia(folderId, mediaIds) {
if (!this.adminToken)
throw new Error('You must specify an admin token for media folder creation');
await fetch(`${this.strapiBaseUrl}/upload/actions/bulk-move`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.adminToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
destinationFolderId: folderId,
fileIds: mediaIds
})
});
}
async addPublishDate(contentType, id, date) {
return this._addPublisherDate(true, contentType, id, date);
}
async addUnpublishDate(contentType, id, date) {
return this._addPublisherDate(false, contentType, id, date);
}
async _addPublisherDate(publish, contentType, id, date) {
if (!this.schedulerPlugin)
throw new Error('You must specify a scheduler plugin in configureScheduler()');
if (!this.adminToken)
throw new Error('You must specify an admin token for adding a (un)publish date');
const body = {
'@webbio-strapi-plugin-scheduler': {
contentId: id,
uid: `api::${contentType}.${contentType}`,
scheduledDatetime: date,
scheduleType: publish ? 'schedule' : 'depublish',
},
'strapi-plugin-publisher': {
data: {
entityId: id,
entitySlug: `api::${contentType}.${contentType}`,
executeAt: date,
mode: publish ? 'publish' : 'unpublish',
}
}
}[this.schedulerPlugin];
const urlPath = {
'@webbio-strapi-plugin-scheduler': 'scheduler/create',
'strapi-plugin-publisher': 'publisher/actions'
}[this.schedulerPlugin];
await fetch(`${this.strapiBaseUrl}/${urlPath}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.adminToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body)
});
}
}