shared-library/payload/sdk.ts

217 lines
7.8 KiB
TypeScript
Raw Normal View History

2024-07-13 08:55:10 +00:00
import { Collections } from "./constants";
2024-07-13 11:05:20 +00:00
import type {
EndpointWebsiteConfig,
EndpointFolder,
EndpointLanguage,
EndpointCurrency,
EndpointWording,
EndpointPage,
2024-07-13 08:55:10 +00:00
EndpointCollectible,
2024-07-13 11:05:20 +00:00
EndpointCollectibleScans,
EndpointCollectibleScanPage,
2024-07-13 08:55:10 +00:00
EndpointCollectibleGallery,
EndpointCollectibleGalleryImage,
2024-07-13 11:05:20 +00:00
EndpointChronologyEvent,
2024-07-13 08:55:10 +00:00
EndpointImage,
2024-07-13 11:05:20 +00:00
EndpointAudio,
2024-07-13 08:55:10 +00:00
EndpointVideo,
2024-07-13 11:05:20 +00:00
EndpointFile,
EndpointRecorder,
2024-07-13 08:55:10 +00:00
} from "./endpoint-types";
2024-07-26 05:40:33 +00:00
import type { EndpointChange } from "./webhooks";
export enum SDKEndpointNames {
getWebsiteConfig = "getWebsiteConfig",
getFolder = "getFolder",
getLanguages = "getLanguages",
getCurrencies = "getCurrencies",
getWordings = "getWordings",
getPage = "getPage",
getCollectible = "getCollectible",
getCollectibleScans = "getCollectibleScans",
getCollectibleScanPage = "getCollectibleScanPage",
getCollectibleGallery = "getCollectibleGallery",
getCollectibleGalleryImage = "getCollectibleGalleryImage",
getChronologyEvents = "getChronologyEvents",
getChronologyEventByID = "getChronologyEventByID",
getImageByID = "getImageByID",
getAudioByID = "getAudioByID",
getVideoByID = "getVideoByID",
getFileByID = "getFileByID",
getRecorderByID = "getRecorderByID",
getLogin = "getLogin",
getAll = "getAll",
}
2024-07-13 08:55:10 +00:00
export const getSDKEndpoint = {
2024-07-26 05:40:33 +00:00
getWebsiteConfig: () => `/globals/${Collections.WebsiteConfig}/config`,
getFolder: (slug: string) => `/${Collections.Folders}/slug/${slug}`,
getLanguages: () => `/${Collections.Languages}/all`,
getCurrencies: () => `/${Collections.Currencies}/all`,
getWordings: () => `/${Collections.Wordings}/all`,
getPage: (slug: string) => `/${Collections.Pages}/slug/${slug}`,
getCollectible: (slug: string) => `/${Collections.Collectibles}/slug/${slug}`,
getCollectibleScans: (slug: string) => `/${Collections.Collectibles}/slug/${slug}/scans`,
getCollectibleScanPage: (slug: string, index: string) =>
2024-07-13 08:55:10 +00:00
`/${Collections.Collectibles}/slug/${slug}/scans/${index}`,
2024-07-26 05:40:33 +00:00
getCollectibleGallery: (slug: string) => `/${Collections.Collectibles}/slug/${slug}/gallery`,
getCollectibleGalleryImage: (slug: string, index: string) =>
2024-07-13 08:55:10 +00:00
`/${Collections.Collectibles}/slug/${slug}/gallery/${index}`,
2024-07-26 05:40:33 +00:00
getChronologyEvents: () => `/${Collections.ChronologyEvents}/all`,
getChronologyEventByID: (id: string) => `/${Collections.ChronologyEvents}/id/${id}`,
getImageByID: (id: string) => `/${Collections.Images}/id/${id}`,
getAudioByID: (id: string) => `/${Collections.Audios}/id/${id}`,
getVideoByID: (id: string) => `/${Collections.Videos}/id/${id}`,
getFileByID: (id: string) => `/${Collections.Files}/id/${id}`,
getRecorderByID: (id: string) => `/${Collections.Recorders}/id/${id}`,
getLogin: () => `/${Collections.Recorders}/login`,
getAll: () => `/all`,
} satisfies Record<SDKEndpointNames, (...params: string[]) => string>;
2024-07-13 08:55:10 +00:00
2024-07-14 15:04:57 +00:00
export type PayloadSDKResponse<T> = {
2024-07-13 08:55:10 +00:00
data: T;
endpointCalled: string;
};
type PayloadTokenCache = {
set: (token: string, expirationTimestamp: number) => void;
get: () => string | undefined;
};
type PayloadDataCache = {
set: (url: string, response: any) => void;
get: (url: string) => any | undefined;
};
export class PayloadSDK {
private tokenCache: PayloadTokenCache | undefined;
private dataCache: PayloadDataCache | undefined;
constructor(
private readonly apiURL: string,
private readonly email: string,
private readonly password: string
) {}
addTokenCache(tokenCache: PayloadTokenCache) {
this.tokenCache = tokenCache;
}
addDataCache(dataCache: PayloadDataCache) {
this.dataCache = dataCache;
}
private logResponse(res: Response) {
console.log(res.status, res.statusText, res.url);
}
private async refreshToken() {
2024-07-26 05:40:33 +00:00
const loginUrl = `${this.apiURL}${getSDKEndpoint.getLogin()}`;
2024-07-13 08:55:10 +00:00
const loginResult = await fetch(loginUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email: this.email, password: this.password }),
});
this.logResponse(loginResult);
if (loginResult.status !== 200) {
throw new Error("Unable to login");
}
const { token, exp } = (await loginResult.json()) as {
token: string;
exp: number;
};
this.tokenCache?.set(token, exp);
return token;
}
async request<T>(endpoint: string): Promise<PayloadSDKResponse<T>> {
const cachedResponse = this.dataCache?.get(endpoint);
if (cachedResponse) {
return cachedResponse;
}
const result = await fetch(`${this.apiURL}${endpoint}`, {
headers: {
Authorization: `JWT ${this.tokenCache?.get() ?? (await this.refreshToken())}`,
2024-07-13 08:55:10 +00:00
},
});
this.logResponse(result);
if (!result.ok) {
throw new Error("Unhandled fetch error");
}
const response = { data: await result.json(), endpointCalled: endpoint };
this.dataCache?.set(endpoint, response);
return response;
}
2024-07-26 05:40:33 +00:00
async getWebsiteConfig(): Promise<PayloadSDKResponse<EndpointWebsiteConfig>> {
return await this.request(getSDKEndpoint.getWebsiteConfig());
2024-07-13 08:55:10 +00:00
}
async getFolder(slug: string): Promise<PayloadSDKResponse<EndpointFolder>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getFolder(slug));
2024-07-13 08:55:10 +00:00
}
async getLanguages(): Promise<PayloadSDKResponse<EndpointLanguage[]>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getLanguages());
2024-07-13 08:55:10 +00:00
}
async getCurrencies(): Promise<PayloadSDKResponse<EndpointCurrency[]>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getCurrencies());
2024-07-13 08:55:10 +00:00
}
async getWordings(): Promise<PayloadSDKResponse<EndpointWording[]>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getWordings());
2024-07-13 08:55:10 +00:00
}
async getPage(slug: string): Promise<PayloadSDKResponse<EndpointPage>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getPage(slug));
2024-07-13 08:55:10 +00:00
}
async getCollectible(slug: string): Promise<PayloadSDKResponse<EndpointCollectible>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getCollectible(slug));
2024-07-13 08:55:10 +00:00
}
async getCollectibleScans(slug: string): Promise<PayloadSDKResponse<EndpointCollectibleScans>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getCollectibleScans(slug));
2024-07-13 08:55:10 +00:00
}
async getCollectibleScanPage(
slug: string,
index: string
): Promise<PayloadSDKResponse<EndpointCollectibleScanPage>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getCollectibleScanPage(slug, index));
2024-07-13 08:55:10 +00:00
}
async getCollectibleGallery(
slug: string
): Promise<PayloadSDKResponse<EndpointCollectibleGallery>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getCollectibleGallery(slug));
2024-07-13 08:55:10 +00:00
}
async getCollectibleGalleryImage(
slug: string,
index: string
): Promise<PayloadSDKResponse<EndpointCollectibleGalleryImage>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getCollectibleGalleryImage(slug, index));
2024-07-13 08:55:10 +00:00
}
async getChronologyEvents(): Promise<PayloadSDKResponse<EndpointChronologyEvent[]>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getChronologyEvents());
2024-07-13 08:55:10 +00:00
}
async getChronologyEventByID(id: string): Promise<PayloadSDKResponse<EndpointChronologyEvent>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getChronologyEventByID(id));
2024-07-13 08:55:10 +00:00
}
async getImageByID(id: string): Promise<PayloadSDKResponse<EndpointImage>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getImageByID(id));
2024-07-13 08:55:10 +00:00
}
async getAudioByID(id: string): Promise<PayloadSDKResponse<EndpointAudio>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getAudioByID(id));
2024-07-13 08:55:10 +00:00
}
async getVideoByID(id: string): Promise<PayloadSDKResponse<EndpointVideo>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getVideoByID(id));
2024-07-13 08:55:10 +00:00
}
async getFileByID(id: string): Promise<PayloadSDKResponse<EndpointFile>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getFileByID(id));
2024-07-13 08:55:10 +00:00
}
async getRecorderByID(id: string): Promise<PayloadSDKResponse<EndpointRecorder>> {
2024-07-26 05:40:33 +00:00
return await this.request(getSDKEndpoint.getRecorderByID(id));
2024-07-13 08:55:10 +00:00
}
2024-07-26 05:40:33 +00:00
async getAll(): Promise<PayloadSDKResponse<EndpointChange[]>> {
return await this.request(getSDKEndpoint.getAll());
2024-07-13 08:55:10 +00:00
}
}