Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
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
4 changes: 3 additions & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"buildCommand": "build",
"packages": ["packages/*"],
"sandboxes": ["github/algolia/create-instantsearch-app/tree/templates/javascript-client"]
"sandboxes": ["vanilla", "github/algolia/instantsearch/tree/3331ed781d3627bc10e73b6eff89b5037a78a0fc/javascript-client"],
"node": "12"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"bundlesize": [
{
"path": "packages/algoliasearch/dist/algoliasearch.umd.js",
"maxSize": "8.4KB"
"maxSize": "9.1KB"
},
{
"path": "packages/algoliasearch/dist/algoliasearch-lite.umd.js",
Expand Down
43 changes: 43 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,47 @@ 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 wrong transformation.region', () => {
expect(() =>
// @ts-ignore
algoliasearch('APP_ID', 'API_KEY', { transformation: { region: 'cn' } })
).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(
index.saveObjectsWithTransformation([{ objectID: 'bar', baz: 42 }], { waitForTasks: true })
).rejects.toThrow(
'`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();
});
});
});
127 changes: 80 additions & 47 deletions packages/algoliasearch/src/builds/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,24 @@ import {
import { createBrowserXhrRequester } from '@algolia/requester-browser-xhr';
import { createUserAgent, Request, RequestOptions } from '@algolia/transporter';

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

export default function algoliasearch(
appId: string,
apiKey: string,
options?: AlgoliaSearchOptions
options?: AlgoliaSearchOptions & TransformationOptions
): SearchClient {
const commonOptions = {
appId,
Expand Down Expand Up @@ -244,6 +256,17 @@ export default function algoliasearch(
});
};

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

if (options && 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 +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 @@ -546,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
127 changes: 80 additions & 47 deletions packages/algoliasearch/src/builds/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,24 @@ 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 {
createIngestionClient,
partialUpdateObjectsWithTransformation,
saveObjectsWithTransformation,
} from '../ingestion';
import {
AlgoliaSearchOptions,
IngestionClient,
IngestionMethods,
InitAnalyticsOptions,
InitPersonalizationOptions,
TransformationOptions,
} from '../types';

export default function algoliasearch(
appId: string,
apiKey: string,
options?: AlgoliaSearchOptions
options?: AlgoliaSearchOptions & TransformationOptions
): SearchClient {
const commonOptions = {
appId,
Expand Down Expand Up @@ -244,6 +256,17 @@ export default function algoliasearch(
});
};

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

if (options && 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 +311,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 +582,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
Loading