import { InstanceOptions , RequestTracingConfig } from '../../HttpClient' ; import { BucketMetadata } from '../../responses' ; import { IOContext } from '../../service/worker/runtime/typings' ; import { InfraClient } from './InfraClient' ; const appId = process . env . VTEX_APP_ID ; const [ runningAppName ] = appId ? appId . split ( '@' ) : [ '' ] ; const routes = { Bucket : ( bucket ) => `/buckets/${ runningAppName }/${ bucket }` , Metadata : ( bucket ) => `${ routes . Bucket ( bucket ) }/metadata` , MetadataKey : ( bucket , key ) => `${ routes . Metadata ( bucket ) }/${ key }` , } ; export interface MetadataEntry { Key : string ; Hash : string ; Value : any ; } export interface MetadataEntryList { Data : MetadataEntry [ ] ; Next : string ; } export class Metadata extends InfraClient { constructor ( context , options ? ) { super ( 'router' , context , options ) ; if ( runningAppName === '' ) { throw new Error ( `Invalid path to access Metadata. Variable VTEX_APP_ID is not available.` ) ; } } public getBuckets = ( bucket , tracingConfig ? ) => { const metric = 'meta-get-buckets' ; return this . http . get < BucketMetadata > ( routes . Bucket ( bucket ) , { metric , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } } ) ; } ; public list = ( bucket , includeValue , limit ? , nextMarker ? , tracingConfig ? ) => { const query = { value : includeValue , _limit : 10 } ; if ( limit && limit > 0 ) { query . _limit = limit ; } if ( nextMarker ) { query . _marker = nextMarker ; } const metric = 'meta-list' ; return this . http . get < MetadataEntryList > ( routes . Metadata ( bucket ) , { metric , params : query , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } } ) ; } ; public listAll = ( bucket , includeValue , tracingConfig ? ) => { const query = { value : includeValue , _limit : 1000 } ; const metric = 'meta-list-all' ; return this . http . get < MetadataEntryList > ( routes . Metadata ( bucket ) , { metric , params : query , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } } ) ; } ; public get = ( bucket , key , tracingConfig ? ) => { const metric = 'meta-get' ; return this . http . get < any > ( routes . MetadataKey ( bucket , key ) , { metric , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } } ) ; } ; public save = ( bucket , key , data , tracingConfig ? ) => { const metric = 'meta-save' ; return this . http . put ( routes . MetadataKey ( bucket , key ) , data , { metric , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } } ) ; } ; public saveAll = ( bucket , data , tracingConfig ? ) => { const metric = 'meta-save-all' ; return this . http . put ( routes . Metadata ( bucket ) , data , { metric , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } } ) ; } ; public delete = ( bucket , key , tracingConfig ? ) => { const metric = 'meta-delete' ; return this . http . delete ( routes . MetadataKey ( bucket , key ) , { metric , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } } ) ; } ; public deleteAll = ( bucket , tracingConfig ? ) => { const metric = 'meta-delete-all' ; return this . http . delete ( routes . Metadata ( bucket ) , { metric , tracing : { requestSpanNameSuffix : metric , ... tracingConfig ?. tracing , } } ) ; } ; }