Skip to content

Commit 472502b

Browse files
authored
fix(templates): simplify JavaScript mustache templates (#666)
1 parent a1f7642 commit 472502b

9 files changed

+185
-203
lines changed

templates/javascript/api-single.mustache

+22-131
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,9 @@
1-
import {
2-
createAuth,
3-
createTransporter,
4-
getAlgoliaAgent,
5-
shuffle,
6-
{{#isSearchClient}}
7-
createRetryablePromise,
8-
{{/isSearchClient}}
9-
} from '@experimental-api-clients-automation/client-common';
10-
import type {
11-
CreateClientOptions,
12-
Headers,
13-
Host,
14-
Request,
15-
RequestOptions,
16-
QueryParameters,
17-
{{#isSearchClient}}
18-
CreateRetryablePromiseOptions,
19-
{{/isSearchClient}}
20-
} from '@experimental-api-clients-automation/client-common';
21-
22-
{{#imports}}
23-
import { {{classname}} } from '{{filename}}';
24-
{{/imports}}
25-
26-
{{#operations}}
27-
import type {
28-
{{#operation}}
29-
{{#vendorExtensions.x-create-wrapping-object}}
30-
{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
31-
{{/vendorExtensions.x-create-wrapping-object}}
32-
{{/operation}}
33-
} from '../model/clientMethodProps';
34-
{{/operations}}
1+
{{> api/imports}}
352

363
export const apiClientVersion = '{{packageVersion}}';
374

385
{{#operations}}
39-
{{#hasRegionalHost}}
40-
export const REGIONS = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}] as const;
41-
export type Region = typeof REGIONS[number];
42-
{{/hasRegionalHost}}
43-
44-
{{^hasRegionalHost}}
45-
function getDefaultHosts(appId: string): Host[] {
46-
return (
47-
[
48-
{
49-
url: `${appId}-dsn.algolia.net`,
50-
accept: 'read',
51-
protocol: 'https',
52-
},
53-
{
54-
url: `${appId}.algolia.net`,
55-
accept: 'write',
56-
protocol: 'https',
57-
},
58-
] as Host[]
59-
).concat(
60-
shuffle([
61-
{
62-
url: `${appId}-1.algolianet.com`,
63-
accept: 'readWrite',
64-
protocol: 'https',
65-
},
66-
{
67-
url: `${appId}-2.algolianet.com`,
68-
accept: 'readWrite',
69-
protocol: 'https',
70-
},
71-
{
72-
url: `${appId}-3.algolianet.com`,
73-
accept: 'readWrite',
74-
protocol: 'https',
75-
},
76-
])
77-
);
78-
}
79-
{{/hasRegionalHost}}
80-
81-
{{#hasRegionalHost}}
82-
function getDefaultHosts(region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region): Host[] {
83-
const url = {{#fallbackToAliasHost}}!region ? '{{{hostWithFallback}}}' : {{/fallbackToAliasHost}} '{{{host}}}'.replace('{region}', region);
84-
85-
return [{ url, accept: 'readWrite', protocol: 'https' }];
86-
}
87-
{{/hasRegionalHost}}
6+
{{> api/hosts}}
887

898
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
909
export function create{{capitalizedApiName}}({
@@ -147,45 +66,8 @@ export function create{{capitalizedApiName}}({
14766
},
14867
{{/isSearchClient}}
14968
{{#operation}}
150-
/**
151-
{{#notes}}
152-
* {{&notes}}
153-
{{/notes}}
154-
{{#summary}}
155-
* @summary {{&summary}}
156-
{{/summary}}
157-
{{#vendorExtensions}}
158-
{{#x-create-wrapping-object}}
159-
* @param {{nickname}} - The {{nickname}} object.
160-
{{#allParams}}
161-
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
162-
{{/allParams}}
163-
{{/x-create-wrapping-object}}
164-
{{#x-is-single-body-param}}
165-
{{#bodyParams}}
166-
* @param {{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
167-
{{/bodyParams}}
168-
{{/x-is-single-body-param}}
169-
{{/vendorExtensions}}
170-
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
171-
*/
172-
{{nickname}}(
173-
{{#vendorExtensions}}
174-
{{#x-create-wrapping-object}}
175-
{
176-
{{#allParams}}
177-
{{paramName}},
178-
{{/allParams}}
179-
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
180-
{{/x-create-wrapping-object}}
181-
{{#x-is-single-body-param}}
182-
{{#bodyParams}}
183-
{{paramName}}: {{{dataType}}},
184-
{{/bodyParams}}
185-
{{/x-is-single-body-param}}
186-
{{/vendorExtensions}}
187-
requestOptions?: RequestOptions
188-
) : Promise<{{{returnType}}}> {
69+
{{> api/operation/jsdoc}}
70+
{{nickname}}( {{> api/operation/parameters}} ) : Promise<{{{returnType}}}> {
18971
{{#allParams}}
19072
{{#required}}
19173
if ({{#isBoolean}}{{paramName}} === null || {{paramName}} === undefined{{/isBoolean}}{{^isBoolean}}!{{paramName}}{{/isBoolean}}) {
@@ -203,9 +85,16 @@ export function create{{capitalizedApiName}}({
20385
{{/required}}
20486
{{/allParams}}
20587

206-
const requestPath = '{{{path}}}'{{#vendorExtensions}}{{#pathParams}}.replace(
207-
{{=<% %>=}}'{<%baseName%>}'<%={{ }}=%>,{{#x-is-custom-request}}{{paramName}}{{/x-is-custom-request}}{{^x-is-custom-request}}encodeURIComponent({{paramName}}){{/x-is-custom-request}}
208-
){{/pathParams}}{{/vendorExtensions}};
88+
{{#vendorExtensions}}
89+
const requestPath = '{{{path}}}'{{#pathParams}}.replace({{=<% %>=}}'{<%baseName%>}'<%={{ }}=%>,
90+
{{#x-is-custom-request}}
91+
{{paramName}}
92+
{{/x-is-custom-request}}
93+
{{^x-is-custom-request}}
94+
encodeURIComponent({{paramName}})
95+
{{/x-is-custom-request}}
96+
){{/pathParams}};
97+
{{/vendorExtensions}}
20998
const headers: Headers = {};
21099
const queryParameters: QueryParameters = {{#vendorExtensions.x-is-custom-request}}parameters || {{/vendorExtensions.x-is-custom-request}}{};
211100

@@ -230,12 +119,14 @@ export function create{{capitalizedApiName}}({
230119
{{#bodyParam}}
231120
data: {{paramName}},
232121
{{/bodyParam}}
233-
{{#vendorExtensions.x-use-read-transporter}}
234-
useReadTransporter: true,
235-
{{/vendorExtensions.x-use-read-transporter}}
236-
{{#vendorExtensions.x-cacheable}}
237-
cacheable: true,
238-
{{/vendorExtensions.x-cacheable}}
122+
{{#vendorExtensions}}
123+
{{#x-use-read-transporter}}
124+
useReadTransporter: true,
125+
{{/x-use-read-transporter}}
126+
{{#x-cacheable}}
127+
cacheable: true,
128+
{{/x-cacheable}}
129+
{{/vendorExtensions}}
239130
};
240131

241132
return transporter.request(request, requestOptions);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
if (!appId || typeof appId !== 'string') {
2+
throw new Error("`appId` is missing.");
3+
}
4+
5+
if (!apiKey || typeof apiKey !== 'string') {
6+
throw new Error("`apiKey` is missing.");
7+
}
8+
9+
{{#hasRegionalHost}}
10+
{{^fallbackToAliasHost}}
11+
if (!region) {
12+
throw new Error("`region` is missing.");
13+
}
14+
{{/fallbackToAliasHost}}
15+
16+
if (region && (typeof region !== 'string' || !REGIONS.includes(region))) {
17+
throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`);
18+
}
19+
{{/hasRegionalHost}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { InitClientOptions } from '@experimental-api-clients-automation/client-common';
2+
import { createMemoryCache, createFallbackableCache, createBrowserLocalStorageCache, createNullCache } from '@experimental-api-clients-automation/client-common';
3+
4+
import { create{{capitalizedApiName}}, apiClientVersion } from '../src/{{apiName}}';
5+
import type { {{capitalizedApiName}} } from '../src/{{apiName}}';
6+
7+
{{#hasRegionalHost}}
8+
import { Region, REGIONS } from '../src/{{apiName}}';
9+
{{/hasRegionalHost}}
10+
11+
{{! We don't use `export *` to prevent exposing the factory, to avoid confusion for the user }}
12+
export {
13+
apiClientVersion,
14+
{{capitalizedApiName}},
15+
} from '../src/{{apiName}}';
16+
export * from '../model';
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{{#hasRegionalHost}}
2+
export const REGIONS = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}] as const;
3+
export type Region = typeof REGIONS[number];
4+
{{/hasRegionalHost}}
5+
6+
{{^hasRegionalHost}}
7+
function getDefaultHosts(appId: string): Host[] {
8+
return (
9+
[
10+
{
11+
url: `${appId}-dsn.algolia.net`,
12+
accept: 'read',
13+
protocol: 'https',
14+
},
15+
{
16+
url: `${appId}.algolia.net`,
17+
accept: 'write',
18+
protocol: 'https',
19+
},
20+
] as Host[]
21+
).concat(
22+
shuffle([
23+
{
24+
url: `${appId}-1.algolianet.com`,
25+
accept: 'readWrite',
26+
protocol: 'https',
27+
},
28+
{
29+
url: `${appId}-2.algolianet.com`,
30+
accept: 'readWrite',
31+
protocol: 'https',
32+
},
33+
{
34+
url: `${appId}-3.algolianet.com`,
35+
accept: 'readWrite',
36+
protocol: 'https',
37+
},
38+
])
39+
);
40+
}
41+
{{/hasRegionalHost}}
42+
43+
{{#hasRegionalHost}}
44+
function getDefaultHosts(region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region): Host[] {
45+
const url = {{#fallbackToAliasHost}}!region ? '{{{hostWithFallback}}}' : {{/fallbackToAliasHost}} '{{{host}}}'.replace('{region}', region);
46+
47+
return [{ url, accept: 'readWrite', protocol: 'https' }];
48+
}
49+
{{/hasRegionalHost}}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
createAuth,
3+
createTransporter,
4+
getAlgoliaAgent,
5+
shuffle,
6+
{{#isSearchClient}}
7+
createRetryablePromise,
8+
{{/isSearchClient}}
9+
} from '@experimental-api-clients-automation/client-common';
10+
import type {
11+
CreateClientOptions,
12+
Headers,
13+
Host,
14+
Request,
15+
RequestOptions,
16+
QueryParameters,
17+
{{#isSearchClient}}
18+
CreateRetryablePromiseOptions,
19+
{{/isSearchClient}}
20+
} from '@experimental-api-clients-automation/client-common';
21+
22+
{{#imports}}
23+
import { {{classname}} } from '{{filename}}';
24+
{{/imports}}
25+
26+
{{#operations}}
27+
import type {
28+
{{#operation}}
29+
{{#vendorExtensions}}
30+
{{#x-create-wrapping-object}}
31+
{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
32+
{{/x-create-wrapping-object}}
33+
{{/vendorExtensions}}
34+
{{/operation}}
35+
} from '../model/clientMethodProps';
36+
{{/operations}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
{{#notes}}
3+
* {{&notes}}
4+
{{/notes}}
5+
{{#summary}}
6+
* @summary {{&summary}}
7+
{{/summary}}
8+
{{#vendorExtensions}}
9+
{{#x-create-wrapping-object}}
10+
* @param {{nickname}} - The {{nickname}} object.
11+
{{#allParams}}
12+
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
13+
{{/allParams}}
14+
{{/x-create-wrapping-object}}
15+
{{#x-is-single-body-param}}
16+
{{#bodyParams}}
17+
* @param {{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
18+
{{/bodyParams}}
19+
{{/x-is-single-body-param}}
20+
{{/vendorExtensions}}
21+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
22+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{{#vendorExtensions}}
2+
{{#x-create-wrapping-object}}
3+
{
4+
{{#allParams}}
5+
{{paramName}},
6+
{{/allParams}}
7+
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
8+
{{/x-create-wrapping-object}}
9+
{{#x-is-single-body-param}}
10+
{{#bodyParams}}
11+
{{paramName}}: {{{dataType}}},
12+
{{/bodyParams}}
13+
{{/x-is-single-body-param}}
14+
{{/vendorExtensions}}
15+
requestOptions?: RequestOptions

templates/javascript/browser.mustache

+3-36
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,13 @@
1-
import type { InitClientOptions } from '@experimental-api-clients-automation/client-common';
2-
import { DEFAULT_CONNECT_TIMEOUT_BROWSER, DEFAULT_READ_TIMEOUT_BROWSER, DEFAULT_WRITE_TIMEOUT_BROWSER } from '@experimental-api-clients-automation/client-common';
3-
import { createMemoryCache, createFallbackableCache, createBrowserLocalStorageCache } from '@experimental-api-clients-automation/client-common';
41
import { createXhrRequester } from '@experimental-api-clients-automation/requester-browser-xhr';
5-
6-
import { create{{capitalizedApiName}}, apiClientVersion } from '../src/{{apiName}}';
7-
import type { {{capitalizedApiName}} } from '../src/{{apiName}}';
8-
9-
{{#hasRegionalHost}}
10-
import { Region, REGIONS } from '../src/{{apiName}}';
11-
{{/hasRegionalHost}}
12-
13-
{{! We don't use `export *` to prevent exposing the factory, to avoid confusion for the user }}
14-
export {
15-
apiClientVersion,
16-
{{capitalizedApiName}},
17-
} from '../src/{{apiName}}';
18-
export * from '../model';
2+
import { DEFAULT_CONNECT_TIMEOUT_BROWSER, DEFAULT_READ_TIMEOUT_BROWSER, DEFAULT_WRITE_TIMEOUT_BROWSER } from '@experimental-api-clients-automation/client-common';
3+
{{> api/builds/imports}}
194

205
export function {{apiName}}(
216
appId: string,
227
apiKey: string,{{#hasRegionalHost}}region{{#fallbackToAliasHost}}?{{/fallbackToAliasHost}}: Region,{{/hasRegionalHost}}
238
options?: InitClientOptions
249
): {{capitalizedApiName}} {
25-
if (!appId || typeof appId !== 'string') {
26-
throw new Error("`appId` is missing.");
27-
}
28-
29-
if (!apiKey || typeof apiKey !== 'string') {
30-
throw new Error("`apiKey` is missing.");
31-
}
32-
33-
{{#hasRegionalHost}}
34-
{{^fallbackToAliasHost}}
35-
if (!region) {
36-
throw new Error("`region` is missing.");
37-
}
38-
{{/fallbackToAliasHost}}
39-
40-
if (region && (typeof region !== 'string' || !REGIONS.includes(region))) {
41-
throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`);
42-
}
43-
{{/hasRegionalHost}}
10+
{{> api/builds/checkParameters}}
4411

4512
return create{{capitalizedApiName}}({
4613
appId,

0 commit comments

Comments
 (0)