Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/algoliasearch/src/__tests__/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,40 @@ describe('default preset', () => {
expect(client).toHaveProperty('destroy');
}
});

describe('bridge methods', () => {
const clientWithTransformation = algoliasearch('appId', 'apiKey', {
transformation: { region: 'us' },
});
const indexWithTransformation = clientWithTransformation.initIndex('foo');
const index = client.initIndex('foo');

test('throws when missing transformation.region', () => {
// @ts-ignore
expect(() => algoliasearch('APP_ID', 'API_KEY', { transformation: {} })).toThrow(
'`region` must be provided when leveraging the transformation pipeline'
);
});

test('throws when calling the transformation methods without init parameters', async () => {
await expect(
index.saveObjectsWithTransformation([{ objectID: 'bar', baz: 42 }], { waitForTasks: true })
).rejects.toThrow(
'`options.transformation.region` must be provided at client instantiation before calling this method.'
);

await expect(
index.partialUpdateObjectsWithTransformation([{ objectID: 'bar', baz: 42 }], {
waitForTasks: true,
})
).rejects.toThrow(
'`transformation.region` must be provided at client instantiation before calling this method.'
);
});

test('exposes the transformation methods at the root of the client', () => {
expect(indexWithTransformation.saveObjectsWithTransformation).not.toBeUndefined();
expect(indexWithTransformation.partialUpdateObjectsWithTransformation).not.toBeUndefined();
});
});
});
122 changes: 76 additions & 46 deletions packages/algoliasearch/src/builds/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,16 @@ import {
import { createBrowserXhrRequester } from '@algolia/requester-browser-xhr';
import { createUserAgent, Request, RequestOptions } from '@algolia/transporter';

import { AlgoliaSearchOptions, InitAnalyticsOptions, InitPersonalizationOptions } from '../types';
import {
AlgoliaSearchOptions,
createIngestionClient,
IngestionClient,
IngestionMethods,
InitAnalyticsOptions,
InitPersonalizationOptions,
partialUpdateObjectsWithTransformation,
saveObjectsWithTransformation,
} from '../types';

export default function algoliasearch(
appId: string,
Expand Down Expand Up @@ -244,6 +253,17 @@ export default function algoliasearch(
});
};

/* eslint functional/no-let: "off" */
let ingestionTransporter: IngestionClient | undefined;

if (options?.transformation) {
if (!options.transformation.region) {
throw new Error('`region` must be provided when leveraging the transformation pipeline');
}

ingestionTransporter = createIngestionClient({ ...options, ...commonOptions });
}

