Skip to content

fix(javascript): use transporter directly #3682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ export function algoliasearch(appId: string, apiKey: string, options?: ClientOpt
throw new Error('`apiKey` is missing.');
}

const client = searchClient(appId, apiKey, options);

return {
...searchClient(appId, apiKey, options),
...client,
/**
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
*/
get _ua(): string {
return this.transporter.algoliaAgent.value;
return client.transporter.algoliaAgent.value;
},
initRecommend: (initOptions: InitClientOptions = {}): RecommendClient => {
return recommendClient(initOptions.appId || appId, initOptions.apiKey || apiKey, initOptions.options);
Expand Down
51 changes: 26 additions & 25 deletions templates/javascript/clients/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,27 @@ export function create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}({
...options
}: CreateClientOptions{{#hasRegionalHost}} & {region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region }{{/hasRegionalHost}}) {
const auth = createAuth(appIdOption, apiKeyOption, authMode);
const transporter = createTransporter({
hosts: getDefaultHosts({{#hostWithAppID}}appIdOption{{/hostWithAppID}}{{#hasRegionalHost}}regionOption{{/hasRegionalHost}}),
...options,
algoliaAgent: getAlgoliaAgent({
algoliaAgents,
client: '{{{algoliaAgent}}}',
version: apiClientVersion,
}),
baseHeaders: {
'content-type': 'text/plain',
...auth.headers(),
...options.baseHeaders,
},
baseQueryParameters: {
...auth.queryParameters(),
...options.baseQueryParameters,
},
});

return {
transporter: createTransporter({
hosts: getDefaultHosts({{#hostWithAppID}}appIdOption{{/hostWithAppID}}{{#hasRegionalHost}}regionOption{{/hasRegionalHost}}),
...options,
algoliaAgent: getAlgoliaAgent({
algoliaAgents,
client: '{{{algoliaAgent}}}',
version: apiClientVersion,
}),
baseHeaders: {
'content-type': 'text/plain',
...auth.headers(),
...options.baseHeaders,
},
baseQueryParameters: {
...auth.queryParameters(),
...options.baseQueryParameters,
},
}),
transporter,

/**
* The `appId` currently in use.
Expand All @@ -52,16 +53,16 @@ export function create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}({
*/
clearCache(): Promise<void> {
return Promise.all([
this.transporter.requestsCache.clear(),
this.transporter.responsesCache.clear(),
transporter.requestsCache.clear(),
transporter.responsesCache.clear(),
]).then(() => undefined);
},

/**
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
*/
get _ua(): string {
return this.transporter.algoliaAgent.value;
return transporter.algoliaAgent.value;
},

/**
Expand All @@ -71,7 +72,7 @@ export function create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}({
* @param version - The version of the agent.
*/
addAlgoliaAgent(segment: string, version?: string): void {
this.transporter.algoliaAgent.add({ segment, version });
transporter.algoliaAgent.add({ segment, version });
},

/**
Expand All @@ -82,9 +83,9 @@ export function create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}({
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
if (!authMode || authMode === 'WithinHeaders') {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
transporter.baseHeaders['x-algolia-api-key'] = apiKey;
} else {
this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
}
},

Expand Down Expand Up @@ -156,7 +157,7 @@ export function create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}({
{{/vendorExtensions}}
};

return this.transporter.request(request, requestOptions);
return transporter.request(request, requestOptions);
},

{{/operation}}
Expand Down
Loading