Skip to content

Commit

Permalink
feat(javascript): expose requestOptions and cache options (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Mar 24, 2022
1 parent 272ebd3 commit 09259f7
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/.cache_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5
6
22 changes: 11 additions & 11 deletions clients/algoliasearch-client-javascript/bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,47 @@
"files": [
{
"path": "packages/algoliasearch/dist/algoliasearch.umd.browser.js",
"maxSize": "6.95KB"
"maxSize": "7.05KB"
},
{
"path": "packages/client-abtesting/dist/client-abtesting.umd.browser.js",
"maxSize": "3.75KB"
"maxSize": "3.85KB"
},
{
"path": "packages/client-analytics/dist/client-analytics.umd.browser.js",
"maxSize": "4.35KB"
"maxSize": "4.45KB"
},
{
"path": "packages/client-insights/dist/client-insights.umd.browser.js",
"maxSize": "3.60KB"
"maxSize": "3.70KB"
},
{
"path": "packages/client-personalization/dist/client-personalization.umd.browser.js",
"maxSize": "3.75KB"
"maxSize": "3.85KB"
},
{
"path": "packages/client-query-suggestions/dist/client-query-suggestions.umd.browser.js",
"maxSize": "3.80KB"
"maxSize": "3.90KB"
},
{
"path": "packages/client-search/dist/client-search.umd.browser.js",
"maxSize": "5.80KB"
"maxSize": "5.85KB"
},
{
"path": "packages/client-sources/dist/client-sources.umd.browser.js",
"maxSize": "3.60KB"
"maxSize": "3.70KB"
},
{
"path": "packages/recommend/dist/recommend.umd.browser.js",
"maxSize": "3.70KB"
"maxSize": "3.80KB"
},
{
"path": "packages/client-common/dist/client-common.esm.node.js",
"maxSize": "3.65KB"
"maxSize": "3.80KB"
},
{
"path": "packages/requester-browser-xhr/dist/requester-browser-xhr.esm.node.js",
"maxSize": "900B"
"maxSize": "825B"
},
{
"path": "packages/requester-node-http/dist/requester-node-http.esm.node.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {
StackFrame,
TransporterOptions,
Transporter,
Headers,
QueryParameters,
} from '../types';

import { createStatefulHost } from './createStatefulHost';
Expand Down Expand Up @@ -217,9 +219,37 @@ export function createTransporter({
}

function createRequest<TResponse>(
request: Request,
requestOptions: RequestOptions
baseRequest: Request,
methodOptions: {
headers: Headers;
queryParameters: QueryParameters;
},
baseRequestOptions?: RequestOptions
): Promise<TResponse> {
const mergedData: Request['data'] = Array.isArray(baseRequest.data)
? baseRequest.data
: {
...baseRequest.data,
...baseRequestOptions?.data,
};
const request: Request = {
...baseRequest,
data: mergedData,
};
const requestOptions: RequestOptions = {
cacheable: baseRequestOptions?.cacheable,
timeout: baseRequestOptions?.timeout,
queryParameters: {
...baseRequestOptions?.queryParameters,
...methodOptions.queryParameters,
},
headers: {
Accept: 'application/json',
...baseRequestOptions?.headers,
...methodOptions.headers,
},
};

if (request.method !== 'GET') {
/**
* On write requests, no cache mechanisms are applied, and we
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Cache } from './Cache';
import type { Host } from './Host';
import type { Requester } from './Requester';
import type {
Expand All @@ -20,3 +21,11 @@ export type CreateClientOptions = Pick<
hosts?: Host[];
authMode?: AuthMode;
};

export type InitClientOptions = Partial<{
requester: Requester;
hosts: Host[];
responsesCache: Cache;
requestsCache: Cache;
hostsCache: Cache;
}>;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
export type Request = {
method: Method;
path: string;
data?: Record<string, any>;
data?: Array<Record<string, any>> | Record<string, any>;
cacheable?: boolean;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type RequestOptions = {
* Custom data for the request. This data are
* going to be merged the transporter data.
*/
data?: Record<string, any>;
data?: Array<Record<string, any>> | Record<string, any>;

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

/**
* Performs a read request using read hosts.
* Performs a request.
* The `baseRequest` and `baseRequestOptions` will be merged accordignly.
*/
request: <TResponse>(
request: Request,
requestOptions: RequestOptions
baseRequest: Request,
methodOptions: {
headers: Headers;
queryParameters: QueryParameters;
},
baseRequestOptions?: RequestOptions
) => Promise<TResponse>;
};
11 changes: 5 additions & 6 deletions templates/javascript/api-all.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{{! This file will be renamed and moved to `builds/browser.ts` after generating the client }}

import type { Host, Requester } from '@experimental-api-clients-automation/client-common';
import type { InitClientOptions } from '@experimental-api-clients-automation/client-common';
import { createMemoryCache, createFallbackableCache, createBrowserLocalStorageCache } from '@experimental-api-clients-automation/client-common';
import { createXhrRequester } from '@experimental-api-clients-automation/requester-browser-xhr';

Expand All @@ -17,7 +16,7 @@ export * from '../src/{{apiName}}Api';
export function {{apiName}}Api(
appId: string,
apiKey: string,{{#hasRegionalHost}}region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region,{{/hasRegionalHost}}
options?: { requester?: Requester; hosts?: Host[] }
options?: InitClientOptions
): {{capitalizedApiName}}Api {
if (!appId) {
throw new Error("`appId` is missing.");
Expand Down Expand Up @@ -46,9 +45,9 @@ export function {{apiName}}Api(
requester: options?.requester ?? createXhrRequester(),
userAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
responsesCache: createMemoryCache(),
requestsCache: createMemoryCache({ serializable: false }),
hostsCache: createFallbackableCache({
responsesCache: options?.responsesCache ?? createMemoryCache(),
requestsCache: options?.requestsCache ?? createMemoryCache({ serializable: false }),
hostsCache: options?.hostsCache ?? createFallbackableCache({
caches: [
createBrowserLocalStorageCache({ key: `${apiClientVersion}-${appId}` }),
createMemoryCache(),
Expand Down
17 changes: 10 additions & 7 deletions templates/javascript/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
Headers,
Host,
Request,
RequestOptions,
QueryParameters,
} from '@experimental-api-clients-automation/client-common';

{{#imports}}
Expand Down Expand Up @@ -159,7 +161,7 @@ export function create{{capitalizedApiName}}Api(options: CreateClientOptions{{#h
{{#allParams}}
{{paramName}},
{{/allParams}}
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
{{/bodyParams.0}}
{{#bodyParams.0}}
{{^queryParams.0}}
Expand All @@ -170,7 +172,7 @@ export function create{{capitalizedApiName}}Api(options: CreateClientOptions{{#h
{{#allParams}}
{{paramName}},
{{/allParams}}
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
{{/bodyParams.1}}
{{/bodyParams.0.isArray}}
{{^bodyParams.0.isArray}}
Expand All @@ -184,27 +186,28 @@ export function create{{capitalizedApiName}}Api(options: CreateClientOptions{{#h
{{#allParams}}
{{paramName}},
{{/allParams}}
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
{{/pathParams.0}}
{{/queryParams.0}}
{{#queryParams.0}}
{
{{#allParams}}
{{paramName}},
{{/allParams}}
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
{{/queryParams.0}}
{{/bodyParams.0}}
{{/allParams.0}}
requestOptions?: RequestOptions
) : Promise<{{{returnType}}}> {
const requestPath = '{{{path}}}'{{#pathParams}}.replace(
{{=<% %>=}}
'{<%baseName%>}',
<%={{ }}=%>
encodeURIComponent(String({{paramName}}))
){{/pathParams}};
let headers: Headers = { Accept: 'application/json' };
let queryParameters: Record<string, string> = {};
const headers: Headers = {};
const queryParameters: QueryParameters = {};

{{#allParams}}
{{#required}}
Expand Down Expand Up @@ -243,7 +246,7 @@ export function create{{capitalizedApiName}}Api(options: CreateClientOptions{{#h
return transporter.request(request, {
queryParameters,
headers,
});
}, requestOptions);
}

{{/operation}}
Expand Down
11 changes: 5 additions & 6 deletions templates/javascript/api.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{{! This file will be renamed and moved to `builds/node.ts` after generating the client }}

import type { Host, Requester } from '@experimental-api-clients-automation/client-common';
import type { InitClientOptions } from '@experimental-api-clients-automation/client-common';
import { createMemoryCache, createNullCache } from '@experimental-api-clients-automation/client-common';
import { createHttpRequester } from '@experimental-api-clients-automation/requester-node-http';

Expand All @@ -16,7 +15,7 @@ export * from '../src/{{apiName}}Api';
export function {{apiName}}Api(
appId: string,
apiKey: string,{{#hasRegionalHost}}region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region,{{/hasRegionalHost}}
options?: { requester?: Requester; hosts?: Host[] }
options?: InitClientOptions
): {{capitalizedApiName}}Api {
if (!appId) {
throw new Error("`appId` is missing.");
Expand Down Expand Up @@ -44,9 +43,9 @@ export function {{apiName}}Api(
},
requester: options?.requester ?? createHttpRequester(),
userAgents: [{ segment: 'Node.js', version: process.versions.node }],
responsesCache: createNullCache(),
requestsCache: createNullCache(),
hostsCache: createMemoryCache(),
responsesCache: options?.responsesCache ?? createNullCache(),
requestsCache: options?.requestsCache ?? createNullCache(),
hostsCache: options?.hostsCache ?? createMemoryCache(),
...options,
});
}

0 comments on commit 09259f7

Please sign in to comment.