Skip to content

Commit 118be28

Browse files
committed
fix: assert region
1 parent 716f120 commit 118be28

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

packages/algoliasearch/src/__tests__/default.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ describe('default preset', () => {
187187
);
188188
});
189189

190+
test('throws when wrong transformation.region', () => {
191+
expect(() =>
192+
// @ts-ignore
193+
algoliasearch('APP_ID', 'API_KEY', { transformation: { region: 'cn' } })
194+
).toThrow('`region` is required and must be one of the following: eu, us}`');
195+
});
196+
190197
test('throws when calling the transformation methods without init parameters', async () => {
191198
await expect(
192199
index.saveObjectsWithTransformation([{ objectID: 'bar', baz: 42 }], { waitForTasks: true })

packages/algoliasearch/src/ingestion.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
IngestionClient,
1414
PushOptions,
1515
PushProps,
16+
REGIONS,
1617
TransformationOptions,
1718
WatchResponse,
1819
} from './types';
@@ -24,6 +25,12 @@ export function createIngestionClient(
2425
throw new Error('`region` must be provided when leveraging the transformation pipeline');
2526
}
2627

28+
if (!REGIONS.includes(options.transformation.region)) {
29+
throw new Error(
30+
`\`region\` is required and must be one of the following: ${REGIONS.join(', ')}`
31+
);
32+
}
33+
2734
const appId = options.appId;
2835

2936
const auth = createAuth(AuthMode.WithinHeaders, appId, options.apiKey);

packages/algoliasearch/src/types/Ingestion.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import {
77
} from '@algolia/client-search';
88
import { RequestOptions } from '@algolia/transporter';
99

10+
export const REGIONS = ['eu', 'us'] as const;
11+
export type Region = typeof REGIONS[number];
12+
1013
export type TransformationOptions = {
1114
// When provided, a second transporter will be created in order to leverage the `*WithTransformation` methods exposed by the Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/).
1215
readonly transformation?: {
1316
// The region of your Algolia application ID, used to target the correct hosts of the transformation service.
14-
readonly region: 'eu' | 'us';
17+
readonly region: Region;
1518
};
1619
};
1720

0 commit comments

Comments
 (0)