11import { RequestTracingConfig , RequestConfig , inflightUrlWithQuery } from '../../../HttpClient'
22import { JanusClient } from '../JanusClient'
3- import { OptionsListBindings , APIBindingRes , OptionsGetBinding } from './types'
3+ import {
4+ OptionsListBindings ,
5+ APIBindingRes ,
6+ OptionsGetBinding ,
7+ OptionsCreateBinding ,
8+ APIBindingCreate ,
9+ Addr ,
10+ APICreateBindingRes ,
11+ } from './types'
412
513const TWO_MINUTES_S = 2 * 60
614
@@ -12,6 +20,7 @@ const routes = {
1220 topbarData : ( ) => `${ BASE_URL } /site/pvt/newtopbar` ,
1321 listBindings : ( tenant : string ) => `${ BASE_URL } /binding/site/${ encodeURIComponent ( tenant ) } ` ,
1422 getBinding : ( bindingId : string ) => `${ BASE_URL } /binding/${ encodeURIComponent ( bindingId ) } ` ,
23+ createBinding : ( ) => `${ BASE_URL } /binding` ,
1524}
1625
1726export class LicenseManager extends JanusClient {
@@ -97,4 +106,69 @@ export class LicenseManager extends JanusClient {
97106 } ,
98107 } )
99108 }
109+
110+ public createBinding = (
111+ {
112+ tenant,
113+ adminUserAuthToken,
114+ defaultLocale,
115+ supportedLocales,
116+ salesChannelId,
117+ addrs,
118+ canonicalAddr,
119+ } : OptionsCreateBinding ,
120+ config ?: RequestConfig
121+ ) => {
122+ const metric = 'lm-create-binding'
123+
124+ if ( addrs . length === 0 ) {
125+ throw new Error ( 'A binding must have at least one address' )
126+ }
127+
128+ const canonicalAddrExists = addrs . some (
129+ ( addr ) => canonicalAddr . host === addr . host && canonicalAddr . path === addr . path
130+ )
131+
132+ if ( ! canonicalAddrExists ) {
133+ throw new Error ( 'The canonical address must exist within the address list' )
134+ }
135+
136+ if ( supportedLocales . length === 0 ) {
137+ throw new Error ( 'A binding must have at least one locale' )
138+ }
139+
140+ const defaultLocaleExists = supportedLocales . some ( ( locale ) => defaultLocale === locale )
141+
142+ if ( ! defaultLocaleExists ) {
143+ throw new Error ( 'The default locale must exist within the supported locales' )
144+ }
145+
146+ const bindingObj : APIBindingCreate = {
147+ SiteName : tenant ,
148+ DefaultLocale : defaultLocale ,
149+ SupportedLocales : supportedLocales ,
150+ DefaultSalesChannelId : salesChannelId ,
151+ Addresses : addrs . map ( ( addr : Addr ) => ( {
152+ Host : addr . host ,
153+ BasePath : addr . path ,
154+ IsCanonical : canonicalAddr . host === addr . host && canonicalAddr . path === addr . path ,
155+ Localization : {
156+ '' : defaultLocale ,
157+ } ,
158+ } ) ) ,
159+ }
160+
161+ return this . http . post < APICreateBindingRes [ ] > ( routes . createBinding ( ) , bindingObj , {
162+ inflightKey : inflightUrlWithQuery ,
163+ metric,
164+ headers : {
165+ VtexIdclientAutCookie : adminUserAuthToken ,
166+ } ,
167+ ...config ,
168+ tracing : {
169+ requestSpanNameSuffix : metric ,
170+ ...config ?. tracing ,
171+ } ,
172+ } )
173+ }
100174}
0 commit comments