Skip to content

Commit

Permalink
feat(javascript): allow overriding all transporter options
Browse files Browse the repository at this point in the history
In v4 these are all allowed, and this allows users to work around possible issues we have smoothly, see eg. algolia/react-instantsearch#3487

At the same time I also simplified the passing of options in node/browser, they were double checking a variable that later overrides the original with the spread anyway.

Note to self: #614 touches a similar part of the code, whichever merges last will have to deal with a conflict
  • Loading branch information
Haroenv committed May 31, 2022
1 parent f2aa343 commit 90dbaf8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
import type { Cache } from './Cache';
import type { Host } from './Host';
import type { Requester } from './Requester';
import type {
Timeouts,
AlgoliaAgentOptions,
TransporterOptions,
} from './Transporter';
import type { AlgoliaAgentOptions, TransporterOptions } from './Transporter';

export type AuthMode = 'WithinHeaders' | 'WithinQueryParameters';

export type CreateClientOptions = Pick<
type OverriddenTransporterOptions =
| 'baseHeaders'
| 'baseQueryParameters'
| 'hosts';

export type CreateClientOptions = Omit<
TransporterOptions,
'hostsCache' | 'requestsCache' | 'responsesCache'
> & {
appId: string;
apiKey: string;
requester: Requester;
timeouts: Timeouts;
algoliaAgents: AlgoliaAgentOptions[];
hosts?: Host[];
authMode?: AuthMode;
};
OverriddenTransporterOptions | 'algoliaAgent'
> &
Partial<Pick<TransporterOptions, OverriddenTransporterOptions>> & {
appId: string;
apiKey: string;
authMode?: AuthMode;
algoliaAgents: AlgoliaAgentOptions[];
};

export type InitClientOptions = Partial<{
requester: Requester;
hosts: Host[];
responsesCache: Cache;
requestsCache: Cache;
hostsCache: Cache;
}>;
export type InitClientOptions = Partial<
Omit<CreateClientOptions, 'apiKey' | 'appId'>
>;
34 changes: 20 additions & 14 deletions templates/javascript/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,31 @@ function getDefaultHosts(region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}
{{/hasRegionalHost}}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasRegionalHost}} & {region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region }{{/hasRegionalHost}}) {
const auth = createAuth(options.appId, options.apiKey, options.authMode);
export function create{{capitalizedApiName}}({
appId,
apiKey,
authMode,
algoliaAgents,{{#hasRegionalHost}} region,{{/hasRegionalHost}}
...options
}: CreateClientOptions{{#hasRegionalHost}} & {region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region }{{/hasRegionalHost}}) {
const auth = createAuth(appId, apiKey, authMode);
const transporter = createTransporter({
hosts: options?.hosts ?? getDefaultHosts({{^hasRegionalHost}}options.appId{{/hasRegionalHost}}{{#hasRegionalHost}}options.region{{/hasRegionalHost}}),
hostsCache: options.hostsCache,
requestsCache: options.requestsCache,
responsesCache: options.responsesCache,
baseHeaders: {
'content-type': 'application/x-www-form-urlencoded',
...auth.headers(),
},
baseQueryParameters: auth.queryParameters(),
hosts: getDefaultHosts({{^hasRegionalHost}}appId{{/hasRegionalHost}}{{#hasRegionalHost}}region{{/hasRegionalHost}}),
...options,
algoliaAgent: getAlgoliaAgent({
algoliaAgents: options.algoliaAgents,
algoliaAgents: algoliaAgents,
client: '{{{algoliaAgent}}}',
version: apiClientVersion,
}),
timeouts: options.timeouts,
requester: options.requester,
baseHeaders: {
'content-type': 'application/x-www-form-urlencoded',
...auth.headers(),
...options.baseHeaders,
},
baseQueryParameters: {
...auth.queryParameters(),
...options.baseQueryParameters,
},
});

function addAlgoliaAgent(segment: string, version?: string): void {
Expand Down
8 changes: 4 additions & 4 deletions templates/javascript/browser.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export function {{apiName}}(
read: 2,
write: 30,
},
requester: options?.requester ?? createXhrRequester(),
requester: createXhrRequester(),
algoliaAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
responsesCache: options?.responsesCache ?? createMemoryCache(),
requestsCache: options?.requestsCache ?? createMemoryCache({ serializable: false }),
hostsCache: options?.hostsCache ?? createFallbackableCache({
responsesCache: createMemoryCache(),
requestsCache: createMemoryCache({ serializable: false }),
hostsCache: createFallbackableCache({
caches: [
createBrowserLocalStorageCache({ key: `${apiClientVersion}-${appId}` }),
createMemoryCache(),
Expand Down
8 changes: 4 additions & 4 deletions templates/javascript/node.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export function {{apiName}}(
read: 5,
write: 30,
},
requester: options?.requester ?? createHttpRequester(),
requester: createHttpRequester(),
algoliaAgents: [{ segment: 'Node.js', version: process.versions.node }],
responsesCache: options?.responsesCache ?? createNullCache(),
requestsCache: options?.requestsCache ?? createNullCache(),
hostsCache: options?.hostsCache ?? createMemoryCache(),
responsesCache: createNullCache(),
requestsCache: createNullCache(),
hostsCache: createMemoryCache(),
...options,
});
}

0 comments on commit 90dbaf8

Please sign in to comment.