Skip to content

Commit 6e77aa5

Browse files
authored
fix(javascript): check that appId, apiKey and region are valid parameters (#622)
1 parent e53dd5d commit 6e77aa5

File tree

7 files changed

+82
-7
lines changed

7 files changed

+82
-7
lines changed

templates/javascript/api-single.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const apiClientVersion = '{{packageVersion}}';
3434
{{/description}}
3535

3636
{{#hasRegionalHost}}
37-
export type Region = {{#allowedRegions}}'{{.}}'{{^-last}}|{{/-last}}{{/allowedRegions}};
37+
export const REGIONS = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}] as const;
38+
export type Region = typeof REGIONS[number];
3839
{{/hasRegionalHost}}
3940

4041
{{^hasRegionalHost}}

templates/javascript/browser.mustache

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { create{{capitalizedApiName}}, apiClientVersion } from '../src/{{apiName
66
import type { {{capitalizedApiName}} } from '../src/{{apiName}}';
77

88
{{#hasRegionalHost}}
9-
import type { Region } from '../src/{{apiName}}';
9+
import { Region, REGIONS } from '../src/{{apiName}}';
1010
{{/hasRegionalHost}}
1111

1212
export * from '../src/{{apiName}}';
@@ -16,11 +16,11 @@ export function {{apiName}}(
1616
apiKey: string,{{#hasRegionalHost}}region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region,{{/hasRegionalHost}}
1717
options?: InitClientOptions
1818
): {{capitalizedApiName}} {
19-
if (!appId) {
19+
if (!appId || typeof appId !== 'string') {
2020
throw new Error("`appId` is missing.");
2121
}
2222

23-
if (!apiKey) {
23+
if (!apiKey || typeof apiKey !== 'string') {
2424
throw new Error("`apiKey` is missing.");
2525
}
2626

@@ -29,6 +29,9 @@ export function {{apiName}}(
2929
if (!region) {
3030
throw new Error("`region` is missing.");
3131
}
32+
if (typeof region !== 'string' || !REGIONS.includes(region)) {
33+
throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`);
34+
}
3235
{{/fallbackToAliasHost}}
3336
{{/hasRegionalHost}}
3437

templates/javascript/node.mustache

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { create{{capitalizedApiName}} } from '../src/{{apiName}}';
66
import type { {{capitalizedApiName}} } from '../src/{{apiName}}';
77

88
{{#hasRegionalHost}}
9-
import type { Region } from '../src/{{apiName}}';
9+
import { Region, REGIONS } from '../src/{{apiName}}';
1010
{{/hasRegionalHost}}
1111

1212
export * from '../src/{{apiName}}';
@@ -16,11 +16,11 @@ export function {{apiName}}(
1616
apiKey: string,{{#hasRegionalHost}}region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region,{{/hasRegionalHost}}
1717
options?: InitClientOptions
1818
): {{capitalizedApiName}} {
19-
if (!appId) {
19+
if (!appId || typeof appId !== 'string') {
2020
throw new Error("`appId` is missing.");
2121
}
2222

23-
if (!apiKey) {
23+
if (!apiKey || typeof apiKey !== 'string') {
2424
throw new Error("`apiKey` is missing.");
2525
}
2626

@@ -29,6 +29,9 @@ export function {{apiName}}(
2929
if (!region) {
3030
throw new Error("`region` is missing.");
3131
}
32+
if (typeof region !== 'string' || !REGIONS.includes(region)) {
33+
throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`);
34+
}
3235
{{/fallbackToAliasHost}}
3336
{{/hasRegionalHost}}
3437

tests/CTS/client/personalization/parameters.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616
}
1717
]
1818
},
19+
{
20+
"testName": "throws when incorrect region is given",
21+
"autoCreateClient": false,
22+
"steps": [
23+
{
24+
"type": "createClient",
25+
"parameters": {
26+
"appId": "my-app-id",
27+
"apiKey": "my-api-key",
28+
"region": "not_a_region"
29+
},
30+
"expected": {
31+
"error": "`region` must be one of the following: eu, us"
32+
}
33+
}
34+
]
35+
},
1936
{
2037
"testName": "does not throw when region is given",
2138
"autoCreateClient": false,

tests/CTS/client/predict/parameters.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616
}
1717
]
1818
},
19+
{
20+
"testName": "throws when incorrect region is given",
21+
"autoCreateClient": false,
22+
"steps": [
23+
{
24+
"type": "createClient",
25+
"parameters": {
26+
"appId": "my-app-id",
27+
"apiKey": "my-api-key",
28+
"region": "not_a_region"
29+
},
30+
"expected": {
31+
"error": "`region` must be one of the following: ue, ew"
32+
}
33+
}
34+
]
35+
},
1936
{
2037
"testName": "does not throw when region is given",
2138
"autoCreateClient": false,

tests/CTS/client/query-suggestions/parameters.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616
}
1717
]
1818
},
19+
{
20+
"testName": "throws when incorrect region is given",
21+
"autoCreateClient": false,
22+
"steps": [
23+
{
24+
"type": "createClient",
25+
"parameters": {
26+
"appId": "my-app-id",
27+
"apiKey": "my-api-key",
28+
"region": "not_a_region"
29+
},
30+
"expected": {
31+
"error": "`region` must be one of the following: eu, us"
32+
}
33+
}
34+
]
35+
},
1936
{
2037
"testName": "does not throw when region is given",
2138
"autoCreateClient": false,

tests/CTS/client/sources/parameters.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616
}
1717
]
1818
},
19+
{
20+
"testName": "throws when incorrect region is given",
21+
"autoCreateClient": false,
22+
"steps": [
23+
{
24+
"type": "createClient",
25+
"parameters": {
26+
"appId": "my-app-id",
27+
"apiKey": "my-api-key",
28+
"region": "not_a_region"
29+
},
30+
"expected": {
31+
"error": "`region` must be one of the following: de, us"
32+
}
33+
}
34+
]
35+
},
1936
{
2037
"testName": "does not throw when region is given",
2138
"autoCreateClient": false,

0 commit comments

Comments
 (0)