Skip to content

Commit 299a391

Browse files
committed
chore(LicenseManager): refactor to use input assertion abstraction
1 parent 87313d8 commit 299a391

File tree

2 files changed

+37
-62
lines changed

2 files changed

+37
-62
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { OptionsCreateBinding } from './types'
2+
3+
const assertBindingInput = ({ addrs, canonicalAddr, supportedLocales, defaultLocale }: OptionsCreateBinding) => {
4+
if (addrs.length === 0) {
5+
throw new Error('A binding must have at least one address')
6+
}
7+
8+
const canonicalAddrExists = addrs.some((addr) => canonicalAddr.host === addr.host && canonicalAddr.path === addr.path)
9+
10+
if (!canonicalAddrExists) {
11+
throw new Error('The canonical address must exist within the address list')
12+
}
13+
14+
if (supportedLocales.length === 0) {
15+
throw new Error('A binding must have at least one locale')
16+
}
17+
18+
const defaultLocaleExists = supportedLocales.some((locale) => defaultLocale === locale)
19+
20+
if (!defaultLocaleExists) {
21+
throw new Error('The default locale must exist within the supported locales')
22+
}
23+
}
24+
25+
export default assertBindingInput

src/clients/janus/LicenseManager/index.ts

Lines changed: 12 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RequestTracingConfig, RequestConfig, inflightUrlWithQuery } from '../../../HttpClient'
22
import { JanusClient } from '../JanusClient'
3+
import assertBindingInput from './assertBindingInput'
34
import {
45
OptionsListBindings,
56
APIBindingRes,
@@ -112,41 +113,12 @@ export class LicenseManager extends JanusClient {
112113
})
113114
}
114115

115-
public createBinding = (
116-
{
117-
tenant,
118-
adminUserAuthToken,
119-
defaultLocale,
120-
supportedLocales,
121-
salesChannelId,
122-
addrs,
123-
canonicalAddr,
124-
}: OptionsCreateBinding,
125-
config?: RequestConfig
126-
) => {
127-
const metric = 'lm-create-binding'
128-
129-
if (addrs.length === 0) {
130-
throw new Error('A binding must have at least one address')
131-
}
132-
133-
const canonicalAddrExists = addrs.some(
134-
(addr) => canonicalAddr.host === addr.host && canonicalAddr.path === addr.path
135-
)
136-
137-
if (!canonicalAddrExists) {
138-
throw new Error('The canonical address must exist within the address list')
139-
}
140-
141-
if (supportedLocales.length === 0) {
142-
throw new Error('A binding must have at least one locale')
143-
}
144-
145-
const defaultLocaleExists = supportedLocales.some((locale) => defaultLocale === locale)
116+
public createBinding = (options: OptionsCreateBinding, config?: RequestConfig) => {
117+
assertBindingInput(options)
146118

147-
if (!defaultLocaleExists) {
148-
throw new Error('The default locale must exist within the supported locales')
149-
}
119+
const metric = 'lm-create-binding'
120+
const { tenant, adminUserAuthToken, defaultLocale, supportedLocales, salesChannelId, addrs, canonicalAddr } =
121+
options
150122

151123
const bindingObj: APIBindingCreate = {
152124
SiteName: tenant,
@@ -194,8 +166,11 @@ export class LicenseManager extends JanusClient {
194166
})
195167
}
196168

197-
public updateBinding = (
198-
{
169+
public updateBinding = (options: OptionsUpdateBinding, config?: RequestConfig) => {
170+
assertBindingInput(options)
171+
172+
const metric = 'lm-update-binding'
173+
const {
199174
tenant,
200175
adminUserAuthToken,
201176
bindingId,
@@ -204,32 +179,7 @@ export class LicenseManager extends JanusClient {
204179
salesChannelId,
205180
addrs,
206181
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-
}
182+
} = options
233183

234184
const bindingObj: APIBindingUpdate = {
235185
Id: bindingId,

0 commit comments

Comments
 (0)