Skip to content

Commit 9847aff

Browse files
authored
fix(javascript): move node related helpers at node bundle level (#2807)
1 parent ec5eb6d commit 9847aff

18 files changed

+240
-214
lines changed

.github/.cache_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.21
1+
1.0.22

generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void processOpts() {
7979
// `lite` builds
8080
supportingFiles.add(new SupportingFile("client/builds/browser.mustache", "lite/builds", "browser.ts"));
8181
supportingFiles.add(new SupportingFile("client/builds/node.mustache", "lite/builds", "node.ts"));
82+
supportingFiles.add(new SupportingFile("client/builds/liteNode.mustache", "lite/builds", "node.ts"));
8283

8384
// `lite` models
8485
supportingFiles.add(new SupportingFile("client/model/clientMethodProps.mustache", "lite/model", "clientMethodProps.ts"));
@@ -146,6 +147,7 @@ private void setDefaultGeneratorOptions() {
146147
additionalProperties.put("isAlgoliasearchClient", isAlgoliasearchClient);
147148
additionalProperties.put("packageVersion", Helpers.getPackageJsonVersion(packageName));
148149
additionalProperties.put("packageName", packageName);
150+
additionalProperties.put("nodeSearchHelpers", CLIENT.equals("search") || isAlgoliasearchClient);
149151

150152
if (isAlgoliasearchClient) {
151153
// Files used to create the package.json of the algoliasearch package
Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
// {{{generationBanner}}}
22

3-
import {
4-
createMemoryCache,
5-
createFallbackableCache,
6-
createBrowserLocalStorageCache,
7-
DEFAULT_CONNECT_TIMEOUT_BROWSER,
8-
DEFAULT_READ_TIMEOUT_BROWSER,
9-
DEFAULT_WRITE_TIMEOUT_BROWSER,
10-
} from '{{{npmNamespace}}}/client-common';
11-
12-
import { createXhrRequester } from '{{{npmNamespace}}}/requester-browser-xhr';
13-
14-
{{> algoliasearch/builds/imports}}
15-
16-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
17-
export function algoliasearch(
18-
appId: string,
19-
apiKey: string,
20-
options?: ClientOptions
21-
) {
22-
{{> algoliasearch/builds/checkParameters}}
23-
3+
{{> algoliasearch/builds/definition}}
244
const commonOptions: CreateClientOptions = {
255
apiKey,
266
appId,
@@ -44,4 +24,17 @@ export function algoliasearch(
4424
};
4525

4626
{{> algoliasearch/builds/initClients}}
47-
}
27+
28+
return {
29+
...createSearchClient(commonOptions),
30+
/**
31+
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
32+
*/
33+
get _ua(): string {
34+
return this.transporter.algoliaAgent.value;
35+
},
36+
initAnalytics,
37+
initPersonalization,
38+
initAbtesting,
39+
};
40+
}

templates/javascript/clients/algoliasearch/builds/checkParameters.mustache

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type { PersonalizationClient } from '@algolia/client-personalization';
2+
import type { AnalyticsClient } from '@algolia/client-analytics';
3+
import type { AbtestingClient } from '@algolia/client-abtesting';
4+
5+
import type { Region as AnalyticsRegion } from '{{{npmNamespace}}}/client-analytics/src/analyticsClient';
6+
import { createAnalyticsClient, REGIONS as analyticsRegions } from '{{{npmNamespace}}}/client-analytics/src/analyticsClient';
7+
8+
import type { Region as AbtestingRegion } from '{{{npmNamespace}}}/client-abtesting/src/abtestingClient';
9+
import { createAbtestingClient, REGIONS as abtestingRegions } from '{{{npmNamespace}}}/client-abtesting/src/abtestingClient';
10+
11+
import type { Region as PersonalizationRegion } from '{{{npmNamespace}}}/client-personalization/src/personalizationClient';
12+
import { createPersonalizationClient, REGIONS as personalizationRegions } from '{{{npmNamespace}}}/client-personalization/src/personalizationClient';
13+
import { createSearchClient, apiClientVersion as searchClientVersion } from '{{{npmNamespace}}}/client-search/src/searchClient';
14+
15+
import type { serializeQueryParameters } from '{{{npmNamespace}}}/client-common';
16+
import type {} from '{{{npmNamespace}}}/client-common';
17+
import type { InitClientOptions, InitClientRegion, GenerateSecuredApiKeyOptions, GetSecuredApiKeyRemainingValidityOptions } from "./models"
18+
19+
{{#nodeSearchHelpers}}
20+
import {createHmac} from 'crypto';
21+
{{/nodeSearchHelpers}}
22+
23+
import {
24+
ClientOptions,
25+
CreateClientOptions,
26+
DEFAULT_CONNECT_TIMEOUT_BROWSER,
27+
DEFAULT_CONNECT_TIMEOUT_NODE,
28+
DEFAULT_READ_TIMEOUT_BROWSER,
29+
DEFAULT_READ_TIMEOUT_NODE,
30+
DEFAULT_WRITE_TIMEOUT_BROWSER,
31+
DEFAULT_WRITE_TIMEOUT_NODE,
32+
createBrowserLocalStorageCache,
33+
createFallbackableCache,
34+
createMemoryCache,
35+
createNullCache,
36+
} from '{{{npmNamespace}}}/client-common';
37+
38+
import { createHttpRequester } from '{{{npmNamespace}}}/requester-node-http';
39+
40+
import { createXhrRequester } from '{{{npmNamespace}}}/requester-browser-xhr';
41+
42+
export * from './models';
43+
44+
export const apiClientVersion = searchClientVersion;
45+
46+
/**
47+
* The client type.
48+
*/
49+
export type Algoliasearch = ReturnType<typeof algoliasearch>;
50+
51+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
52+
export function algoliasearch(
53+
appId: string,
54+
apiKey: string,
55+
options?: ClientOptions
56+
) {
57+
if (!appId || typeof appId !== 'string') {
58+
throw new Error('`appId` is missing.');
59+
}
60+
61+
if (!apiKey || typeof apiKey !== 'string') {
62+
throw new Error('`apiKey` is missing.');
63+
}

templates/javascript/clients/algoliasearch/builds/imports.mustache

Lines changed: 0 additions & 23 deletions
This file was deleted.

templates/javascript/clients/algoliasearch/builds/initClients.mustache

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,4 @@ function initPersonalization(initOptions: InitClientOptions & Required<InitClien
5050
...initOptions.options,
5151
...initOptions,
5252
});
53-
}
54-
55-
return {
56-
...createSearchClient(commonOptions),
57-
/**
58-
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
59-
*/
60-
get _ua(): string {
61-
return this.transporter.algoliaAgent.value;
62-
},
63-
initAnalytics,
64-
initPersonalization,
65-
initAbtesting,
66-
};
53+
}
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
// {{{generationBanner}}}
22

3-
import {
4-
DEFAULT_CONNECT_TIMEOUT_NODE,
5-
DEFAULT_READ_TIMEOUT_NODE,
6-
DEFAULT_WRITE_TIMEOUT_NODE,
7-
createMemoryCache,
8-
createNullCache,
9-
} from '{{{npmNamespace}}}/client-common';
10-
11-
import { createHttpRequester } from '{{{npmNamespace}}}/requester-node-http';
12-
13-
{{> algoliasearch/builds/imports}}
14-
15-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
16-
export function algoliasearch(
17-
appId: string,
18-
apiKey: string,
19-
options?: ClientOptions
20-
) {
21-
{{> algoliasearch/builds/checkParameters}}
22-
3+
{{> algoliasearch/builds/definition}}
234
const commonOptions: CreateClientOptions = {
245
apiKey,
256
appId,
@@ -37,4 +18,20 @@ export function algoliasearch(
3718
};
3819

3920
{{> algoliasearch/builds/initClients}}
40-
}
21+
22+
return {
23+
...createSearchClient(commonOptions),
24+
/**
25+
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
26+
*/
27+
get _ua(): string {
28+
return this.transporter.algoliaAgent.value;
29+
},
30+
initAnalytics,
31+
initPersonalization,
32+
initAbtesting,
33+
{{#nodeSearchHelpers}}
34+
{{> client/api/nodeHelpers}}
35+
{{/nodeSearchHelpers}}
36+
};
37+
}

templates/javascript/clients/api-single.mustache

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,4 @@ export function create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}({
153153
};
154154
}
155155

156-
/**
157-
* The client type.
158-
*/
159-
export type {{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}} = ReturnType<typeof create{{#lambda.titlecase}}{{apiName}}{{/lambda.titlecase}}>;
160-
161156
{{/operations}}

templates/javascript/clients/client/api/helpers.mustache

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -263,49 +263,6 @@ searchForFacets(
263263
return this.search(searchMethodParams, requestOptions) as Promise<{results: Array<SearchForFacetValuesResponse>}>;
264264
},
265265

266-
/**
267-
* Helper: Generates a secured API key based on the given `parentApiKey` and given `restrictions`.
268-
*
269-
* @summary Helper: Generates a secured API key based on the given `parentApiKey` and given `restrictions`.
270-
* @param generateSecuredApiKey - The `generateSecuredApiKey` object.
271-
* @param generateSecuredApiKey.parentApiKey - The base API key from which to generate the new secured one.
272-
* @param generateSecuredApiKey.restrictions - A set of properties defining the restrictions of the secured API key.
273-
*/
274-
generateSecuredApiKey({
275-
parentApiKey,
276-
restrictions = {},
277-
}: GenerateSecuredApiKeyOptions): string {
278-
const queryParameters = serializeQueryParameters(restrictions);
279-
return Buffer.from(
280-
createHmac('sha256', parentApiKey)
281-
.update(queryParameters)
282-
.digest('hex') + queryParameters
283-
).toString('base64');
284-
},
285-
286-
/**
287-
* Helper: Retrieves the remaining validity of the previous generated `secured_api_key`, the `ValidUntil` parameter must have been provided.
288-
*
289-
* @summary Helper: Retrieves the remaining validity of the previous generated `secured_api_key`, the `ValidUntil` parameter must have been provided.
290-
* @param getSecuredApiKeyRemainingValidity - The `getSecuredApiKeyRemainingValidity` object.
291-
* @param getSecuredApiKeyRemainingValidity.securedApiKey - The secured API key generated with the `generateSecuredApiKey` method.
292-
*/
293-
getSecuredApiKeyRemainingValidity({
294-
securedApiKey,
295-
}: GetSecuredApiKeyRemainingValidityOptions): number {
296-
const decodedString = Buffer.from(securedApiKey, 'base64').toString(
297-
'ascii'
298-
);
299-
const regex = /validUntil=(\d+)/;
300-
const match = decodedString.match(regex);
301-
302-
if (match === null) {
303-
throw new Error('validUntil not found in given secured api key.');
304-
}
305-
306-
return parseInt(match[1], 10) - Math.round(new Date().getTime() / 1000);
307-
},
308-
309266
/**
310267
* Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests.
311268
*

0 commit comments

Comments
 (0)