import { RequestFactory } from "./Requests/RequestFactory"; import { Data } from "./Data"; import { Query } from "./Query"; import { utils } from "./utils"; import { ResponseHandlers } from "./Requests/ExecutableRequest"; import { BatchRequest } from "./Requests/BatchRequest"; export class Sitefinity { public static Query: typeof Query = Query; private factory: RequestFactory; private baseUrl: string; private siteId: string; /** * The constructor of the Sitefinity JavaScript SDK. This is the entry point for the SDK. */ constructor(options: SDKOptions) { this.baseUrl = options.baseUrl; this.siteId = options.siteId; this.factory = new RequestFactory(options); } data(options: TypeOptions): Data { const urlOptions: TypeOptions = { baseUrl: utils.addTrailingSlash(options.baseUrl || this.baseUrl), provider: options.provider, culture: options.culture, siteId: options.siteId || this.siteId, additionalQueryParams: options.additionalQueryParams, additionalHeaders: options.additionalHeaders, entitySet: options.entitySet }; return new Data(urlOptions, this.factory); } batch(success?: Function, failure?: Function, progress?: Function, options?: TypeOptions): BatchRequest { if (!options) options = {}; const batch = this.factory.batch({ successCb: success, failureCb: failure, progressCb: progress, urlOptions: { baseUrl: utils.addTrailingSlash(options.baseUrl || this.baseUrl), provider: options.provider, culture: options.culture, siteId: options.siteId || this.siteId, additionalQueryParams: options.additionalQueryParams, additionalHeaders: options.additionalHeaders } }); return batch; } /** * Sets the authentication token. */ setToken(token: Token) { this.factory.token = token; } } interface OptionsBase { /** * Your service url that points to the configured instance of Sitefinity service. */ baseUrl?: string; /** * The ID of the site, for which the queries will be executed. */ siteId?: string; } /** * Defines an object containing configuration options for the SDK object. */ export interface SDKOptions extends OptionsBase { /** * The global handlers for request execution */ handlers?: ResponseHandlers; enableUnlimitedChoices?: boolean; integrationMode?: boolean; } /** * Defines an object containing configuration options related to a specific type. */ export interface TypeOptions extends OptionsBase { /** * The URL name of the type. */ entitySet?: string; /** * The name of the provider. If empty the default provider for the site will be used. */ provider?: string; /** * The name of the culture. If empty the default culture for the site will be used. */ culture?: string; /** * Query params to be added to the request url. */ additionalQueryParams?: { [key: string]: string }; /** * Headers to be added to the request. */ additionalHeaders?: { [key: string]: string }; } export interface Token { type: string; value: string; }