Skip to content

Commit d17ff42

Browse files
committed
add browse* methods, update docs
1 parent 1339815 commit d17ff42

File tree

8 files changed

+229
-11
lines changed

8 files changed

+229
-11
lines changed

clients/algoliasearch-client-javascript/bundlesize.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"files": [
33
{
44
"path": "packages/algoliasearch/dist/algoliasearch.umd.js",
5-
"maxSize": "8.40KB"
5+
"maxSize": "8.60KB"
66
},
77
{
88
"path": "packages/algoliasearch/dist/lite/lite.umd.js",
@@ -30,7 +30,7 @@
3030
},
3131
{
3232
"path": "packages/client-search/dist/client-search.umd.js",
33-
"maxSize": "6.70KB"
33+
"maxSize": "6.90KB"
3434
},
3535
{
3636
"path": "packages/client-sources/dist/client-sources.umd.js",

clients/algoliasearch-client-javascript/packages/client-common/src/types/CreateIterablePromise.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export type IterableOptions<TResponse> = Partial<{
22
/**
3-
* The function that runs right after the `func` method has been executed, allows you to do anything with the response before `validate`.
3+
* The function that runs right after the API call has been resolved, allows you to do anything with the response before `validate`.
44
*/
55
aggregator: (response: TResponse) => void;
66

@@ -34,7 +34,7 @@ export type CreateIterablePromise<TResponse> = IterableOptions<TResponse> & {
3434
func: (previousResponse?: TResponse) => Promise<TResponse>;
3535

3636
/**
37-
* The validator function. It receives the resolved return of `func`.
37+
* The validator function. It receive the resolved return of the API call.
3838
*/
3939
validate: (response: TResponse) => boolean;
4040
};

playground/javascript/node/search.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { searchClient } from '@algolia/client-search';
2-
import { ApiError } from '@algolia/client-common';
1+
import {
2+
BrowseResponse,
3+
Rule,
4+
searchClient,
5+
SearchRulesResponse,
6+
SearchSynonymsResponse,
7+
} from '@algolia/client-search';
8+
import { ApiError, createIterablePromise } from '@algolia/client-common';
39
import dotenv from 'dotenv';
410

511
dotenv.config({ path: '../../.env' });
@@ -17,9 +23,15 @@ client.addAlgoliaAgent('Node playground', '0.0.1');
1723

1824
async function testSearch() {
1925
try {
20-
const res = await client.search<{ name: string }>({ requests: [{ indexName: searchIndex, query: searchQuery }] });
26+
const records: Record<string, any> = [];
2127

22-
console.log(`[OK]`, res.results[0].hits![0].name);
28+
await client.browseObjects({
29+
indexName: 'gatsbyjs',
30+
31+
aggregator: (response) => records.push(...response.hits),
32+
});
33+
34+
console.log(records, records.length);
2335
} catch (e: any) {
2436
// Instance of
2537
if (e instanceof ApiError) {

specs/search/paths/rules/searchRules.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ post:
99
parameters:
1010
- $ref: '../../../common/parameters.yml#/IndexName'
1111
requestBody:
12-
required: true
1312
content:
1413
application/json:
1514
schema:

templates/javascript/clients/client/api/helpers.mustache

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Helper: Wait for a task to be published (completed) for a given `indexName` and `taskID`.
33
*
4-
* @summary Wait for a task to be published (completed).
4+
* @summary Helper method that waits for a task to be published (completed).
55
* @param waitForTaskOptions - The waitForTaskOptions object.
66
* @param waitForTaskOptions.indexName - The `indexName` where the operation was performed.
77
* @param waitForTaskOptions.taskID - The `taskID` returned in the method response.
@@ -37,7 +37,7 @@ waitForTask(
3737
/**
3838
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
3939
*
40-
* @summary Wait for an API key task to be processed.
40+
* @summary Helper method that waits for an API key task to be processed.
4141
* @param waitForApiKeyOptions - The waitForApiKeyOptions object.
4242
* @param waitForApiKeyOptions.operation - The `operation` that was done on a `key`.
4343
* @param waitForApiKeyOptions.key - The `key` that has been added, deleted or updated.
@@ -106,3 +106,126 @@ waitForApiKey(
106106
operation === 'add' ? error.status !== 404 : error.status === 404,
107107
});
108108
},
109+
110+
/**
111+
* Helper: Iterate on the `browse` method of the client to allow aggregating objects of an index.
112+
*
113+
* @summary Helper method that iterates on the `browse` method.
114+
* @param browseObjects - The browseObjects object.
115+
* @param browseObjects.indexName - The index in which to perform the request.
116+
* @param browseObjects.browseRequest - The `browse` method parameters.
117+
* @param browseObjects.validate - The validator function. It receive the resolved return of the API call.
118+
* @param browseObjects.aggregator - The function that runs right after the API call has been resolved, allows you to do anything with the response before `validate`.
119+
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `browse` method and merged with the transporter requestOptions.
120+
*/
121+
browseObjects<T>(
122+
{
123+
indexName,
124+
browseRequest,
125+
...browseObjectsOptions
126+
}: BrowseOptions<BrowseResponse<T>> & BrowseProps,
127+
requestOptions?: RequestOptions
128+
): Promise<BrowseResponse<T>> {
129+
return createIterablePromise<BrowseResponse<T>>({
130+
func: (previousResponse) => {
131+
return this.browse(
132+
{
133+
indexName,
134+
browseRequest: {
135+
cursor: previousResponse ? previousResponse.cursor : undefined,
136+
...browseRequest,
137+
},
138+
},
139+
requestOptions
140+
);
141+
},
142+
validate: (response) => response.cursor === undefined,
143+
...browseObjectsOptions,
144+
});
145+
},
146+
147+
/**
148+
* Helper: Iterate on the `searchRules` method of the client to allow aggregating rules of an index.
149+
*
150+
* @summary Helper method that iterates on the `searchRules` method.
151+
* @param browseObjects - The browseObjects object.
152+
* @param browseObjects.indexName - The index in which to perform the request.
153+
* @param browseObjects.searchRulesParams - The `searchRules` method parameters.
154+
* @param browseObjects.validate - The validator function. It receive the resolved return of the API call.
155+
* @param browseObjects.aggregator - The function that runs right after the API call has been resolved, allows you to do anything with the response before `validate`.
156+
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `searchRules` method and merged with the transporter requestOptions.
157+
*/
158+
browseRules(
159+
{
160+
indexName,
161+
searchRulesParams,
162+
...browseRulesOptions
163+
}: BrowseOptions<SearchRulesResponse> & SearchRulesProps,
164+
requestOptions?: RequestOptions
165+
): Promise<SearchRulesResponse> {
166+
const params = {
167+
hitsPerPage: 1000,
168+
...searchRulesParams,
169+
};
170+
171+
return createIterablePromise<SearchRulesResponse>({
172+
func: (previousResponse) => {
173+
return this.searchRules(
174+
{
175+
indexName,
176+
searchRulesParams: {
177+
...params,
178+
page: previousResponse
179+
? previousResponse.page + 1
180+
: params.page || 0,
181+
},
182+
},
183+
requestOptions
184+
);
185+
},
186+
validate: (response) => response.nbHits < params.hitsPerPage,
187+
...browseRulesOptions,
188+
});
189+
},
190+
191+
/**
192+
* Helper: Iterate on the `searchSynonyms` method of the client to allow aggregating rules of an index.
193+
*
194+
* @summary Helper method that iterates on the `searchSynonyms` method.
195+
* @param browseObjects - The browseObjects object.
196+
* @param browseObjects.indexName - The index in which to perform the request.
197+
* @param browseObjects.validate - The validator function. It receive the resolved return of the API call.
198+
* @param browseObjects.aggregator - The function that runs right after the API call has been resolved, allows you to do anything with the response before `validate`.
199+
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `searchSynonyms` method and merged with the transporter requestOptions.
200+
*/
201+
browseSynonyms(
202+
{
203+
indexName,
204+
validate,
205+
aggregator,
206+
...browseSynonymsOptions
207+
}: BrowseOptions<SearchSynonymsResponse> & SearchSynonymsProps,
208+
requestOptions?: RequestOptions
209+
): Promise<SearchSynonymsResponse> {
210+
const params = {
211+
hitsPerPage: 1000,
212+
...browseSynonymsOptions,
213+
};
214+
215+
return createIterablePromise<SearchSynonymsResponse>({
216+
func: (previousResponse) => {
217+
return this.searchSynonyms(
218+
{
219+
...params,
220+
indexName,
221+
page: previousResponse
222+
? previousResponse.page + 1
223+
: browseSynonymsOptions.page || 0,
224+
},
225+
requestOptions
226+
);
227+
},
228+
validate: (response) => response.nbHits < params.hitsPerPage,
229+
...browseSynonymsOptions,
230+
});
231+
},

templates/javascript/clients/client/api/imports.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { {{classname}} } from '{{filename}}';
2727
{{#operations}}
2828
import type {
2929
{{#isSearchClient}}
30+
BrowseOptions,
3031
WaitForTaskOptions,
3132
WaitForApiKeyOptions,
3233
{{/isSearchClient}}

templates/javascript/clients/client/model/clientMethodProps.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
4040
{{/operations}}
4141

4242
{{#isSearchClient}}
43+
/**
44+
* The `browseObjects`, `browseRules`, `browseSynonyms` options.
45+
*/
46+
export type BrowseOptions<T> = Partial<
47+
Pick<CreateIterablePromise<T>, 'validate'>
48+
> &
49+
Required<Pick<CreateIterablePromise<T>, 'aggregator'>>;
50+
4351
type WaitForOptions<T> = Omit<
4452
CreateIterablePromise<T>,
4553
'func' | 'timeout' | 'validate'

website/docs/clients/migration-guides/index.mdx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,81 @@ client.batch("<YOUR_INDEX_NAME>", new BatchWriteParams().setRequests(batch));
415415

416416
</td></tr></tbody></table>
417417

418+
<table width="100%"><tbody><tr><td width="400px" valign="top">
419+
420+
### `browseObjects`/`browseRules`/`browseSynonyms`
421+
422+
<hr />
423+
424+
The `browseObjects`/`browseRules`/`browseSynonyms` signature have changed to match the general usage of the API clients.
425+
426+
</td><td width="600px">
427+
428+
<TabsLanguage>
429+
<TabItem value="javascript">
430+
431+
```js
432+
// browse records
433+
const records: Record<string, any> = [];
434+
435+
await client.browseObjects({
436+
// all base `browse` options are forwarded to the `browse` method
437+
indexName: '<YOUR_INDEX_NAME>',
438+
439+
// the aggregator to execute right after the API call has been resolved
440+
aggregator: (response) => records.push(...response.hits),
441+
});
442+
443+
console.log(records, records.length);
444+
445+
// browse rules
446+
const rules: Record<string, any> = [];
447+
448+
await client.browseRules({
449+
// all base `searchRules` options are forwarded to the `searchRules` method
450+
indexName: '<YOUR_INDEX_NAME>',
451+
452+
// the aggregator to execute right after the API call has been resolved
453+
aggregator: (response) => rules.push(...response.hits),
454+
});
455+
456+
console.log(rules, rules.length);
457+
458+
// browse synonyms
459+
const synonyms: Record<string, any> = [];
460+
461+
await client.browseSynonyms({
462+
// all base `searchSynonyms` options are forwarded to the `searchSynonyms` method
463+
indexName: '<YOUR_INDEX_NAME>',
464+
465+
// the aggregator to execute right after the API call has been resolved
466+
aggregator: (response) => synonyms.push(...response.hits),
467+
});
468+
469+
console.log(synonyms, synonyms.length);
470+
```
471+
472+
</TabItem>
473+
<TabItem value="php">
474+
475+
```java
476+
// WIP
477+
478+
```
479+
480+
</TabItem>
481+
<TabItem value="java">
482+
483+
```java
484+
// WIP
485+
486+
```
487+
488+
</TabItem>
489+
</TabsLanguage>
490+
491+
</td></tr></tbody></table>
492+
418493
## Client's specific breaking changes
419494

420495
You can find specific client breaking changes in their own section:

0 commit comments

Comments
 (0)