Skip to content

Commit 762cc81

Browse files
feat(javascript): add requester-fetch
algolia/api-clients-automation#855 Co-authored-by: Clément Vannicatte <[email protected]>
1 parent 9b291b6 commit 762cc81

File tree

12 files changed

+566
-118
lines changed

12 files changed

+566
-118
lines changed

base.rollup.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const UTILS = {
1616
external: ['dom'],
1717
dependencies: [`${NPM_ORG}client-common`],
1818
},
19+
'requester-fetch': {
20+
external: ['dom'],
21+
dependencies: [`${NPM_ORG}client-common`],
22+
},
1923
'requester-node-http': {
2024
external: ['https', 'http', 'url'],
2125
dependencies: [`${NPM_ORG}client-common`],

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"build:all": "./scripts/build_all.sh",
1010
"build:utils": "yarn build utils",
1111
"clean": "rm -rf packages/*/dist || true",
12-
"clean:utils": "yarn workspace @algolia/client-common clean && yarn workspace @algolia/requester-node-http clean && yarn workspace @algolia/requester-browser-xhr clean",
12+
"clean:utils": "yarn workspace @algolia/client-common clean && yarn workspace @algolia/requester-node-http clean && yarn workspace @algolia/requester-browser-xhr clean && yarn workspace @algolia/requester-fetch clean",
1313
"release:bump": "lerna version ${0:-patch} --no-changelog --no-git-tag-version --no-push --exact --force-publish --yes",
1414
"release:publish": "ts-node --project tsconfig.script.json scripts/publish.ts",
1515
"test:lint": "eslint . --ext .js,.ts",

packages/requester-browser-xhr/src/__tests__/browser-xhr-requester.test.ts

+11-49
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,20 @@
1-
import http from 'http';
1+
import type http from 'http';
22

3-
import type { EndRequest, Headers } from '@algolia/client-common';
3+
import type { EndRequest } from '@algolia/client-common';
44
import type { MockRequest, MockResponse } from 'xhr-mock';
55
import mock from 'xhr-mock';
66

77
import { createXhrRequester } from '../..';
8+
import {
9+
BASE_URL,
10+
headers,
11+
timeoutRequest,
12+
requestStub,
13+
getStringifiedBody,
14+
createTestServer,
15+
} from '../../../../tests/utils';
816

917
const requester = createXhrRequester();
10-
const BASE_URL = 'https://algolia-dns.net/foo?x-algolia-header=bar';
11-
12-
function getStringifiedBody(
13-
body: Record<string, any> = { foo: 'bar' }
14-
): string {
15-
return JSON.stringify(body);
16-
}
17-
18-
const headers: Headers = {
19-
'content-type': 'text/plain',
20-
};
21-
22-
const timeoutRequest: EndRequest = {
23-
url: 'missing-url-here',
24-
data: '',
25-
headers: {},
26-
method: 'GET',
27-
responseTimeout: 2000,
28-
connectTimeout: 1000,
29-
};
30-
31-
const requestStub: EndRequest = {
32-
url: BASE_URL,
33-
method: 'POST',
34-
headers,
35-
data: getStringifiedBody(),
36-
responseTimeout: 1000,
37-
connectTimeout: 2000,
38-
};
3918

4019
describe('status code handling', () => {
4120
beforeEach(() => mock.setup());
@@ -123,24 +102,7 @@ describe('timeout handling', () => {
123102
let server: http.Server;
124103
// setup http server to test timeout
125104
beforeAll(() => {
126-
server = http.createServer(function (_req, res) {
127-
res.writeHead(200, {
128-
'content-type': 'text/plain',
129-
'access-control-allow-origin': '*',
130-
'x-powered-by': 'nodejs',
131-
});
132-
133-
res.write('{"foo":');
134-
135-
setTimeout(() => {
136-
res.write(' "bar"');
137-
}, 1000);
138-
139-
setTimeout(() => {
140-
res.write('}');
141-
res.end();
142-
}, 5000);
143-
});
105+
server = createTestServer();
144106

145107
server.listen('1111');
146108
});

packages/requester-fetch/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './src/createFetchRequester';
+9
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;

packages/requester-fetch/package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@algolia/requester-fetch",
3+
"version": "5.0.0-alpha.0",
4+
"description": "Promise-based request library using Fetch.",
5+
"repository": "algolia/algoliasearch-client-javascript",
6+
"license": "MIT",
7+
"author": "Algolia",
8+
"main": "dist/requester-fetch.cjs.js",
9+
"module": "dist/requester-fetch.esm.node.js",
10+
"types": "dist/index.d.ts",
11+
"files": [
12+
"dist",
13+
"src",
14+
"index.ts"
15+
],
16+
"scripts": {
17+
"clean": "rm -rf dist/",
18+
"test": "jest"
19+
},
20+
"dependencies": {
21+
"@algolia/client-common": "5.0.0-alpha.0"
22+
},
23+
"devDependencies": {
24+
"@types/jest": "28.1.4",
25+
"@types/node": "16.11.45",
26+
"cross-fetch": "3.1.5",
27+
"jest": "28.1.2",
28+
"nock": "13.2.8",
29+
"ts-jest": "28.0.5",
30+
"typescript": "4.7.4"
31+
},
32+
"engines": {
33+
"node": ">= 14.0.0"
34+
}
35+
}

0 commit comments

Comments
 (0)