import { IHttpService } from '@kentico/kontent-core' ; import { IDeliveryClientConfig } from '../config' ; import { Contracts } from '../contracts' ; import { Responses , IContentItem , IContentTypeQueryConfig , IGroupedNetworkResponse , IItemQueryConfig , IDeliveryNetworkResponse , ILanguagesQueryConfig , ITaxonomyQueryConfig } from '../models' ; import { IKontentListAllResponse , IKontentListResponse , IListAllQueryConfig , ISDKInfo } from '../models/common/common-models' ; import { BaseDeliveryQueryService } from './base-delivery-query.service' ; import { IMappingService } from './mapping.service' ; export class QueryService extends BaseDeliveryQueryService { constructor ( config , httpService , sdkInfo , mappingService ) { super ( config , httpService , sdkInfo , mappingService ) ; } param url param queryConfig async getSingleItemAsync < TContentItem extends IContentItem = IContentItem > ( url , queryConfig ) { const response = await this . getResponseAsync < Contracts . IViewContentItemContract > ( url , queryConfig ) ; return this . mapNetworkResponse < Responses . IViewContentItemResponse < TContentItem > , Contracts . IViewContentItemContract > ( this . mappingService . viewContentItemResponse < TContentItem > ( response . data ) , response ) ; } param url param queryConfig async getItemsFeed < TContentItem extends IContentItem = IContentItem > ( url , queryConfig ) { const response = await this . getResponseAsync < Contracts . IItemsFeedContract > ( url , queryConfig ) ; return this . mapNetworkResponse ( this . mappingService . itemsFeedResponse < TContentItem > ( response . data ) , response ) ; } param url param queryConfig async getMultipleItems < TContentItem extends IContentItem = IContentItem > ( url , queryConfig ) { const response = await this . getResponseAsync < Contracts . IListContentItemsContract > ( url , queryConfig ) ; return this . mapNetworkResponse ( this . mappingService . listContentItemsResponse ( response . data ) , response ) ; } param url param queryConfig async getSingleType ( url , queryConfig ) { const response = await this . getResponseAsync < Contracts . IViewContentTypeContract > ( url , queryConfig ) ; return this . mapNetworkResponse ( this . mappingService . viewContentTypeResponse ( response . data ) , response ) ; } param url param queryConfig async getMultipleTypes ( url , queryConfig ) { const response = await this . getResponseAsync < Contracts . IListContentTypeContract > ( url , queryConfig ) ; return this . mapNetworkResponse ( this . mappingService . listContentTypesResponse ( response . data ) , response ) ; } param url param queryConfig async getLanguages ( url , queryConfig ) { const response = await this . getResponseAsync < Contracts . IListLanguagesContract > ( url , queryConfig ) ; return this . mapNetworkResponse ( this . mappingService . listLanguagesResponse ( response . data ) , response ) ; } param url param queryConfig async getTaxonomy ( url , queryConfig ) { const response = await this . getResponseAsync < Contracts . IViewTaxonomyGroupContract > ( url , queryConfig ) ; return this . mapNetworkResponse ( this . mappingService . viewTaxonomyResponse ( response . data ) , response ) ; } param url param queryConfig async getTaxonomies ( url , queryConfig ) { const response = await this . getResponseAsync < Contracts . IListTaxonomyGroupsContract > ( url , queryConfig ) ; return this . mapNetworkResponse ( this . mappingService . listTaxonomiesResponse ( response . data ) , response ) ; } param url param queryConfig async getElementAsync ( url , queryConfig ) { const response = await this . getResponseAsync < Contracts . IViewContentTypeElementContract > ( url , queryConfig ) ; return this . mapNetworkResponse ( this . mappingService . viewContentTypeElementResponse ( response . data ) , response ) ; } async getListAllResponse < TResponse extends IKontentListResponse , TAllResponse extends IKontentListAllResponse , TContract > ( data ) { const responses = await this . getListAllResponseInternalAsync ( { page : data . page , resolvedResponses : [ ] , getResponse : data . getResponse , nextPageUrl : undefined , continuationToken : undefined , listQueryConfig : data . listQueryConfig } ) ; return data . allResponseFactory ( responses . reduce ( ( prev , current ) => { prev . push ( ... current . data . items ) ; return prev ; } , [ ] ) , responses ) ; } private async getListAllResponseInternalAsync < TResponse extends IKontentListResponse , TContract > ( data ) { if ( data . listQueryConfig ?. pages ) { if ( data . page > data . listQueryConfig . pages ) { return data . resolvedResponses ; } } const response = await data . getResponse ( data . nextPageUrl , data . continuationToken ) ; if ( data . listQueryConfig ?. delayBetweenRequests ) { await this . sleep ( data . listQueryConfig . delayBetweenRequests ) ; } data . resolvedResponses . push ( response ) ; if ( data . listQueryConfig ?. responseFetched ) { data . listQueryConfig . responseFetched ( response , data . nextPageUrl , data . continuationToken ) ; } if ( response . data . pagination ?. nextPage || response . xContinuationToken ) { return await this . getListAllResponseInternalAsync ( { page : data . page + 1 , nextPageUrl : response . data . pagination ?. nextPage , continuationToken : response . xContinuationToken , listQueryConfig : data . listQueryConfig , getResponse : data . getResponse , resolvedResponses : data . resolvedResponses } ) ; } return data . resolvedResponses ; } private sleep ( ms ) { return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; } }