Skip to content

Instantly share code, notes, and snippets.

@AlessioGr
Last active March 12, 2023 12:34
Show Gist options
  • Save AlessioGr/d3e7f46831ccbf9dcb8a4118171e428e to your computer and use it in GitHub Desktop.
Save AlessioGr/d3e7f46831ccbf9dcb8a4118171e428e to your computer and use it in GitHub Desktop.
Simple helper you can use to communicate with Payload CMS from your application via their REST API. Authorization via API key needs to be enabled in your users collection. More on enabling API keys here: https://payloadcms.com/docs/authentication/config#api-keys
import qs from 'qs';
export const cmsURL = "cms URL without the / at the end";
export const apiKey = "yourAPIkey";
export async function getDocuments (collectionName: string, whereQuery?) {
if (whereQuery) {
const stringifiedQuery = qs.stringify({
where: whereQuery
}, { addQueryPrefix: true });
const potentialFoundDocument = await (await fetch(`${cmsURL}/api/${collectionName}${stringifiedQuery}`, {
method: 'GET',
headers: {
"Content-Type": "application/json",
Authorization: `users API-Key ${apiKey}`
},
})).json()
return potentialFoundDocument;
} else {
const potentialFoundDocument = await (await fetch(`${cmsURL}/api/${collectionName}`, {
method: 'GET',
headers: {
"Content-Type": "application/json",
Authorization: `users API-Key ${apiKey}`
},
})).json()
return potentialFoundDocument;
}
}
export async function getDocumentByID (collectionName: string, id: string) {
const potentialFoundDocument = await (await fetch(`${cmsURL}/api/${collectionName}/${id}`, {
method: 'GET',
headers: {
"Content-Type": "application/json",
Authorization: `users API-Key ${apiKey}`
},
})).json()
return potentialFoundDocument;
}
export async function newDocument (collectionName: string, initialFields) {
const requestResponse = await (await fetch(`${cmsURL}/api/${collectionName}`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
Authorization: `users API-Key ${apiKey}`
},
body: JSON.stringify(initialFields),
})).json();
return requestResponse;
}
export async function updateDocument (collectionName: string, documentToUpdateID: string, updatedFields) {
const requestResponse = await (await fetch(`${cmsURL}/api/${collectionName}/${documentToUpdateID}`, {
method: 'PATCH',
headers: {
"Content-Type": "application/json",
Authorization: `users API-Key ${apiKey}`
},
body: JSON.stringify(updatedFields),
})).json();
return requestResponse;
}
import { getDocuments, getDocumentByID, newDocument, updateDocument } from './PayloadCommunicator';
export async function example (someObject) {
// Getting documents via where filter
const potentialFoundCategory = await getDocuments('categories', {
name: {
equals: someObject.category.name,
},
})
// Getting document by ID
const potentialFoundCategory2 = await getDocumentByID('categories', '63ad03ec20022d8e1b03cd21')
// Creating new document
const newArticleResponse = await newDocument('articles', {
path: someObject.path,
timestamp: someObject.timestamp,
category_relationship: potentialFoundCategory.docs[0].id,
});
// Updating document:
const updatedArticleResponse = await updateDocument('articles', newArticleResponse.doc.id, {
article_id_in_category: potentialFoundCategory.docs[0].article_count + 1,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment