Skip to content

Commit 3b2035c

Browse files
committed
fix(javascript): ensure requesters work as in v4
1 parent 9bdedf1 commit 3b2035c

File tree

19 files changed

+816
-75
lines changed

19 files changed

+816
-75
lines changed

.github/workflows/check.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ jobs:
163163
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
164164
run: cd clients/algoliasearch-client-javascript && yarn build ${{ matrix.client }}
165165

166-
- name: Run tests for 'client-common'
167-
if: ${{ steps.cache.outputs.cache-hit != 'true' && matrix.client == 'client-common' }}
168-
run: cd clients/algoliasearch-client-javascript && yarn workspace @algolia/client-common test
166+
- name: Run tests for '${{ matrix.client }}'
167+
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
168+
run: cd clients/algoliasearch-client-javascript && yarn workspace @algolia/${{ matrix.client }} test
169169

170170
- name: Store '${{ matrix.client }}' JavaScript utils package
171171
uses: actions/upload-artifact@v3
Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EchoResponse, EndRequest, Request, Response } from './types';
1+
import type { EchoResponse, EndRequest, Requester, Response } from './types';
22

33
export type EchoRequesterParams = {
44
getURL: (url: string) => URL;
@@ -8,7 +8,8 @@ export type EchoRequesterParams = {
88
function getUrlParams({
99
host,
1010
searchParams: urlSearchParams,
11-
}: URL): Pick<EchoResponse, 'algoliaAgent' | 'host' | 'searchParams'> {
11+
pathname,
12+
}: URL): Pick<EchoResponse, 'algoliaAgent' | 'host' | 'path' | 'searchParams'> {
1213
const algoliaAgent = urlSearchParams.get('x-algolia-agent') || '';
1314
const searchParams = {};
1415

@@ -25,39 +26,34 @@ function getUrlParams({
2526
algoliaAgent,
2627
searchParams:
2728
Object.keys(searchParams).length === 0 ? undefined : searchParams,
29+
path: pathname,
2830
};
2931
}
3032

31-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
3233
export function createEchoRequester({
3334
getURL,
3435
status = 200,
35-
}: EchoRequesterParams) {
36-
function send(
37-
{ headers, url, connectTimeout, responseTimeout }: EndRequest,
38-
{ data, ...originalRequest }: Request
39-
): Promise<Response> {
40-
const { host, searchParams, algoliaAgent } = getUrlParams(getURL(url));
41-
const originalData =
42-
data && Object.keys(data).length > 0 ? data : undefined;
36+
}: EchoRequesterParams): Requester {
37+
function send(request: EndRequest): Promise<Response> {
38+
const { host, searchParams, algoliaAgent, path } = getUrlParams(
39+
getURL(request.url)
40+
);
41+
42+
const content: EchoResponse = {
43+
...request,
44+
data: request.data ? JSON.parse(request.data) : undefined,
45+
path,
46+
host,
47+
algoliaAgent: encodeURI(algoliaAgent),
48+
searchParams,
49+
};
4350

4451
return Promise.resolve({
45-
content: JSON.stringify({
46-
...originalRequest,
47-
host,
48-
headers,
49-
connectTimeout,
50-
responseTimeout,
51-
algoliaAgent: encodeURI(algoliaAgent),
52-
searchParams,
53-
data: originalData,
54-
}),
52+
content: JSON.stringify(content),
5553
isTimedOut: false,
5654
status,
5755
});
5856
}
5957

6058
return { send };
6159
}
62-
63-
export type EchoRequester = ReturnType<typeof createEchoRequester>;

clients/algoliasearch-client-javascript/packages/client-common/src/transporter/createTransporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export function createTransporter({
176176
return stackFrame;
177177
};
178178

179-
const response = await requester.send(payload, request);
179+
const response = await requester.send(payload);
180180

181181
if (isRetryable(response)) {
182182
const stackFrame = pushToStackTrace(response);
Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import type { Headers, QueryParameters } from './Transporter';
22

3+
/**
4+
* The method of the request.
5+
*/
36
export type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
47

58
export type Request = {
69
method: Method;
10+
/**
11+
* The path of the REST API to send the request to.
12+
*/
713
path: string;
814
queryParameters: QueryParameters;
915
data?: Array<Record<string, any>> | Record<string, any>;
@@ -20,33 +26,47 @@ export type Request = {
2026
useReadTransporter?: boolean;
2127
};
2228

23-
export type EndRequest = {
24-
method: Method;
29+
export type EndRequest = Pick<Request, 'headers' | 'method'> & {
30+
/**
31+
* The full URL of the REST API.
32+
*/
2533
url: string;
34+
/**
35+
* The connection timeout, in milliseconds.
36+
*/
2637
connectTimeout: number;
38+
/**
39+
* The response timeout, in milliseconds.
40+
*/
2741
responseTimeout: number;
28-
headers: Headers;
2942
data?: string;
3043
};
3144

3245
export type Response = {
46+
/**
47+
* The body of the response.
48+
*/
3349
content: string;
50+
/**
51+
* Wether the API call is timed out or not.
52+
*/
3453
isTimedOut: boolean;
54+
/**
55+
* The status code of the response.
56+
*/
3557
status: number;
3658
};
3759

3860
export type Requester = {
3961
/**
4062
* Sends the given `request` to the server.
4163
*/
42-
send: (request: EndRequest, originalRequest: Request) => Promise<Response>;
64+
send: (request: EndRequest) => Promise<Response>;
4365
};
4466

45-
export type EchoResponse = Request & {
46-
connectTimeout: number;
47-
host: string;
48-
headers: Headers;
49-
responseTimeout: number;
50-
algoliaAgent: string;
51-
searchParams?: Record<string, string>;
52-
};
67+
export type EchoResponse = Omit<EndRequest, 'data'> &
68+
Pick<Request, 'data' | 'path'> & {
69+
host: string;
70+
algoliaAgent: string;
71+
searchParams?: Record<string, string>;
72+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type RequestOptions = Pick<Request, 'cacheable'> & {
2626
queryParameters?: QueryParameters;
2727

2828
/**
29-
* Custom data for the request. This data are
29+
* Custom data for the request. This data is
3030
* going to be merged the transporter data.
3131
*/
3232
data?: Array<Record<string, any>> | Record<string, any>;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Config } from '@jest/types';
2+
3+
const config: Config.InitialOptions = {
4+
preset: 'ts-jest',
5+
roots: ['src/__tests__'],
6+
testEnvironment: 'jsdom',
7+
};
8+
9+
export default config;

clients/algoliasearch-client-javascript/packages/requester-browser-xhr/package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@
1414
"index.ts"
1515
],
1616
"scripts": {
17-
"build": "tsc",
18-
"clean": "rm -rf dist/"
17+
"clean": "rm -rf dist/",
18+
"test": "jest"
1919
},
2020
"dependencies": {
2121
"@algolia/client-common": "5.0.0-alpha.0"
2222
},
2323
"devDependencies": {
24+
"@types/jest": "28.1.4",
2425
"@types/node": "16.11.45",
25-
"typescript": "4.7.4"
26+
"jest": "28.1.2",
27+
"jest-environment-jsdom": "28.1.2",
28+
"ts-jest": "28.0.5",
29+
"typescript": "4.7.4",
30+
"xhr-mock": "2.5.1"
2631
},
2732
"engines": {
2833
"node": ">= 14.0.0"

0 commit comments

Comments
 (0)