1
1
import type {
2
- AnalyticsClient ,
2
+ Region as AbtestingRegion ,
3
+ AbtestingClient ,
4
+ } from '@experimental-api-clients-automation/client-abtesting/src/abtestingClient' ;
5
+ import {
6
+ createAbtestingClient ,
7
+ REGIONS as abtestingRegions ,
8
+ } from '@experimental-api-clients-automation/client-abtesting/src/abtestingClient' ;
9
+ import type {
3
10
Region as AnalyticsRegion ,
11
+ AnalyticsClient ,
12
+ } from '@experimental-api-clients-automation/client-analytics/src/analyticsClient' ;
13
+ import {
14
+ createAnalyticsClient ,
15
+ REGIONS as analyticsRegions ,
4
16
} from '@experimental-api-clients-automation/client-analytics/src/analyticsClient' ;
5
- import { createAnalyticsClient } from '@experimental-api-clients-automation/client-analytics/src/analyticsClient' ;
6
17
import {
7
18
createMemoryCache ,
8
19
createFallbackableCache ,
@@ -11,22 +22,27 @@ import {
11
22
DEFAULT_READ_TIMEOUT_BROWSER ,
12
23
DEFAULT_WRITE_TIMEOUT_BROWSER ,
13
24
} from '@experimental-api-clients-automation/client-common' ;
25
+ import type { CreateClientOptions } from '@experimental-api-clients-automation/client-common' ;
26
+ import {
27
+ createPersonalizationClient ,
28
+ REGIONS as personalizationRegions ,
29
+ } from '@experimental-api-clients-automation/client-personalization/src/personalizationClient' ;
14
30
import type {
15
- CreateClientOptions ,
16
- Host ,
17
- Requester ,
18
- } from '@experimental-api-clients-automation/client-common' ;
19
- import type {
20
- PersonalizationClient ,
21
31
Region as PersonalizationRegion ,
32
+ PersonalizationClient ,
22
33
} from '@experimental-api-clients-automation/client-personalization/src/personalizationClient' ;
23
- import { createPersonalizationClient } from '@experimental-api-clients-automation/client-personalization/src/personalizationClient' ;
24
34
import {
25
35
createSearchClient ,
26
36
apiClientVersion as searchClientVersion ,
27
37
} from '@experimental-api-clients-automation/client-search/src/searchClient' ;
28
38
import { createXhrRequester } from '@experimental-api-clients-automation/requester-browser-xhr' ;
29
39
40
+ import type {
41
+ CommonInitOptions ,
42
+ InitRegion ,
43
+ CommonClientOptions ,
44
+ } from './models' ;
45
+
30
46
export * from './models' ;
31
47
32
48
export const apiClientVersion = searchClientVersion ;
@@ -37,7 +53,7 @@ export type Algoliasearch = ReturnType<typeof algoliasearch>;
37
53
export function algoliasearch (
38
54
appId : string ,
39
55
apiKey : string ,
40
- options ?: { requester ?: Requester ; hosts ?: Host [ ] }
56
+ options ?: CommonClientOptions
41
57
) {
42
58
if ( ! appId || typeof appId !== 'string' ) {
43
59
throw new Error ( '`appId` is missing.' ) ;
@@ -68,42 +84,85 @@ export function algoliasearch(
68
84
} ;
69
85
70
86
function initAnalytics (
71
- analyticsAppId : string ,
72
- analyticsApiKey : string ,
73
- region ?: AnalyticsRegion ,
74
- analyticsOptions ?: { requester ?: Requester ; hosts ?: Host [ ] }
87
+ initOptions : CommonInitOptions & InitRegion < AnalyticsRegion > = { }
75
88
) : AnalyticsClient {
89
+ if (
90
+ initOptions . region &&
91
+ ( typeof initOptions . region !== 'string' ||
92
+ ! analyticsRegions . includes ( initOptions . region ) )
93
+ ) {
94
+ throw new Error (
95
+ `\`region\` must be one of the following: ${ analyticsRegions . join (
96
+ ', '
97
+ ) } `
98
+ ) ;
99
+ }
100
+
76
101
return createAnalyticsClient ( {
77
- appId : analyticsAppId ,
78
- apiKey : analyticsApiKey ,
79
- region,
80
- ...analyticsOptions ,
102
+ ...initOptions . options ,
103
+ ...commonOptions ,
104
+ appId : initOptions . appId ?? appId ,
105
+ apiKey : initOptions . apiKey ?? apiKey ,
106
+ region : initOptions . region ,
107
+ } ) ;
108
+ }
109
+
110
+ function initAbtesting (
111
+ initOptions : CommonInitOptions & InitRegion < AbtestingRegion > = { }
112
+ ) : AbtestingClient {
113
+ if (
114
+ initOptions . region &&
115
+ ( typeof initOptions . region !== 'string' ||
116
+ ! abtestingRegions . includes ( initOptions . region ) )
117
+ ) {
118
+ throw new Error (
119
+ `\`region\` must be one of the following: ${ abtestingRegions . join (
120
+ ', '
121
+ ) } `
122
+ ) ;
123
+ }
124
+
125
+ return createAbtestingClient ( {
126
+ ...initOptions . options ,
81
127
...commonOptions ,
128
+ appId : initOptions . appId ?? appId ,
129
+ apiKey : initOptions . apiKey ?? apiKey ,
130
+ region : initOptions . region ,
82
131
} ) ;
83
132
}
84
133
85
134
function initPersonalization (
86
- personalizationAppId : string ,
87
- personalizationApiKey : string ,
88
- region : PersonalizationRegion ,
89
- personalizationOptions ?: { requester ?: Requester ; hosts ?: Host [ ] }
135
+ initOptions : CommonInitOptions & Required < InitRegion < PersonalizationRegion > >
90
136
) : PersonalizationClient {
91
- if ( ! region ) {
137
+ if ( ! initOptions . region ) {
92
138
throw new Error ( '`region` is missing.' ) ;
93
139
}
94
140
141
+ if (
142
+ initOptions . region &&
143
+ ( typeof initOptions . region !== 'string' ||
144
+ ! personalizationRegions . includes ( initOptions . region ) )
145
+ ) {
146
+ throw new Error (
147
+ `\`region\` must be one of the following: ${ personalizationRegions . join (
148
+ ', '
149
+ ) } `
150
+ ) ;
151
+ }
152
+
95
153
return createPersonalizationClient ( {
96
- appId : personalizationAppId ,
97
- apiKey : personalizationApiKey ,
98
- region,
99
- ...personalizationOptions ,
154
+ ...initOptions . options ,
100
155
...commonOptions ,
156
+ appId : initOptions . appId ?? appId ,
157
+ apiKey : initOptions . apiKey ?? apiKey ,
158
+ region : initOptions . region ,
101
159
} ) ;
102
160
}
103
161
104
162
return {
105
163
...createSearchClient ( { appId, apiKey, ...commonOptions } ) ,
106
164
initAnalytics,
107
165
initPersonalization,
166
+ initAbtesting,
108
167
} ;
109
168
}
0 commit comments