diff --git a/packages/algoliasearch/src/__tests__/default.test.ts b/packages/algoliasearch/src/__tests__/default.test.ts index 40b29a658..f7a6ec534 100644 --- a/packages/algoliasearch/src/__tests__/default.test.ts +++ b/packages/algoliasearch/src/__tests__/default.test.ts @@ -191,22 +191,22 @@ describe('default preset', () => { expect(() => // @ts-ignore algoliasearch('APP_ID', 'API_KEY', { transformation: { region: 'cn' } }) - ).toThrow('`region` is required and must be one of the following: eu, us}`'); + ).toThrow('`region` is required and must be one of the following: eu, us'); }); - test('throws when calling the transformation methods without init parameters', async () => { - await expect( + test('throws when calling the transformation methods without init parameters', () => { + expect(() => index.saveObjectsWithTransformation([{ objectID: 'bar', baz: 42 }], { waitForTasks: true }) - ).rejects.toThrow( - '`transformation.region` must be provided at client instantiation before calling this method.' + ).toThrow( + '`options.transformation.region` must be provided at client instantiation before calling this method.' ); - await expect( + expect(() => index.partialUpdateObjectsWithTransformation([{ objectID: 'bar', baz: 42 }], { waitForTasks: true, }) - ).rejects.toThrow( - '`transformation.region` must be provided at client instantiation before calling this method.' + ).toThrow( + '`options.transformation.region` must be provided at client instantiation before calling this method.' ); }); diff --git a/packages/algoliasearch/src/builds/browser.ts b/packages/algoliasearch/src/builds/browser.ts index 45270ac66..c8fd01534 100644 --- a/packages/algoliasearch/src/builds/browser.ts +++ b/packages/algoliasearch/src/builds/browser.ts @@ -207,6 +207,7 @@ import { createIngestionClient, partialUpdateObjectsWithTransformation, saveObjectsWithTransformation, + transformationConfigurationError, } from '../ingestion'; import { AlgoliaSearchOptions, @@ -261,7 +262,9 @@ export default function algoliasearch( if (options && options.transformation) { if (!options.transformation.region) { - throw new Error('`region` must be provided when leveraging the transformation pipeline'); + throw transformationConfigurationError( + '`region` must be provided when leveraging the transformation pipeline' + ); } ingestionTransporter = createIngestionClient({ ...options, ...commonOptions }); diff --git a/packages/algoliasearch/src/builds/node.ts b/packages/algoliasearch/src/builds/node.ts index c24525b8d..db263600b 100644 --- a/packages/algoliasearch/src/builds/node.ts +++ b/packages/algoliasearch/src/builds/node.ts @@ -209,6 +209,7 @@ import { createIngestionClient, partialUpdateObjectsWithTransformation, saveObjectsWithTransformation, + transformationConfigurationError, } from '../ingestion'; import { AlgoliaSearchOptions, @@ -261,7 +262,9 @@ export default function algoliasearch( if (options && options.transformation) { if (!options.transformation.region) { - throw new Error('`region` must be provided when leveraging the transformation pipeline'); + throw transformationConfigurationError( + '`region` must be provided when leveraging the transformation pipeline' + ); } ingestionTransporter = createIngestionClient({ ...options, ...commonOptions }); diff --git a/packages/algoliasearch/src/ingestion.ts b/packages/algoliasearch/src/ingestion.ts index abeb5c311..c3bd940b9 100644 --- a/packages/algoliasearch/src/ingestion.ts +++ b/packages/algoliasearch/src/ingestion.ts @@ -21,11 +21,15 @@ export function createIngestionClient( options: SearchClientOptions & ClientTransporterOptions & TransformationOptions ): IngestionClient { if (!options || !options.transformation || !options.transformation.region) { - throw new Error('`region` must be provided when leveraging the transformation pipeline'); + throw transformationConfigurationError( + '`region` must be provided when leveraging the transformation pipeline' + ); } if (options.transformation.region !== 'eu' && options.transformation.region !== 'us') { - throw new Error('`region` is required and must be one of the following: eu, us'); + throw transformationConfigurationError( + '`region` is required and must be one of the following: eu, us' + ); } const appId = options.appId; @@ -66,29 +70,37 @@ export function createIngestionClient( transporter.responsesCache.clear(), ]).then(() => undefined); }, - async push( + push( { indexName, pushTaskPayload, watch }: PushProps, requestOptions?: RequestOptions - ): Promise { + ): Readonly> { if (!indexName) { - throw new Error('Parameter `indexName` is required when calling `push`.'); + throw transformationConfigurationError( + 'Parameter `indexName` is required when calling `push`.' + ); } if (!pushTaskPayload) { - throw new Error('Parameter `pushTaskPayload` is required when calling `push`.'); + throw transformationConfigurationError( + 'Parameter `pushTaskPayload` is required when calling `push`.' + ); } if (!pushTaskPayload.action) { - throw new Error('Parameter `pushTaskPayload.action` is required when calling `push`.'); + throw transformationConfigurationError( + 'Parameter `pushTaskPayload.action` is required when calling `push`.' + ); } if (!pushTaskPayload.records) { - throw new Error('Parameter `pushTaskPayload.records` is required when calling `push`.'); + throw transformationConfigurationError( + 'Parameter `pushTaskPayload.records` is required when calling `push`.' + ); } const opts: RequestOptions = requestOptions || { queryParameters: {} }; - return await transporter.write( + return transporter.write( { method: MethodEnum.Post, path: encode('1/push/%s', indexName), @@ -107,12 +119,12 @@ export function createIngestionClient( } export function saveObjectsWithTransformation(indexName: string, client?: IngestionClient) { - return async ( + return ( objects: ReadonlyArray>>, requestOptions?: RequestOptions & ChunkOptions & SaveObjectsOptions & PushOptions - ): Promise => { + ): Readonly> => { if (!client) { - throw new Error( + throw transformationConfigurationError( '`options.transformation.region` must be provided at client instantiation before calling this method.' ); } @@ -124,7 +136,7 @@ export function saveObjectsWithTransformation(indexName: string, client?: Ingest : BatchActionEnum.UpdateObject; /* eslint functional/immutable-data: "off" */ - return await client.push( + return client.push( { indexName, pushTaskPayload: { action, records: objects }, @@ -139,12 +151,12 @@ export function partialUpdateObjectsWithTransformation( indexName: string, client?: IngestionClient ) { - return async ( + return ( objects: ReadonlyArray>>, requestOptions?: RequestOptions & ChunkOptions & PartialUpdateObjectsOptions & PushOptions - ): Promise => { + ): Readonly> => { if (!client) { - throw new Error( + throw transformationConfigurationError( '`options.transformation.region` must be provided at client instantiation before calling this method.' ); } @@ -156,7 +168,7 @@ export function partialUpdateObjectsWithTransformation( : BatchActionEnum.PartialUpdateObjectNoCreate; /* eslint functional/immutable-data: "off" */ - return await client.push( + return client.push( { indexName, pushTaskPayload: { action, records: objects }, @@ -166,3 +178,10 @@ export function partialUpdateObjectsWithTransformation( ); }; } + +export function transformationConfigurationError(message: string): Error { + return { + name: 'TransformationConfigurationError', + message, + }; +} diff --git a/packages/algoliasearch/src/types/Ingestion.ts b/packages/algoliasearch/src/types/Ingestion.ts index 04ad3b99d..b483a1567 100644 --- a/packages/algoliasearch/src/types/Ingestion.ts +++ b/packages/algoliasearch/src/types/Ingestion.ts @@ -25,7 +25,7 @@ export type IngestionMethods = { readonly saveObjectsWithTransformation: ( objects: ReadonlyArray>>, requestOptions?: RequestOptions & ChunkOptions & SaveObjectsOptions & PushOptions - ) => Promise; + ) => Readonly>; /** * Helper: Similar to the `partialUpdateObjects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must've been passed to the client instantiation method. @@ -36,7 +36,7 @@ export type IngestionMethods = { readonly partialUpdateObjectsWithTransformation: ( objects: ReadonlyArray>>, requestOptions?: RequestOptions & ChunkOptions & PartialUpdateObjectsOptions & PushOptions - ) => Promise; + ) => Readonly>; }; export type WatchResponse = { @@ -108,5 +108,5 @@ export type IngestionClient = BaseSearchClient & { readonly push: ( { indexName, pushTaskPayload, watch }: PushProps, requestOptions?: RequestOptions - ) => Promise; + ) => Readonly>; };