import { ARequestInit, FetchError, afetch } from '../utils/afetch'; import APIMeNamespace from './namespace/api-me.namespace'; import APIStoreNamespace from './namespace/api-store.namespace'; import APIExtensionsNamespace from './namespace/api-extensions.namespace-'; import APIWorkflowsNamespace from './namespace/api-workflows.namespace'; import APIUserNamespace from './namespace/api-user.namespace'; class API { static getErrorMessage(error: unknown, byStatus?: Record) { if (!FetchError.isFetchError(error)) { return 'Something went wrong!'; } if (byStatus && byStatus[error.status]) return byStatus[error.status]; return error.data.message || error.data.error || error.statusText; } static async handleError( func: () => Promise, handler: Record P>, ): Promise { try { return await func(); } catch (error) { if (FetchError.isFetchError(error) && handler[error.status]) { return handler[error.status](error); } throw error; } } private accessToken: string | null = null; handleError: typeof API.handleError = API.handleError; getErrorMessage: typeof API.getErrorMessage = API.getErrorMessage; readonly me = new APIMeNamespace(this); readonly user = new APIUserNamespace(this); readonly store = new APIStoreNamespace(this); readonly workflows = new APIWorkflowsNamespace(this); readonly extensions = new APIExtensionsNamespace(this); constructor( readonly apiBaseURL: string, readonly apiKey?: string, ) {} async authorizeFetch( path: string, init: ARequestInit & { isPublic?: boolean; authToken?: string } = {}, ): Promise { if (!this.accessToken && !init?.isPublic && !init.authToken) { throw new Error('Unauthorized'); } const token = init.authToken ?? this.accessToken; return afetch(`${this.apiBaseURL}${path}`, { ...init, auth: token ? `Bearer ${token}` : undefined, }); } $setAccessToken(session: string | null) { this.accessToken = session; } $getAccessToken() { return this.accessToken; } } export default API;