import { InstanceOptions , IOContext , RequestTracingConfig , UserInputError } from '../..' ; import { CacheType } from '../../HttpClient' ; import { ExternalClient } from './ExternalClient' ; const routes = { document : ( dataEntity , id ) => `${ dataEntity }/documents/${ id }` , documents : ( dataEntity ) => `${ dataEntity }/documents` , publicSchema : ( dataEntity , schema ) => `${ dataEntity }/schemas/${ schema }/public` , schema : ( dataEntity , schema ) => `${ dataEntity }/schemas/${ schema }` , scroll : ( dataEntity ) => `${ dataEntity }/scroll` , search : ( dataEntity ) => `${ dataEntity }/search` , } ; export class MasterData extends ExternalClient { public constructor ( ctx , options ? ) { super ( `http://api.vtex.com/api/dataentities` , ctx , { ... options , headers : { Accept : 'application/json' , VtexIdclientAutCookie : ctx . authToken , 'x-vtex-api-appService' : ctx . userAgent , ... options ?. headers , } , params : { an : ctx . account , ... options ?. params , } , } ) ; } public getSchema < T > ( { dataEntity , schema } , tracingConfig ? ) { const metric = 'masterdata-getSchema' ; return this . http . get < T > ( routes . schema ( dataEntity , schema ) , { metric , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } public createOrUpdateSchema < T > ( { dataEntity , schemaName , schemaBody } , tracingConfig ? ) { const metric = 'masterdata-createOrUpdateSchema' ; return this . http . put < T > ( routes . schema ( dataEntity , schemaName ) , schemaBody , { metric , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } public getPublicSchema < T > ( { dataEntity , schema } , tracingConfig ? ) { const metric = 'masterdata-getPublicSchema' ; return this . http . get < T > ( routes . publicSchema ( dataEntity , schema ) , { metric , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } public getDocument < T > ( { dataEntity , id , fields } , tracingConfig ? ) { const metric = 'masterdata-getDocument' ; return this . http . get < T > ( routes . document ( dataEntity , id ) , { metric , params : { _fields : generateFieldsArg ( fields ) , } , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } public createDocument ( { dataEntity , fields , schema } , tracingConfig ? ) { const metric = 'masterdata-createDocument' ; return this . http . post < DocumentResponse > ( routes . documents ( dataEntity ) , fields , { metric , params : { _schema : schema , } , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } public createOrUpdateEntireDocument ( { dataEntity , fields , id , schema } , tracingConfig ? ) { const metric = 'masterdata-createOrUpdateEntireDocument' ; return this . http . put < DocumentResponse > ( routes . documents ( dataEntity ) , { id , ... fields } , { metric , params : { _schema : schema , } , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } public createOrUpdatePartialDocument ( { dataEntity , fields , id , schema } , tracingConfig ? ) { const metric = 'masterdata-createOrUpdatePartialDocument' ; return this . http . patch < DocumentResponse > ( routes . documents ( dataEntity ) , { id , ... fields } , { metric , params : { _schema : schema , } , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } public updateEntireDocument ( { dataEntity , id , fields , schema } , tracingConfig ? ) { const metric = 'masterdata-updateEntireDocument' ; return this . http . put ( routes . document ( dataEntity , id ) , fields , { metric , params : { _schema : schema , } , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } public updatePartialDocument ( { dataEntity , id , fields , schema } , tracingConfig ? ) { const metric = 'masterdata-updatePartialDocument' ; return this . http . patch ( routes . document ( dataEntity , id ) , fields , { metric , params : { _schema : schema , } , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } public searchDocuments < T > ( { dataEntity , fields , where , pagination , schema , sort } , tracingConfig ? ) { const metric = 'masterdata-searchDocuments' ; return this . http . get < T [ ] > ( routes . search ( dataEntity ) , { cacheable : CacheType . None , headers : paginationArgsToHeaders ( pagination ) , metric , params : { _fields : generateFieldsArg ( fields ) , _schema : schema , _sort : sort , _where : where , } , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } public async searchDocumentsWithPaginationInfo < T > ( { dataEntity , fields , where , pagination , schema , sort } , tracingConfig ? ) { const metric = 'masterdata-searchDocumentsWithPagination' ; const result = await this . http . getRaw < T [ ] > ( routes . search ( dataEntity ) , { cacheable : CacheType . None , headers : paginationArgsToHeaders ( pagination ) , metric , params : { _fields : generateFieldsArg ( fields ) , _schema : schema , _sort : sort , _where : where , } , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; const headers = result . headers ; const restContentRange = headers [ 'rest-content-range' ] ; const total = Number ( restContentRange . split ( '/' ) [ 1 ] ) ; const paginationInfo = { ... pagination , total } ; return { data : result . data , pagination : paginationInfo } ; } public scrollDocuments < T > ( { dataEntity , fields , mdToken , schema , size , sort } , tracingConfig ? ) { const metric = 'masterdata-scrollDocuments' ; return this . http . getRaw < ScrollResponse < T > > ( routes . scroll ( dataEntity ) , { cacheable : CacheType . None , metric , params : { _fields : generateFieldsArg ( fields ) , _schema : schema , _size : size , _sort : sort , _token : mdToken , } , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) . then ( ( { headers : { 'x-vtex-md-token' : resToken } , data } ) => ( { mdToken : resToken , data } ) ) ; } public deleteDocument ( { dataEntity , id } , tracingConfig ? ) { const metric = 'masterdata-deleteDocument' ; return this . http . delete ( routes . document ( dataEntity , id ) , { metric , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } , } ) ; } } function paginationArgsToHeaders ( { page , pageSize } ) { if ( page < 1 ) { throw new UserInputError ( 'Smallest page value is 1' ) ; } const startIndex = ( page - 1 ) * pageSize ; const endIndex = startIndex + pageSize ; return { 'REST-Range' : `resources=${ startIndex }-${ endIndex }` , } ; } function generateFieldsArg ( fields ) { return fields . join ( ',' ) ; } interface PaginationArgs { page : number ; pageSize : number ; } interface DocumentResponse { Id : string ; Href : string ; DocumentId : string ; } interface GetSchemaInput { dataEntity : string ; schema : string ; } interface CreateSchemaInput { dataEntity : string ; schemaName : string ; schemaBody : object ; } interface GetDocumentInput { dataEntity : string ; id : string ; fields : string [ ] ; } interface CreateDocumentInput { dataEntity : string ; fields : object ; schema ? : string ; } interface CreateOrUpdateInput { dataEntity : string ; fields : object ; id ? : string ; schema ? : string ; } interface UpdateInput { dataEntity : string ; id : string ; fields : object ; schema ? : string ; } interface SearchInput { dataEntity : string ; fields : string [ ] ; where ? : string ; pagination : PaginationArgs ; schema ? : string ; sort ? : string ; } interface ScrollInput { dataEntity : string ; fields : string [ ] ; schema ? : string ; sort ? : string ; size ? : number ; mdToken ? : string ; } interface DeleteInput { dataEntity : string ; id : string ; } interface ScrollResponse < T > { data : T [ ] ; mdToken : string ; }