From bd1cd47dc18b85045386d6826bbd8f061c2fa1dc Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Sat, 29 Jun 2024 10:46:22 +0200 Subject: [PATCH] Turned SDK into a class --- src/sdk.ts | 216 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 130 insertions(+), 86 deletions(-) diff --git a/src/sdk.ts b/src/sdk.ts index ee4062f..da50780 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -537,30 +537,17 @@ export type EndpointAllSDKUrls = { // SDK -type GetPayloadSDKParams = { - apiURL: string; - email: string; - password: string; - tokenCache?: { - set: (token: string, expirationTimestamp: number) => void; - get: () => string | undefined; - }; - responseCache?: { - set: (url: string, response: any) => void; - get: (url: string) => any | undefined; - }; -}; - -const logResponse = (res: Response) => console.log(res.status, res.statusText, res.url); - export const getSDKEndpoint = { getConfigEndpoint: () => `/globals/${Collections.WebsiteConfig}/config`, getFolderEndpoint: (slug: string) => `/${Collections.Folders}/slug/${slug}`, + getFolderSlugsEndpoint: () => `/${Collections.Folders}/slugs`, getLanguagesEndpoint: () => `/${Collections.Languages}/all`, getCurrenciesEndpoint: () => `/${Collections.Currencies}/all`, getWordingsEndpoint: () => `/${Collections.Wordings}/all`, getPageEndpoint: (slug: string) => `/${Collections.Pages}/slug/${slug}`, + getPageSlugsEndpoint: () => `/${Collections.Pages}/slugs`, getCollectibleEndpoint: (slug: string) => `/${Collections.Collectibles}/slug/${slug}`, + getCollectibleSlugsEndpoint: () => `/${Collections.Collectibles}/slugs`, getCollectibleScansEndpoint: (slug: string) => `/${Collections.Collectibles}/slug/${slug}/scans`, getCollectibleScanPageEndpoint: (slug: string, index: string) => `/${Collections.Collectibles}/slug/${slug}/scans/${index}`, @@ -579,21 +566,51 @@ export const getSDKEndpoint = { getLoginEndpoint: () => `/${Collections.Recorders}/login`, }; -export const getPayloadSDK = ({ - apiURL, - email, - password, - tokenCache, - responseCache, -}: GetPayloadSDKParams) => { - const refreshToken = async () => { - const loginUrl = `${apiURL}${getSDKEndpoint.getLoginEndpoint()}`; +type PayloadSDKResponse = { + 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() { + const loginUrl = `${this.apiURL}${getSDKEndpoint.getLoginEndpoint()}`; const loginResult = await fetch(loginUrl, { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ email, password }), + body: JSON.stringify({ email: this.email, password: this.password }), }); - logResponse(loginResult); + this.logResponse(loginResult); if (loginResult.status !== 200) { throw new Error("Unable to login"); @@ -603,77 +620,104 @@ export const getPayloadSDK = ({ token: string; exp: number; }; - tokenCache?.set(token, exp); + this.tokenCache?.set(token, exp); return token; - }; + } - const request = async (endpoint: string): Promise => { - const cachedResponse = responseCache?.get(endpoint); + async request(endpoint: string): Promise> { + const cachedResponse = this.dataCache?.get(endpoint); if (cachedResponse) { return cachedResponse; } - const result = await fetch(`${apiURL}${endpoint}`, { + const result = await fetch(`${this.apiURL}${endpoint}`, { headers: { - Authorization: `JWT ${tokenCache?.get() ?? (await refreshToken())}`, + Authorization: `JWT ${this.tokenCache?.get() ?? (await this.refreshToken())}`, }, }); - logResponse(result); + this.logResponse(result); if (!result.ok) { throw new Error("Unhandled fetch error"); } - const data = await result.json(); - responseCache?.set(endpoint, data); - return data; - }; + const response = { data: await result.json(), endpointCalled: endpoint }; + this.dataCache?.set(endpoint, response); + return response; + } - return { - getConfig: async (): Promise => - await request(getSDKEndpoint.getConfigEndpoint()), - getFolder: async (slug: string): Promise => - await request(getSDKEndpoint.getFolderEndpoint(slug)), - getLanguages: async (): Promise => - await request(getSDKEndpoint.getLanguagesEndpoint()), - getCurrencies: async (): Promise => - await request(getSDKEndpoint.getCurrenciesEndpoint()), - getWordings: async (): Promise => - await request(getSDKEndpoint.getWordingsEndpoint()), - getPage: async (slug: string): Promise => - await request(getSDKEndpoint.getPageEndpoint(slug)), - getCollectible: async (slug: string): Promise => - await request(getSDKEndpoint.getCollectibleEndpoint(slug)), - getCollectibleScans: async (slug: string): Promise => - await request(getSDKEndpoint.getCollectibleScansEndpoint(slug)), - getCollectibleScanPage: async ( - slug: string, - index: string - ): Promise => - await request(getSDKEndpoint.getCollectibleScanPageEndpoint(slug, index)), - getCollectibleGallery: async (slug: string): Promise => - await request(getSDKEndpoint.getCollectibleGalleryEndpoint(slug)), - getCollectibleGalleryImage: async ( - slug: string, - index: string - ): Promise => - await request(getSDKEndpoint.getCollectibleGalleryImageEndpoint(slug, index)), - getChronologyEvents: async (): Promise => - await request(getSDKEndpoint.getChronologyEventsEndpoint()), - getChronologyEventByID: async (id: string): Promise => - await request(getSDKEndpoint.getChronologyEventByIDEndpoint(id)), - getImageByID: async (id: string): Promise => - await request(getSDKEndpoint.getImageByIDEndpoint(id)), - getAudioByID: async (id: string): Promise => - await request(getSDKEndpoint.getAudioByIDEndpoint(id)), - getVideoByID: async (id: string): Promise => - await request(getSDKEndpoint.getVideoByIDEndpoint(id)), - getFileByID: async (id: string): Promise => - await request(getSDKEndpoint.getFileByIDEndpoint(id)), - getRecorderByID: async (id: string): Promise => - await request(getSDKEndpoint.getRecorderByIDEndpoint(id)), - getAllSdkUrls: async (): Promise => - await request(getSDKEndpoint.getAllSDKUrlsEndpoint()), - request: async (pathname: string): Promise => await request(pathname), - }; -}; + async getConfig(): Promise> { + return await this.request(getSDKEndpoint.getConfigEndpoint()); + } + async getFolder(slug: string): Promise> { + return await this.request(getSDKEndpoint.getFolderEndpoint(slug)); + } + async getFolderSlugs(): Promise> { + return await this.request(getSDKEndpoint.getFolderSlugsEndpoint()); + } + async getLanguages(): Promise> { + return await this.request(getSDKEndpoint.getLanguagesEndpoint()); + } + async getCurrencies(): Promise> { + return await this.request(getSDKEndpoint.getCurrenciesEndpoint()); + } + async getWordings(): Promise> { + return await this.request(getSDKEndpoint.getWordingsEndpoint()); + } + async getPage(slug: string): Promise> { + return await this.request(getSDKEndpoint.getPageEndpoint(slug)); + } + async getPageSlugs(): Promise> { + return await this.request(getSDKEndpoint.getPageSlugsEndpoint()); + } + async getCollectible(slug: string): Promise> { + return await this.request(getSDKEndpoint.getCollectibleEndpoint(slug)); + } + async getCollectibleSlugs(): Promise> { + return await this.request(getSDKEndpoint.getCollectibleSlugsEndpoint()); + } + async getCollectibleScans(slug: string): Promise> { + return await this.request(getSDKEndpoint.getCollectibleScansEndpoint(slug)); + } + async getCollectibleScanPage( + slug: string, + index: string + ): Promise> { + return await this.request(getSDKEndpoint.getCollectibleScanPageEndpoint(slug, index)); + } + async getCollectibleGallery( + slug: string + ): Promise> { + return await this.request(getSDKEndpoint.getCollectibleGalleryEndpoint(slug)); + } + async getCollectibleGalleryImage( + slug: string, + index: string + ): Promise> { + return await this.request(getSDKEndpoint.getCollectibleGalleryImageEndpoint(slug, index)); + } + async getChronologyEvents(): Promise> { + return await this.request(getSDKEndpoint.getChronologyEventsEndpoint()); + } + async getChronologyEventByID(id: string): Promise> { + return await this.request(getSDKEndpoint.getChronologyEventByIDEndpoint(id)); + } + async getImageByID(id: string): Promise> { + return await this.request(getSDKEndpoint.getImageByIDEndpoint(id)); + } + async getAudioByID(id: string): Promise> { + return await this.request(getSDKEndpoint.getAudioByIDEndpoint(id)); + } + async getVideoByID(id: string): Promise> { + return await this.request(getSDKEndpoint.getVideoByIDEndpoint(id)); + } + async getFileByID(id: string): Promise> { + return await this.request(getSDKEndpoint.getFileByIDEndpoint(id)); + } + async getRecorderByID(id: string): Promise> { + return await this.request(getSDKEndpoint.getRecorderByIDEndpoint(id)); + } + async getAllSdkUrls(): Promise> { + return await this.request(getSDKEndpoint.getAllSDKUrlsEndpoint()); + } +}