import { AxiosRequestConfig } from 'axios' ; import { createRequestConfig } from 'contentful-sdk-core' ; import { Collection , MakeRequest , PaginationQueryParams , QueryOptions , QueryParams , } from './common-types' ; import entities from './entities' ; import { Organization , OrganizationProp } from './entities/organization' ; import { CreatePersonalAccessTokenProps } from './entities/personal-access-token' ; import { Space , SpaceProps } from './entities/space' ; import { UsageQuery } from './entities/usage' ; import { UserProps } from './entities/user' ; export type ClientAPI = ReturnType < typeof createClientApi > ; private export default function createClientApi ( makeRequest ) { const { wrapSpace , wrapSpaceCollection } = entities . space ; const { wrapUser } = entities . user ; const { wrapPersonalAccessToken , wrapPersonalAccessTokenCollection } = entities . personalAccessToken ; const { wrapOrganization , wrapOrganizationCollection } = entities . organization ; const { wrapUsageCollection } = entities . usage ; return { return getSpaces : function getSpaces ( query = { } ) : Promise < Collection < Space , SpaceProps > > { return makeRequest ( { entityType : 'Space' , action : 'getMany' , params : { query : createRequestConfig ( { query : query } ) . params } , } ) . then ( ( data ) => wrapSpaceCollection ( makeRequest , data ) ) ; } , param spaceId return getSpace : function getSpace ( spaceId ) : Promise < Space > { return makeRequest ( { entityType : 'Space' , action : 'get' , params : { spaceId } , } ) . then ( ( data ) => wrapSpace ( makeRequest , data ) ) ; } , param spaceData param organizationId return example createSpace : function createSpace ( spaceData , organizationId ) : Promise < Space > { return makeRequest ( { entityType : 'Space' , action : 'create' , params : { organizationId } , payload : spaceData , } ) . then ( ( data ) => wrapSpace ( makeRequest , data ) ) ; } , param id return example getOrganization : function getOrganization ( id ) : Promise < Organization > { return makeRequest ( { entityType : 'Organization' , action : 'get' , params : { organizationId : id } , } ) . then ( ( data ) => wrapOrganization ( makeRequest , data ) ) ; } , return example getOrganizations : function getOrganizations ( query = { } ) : Promise < Collection < Organization , OrganizationProp > > { return makeRequest ( { entityType : 'Organization' , action : 'getMany' , params : { query : createRequestConfig ( { query } ) . params } , } ) . then ( ( data ) => wrapOrganizationCollection ( makeRequest , data ) ) ; } , return example getCurrentUser : function getCurrentUser < T = UserProps > ( params ? ) : Promise < T > { return makeRequest ( { entityType : 'User' , action : 'getCurrent' , params , } ) . then ( ( data ) => wrapUser < T > ( makeRequest , data ) ) ; } , param data return example createPersonalAccessToken : function createPersonalAccessToken ( data ) { return makeRequest ( { entityType : 'PersonalAccessToken' , action : 'create' , params : { } , payload : data , } ) . then ( ( response ) => wrapPersonalAccessToken ( makeRequest , response ) ) ; } , param data return example getPersonalAccessToken : function getPersonalAccessToken ( tokenId ) { return makeRequest ( { entityType : 'PersonalAccessToken' , action : 'get' , params : { tokenId } , } ) . then ( ( data ) => wrapPersonalAccessToken ( makeRequest , data ) ) ; } , return example getPersonalAccessTokens : function getPersonalAccessTokens ( ) { return makeRequest ( { entityType : 'PersonalAccessToken' , action : 'getMany' , params : { } , } ) . then ( ( data ) => wrapPersonalAccessTokenCollection ( makeRequest , data ) ) ; } , /**
         * Get organization usage grouped by  UsageMetricEnum param organizationId param query return example getOrganizationUsage : function getOrganizationUsage ( organizationId , query = { } ) { return makeRequest ( { entityType : 'Usage' , action : 'getManyForOrganization' , params : { organizationId , query } , } ) . then ( ( data ) => wrapUsageCollection ( makeRequest , data ) ) ; } , param organizationId param query return getSpaceUsage : function getSpaceUsage ( organizationId , query = { } ) { return makeRequest ( { entityType : 'Usage' , action : 'getManyForSpace' , params : { organizationId , query , } , } ) . then ( ( data ) => wrapUsageCollection ( makeRequest , data ) ) ; } , param opts return rawRequest : function rawRequest ( { url , ... config } ) { return makeRequest ( { entityType : 'Http' , action : 'request' , params : { url , config } , } ) ; } , } ; }