Skip to content

Commit e5c70d5

Browse files
committed
feat(LicenseManager): add updateBinding method
1 parent d59e35e commit e5c70d5

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/clients/janus/LicenseManager/index.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
Addr,
1010
APICreateBindingRes,
1111
OptionsDeleteBinding,
12+
OptionsUpdateBinding,
13+
APIBindingUpdate,
1214
} from './types'
1315

1416
const 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

2831
export 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
}

src/clients/janus/LicenseManager/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export interface OptionsCreateBinding {
2828
canonicalAddr: Addr
2929
}
3030

31+
export interface OptionsUpdateBinding extends OptionsCreateBinding {
32+
bindingId: string
33+
}
34+
3135
export interface APIAddress {
3236
Host: string
3337
IsCanonical: boolean
@@ -45,6 +49,10 @@ export interface APIBindingCreate {
4549
SupportedLocales: string[]
4650
}
4751

52+
export interface APIBindingUpdate extends APIBindingCreate {
53+
Id: string
54+
}
55+
4856
export interface APIBindingRes extends APIBindingCreate {
4957
Id: string
5058
}

0 commit comments

Comments
 (0)