return createSearchClient({
...searchClientOptions,
methods: {
Expand Down Expand Up @@ -285,50 +305,60 @@ export default function algoliasearch(
setDictionarySettings,
waitAppTask,
customRequest,
initIndex: base => (indexName: string): SearchIndex => {
return initIndex(base)(indexName, {
methods: {
batch,
delete: deleteIndex,
findAnswers,
getObject,
getObjects,
saveObject,
saveObjects,
search,
searchForFacetValues,
waitTask,
setSettings,
getSettings,
partialUpdateObject,
partialUpdateObjects,
deleteObject,
deleteObjects,
deleteBy,
clearObjects,
browseObjects,
getObjectPosition,
findObject,
exists,
saveSynonym,
saveSynonyms,
getSynonym,
searchSynonyms,
browseSynonyms,
deleteSynonym,
clearSynonyms,
replaceAllObjects,
replaceAllSynonyms,
searchRules,
getRule,
deleteRule,
saveRule,
saveRules,
replaceAllRules,
browseRules,
clearRules,
},
});
initIndex: base => (indexName: string): SearchIndex & IngestionMethods => {
return {
...initIndex(base)(indexName, {
methods: {
batch,
delete: deleteIndex,
findAnswers,
getObject,
getObjects,
saveObject,
saveObjects,
search,
searchForFacetValues,
waitTask,
setSettings,
getSettings,
partialUpdateObject,
partialUpdateObjects,
deleteObject,
deleteObjects,
deleteBy,
clearObjects,
browseObjects,
getObjectPosition,
findObject,
exists,
saveSynonym,
saveSynonyms,
getSynonym,
searchSynonyms,
browseSynonyms,
deleteSynonym,
clearSynonyms,
replaceAllObjects,
replaceAllSynonyms,
searchRules,
getRule,
deleteRule,
saveRule,
saveRules,
replaceAllRules,
browseRules,
clearRules,
},
}),
saveObjectsWithTransformation: saveObjectsWithTransformation(
indexName,
ingestionTransporter
),
partialUpdateObjectsWithTransformation: partialUpdateObjectsWithTransformation(
indexName,
ingestionTransporter
),
};
},
initAnalytics: () => (clientOptions?: InitAnalyticsOptions): AnalyticsClient => {
return createAnalyticsClient({
Expand Down Expand Up @@ -546,7 +576,7 @@ export type SearchIndex = BaseSearchIndex & {
};

export type SearchClient = BaseSearchClient & {
readonly initIndex: (indexName: string) => SearchIndex;
readonly initIndex: (indexName: string) => SearchIndex & IngestionMethods;
readonly search: <TObject>(
queries: readonly MultipleQueriesQuery[],
requestOptions?: RequestOptions & MultipleQueriesOptions
Expand Down
122 changes: 76 additions & 46 deletions packages/algoliasearch/src/builds/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,16 @@ import { Destroyable } from '@algolia/requester-common';
import { createNodeHttpRequester } from '@algolia/requester-node-http';
import { createUserAgent, Request, RequestOptions } from '@algolia/transporter';

import { AlgoliaSearchOptions, InitAnalyticsOptions, InitPersonalizationOptions } from '../types';
import {
AlgoliaSearchOptions,
createIngestionClient,
IngestionClient,
IngestionMethods,
InitAnalyticsOptions,
InitPersonalizationOptions,
partialUpdateObjectsWithTransformation,
saveObjectsWithTransformation,
} from '../types';

export default function algoliasearch(
appId: string,
Expand Down Expand Up @@ -244,6 +253,17 @@ export default function algoliasearch(
});
};

/* eslint functional/no-let: "off" */
let ingestionTransporter: IngestionClient | undefined;

if (options?.transformation) {
if (!options.transformation.region) {
throw new Error('`region` must be provided when leveraging the transformation pipeline');
}

ingestionTransporter = createIngestionClient({ ...options, ...commonOptions });
}

return createSearchClient({
...searchClientOptions,
methods: {
Expand Down Expand Up @@ -288,50 +308,60 @@ export default function algoliasearch(
setDictionarySettings,
waitAppTask,
customRequest,
initIndex: base => (indexName: string): SearchIndex => {
return initIndex(base)(indexName, {
methods: {
batch,
delete: deleteIndex,
findAnswers,
getObject,
getObjects,
saveObject,
saveObjects,
search,
searchForFacetValues,
waitTask,
setSettings,
getSettings,
partialUpdateObject,
partialUpdateObjects,
deleteObject,
deleteObjects,
deleteBy,
clearObjects,
browseObjects,
getObjectPosition,
findObject,
exists,
saveSynonym,
saveSynonyms,
getSynonym,
searchSynonyms,
browseSynonyms,
deleteSynonym,
clearSynonyms,
replaceAllObjects,
replaceAllSynonyms,
searchRules,
getRule,
deleteRule,
saveRule,
saveRules,
replaceAllRules,
browseRules,
clearRules,
},
});
initIndex: base => (indexName: string): SearchIndex & IngestionMethods => {
return {
...initIndex(base)(indexName, {
methods: {
batch,
delete: deleteIndex,
findAnswers,
getObject,
getObjects,
saveObject,
saveObjects,
search,
searchForFacetValues,
waitTask,
setSettings,
getSettings,
partialUpdateObject,
partialUpdateObjects,
deleteObject,
deleteObjects,
deleteBy,
clearObjects,
browseObjects,
getObjectPosition,
findObject,
exists,
saveSynonym,
saveSynonyms,
getSynonym,
searchSynonyms,
browseSynonyms,
deleteSynonym,
clearSynonyms,
replaceAllObjects,
replaceAllSynonyms,
searchRules,
getRule,
deleteRule,
saveRule,
saveRules,
replaceAllRules,
browseRules,
clearRules,
},
}),
saveObjectsWithTransformation: saveObjectsWithTransformation(
indexName,
ingestionTransporter
),
partialUpdateObjectsWithTransformation: partialUpdateObjectsWithTransformation(
indexName,
ingestionTransporter
),
};
},
initAnalytics: () => (clientOptions?: InitAnalyticsOptions): AnalyticsClient => {
return createAnalyticsClient({
Expand Down Expand Up @@ -549,7 +579,7 @@ export type SearchIndex = BaseSearchIndex & {
};

export type SearchClient = BaseSearchClient & {
readonly initIndex: (indexName: string) => SearchIndex;
readonly initIndex: (indexName: string) => SearchIndex & IngestionMethods;
readonly search: <TObject>(
queries: readonly MultipleQueriesQuery[],
requestOptions?: RequestOptions & MultipleQueriesOptions
Expand Down
5 changes: 4 additions & 1 deletion packages/algoliasearch/src/types/AlgoliaSearchOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ClientTransporterOptions } from '@algolia/client-common';
import { PersonalizationClientOptions } from '@algolia/client-personalization';
import { SearchClientOptions } from '@algolia/client-search';

import { TransformationOptions } from '.';

type Credentials = { readonly appId: string; readonly apiKey: string };
export type WithoutCredentials<TClientOptions extends Credentials> = Omit<
TClientOptions,
Expand All @@ -15,7 +17,8 @@ export type OptionalCredentials<TClientOptions extends Credentials> = Omit<
Pick<Partial<TClientOptions>, keyof Credentials>;

export type AlgoliaSearchOptions = Partial<ClientTransporterOptions> &
WithoutCredentials<SearchClientOptions>;
WithoutCredentials<SearchClientOptions> &
TransformationOptions;

export type InitAnalyticsOptions = Partial<ClientTransporterOptions> &
OptionalCredentials<AnalyticsClientOptions>;
Expand Down
Loading