import { QelosSDKOptions } from './types'; import BaseSDK from './base-sdk'; import QlAppConfigurations from './configurations'; import QlBlocks from './blocks'; import QlAuthentication from './authentication'; import QlWorkspaces from './workspaces'; import QlInvites from './invites'; import QlBlueprints from './blueprints'; import QlBlueprintEntities from './blueprints-entities'; import QlAI from './ai'; import QlLambdas from './lambdas'; import QlPayments from './payments'; const noExtraHeadersUrls = new Set([ '/api/token/refresh', '/api/signin', '/api/signup', '/api/auth/callback', ]) export type { SocialCallbackInput, SocialAuthCallbackPayload, SocialProvider, SocialLoginOptions, } from './authentication'; export type { BlueprintEntitiesRegistry } from './blueprints'; export { parseSocialCallbackRefreshToken, getSocialAuthSetCookieParts, applySocialAuthCookiesToServerResponse, } from './authentication'; export default class QelosSDK extends BaseSDK { #customHeaders = {} blocks: QlBlocks; appConfigurations: QlAppConfigurations; authentication: QlAuthentication; workspaces: QlWorkspaces; invites: QlInvites; blueprints: QlBlueprints; ai: QlAI; lambdas: QlLambdas; payments: QlPayments; constructor(private options: QelosSDKOptions) { super(options); this.blocks = new QlBlocks(this.options); this.appConfigurations = new QlAppConfigurations(this.options); this.authentication = new QlAuthentication(this.options); this.workspaces = new QlWorkspaces(this.options); this.invites = new QlInvites(this.options); this.blueprints = new QlBlueprints(this.options); this.ai = new QlAI(this.options); this.lambdas = new QlLambdas(this.options); this.payments = new QlPayments(this.options); const isBrowser = globalThis.navigator && globalThis.window && globalThis.document if (!options.getAccessToken) { options.getAccessToken = () => this.authentication.accessToken; } if (!options.extraHeaders) { options.extraHeaders = async (relativeUrl: string, forceRefresh?: boolean) => { if (options.apiToken) { return { 'x-api-key': options.apiToken, ...this.#customHeaders }; } if (isBrowser || noExtraHeadersUrls.has(relativeUrl)) { return { ...this.#customHeaders }; } let token = forceRefresh ? '' : options.getAccessToken(); if (!token) { await this.authentication.refreshToken(); token = options.getAccessToken(); } if (token) { return { authorization: 'Bearer ' + token, ...this.#customHeaders } } return { ...this.#customHeaders } } } } entities(blueprintKey: string): QlBlueprintEntities { return this.blueprints.entitiesOf(blueprintKey); } setCustomHeader(key: string, value: string) { this.#customHeaders[key] = value; } removeCustomHeader(key: string) { delete this.#customHeaders[key]; } }