import { HttpService } from '@kontent-ai/core-sdk'; import { IDeliveryClientConfig } from '../config'; import { ClientTypes, IContentItem } from '../models'; import { ElementQuery, ItemsFeedQuery, MultipleItemsQuery, MultipleTypeQuery, SingleItemQuery, SingleTypeQuery, TaxonomiesQuery, TaxonomyQuery, LanguagesQuery, InitializeSyncQuery, SyncChangesQuery, UsedInQuery } from '../query'; import { sdkInfo } from '../sdk-info.generated'; import { IMappingService, MappingService, QueryService } from '../services'; import { IDeliveryClient } from './idelivery-client.interface'; export class DeliveryClient implements IDeliveryClient { private queryService: QueryService; public mappingService: IMappingService; /** * Delivery client used to fetch data from Kontent.ai * @constructor * @param {IDeliveryClientConfig} config - The client configuration */ constructor(protected config: IDeliveryClientConfig) { if (!config) { throw Error(`Delivery client configuration is not set`); } this.mappingService = new MappingService(config); this.queryService = new QueryService( config, config.httpService ? config.httpService : new HttpService(), { host: sdkInfo.host, name: sdkInfo.name, version: sdkInfo.version }, this.mappingService ); } /** * Gets query for multiple languages */ languages(): LanguagesQuery { return new LanguagesQuery(this.config, this.queryService); } /** * Gets query for multiple types */ types(): MultipleTypeQuery { return new MultipleTypeQuery(this.config, this.queryService); } /** * Gets query for single type * @param {string} typeCodename - Codename of the type to fetch */ type(typeCodename: TClientTypes['contentTypeCodenames']): SingleTypeQuery { return new SingleTypeQuery(this.config, this.queryService, typeCodename); } /** * Gets query for multiple items */ items(): MultipleItemsQuery< TClientTypes, TContentItem > { return new MultipleItemsQuery(this.config, this.queryService); } /** * Gets query for single item * @param {string} codename - Codename of item to fetch */ item( codename: string ): SingleItemQuery { return new SingleItemQuery(this.config, this.queryService, codename); } /** * Gets query for items feed. Executes single HTTP request only */ itemsFeed(): ItemsFeedQuery< TClientTypes, TContentItem > { return new ItemsFeedQuery(this.config, this.queryService); } /** * Gets query for single taxonomy * @param {string} codename - Codename of taxonomy to fetch */ taxonomy(codename: TClientTypes['taxonomyCodenames']): TaxonomyQuery { return new TaxonomyQuery(this.config, this.queryService, codename); } /** * Gets query for multiple taxonomies */ taxonomies(): TaxonomiesQuery { return new TaxonomiesQuery(this.config, this.queryService); } /** * Gets query for an element within a type * @param {string} typeCodename - Codename of the type * @param {string} elementCodename - Codename of the element */ element( typeCodename: TClientTypes['contentTypeCodenames'], elementCodename: TClientTypes['elementCodenames'] ): ElementQuery { return new ElementQuery(this.config, this.queryService, typeCodename, elementCodename); } /** * @deprecated Sync API v1 is deprecated and will be shut down by the end of this year. * Please migrate to Sync API v2 using the `@kontent-ai/sync-sdk` package. * * For migration guidance and full documentation, visit: * https://kontent.ai/learn/docs/apis/openapi/sync-api-v2/ */ initializeSync(): InitializeSyncQuery { return new InitializeSyncQuery(this.config, this.queryService); } /** * @deprecated Sync API v1 is deprecated and will be shut down by the end of this year. * Please migrate to Sync API v2 using the `@kontent-ai/sync-sdk` package. * * For migration guidance and full documentation, visit: * https://kontent.ai/learn/docs/apis/openapi/sync-api-v2/ */ syncChanges(): SyncChangesQuery { return new SyncChangesQuery(this.config, this.queryService); } /** * Item listing of where an asset is used */ assetUsedIn(assetCodename: string): UsedInQuery { return new UsedInQuery(this.config, this.queryService, { entity: 'asset', codename: assetCodename }); } /** * Item listing of where a content item is used */ itemUsedIn(itemCodename: string): UsedInQuery { return new UsedInQuery(this.config, this.queryService, { entity: 'contentItem', codename: itemCodename }); } }