Skip to content

Commit 0e63efd

Browse files
chore: generated code for commit 98b40e87. [skip ci]
algolia/api-clients-automation@98b40e8 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Clément Vannicatte <[email protected]>
1 parent 871fec5 commit 0e63efd

File tree

9 files changed

+249
-5
lines changed

9 files changed

+249
-5
lines changed

packages/algoliasearch-lite/model/clientMethodProps.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import type { SearchForFacetsOptions } from './searchForFacetsOptions';
2+
import type { SearchForHitsOptions } from './searchForHitsOptions';
3+
import type { SearchParamsObject } from './searchParamsObject';
4+
15
/**
26
* Properties for the `post` method.
37
*/
@@ -15,3 +19,35 @@ export type PostProps = {
1519
*/
1620
body?: Record<string, any>;
1721
};
22+
23+
/**
24+
* In v4, the search parameters are wrapped in a `params` parameter.
25+
*
26+
* @deprecated The `search` method now accepts flat `searchParams` at the root of the method.
27+
*/
28+
type LegacySearchParams = {
29+
params?: SearchParamsObject;
30+
};
31+
32+
/**
33+
* In v4, the search parameters are wrapped in a `params` parameter.
34+
*
35+
* @deprecated The `search` method now accepts flat `searchParams` at the root of the method.
36+
*/
37+
type LegacySearchForFacets = LegacySearchParams & SearchForFacetsOptions;
38+
39+
/**
40+
* In v4, the search parameters are wrapped in a `params` parameter.
41+
*
42+
* @deprecated The `search` method now accepts flat `searchParams` at the root of the method.
43+
*/
44+
type LegacySearchForHits = LegacySearchParams & SearchForHitsOptions;
45+
46+
type LegacySearchQuery = LegacySearchForFacets | LegacySearchForHits;
47+
48+
/**
49+
* Search method signature compatible with the `algoliasearch` v4 package. When using this signature, extra computation will be required to make it match the new signature.
50+
*
51+
* @deprecated This signature will be removed from the next major version, we recommend using the `SearchMethodParams` type for performances and future proof reasons.
52+
*/
53+
export type LegacySearchMethodProps = LegacySearchQuery[];

packages/algoliasearch-lite/src/algoliasearchLiteClient.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import type {
1313
QueryParameters,
1414
} from '@experimental-api-clients-automation/client-common';
1515

16-
import type { PostProps } from '../model/clientMethodProps';
16+
import type {
17+
PostProps,
18+
LegacySearchMethodProps,
19+
} from '../model/clientMethodProps';
1720
import type { SearchMethodParams } from '../model/searchMethodParams';
1821
import type { SearchResponses } from '../model/searchResponses';
1922

