Skip to content

Commit 51934a2

Browse files
committed
feat(LicenseManager): add createBinding
1 parent 41e7e0b commit 51934a2

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

src/clients/janus/LicenseManager/index.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { RequestTracingConfig, RequestConfig, inflightUrlWithQuery } from '../../../HttpClient'
22
import { 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

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

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

src/clients/janus/LicenseManager/types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ export interface OptionsGetBinding {
88
bindingId: string
99
}
1010

11+
export interface Addr {
12+
host: string
13+
path: string
14+
}
15+
16+
export interface OptionsCreateBinding {
17+
tenant: string
18+
adminUserAuthToken: string
19+
defaultLocale: string
20+
supportedLocales: string[]
21+
salesChannelId: number | null
22+
addrs: Addr[]
23+
canonicalAddr: Addr
24+
}
25+
1126
export interface APIAddress {
1227
Host: string
1328
IsCanonical: boolean
@@ -28,3 +43,7 @@ export interface APIBindingCreate {
2843
export interface APIBindingRes extends APIBindingCreate {
2944
Id: string
3045
}
46+
47+
export interface APICreateBindingRes {
48+
Id: string
49+
}

0 commit comments

Comments
 (0)