Skip to content

Commit 9f99f02

Browse files
authored
feat(javascript): allow overriding all transporter options (#615)
1 parent e8789be commit 9f99f02

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed
Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
1-
import type { Cache } from './Cache';
2-
import type { Host } from './Host';
3-
import type { Requester } from './Requester';
4-
import type {
5-
Timeouts,
6-
AlgoliaAgentOptions,
7-
TransporterOptions,
8-
} from './Transporter';
1+
import type { AlgoliaAgentOptions, TransporterOptions } from './Transporter';
92

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

12-
export type CreateClientOptions = Pick<
5+
type OverriddenTransporterOptions =
6+
| 'baseHeaders'
7+
| 'baseQueryParameters'
8+
| 'hosts';
9+
10+
export type CreateClientOptions = Omit<
1311
TransporterOptions,
14-
'hostsCache' | 'requestsCache' | 'responsesCache'
15-
> & {
16-
appId: string;
17-
apiKey: string;
18-
requester: Requester;
19-
timeouts: Timeouts;
20-
algoliaAgents: AlgoliaAgentOptions[];
21-
hosts?: Host[];
22-
authMode?: AuthMode;
23-
};
12+
OverriddenTransporterOptions | 'algoliaAgent'
13+
> &
14+
Partial<Pick<TransporterOptions, OverriddenTransporterOptions>> & {
15+
appId: string;
16+
apiKey: string;
17+
authMode?: AuthMode;
18+
algoliaAgents: AlgoliaAgentOptions[];
19+
};
2420

25-
export type InitClientOptions = Partial<{
26-
requester: Requester;
27-
hosts: Host[];
28-
responsesCache: Cache;
29-
requestsCache: Cache;
30-
hostsCache: Cache;
31-
}>;
21+
export type InitClientOptions = Partial<
22+
Omit<CreateClientOptions, 'apiKey' | 'appId'>
23+
>;

templates/javascript/api-single.mustache

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,31 @@ function getDefaultHosts(region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}
8383
{{/hasRegionalHost}}
8484

8585
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
86-
export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasRegionalHost}} & {region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region }{{/hasRegionalHost}}) {
87-
const auth = createAuth(options.appId, options.apiKey, options.authMode);
86+
export function create{{capitalizedApiName}}({
87+
appId: appIdOption,
88+
apiKey: apiKeyOption,
89+
authMode,
90+
algoliaAgents,{{#hasRegionalHost}} region: regionOption,{{/hasRegionalHost}}
91+
...options
92+
}: CreateClientOptions{{#hasRegionalHost}} & {region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region }{{/hasRegionalHost}}) {
93+
const auth = createAuth(appIdOption, apiKeyOption, authMode);
8894
const transporter = createTransporter({
89-
hosts: options?.hosts ?? getDefaultHosts({{^hasRegionalHost}}options.appId{{/hasRegionalHost}}{{#hasRegionalHost}}options.region{{/hasRegionalHost}}),
90-
hostsCache: options.hostsCache,
91-
requestsCache: options.requestsCache,
92-
responsesCache: options.responsesCache,
93-
baseHeaders: {
94-
'content-type': 'text/plain',
95-
...auth.headers(),
96-
},
97-
baseQueryParameters: auth.queryParameters(),
95+
hosts: getDefaultHosts({{^hasRegionalHost}}appIdOption{{/hasRegionalHost}}{{#hasRegionalHost}}regionOption{{/hasRegionalHost}}),
96+
...options,
9897
algoliaAgent: getAlgoliaAgent({
99-
algoliaAgents: options.algoliaAgents,
98+
algoliaAgents: algoliaAgents,
10099
client: '{{{algoliaAgent}}}',
101100
version: apiClientVersion,
102101
}),
103-
timeouts: options.timeouts,
104-
requester: options.requester,
102+
baseHeaders: {
103+
'content-type': 'text/plain',
104+
...auth.headers(),
105+
...options.baseHeaders,
106+
},
107+
baseQueryParameters: {
108+
...auth.queryParameters(),
109+
...options.baseQueryParameters,
110+
},
105111
});
106112

107113
function addAlgoliaAgent(segment: string, version?: string): void {

templates/javascript/browser.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ export function {{apiName}}(
4040
read: 2,
4141
write: 30,
4242
},
43-
requester: options?.requester ?? createXhrRequester(),
43+
requester: createXhrRequester(),
4444
algoliaAgents: [{ segment: 'Browser' }],
4545
authMode: 'WithinQueryParameters',
46-
responsesCache: options?.responsesCache ?? createMemoryCache(),
47-
requestsCache: options?.requestsCache ?? createMemoryCache({ serializable: false }),
48-
hostsCache: options?.hostsCache ?? createFallbackableCache({
46+
responsesCache: createMemoryCache(),
47+
requestsCache: createMemoryCache({ serializable: false }),
48+
hostsCache: createFallbackableCache({
4949
caches: [
5050
createBrowserLocalStorageCache({ key: `${apiClientVersion}-${appId}` }),
5151
createMemoryCache(),

templates/javascript/node.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ export function {{apiName}}(
4040
read: 5,
4141
write: 30,
4242
},
43-
requester: options?.requester ?? createHttpRequester(),
43+
requester: createHttpRequester(),
4444
algoliaAgents: [{ segment: 'Node.js', version: process.versions.node }],
45-
responsesCache: options?.responsesCache ?? createNullCache(),
46-
requestsCache: options?.requestsCache ?? createNullCache(),
47-
hostsCache: options?.hostsCache ?? createMemoryCache(),
45+
responsesCache: createNullCache(),
46+
requestsCache: createNullCache(),
47+
hostsCache: createMemoryCache(),
4848
...options,
4949
});
5050
}

0 commit comments

Comments
 (0)