99 Addr ,
1010 APICreateBindingRes ,
1111 OptionsDeleteBinding ,
12+ OptionsUpdateBinding ,
13+ APIBindingUpdate ,
1214} from './types'
1315
1416const TWO_MINUTES_S = 2 * 60
@@ -23,6 +25,7 @@ const routes = {
2325 getBinding : ( bindingId : string ) => `${ BASE_URL } /binding/${ encodeURIComponent ( bindingId ) } ` ,
2426 createBinding : ( ) => `${ BASE_URL } /binding` ,
2527 deleteBinding : ( bindingId : string ) => `${ BASE_URL } /binding/${ encodeURIComponent ( bindingId ) } ` ,
28+ updateBinding : ( bindingId : string ) => `${ BASE_URL } /binding/${ bindingId } ` ,
2629}
2730
2831export class LicenseManager extends JanusClient {
@@ -190,4 +193,71 @@ export class LicenseManager extends JanusClient {
190193 } ,
191194 } )
192195 }
196+
197+ public updateBinding = (
198+ {
199+ tenant,
200+ adminUserAuthToken,
201+ bindingId,
202+ defaultLocale,
203+ supportedLocales,
204+ salesChannelId,
205+ addrs,
206+ canonicalAddr,
207+ } : OptionsUpdateBinding ,
208+ config ?: RequestConfig
209+ ) => {
210+ const metric = 'lm-update-binding'
211+
212+ if ( addrs . length === 0 ) {
213+ throw new Error ( 'A binding must have at least one address' )
214+ }
215+
216+ const canonicalAddrExists = addrs . some (
217+ ( addr ) => canonicalAddr . host === addr . host && canonicalAddr . path === addr . path
218+ )
219+
220+ if ( ! canonicalAddrExists ) {
221+ throw new Error ( 'The canonical address must exist within the address list' )
222+ }
223+
224+ if ( supportedLocales . length === 0 ) {
225+ throw new Error ( 'A binding must have at least one locale' )
226+ }
227+
228+ const defaultLocaleExists = supportedLocales . some ( ( locale ) => defaultLocale === locale )
229+
230+ if ( ! defaultLocaleExists ) {
231+ throw new Error ( 'The default locale must exist within the supported locales' )
232+ }
233+
234+ const bindingObj : APIBindingUpdate = {
235+ Id : bindingId ,
236+ SiteName : tenant ,
237+ DefaultLocale : defaultLocale ,
238+ SupportedLocales : supportedLocales ,
239+ DefaultSalesChannelId : salesChannelId ,
240+ Addresses : addrs . map ( ( addr : Addr ) => ( {
241+ Host : addr . host ,
242+ BasePath : addr . path ,
243+ IsCanonical : canonicalAddr . host === addr . host && canonicalAddr . path === addr . path ,
244+ Localization : {
245+ '' : defaultLocale ,
246+ } ,
247+ } ) ) ,
248+ }
249+
250+ return this . http . put ( routes . updateBinding ( bindingId ) , bindingObj , {
251+ inflightKey : inflightUrlWithQuery ,
252+ metric,
253+ headers : {
254+ VtexIdclientAutCookie : adminUserAuthToken ,
255+ } ,
256+ ...config ,
257+ tracing : {
258+ requestSpanNameSuffix : metric ,
259+ ...config ?. tracing ,
260+ } ,
261+ } )
262+ }
193263}
0 commit comments