export type RequestPayloadMethod = | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'; export interface RequestPayload { method?: RequestPayloadMethod; query?: Record; body?: any; unwrap?: boolean; forceBlob?: boolean; } export interface ServiceConfig { // Your API-Key, this is optional in the sense of if you omit this, and you're in a browser, the // cookie-authentication (include-credentials) will be used. key?: string; // Your domain, if omitted location.host will be used (browser env). host?: string; // If you want to use https, defaults to location.protocol (browser env). secure?: boolean; // If you want that some and count requests are bundled into multi requests. multiRequest?: boolean; // If you want that the ignoreMissingProperties parameter to be set to true for every post request. ignoreMissingProperties?: boolean; // Optional request/response interceptors. interceptors?: { // Takes the generated request, you can either return a new request, // a response (which will be taken as "the" response) or nothing. // The payload contains the raw input generated by the SDK. request?: ( request: Request, payload: RequestPayload ) => Request | Response | void | Promise; // Takes the response. This can either be the one from the server or an // artificially-crafted one by the request interceptor. response?: (response: Response) => Response | void | Promise; }; // Whether POST should be used instead of GET for some() and count() operations usePost?: boolean; } export interface RequestOptions { signal?: AbortSignal; } type ServiceConfigWithoutMultiRequest = Omit; let globalConfig: ServiceConfig | undefined; export const getGlobalConfig = (): ServiceConfig | undefined => globalConfig; export const setGlobalConfig = (cfg?: ServiceConfig) => (globalConfig = cfg); export const getHost = (cfg: ServiceConfig) => { let host = cfg.host?.replace(/^https?:\/\//, ''); if (!host && typeof location !== 'undefined') { host = location.host; } if (!host) { throw new Error('Please specify a host'); } return host; }; export const getProtocol = (cfg: ServiceConfig) => { const protocol = cfg.secure !== undefined ? cfg.secure ? 'https:' : 'http:' : typeof location !== 'undefined' ? location.protocol : undefined; if (!protocol) { throw new Error('Please specify a protocol (secure)'); } return protocol; };