Skip to content

Commit faba721

Browse files
authored
fix(javascript): remove duplicate echoRequester logic (#650)
1 parent a0eabf2 commit faba721

File tree

9 files changed

+52
-80
lines changed

9 files changed

+52
-80
lines changed
Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
import type { EchoResponse, EndRequest, Request, Response } from './types';
22

3-
export type UrlParams = {
4-
host: string;
5-
algoliaAgent: string;
6-
searchParams: EchoResponse['searchParams'];
7-
};
8-
93
export type EchoRequesterParams = {
10-
getUrlParams: (url: string) => UrlParams;
4+
getURL: (url: string) => URL;
115
status?: number;
126
};
137

8+
function getUrlParams({
9+
host,
10+
searchParams: urlSearchParams,
11+
}: URL): Pick<EchoResponse, 'algoliaAgent' | 'host' | 'searchParams'> {
12+
const algoliaAgent = urlSearchParams.get('x-algolia-agent') || '';
13+
const searchParams = {};
14+
15+
for (const [k, v] of urlSearchParams) {
16+
if (k === 'x-algolia-agent') {
17+
continue;
18+
}
19+
20+
searchParams[k] = v;
21+
}
22+
23+
return {
24+
host,
25+
algoliaAgent,
26+
searchParams:
27+
Object.entries(searchParams).length === 0 ? undefined : searchParams,
28+
};
29+
}
30+
1431
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
1532
export function createEchoRequester({
16-
getUrlParams,
33+
getURL,
1734
status = 200,
1835
}: EchoRequesterParams) {
1936
function send(
2037
{ headers, url, connectTimeout, responseTimeout }: EndRequest,
2138
{ data, ...originalRequest }: Request
2239
): Promise<Response> {
23-
const { host, searchParams, algoliaAgent } = getUrlParams(url);
40+
const { host, searchParams, algoliaAgent } = getUrlParams(getURL(url));
2441
const originalData =
2542
data && Object.entries(data).length > 0 ? data : undefined;
2643

@@ -31,7 +48,7 @@ export function createEchoRequester({
3148
headers,
3249
connectTimeout,
3350
responseTimeout,
34-
algoliaAgent: algoliaAgent ? encodeURI(algoliaAgent) : undefined,
51+
algoliaAgent: encodeURI(algoliaAgent),
3552
searchParams,
3653
data: originalData,
3754
}),
@@ -42,3 +59,5 @@ export function createEchoRequester({
4259

4360
return { send };
4461
}
62+
63+
export type EchoRequester = ReturnType<typeof createEchoRequester>;

clients/algoliasearch-client-javascript/packages/client-common/src/types/Requester.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ export type EchoResponse = Request & {
4747
host: string;
4848
headers: Headers;
4949
responseTimeout: number;
50+
algoliaAgent: string;
5051
searchParams?: Record<string, string>;
51-
algoliaAgent?: string;
5252
};
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
11
import { createEchoRequester } from '@experimental-api-clients-automation/client-common';
2-
import type { UrlParams } from '@experimental-api-clients-automation/client-common';
2+
import type { EchoRequester } from '@experimental-api-clients-automation/client-common';
33

4-
function getUrlParams(url: string): UrlParams {
5-
const { host, searchParams: urlSearchParams } = new URL(url);
6-
const algoliaAgent = urlSearchParams.get('x-algolia-agent') || '';
7-
const searchParams = {};
8-
9-
for (const [k, v] of urlSearchParams) {
10-
if (k === 'x-algolia-agent') {
11-
continue;
12-
}
13-
14-
searchParams[k] = v;
15-
}
16-
17-
return {
18-
host,
19-
algoliaAgent,
20-
searchParams:
21-
Object.entries(searchParams).length === 0 ? undefined : searchParams,
22-
};
23-
}
24-
25-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
26-
export function echoRequester(status: number = 200) {
27-
return createEchoRequester({ getUrlParams, status });
4+
export function echoRequester(status: number = 200): EchoRequester {
5+
return createEchoRequester({ getURL: (url: string) => new URL(url), status });
286
}
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,8 @@
11
import { URL } from 'url';
22

33
import { createEchoRequester } from '@experimental-api-clients-automation/client-common';
4-
import type { UrlParams } from '@experimental-api-clients-automation/client-common';
4+
import type { EchoRequester } from '@experimental-api-clients-automation/client-common';
55

6-
function getUrlParams(url: string): UrlParams {
7-
const { host, searchParams: urlSearchParams } = new URL(url);
8-
const algoliaAgent = urlSearchParams.get('x-algolia-agent') || '';
9-
const searchParams = {};
10-
11-
for (const [k, v] of urlSearchParams) {
12-
if (k === 'x-algolia-agent') {
13-
continue;
14-
}
15-
16-
searchParams[k] = v;
17-
}
18-
19-
return {
20-
host,
21-
algoliaAgent,
22-
searchParams:
23-
Object.entries(searchParams).length === 0 ? undefined : searchParams,
24-
};
25-
}
26-
27-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
28-
export function echoRequester(status: number = 200) {
29-
return createEchoRequester({ getUrlParams, status });
6+
export function echoRequester(status: number = 200): EchoRequester {
7+
return createEchoRequester({ getURL: (url: string) => new URL(url), status });
308
}

clients/algoliasearch-client-javascript/rollup.config.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const client = process.env.CLIENT?.replace(
1414
''
1515
);
1616
const UTILS = ['client-common', 'requester-browser-xhr', 'requester-node-http'];
17-
17+
const BROWSER_FORMATS = ['umd-browser', 'esm-browser', 'cjs-browser'];
18+
const NODE_FORMATS = ['cjs-node', 'esm-node'];
1819
const CLIENT_ALL = 'all';
1920
const CLIENT_UTILS = 'utils';
2021

@@ -61,9 +62,9 @@ function getAvailableClients() {
6162
function getUtilConfigs() {
6263
const commonOptions = {
6364
input: 'index.ts',
64-
formats: ['cjs-node', 'esm-node'],
65+
formats: NODE_FORMATS,
6566
external: [],
66-
dependencies: [],
67+
dependencies: ['@experimental-api-clients-automation/client-common'],
6768
};
6869

6970
return [
@@ -73,6 +74,7 @@ function getUtilConfigs() {
7374
output: 'client-common',
7475
package: 'client-common',
7576
name: '@experimental-api-clients-automation/client-common',
77+
dependencies: [],
7678
},
7779
// Browser requester
7880
{
@@ -81,7 +83,6 @@ function getUtilConfigs() {
8183
package: 'requester-browser-xhr',
8284
name: '@experimental-api-clients-automation/requester-browser-xhr',
8385
external: ['dom'],
84-
dependencies: ['@experimental-api-clients-automation/client-common'],
8586
},
8687
// Node requester
8788
{
@@ -90,7 +91,6 @@ function getUtilConfigs() {
9091
package: 'requester-node-http',
9192
name: '@experimental-api-clients-automation/requester-node-http',
9293
external: ['https', 'http', 'url'],
93-
dependencies: ['@experimental-api-clients-automation/client-common'],
9494
},
9595
];
9696
}
@@ -142,14 +142,12 @@ function getPackageConfigs() {
142142
: ['@experimental-api-clients-automation/client-common'],
143143
external: [],
144144
};
145-
const browserFormats = ['umd-browser', 'esm-browser', 'cjs-browser'];
146-
const nodeFormats = ['cjs-node', 'esm-node'];
147145

148146
return [
149147
{
150148
...commonConfig,
151149
input: 'builds/browser.ts',
152-
formats: browserFormats,
150+
formats: BROWSER_FORMATS,
153151
external: ['dom'],
154152
dependencies: [
155153
...commonConfig.dependencies,
@@ -166,7 +164,7 @@ function getPackageConfigs() {
166164
...commonConfig.dependencies,
167165
'@experimental-api-clients-automation/requester-node-http',
168166
],
169-
formats: nodeFormats,
167+
formats: NODE_FORMATS,
170168
},
171169
];
172170
});

scripts/ci/githubActions/setRunVariables.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable no-console */
2+
import { CLIENTS_JS_UTILS } from '../../common';
23
import { getLanguageFolder } from '../../config';
34

45
import { isBaseChanged } from './utils';
@@ -53,11 +54,9 @@ export const DEPENDENCIES = {
5354
JS_COMMON_TESTS_CHANGED: [
5455
`${JS_CLIENT_FOLDER}/packages/client-common/src/__tests__`,
5556
],
56-
JAVASCRIPT_UTILS_CHANGED: [
57-
`${JS_CLIENT_FOLDER}/packages/client-common`,
58-
`${JS_CLIENT_FOLDER}/packages/requester-browser-xhr`,
59-
`${JS_CLIENT_FOLDER}/packages/requester-node-http`,
60-
],
57+
JAVASCRIPT_UTILS_CHANGED: CLIENTS_JS_UTILS.map(
58+
(clientName) => `${JS_CLIENT_FOLDER}/packages/${clientName}`
59+
),
6160
JAVASCRIPT_CLIENT_CHANGED: [
6261
...CLIENTS_COMMON_FILES,
6362
JS_CLIENT_FOLDER,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{object}}{{#path}}.{{.}}{{/path}}({{{parameters}}});
1+
{{object}}{{#path}}.{{.}}{{/path}}({{{parameters}}})

templates/javascript/tests/client/step.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
const result = {{> client/variable}}
66
{{/isVariable}}
77
{{#isMethod}}
8-
const result = await {{> client/method}}
8+
const result = await ({{> client/method}}) as unknown as EchoResponse;
99
{{/isMethod}}

templates/javascript/tests/client/suite.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/* eslint-disable @typescript-eslint/no-unused-vars */
2-
/* eslint-disable require-await */
1+
/* eslint-disable @typescript-eslint/no-unused-vars, require-await */
32
// @ts-nocheck Failing tests will have type errors, but we cannot suppress them even with @ts-expect-error because it doesn't work for a block of lines.
43
import { {{client}}, {{#lambda.titlecase}}{{client}}{{/lambda.titlecase}} } from '{{{import}}}';
54
import { echoRequester } from '@experimental-api-clients-automation/requester-node-http';
5+
import type { EchoResponse } from '@experimental-api-clients-automation/requester-node-http';
66

77
const appId = 'test-app-id';
88
const apiKey = 'test-api-key';
@@ -25,7 +25,7 @@ describe('{{testType}}', () => {
2525
{{> client/step}}
2626
throw new Error('test is expected to throw error');
2727
} catch(e) {
28-
expect(e.message).toMatch("{{{expectedError}}}");
28+
expect((e as Error).message).toMatch("{{{expectedError}}}");
2929
}
3030
{{/isError}}
3131
{{^isError}}

0 commit comments

Comments
 (0)