@@ -129,9 +132,34 @@ export function createAlgoliasearchLiteClient({
129132
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
130133
*/
131134
search(
132-
searchMethodParams: SearchMethodParams,
135+
searchMethodParams: LegacySearchMethodProps | SearchMethodParams,
133136
requestOptions?: RequestOptions
134137
): Promise<SearchResponses> {
138+
if (searchMethodParams && Array.isArray(searchMethodParams)) {
139+
const newSignatureRequest: SearchMethodParams = {
140+
requests: searchMethodParams.map(({ params, ...legacyRequest }) => {
141+
if (legacyRequest.type === 'facet') {
142+
return {
143+
...legacyRequest,
144+
...params,
145+
type: 'facet',
146+
};
147+
}
148+
149+
return {
150+
...legacyRequest,
151+
...params,
152+
facet: undefined,
153+
maxFacetHits: undefined,
154+
facetQuery: undefined,
155+
};
156+
}),
157+
};
158+
159+
// eslint-disable-next-line no-param-reassign
160+
searchMethodParams = newSignatureRequest;
161+
}
162+
135163
if (!searchMethodParams) {
136164
throw new Error(
137165
'Parameter `searchMethodParams` is required when calling `search`.'
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import type { EchoResponse } from '@experimental-api-clients-automation/client-common';
2+
import { echoRequester } from '@experimental-api-clients-automation/requester-node-http';
3+
4+
import { algoliasearch, apiClientVersion } from '../builds/node';
5+
6+
const client = algoliasearch('APP_ID', 'API_KEY', {
7+
requester: echoRequester(),
8+
});
9+
10+
describe('api', () => {
11+
it('sets the user agent', async () => {
12+
const req = (await client.post({
13+
path: '/test',
14+
})) as unknown as EchoResponse;
15+
16+
expect(req.algoliaAgent).toMatchInlineSnapshot(
17+
`"Algolia%20for%20JavaScript%20(${apiClientVersion});%20Search%20(${apiClientVersion});%20Node.js%20(${process.versions.node})"`
18+
);
19+
});
20+
21+
it('throws with undefined API key', () => {
22+
try {
23+
algoliasearch('APP_ID', '');
24+
} catch (e) {
25+
expect((e as Error).message).toMatch('`apiKey` is missing.');
26+
}
27+
});
28+
29+
it('throws with undefined app ID', () => {
30+
try {
31+
algoliasearch('', 'API_KEY');
32+
} catch (e) {
33+
expect((e as Error).message).toMatch('`appId` is missing.');
34+
}
35+
});
36+
37+
it('provides the search client at the root of the API', () => {
38+
expect(client.search).not.toBeUndefined();
39+
});
40+
41+
it('provides an init method for the analytics client', () => {
42+
expect(client.initAnalytics).not.toBeUndefined();
43+
});
44+
45+
it('provides an init method for the personalization client', () => {
46+
expect(client.initPersonalization).not.toBeUndefined();
47+
});
48+
});
49+
50+
/**
51+
* We only test the legacy signature, as `algoliasearch` inherits methods from the `client-search`.
52+
* The new signatures are already tested in the CTS.
53+
*/
54+
describe('search with legacy signature', () => {
55+
it('allows searching for query', async () => {
56+
const req = (await client.search([
57+
{
58+
indexName: 'theIndexName',
59+
},
60+
])) as unknown as EchoResponse;
61+
62+
expect(req.path).toEqual('/1/indexes/*/queries');
63+
expect(req.method).toEqual('POST');
64+
expect(req.data).toEqual({ requests: [{ indexName: 'theIndexName' }] });
65+
expect(req.searchParams).toStrictEqual(undefined);
66+
});
67+
68+
it('allows searching for facet', async () => {
69+
const req = (await client.search([
70+
{
71+
indexName: 'theIndexName',
72+
type: 'facet',
73+
facet: 'theFacet',
74+
},
75+
])) as unknown as EchoResponse;
76+
77+
expect(req.path).toEqual('/1/indexes/*/queries');
78+
expect(req.method).toEqual('POST');
79+
expect(req.data).toEqual({
80+
requests: [
81+
{ indexName: 'theIndexName', type: 'facet', facet: 'theFacet' },
82+
],
83+
});
84+
expect(req.searchParams).toStrictEqual(undefined);
85+
});
86+
87+
it('accepts a `params` parameter for `searchParams`', async () => {
88+
const req = (await client.search([
89+
{
90+
indexName: 'theIndexName',
91+
params: {
92+
hitsPerPage: 42,
93+
},
94+
},
95+
])) as unknown as EchoResponse;
96+
97+
expect(req.path).toEqual('/1/indexes/*/queries');
98+
expect(req.method).toEqual('POST');
99+
expect(req.data).toEqual({
100+
requests: [{ indexName: 'theIndexName', hitsPerPage: 42 }],
101+
});
102+
expect(req.searchParams).toStrictEqual(undefined);
103+
});
104+
});

packages/algoliasearch/jest.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Config } from '@jest/types';
2+
3+
const config: Config.InitialOptions = {
4+
preset: 'ts-jest',
5+
testEnvironment: 'node',
6+
roots: ['__tests__'],
7+
};
8+
9+
export default config;

packages/algoliasearch/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"browser": "dist/algoliasearch.cjs.browser.js",
1313
"types": "index.d.ts",
1414
"scripts": {
15-
"clean": "rm -rf ./dist"
15+
"clean": "rm -rf ./dist",
16+
"test": "jest"
1617
},
1718
"dependencies": {
1819
"@experimental-api-clients-automation/client-analytics": "0.4.0",
@@ -23,7 +24,9 @@
2324
"@experimental-api-clients-automation/requester-node-http": "0.4.0"
2425
},
2526
"devDependencies": {
27+
"@types/jest": "28.1.1",
2628
"@types/node": "16.11.39",
29+
"jest": "28.1.1",
2730
"typescript": "4.7.3"
2831
},
2932
"engines": {

packages/algoliasearch/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
4+
"types": ["node", "jest"],
45
"outDir": "dist"
56
},
67
"include": ["builds/node.ts", "builds/browser.ts"],
7-
"exclude": ["dist", "node_modules"]
8+
"exclude": ["dist", "node_modules", "__tests__"]
89
}

packages/client-search/model/clientMethodProps.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import type { OperationIndexParams } from './operationIndexParams';
1212
import type { Rule } from './rule';
1313
import type { SearchDictionaryEntriesParams } from './searchDictionaryEntriesParams';
1414
import type { SearchForFacetValuesRequest } from './searchForFacetValuesRequest';
15+
import type { SearchForFacetsOptions } from './searchForFacetsOptions';
16+
import type { SearchForHitsOptions } from './searchForHitsOptions';
1517
import type { SearchParams } from './searchParams';
18+
import type { SearchParamsObject } from './searchParamsObject';
1619
import type { SearchRulesParams } from './searchRulesParams';
1720
import type { Source } from './source';
1821
import type { SynonymHit } from './synonymHit';
@@ -585,6 +588,38 @@ export type SaveSynonymsProps = {
585588
replaceExistingSynonyms?: boolean;
586589
};
587590

591+
/**
592+
* In v4, the search parameters are wrapped in a `params` parameter.
593+
*
594+
* @deprecated The `search` method now accepts flat `searchParams` at the root of the method.
595+
*/
596+
type LegacySearchParams = {
597+
params?: SearchParamsObject;
598+
};
599+
600+
/**
601+
* In v4, the search parameters are wrapped in a `params` parameter.
602+
*
603+
* @deprecated The `search` method now accepts flat `searchParams` at the root of the method.
604+
*/
605+
type LegacySearchForFacets = LegacySearchParams & SearchForFacetsOptions;
606+
607+
/**
608+
* In v4, the search parameters are wrapped in a `params` parameter.
609+
*
610+
* @deprecated The `search` method now accepts flat `searchParams` at the root of the method.
611+
*/
612+
type LegacySearchForHits = LegacySearchParams & SearchForHitsOptions;
613+
614+
type LegacySearchQuery = LegacySearchForFacets | LegacySearchForHits;
615+
616+
/**
617+
* Search method signature compatible with the `algoliasearch` v4 package. When using this signature, extra computation will be required to make it match the new signature.
618+
*
619+
* @deprecated This signature will be removed from the next major version, we recommend using the `SearchMethodParams` type for performances and future proof reasons.
620+
*/
621+
export type LegacySearchMethodProps = LegacySearchQuery[];
622+
588623
/**
589624
* Properties for the `searchDictionaryEntries` method.
590625
*/

packages/client-search/src/searchClient.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import type {
6262
SaveRulesProps,
6363
SaveSynonymProps,
6464
SaveSynonymsProps,
65+
LegacySearchMethodProps,
6566
SearchDictionaryEntriesProps,
6667
SearchForFacetValuesProps,
6768
SearchRulesProps,
@@ -2213,9 +2214,34 @@ export function createSearchClient({
22132214
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
22142215
*/
22152216
search(
2216-
searchMethodParams: SearchMethodParams,
2217+
searchMethodParams: LegacySearchMethodProps | SearchMethodParams,
22172218
requestOptions?: RequestOptions
22182219
): Promise<SearchResponses> {
2220+
if (searchMethodParams && Array.isArray(searchMethodParams)) {
2221+
const newSignatureRequest: SearchMethodParams = {
2222+
requests: searchMethodParams.map(({ params, ...legacyRequest }) => {
2223+
if (legacyRequest.type === 'facet') {
2224+
return {
2225+
...legacyRequest,
2226+
...params,
2227+
type: 'facet',
2228+
};
2229+
}
2230+
2231+
return {
2232+
...legacyRequest,
2233+
...params,
2234+
facet: undefined,
2235+
maxFacetHits: undefined,
2236+
facetQuery: undefined,
2237+
};
2238+
}),
2239+
};
2240+
2241+
// eslint-disable-next-line no-param-reassign
2242+
searchMethodParams = newSignatureRequest;
2243+
}
2244+
22192245
if (!searchMethodParams) {
22202246
throw new Error(
22212247
'Parameter `searchMethodParams` is required when calling `search`.'

yarn.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,9 @@ __metadata:
13531353
"@experimental-api-clients-automation/client-search": 0.4.0
13541354
"@experimental-api-clients-automation/requester-browser-xhr": 0.4.0
13551355
"@experimental-api-clients-automation/requester-node-http": 0.4.0
1356+
"@types/jest": 28.1.1
13561357
"@types/node": 16.11.39
1358+
jest: 28.1.1
13571359
typescript: 4.7.3
13581360
languageName: unknown
13591361
linkType: soft

0 commit comments

Comments
 (0)