Skip to content

Commit 1c63fbf

Browse files
authored
feat(javascript): expose requestOptions and cache options (#283)
1 parent bce2a2a commit 1c63fbf

File tree

9 files changed

+83
-38
lines changed

9 files changed

+83
-38
lines changed

.github/.cache_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5
1+
6

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,47 @@
22
"files": [
33
{
44
"path": "packages/algoliasearch/dist/algoliasearch.umd.browser.js",
5-
"maxSize": "6.95KB"
5+
"maxSize": "7.05KB"
66
},
77
{
88
"path": "packages/client-abtesting/dist/client-abtesting.umd.browser.js",
9-
"maxSize": "3.75KB"
9+
"maxSize": "3.85KB"
1010
},
1111
{
1212
"path": "packages/client-analytics/dist/client-analytics.umd.browser.js",
13-
"maxSize": "4.35KB"
13+
"maxSize": "4.45KB"
1414
},
1515
{
1616
"path": "packages/client-insights/dist/client-insights.umd.browser.js",
17-
"maxSize": "3.60KB"
17+
"maxSize": "3.70KB"
1818
},
1919
{
2020
"path": "packages/client-personalization/dist/client-personalization.umd.browser.js",
21-
"maxSize": "3.75KB"
21+
"maxSize": "3.85KB"
2222
},
2323
{
2424
"path": "packages/client-query-suggestions/dist/client-query-suggestions.umd.browser.js",
25-
"maxSize": "3.80KB"
25+
"maxSize": "3.90KB"
2626
},
2727
{
2828
"path": "packages/client-search/dist/client-search.umd.browser.js",
29-
"maxSize": "5.80KB"
29+
"maxSize": "5.85KB"
3030
},
3131
{
3232
"path": "packages/client-sources/dist/client-sources.umd.browser.js",
33-
"maxSize": "3.60KB"
33+
"maxSize": "3.70KB"
3434
},
3535
{
3636
"path": "packages/recommend/dist/recommend.umd.browser.js",
37-
"maxSize": "3.70KB"
37+
"maxSize": "3.80KB"
3838
},
3939
{
4040
"path": "packages/client-common/dist/client-common.esm.node.js",
41-
"maxSize": "3.65KB"
41+
"maxSize": "3.80KB"
4242
},
4343
{
4444
"path": "packages/requester-browser-xhr/dist/requester-browser-xhr.esm.node.js",
45-
"maxSize": "900B"
45+
"maxSize": "825B"
4646
},
4747
{
4848
"path": "packages/requester-node-http/dist/requester-node-http.esm.node.js",

clients/algoliasearch-client-javascript/packages/client-common/src/transporter/createTransporter.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {
77
StackFrame,
88
TransporterOptions,
99
Transporter,
10+
Headers,
11+
QueryParameters,
1012
} from '../types';
1113

1214
import { createStatefulHost } from './createStatefulHost';
@@ -217,9 +219,37 @@ export function createTransporter({
217219
}
218220

219221
function createRequest<TResponse>(
220-
request: Request,
221-
requestOptions: RequestOptions
222+
baseRequest: Request,
223+
methodOptions: {
224+
headers: Headers;
225+
queryParameters: QueryParameters;
226+
},
227+
baseRequestOptions?: RequestOptions
222228
): Promise<TResponse> {
229+
const mergedData: Request['data'] = Array.isArray(baseRequest.data)
230+
? baseRequest.data
231+
: {
232+
...baseRequest.data,
233+
...baseRequestOptions?.data,
234+
};
235+
const request: Request = {
236+
...baseRequest,
237+
data: mergedData,
238+
};
239+
const requestOptions: RequestOptions = {
240+
cacheable: baseRequestOptions?.cacheable,
241+
timeout: baseRequestOptions?.timeout,
242+
queryParameters: {
243+
...baseRequestOptions?.queryParameters,
244+
...methodOptions.queryParameters,
245+
},
246+
headers: {
247+
Accept: 'application/json',
248+
...baseRequestOptions?.headers,
249+
...methodOptions.headers,
250+
},
251+
};
252+
223253
if (request.method !== 'GET') {
224254
/**
225255
* On write requests, no cache mechanisms are applied, and we

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Cache } from './Cache';
12
import type { Host } from './Host';
23
import type { Requester } from './Requester';
34
import type {
@@ -20,3 +21,11 @@ export type CreateClientOptions = Pick<
2021
hosts?: Host[];
2122
authMode?: AuthMode;
2223
};
24+
25+
export type InitClientOptions = Partial<{
26+
requester: Requester;
27+
hosts: Host[];
28+
responsesCache: Cache;
29+
requestsCache: Cache;
30+
hostsCache: Cache;
31+
}>;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
55
export type Request = {
66
method: Method;
77
path: string;
8-
data?: Record<string, any>;
8+
data?: Array<Record<string, any>> | Record<string, any>;
99
cacheable?: boolean;
1010
};
1111

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type RequestOptions = {
3030
* Custom data for the request. This data are
3131
* going to be merged the transporter data.
3232
*/
33-
data?: Record<string, any>;
33+
data?: Array<Record<string, any>> | Record<string, any>;
3434

3535
/**
3636
* If the given request should persist on the cache. Keep in mind,
@@ -194,10 +194,15 @@ export type Transporter = {
194194
hosts: Host[];
195195

196196
/**
197-
* Performs a read request using read hosts.
197+
* Performs a request.
198+
* The `baseRequest` and `baseRequestOptions` will be merged accordignly.
198199
*/
199200
request: <TResponse>(
200-
request: Request,
201-
requestOptions: RequestOptions
201+
baseRequest: Request,
202+
methodOptions: {
203+
headers: Headers;
204+
queryParameters: QueryParameters;
205+
},
206+
baseRequestOptions?: RequestOptions
202207
) => Promise<TResponse>;
203208
};

templates/javascript/api-all.mustache

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{{! This file will be renamed and moved to `builds/browser.ts` after generating the client }}
2-
3-
import type { Host, Requester } from '@experimental-api-clients-automation/client-common';
2+
import type { InitClientOptions } from '@experimental-api-clients-automation/client-common';
43
import { createMemoryCache, createFallbackableCache, createBrowserLocalStorageCache } from '@experimental-api-clients-automation/client-common';
54
import { createXhrRequester } from '@experimental-api-clients-automation/requester-browser-xhr';
65

@@ -17,7 +16,7 @@ export * from '../src/{{apiName}}Api';
1716
export function {{apiName}}Api(
1817
appId: string,
1918
apiKey: string,{{#hasRegionalHost}}region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region,{{/hasRegionalHost}}
20-
options?: { requester?: Requester; hosts?: Host[] }
19+
options?: InitClientOptions
2120
): {{capitalizedApiName}}Api {
2221
if (!appId) {
2322
throw new Error("`appId` is missing.");
@@ -46,9 +45,9 @@ export function {{apiName}}Api(
4645
requester: options?.requester ?? createXhrRequester(),
4746
userAgents: [{ segment: 'Browser' }],
4847
authMode: 'WithinQueryParameters',
49-
responsesCache: createMemoryCache(),
50-
requestsCache: createMemoryCache({ serializable: false }),
51-
hostsCache: createFallbackableCache({
48+
responsesCache: options?.responsesCache ?? createMemoryCache(),
49+
requestsCache: options?.requestsCache ?? createMemoryCache({ serializable: false }),
50+
hostsCache: options?.hostsCache ?? createFallbackableCache({
5251
caches: [
5352
createBrowserLocalStorageCache({ key: `${apiClientVersion}-${appId}` }),
5453
createMemoryCache(),

templates/javascript/api-single.mustache

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type {
99
Headers,
1010
Host,
1111
Request,
12+
RequestOptions,
13+
QueryParameters,
1214
} from '@experimental-api-clients-automation/client-common';
1315

1416
{{#imports}}
@@ -159,7 +161,7 @@ export function create{{capitalizedApiName}}Api(options: CreateClientOptions{{#h
159161
{{#allParams}}
160162
{{paramName}},
161163
{{/allParams}}
162-
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props
164+
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
163165
{{/bodyParams.0}}
164166
{{#bodyParams.0}}
165167
{{^queryParams.0}}
@@ -170,7 +172,7 @@ export function create{{capitalizedApiName}}Api(options: CreateClientOptions{{#h
170172
{{#allParams}}
171173
{{paramName}},
172174
{{/allParams}}
173-
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props
175+
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
174176
{{/bodyParams.1}}
175177
{{/bodyParams.0.isArray}}
176178
{{^bodyParams.0.isArray}}
@@ -184,27 +186,28 @@ export function create{{capitalizedApiName}}Api(options: CreateClientOptions{{#h
184186
{{#allParams}}
185187
{{paramName}},
186188
{{/allParams}}
187-
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props
189+
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
188190
{{/pathParams.0}}
189191
{{/queryParams.0}}
190192
{{#queryParams.0}}
191193
{
192194
{{#allParams}}
193195
{{paramName}},
194196
{{/allParams}}
195-
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props
197+
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
196198
{{/queryParams.0}}
197199
{{/bodyParams.0}}
198200
{{/allParams.0}}
201+
requestOptions?: RequestOptions
199202
) : Promise<{{{returnType}}}> {
200203
const requestPath = '{{{path}}}'{{#pathParams}}.replace(
201204
{{=<% %>=}}
202205
'{<%baseName%>}',
203206
<%={{ }}=%>
204207
encodeURIComponent(String({{paramName}}))
205208
){{/pathParams}};
206-
let headers: Headers = { Accept: 'application/json' };
207-
let queryParameters: Record<string, string> = {};
209+
const headers: Headers = {};
210+
const queryParameters: QueryParameters = {};
208211

209212
{{#allParams}}
210213
{{#required}}
@@ -243,7 +246,7 @@ export function create{{capitalizedApiName}}Api(options: CreateClientOptions{{#h
243246
return transporter.request(request, {
244247
queryParameters,
245248
headers,
246-
});
249+
}, requestOptions);
247250
}
248251

249252
{{/operation}}

templates/javascript/api.mustache

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{{! This file will be renamed and moved to `builds/node.ts` after generating the client }}
2-
3-
import type { Host, Requester } from '@experimental-api-clients-automation/client-common';
2+
import type { InitClientOptions } from '@experimental-api-clients-automation/client-common';
43
import { createMemoryCache, createNullCache } from '@experimental-api-clients-automation/client-common';
54
import { createHttpRequester } from '@experimental-api-clients-automation/requester-node-http';
65

@@ -16,7 +15,7 @@ export * from '../src/{{apiName}}Api';
1615
export function {{apiName}}Api(
1716
appId: string,
1817
apiKey: string,{{#hasRegionalHost}}region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region,{{/hasRegionalHost}}
19-
options?: { requester?: Requester; hosts?: Host[] }
18+
options?: InitClientOptions
2019
): {{capitalizedApiName}}Api {
2120
if (!appId) {
2221
throw new Error("`appId` is missing.");
@@ -44,9 +43,9 @@ export function {{apiName}}Api(
4443
},
4544
requester: options?.requester ?? createHttpRequester(),
4645
userAgents: [{ segment: 'Node.js', version: process.versions.node }],
47-
responsesCache: createNullCache(),
48-
requestsCache: createNullCache(),
49-
hostsCache: createMemoryCache(),
46+
responsesCache: options?.responsesCache ?? createNullCache(),
47+
requestsCache: options?.requestsCache ?? createNullCache(),
48+
hostsCache: options?.hostsCache ?? createMemoryCache(),
5049
...options,
5150
});
5251
}

0 commit comments

Comments
 (0)