import axios from 'axios'; import type { AxiosInstance } from 'axios'; import { Collections } from './modules/collections'; import { Customers } from './modules/customers'; import { Devices } from './modules/devices'; import { Invoices } from './modules/invoices'; import { Ordering } from './modules/ordering'; import { Ratings } from './modules/ratings'; import { Schedule } from './modules/schedule'; import { Subscriptions } from './modules/subscriptions'; import { Users } from './modules/users'; export interface CultureKitClientConfig { /** * The CultureKit API base URL */ apiUrl: CultureKitClient['apiUrl']; /** * The locale used for requests as an ISO 639-1 string. */ locale?: CultureKitClient['locale']; /** * The access token for authorized requests */ accessToken?: CultureKitClient['accessToken']; /** * Called before every request to get the freshest access token. If set, takes precedence over `accessToken`. */ getAccessToken?: () => Promise; } export class CultureKitClient { public readonly axios: AxiosInstance; public readonly ordering: Ordering; public readonly schedule: Schedule; public readonly ratings: Ratings; public readonly collections: Collections; public readonly customers: Customers; public readonly invoices: Invoices; public readonly users: Users; public readonly subscriptions: Subscriptions; public readonly devices: Devices; /** * Create a new client * @param {CultureKitClientConfig} config */ constructor(config: CultureKitClientConfig) { if (!config) throw new Error('config is required.'); if (!config.apiUrl) throw new Error('config.apiUrl is required.'); this.axios = axios.create({ baseURL: config.apiUrl, headers: { common: { 'Content-Type': 'application/json; charset=utf-8', }, }, }); if (config.locale) { this.locale = config.locale; } if (config.accessToken) { this.accessToken = config.accessToken; } if (config.getAccessToken) { this.axios.interceptors.request.use(async (requestConfig) => { try { this.accessToken = await config.getAccessToken?.(); } catch { // Send the request with the current token rather than failing } requestConfig.headers['Authorization'] = this.axios.defaults.headers['Authorization']; return requestConfig; }); } this.ordering = new Ordering(this.axios); this.schedule = new Schedule(this.axios); this.ratings = new Ratings(this.axios); this.collections = new Collections(this.axios); this.customers = new Customers(this.axios); this.invoices = new Invoices(this.axios); this.users = new Users(this.axios); this.subscriptions = new Subscriptions(this.axios); this.devices = new Devices(this.axios); } /** * The CultureKit API base URL */ public get apiUrl() { return this.axios.defaults.baseURL || ''; } public set apiUrl(apiUrl: string) { this.axios.defaults.baseURL = apiUrl; } /** * The locale used for requests as an ISO 639-1 string. */ public get locale() { const localeHeader = this.axios.defaults.headers['locale'] as | string | undefined; return localeHeader; } public set locale(locale: string | undefined) { if (locale) { this.axios.defaults.headers['locale'] = locale; } else { delete this.axios.defaults.headers['locale']; } } public get accessToken() { const authorizationHeader = this.axios.defaults.headers['Authorization'] as | string | undefined; if (!authorizationHeader) { return; } const accessToken = authorizationHeader.replace('Bearer ', ''); return accessToken; } /** * The access token for authorized requests */ public set accessToken(accessToken: string | undefined) { if (accessToken) { this.axios.defaults.headers['Authorization'] = `Bearer ${accessToken}`; } else { delete this.axios.defaults.headers['Authorization']; } } }