Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/plugins/protocol-http.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sidebar_label: Protocol - Http
---

Makes http/https request.
Uses `node-fetch` library.
Uses native nodejs fetch.

!!! Plugin requires `fetch` polyfill for browsers without support.

Expand Down
9 changes: 7 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module.exports = {
timers: 'legacy',
testURL: 'http://localhost/',
fakeTimers: {
enableGlobally: true,
legacyFakeTimers: true
},
testEnvironmentOptions: {
url: 'http://localhost/'
},
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'babel-jest',
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
"devDependencies": {
"@size-limit/preset-small-lib": "^7.0.8",
"@tramvai/build": "^2.6.11",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.21",
"@types/jest": "^28.0.0",
"@types/node": "^18.0.0",
"fast-glob": "^3.2.11",
"fs-extra": "^10.0.1",
"jest": "^27.5.1",
"jest": "^28.0.0",
"jest-date-mock": "^1.0.8",
"jest-environment-jsdom": "^28.0.0",
"lerna": "^5.6.2",
"prettier": "^2.5.1",
"size-limit": "^7.0.8",
"ts-jest": "^27.1.3",
"ts-jest": "^28.0.0",
"typescript": "^4.5.5"
}
}
2 changes: 1 addition & 1 deletion packages/plugin-cache-etag/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"devDependencies": {
"@tinkoff/request-core": "^0.10.0",
"@tinkoff/request-plugin-protocol-http": "^0.13.2",
"@types/node": "^10.11.7"
"@types/node": "^18.0.0"
},
"peerDependencies": {
"@tinkoff/request-core": "0.x",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-cache-etag/src/etag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default ({

if (etag) {
lruCache.set(cacheKey, {
key: etag,
key: <string>etag,
value: context.getResponse(),
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-cache-fallback/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"devDependencies": {
"@tinkoff/request-core": "^0.10.0",
"@types/fs-extra": "^8.1.0",
"@types/node": "^10.11.7",
"@types/node": "^18.0.0",
"@types/spark-md5": "^3.0.1"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-cache-persistent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@tinkoff/request-core": "^0.10.0",
"@types/node": "^10.12.0"
"@types/node": "^18.0.0"
},
"peerDependencies": {
"@tinkoff/request-core": "0.x"
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-circuit-breaker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"devDependencies": {
"@tinkoff/request-core": "^0.10.0",
"@types/node": "^10.12.0"
"@types/node": "^18.0.0"
},
"peerDependencies": {
"@tinkoff/request-core": "0.x"
Expand Down
102 changes: 102 additions & 0 deletions packages/plugin-protocol-http/MIGRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Migrations

## 0.14.0

## `02.09.2025`

Now `@tinkoff/request` uses the native Node.js fetch implementation — `undici` instead of `node-fetch`. As a result, a number of breaking changes have been introduced:

- Removed node-fetch and the abort-controller polyfill

- Minimum supported Node.js version is now 18+

- The signature of a custom Agent for requests has changed — it is now an undici `dispatcher`

### Undici Agent

The biggest change is the updated Agent signature. Undici has a different set of parameters and default settings.

Now, instead of using http.Agent/https.Agent, use Agent from undici:

```ts
import { Agent } from 'undici';

const options = {
connections: 10
};

http({
agent: {
http: new Agent(options),
https: new Agent(options)
}
})
```

**Caution: you need to install undici separately**

The Agent has no keepAlive option — undici enables keep-alive by default. Also Agent is unified for http/https requests.

Documentation links:

[Example of custom agent usage](https://undici.nodejs.org/#/examples/?id=using-interceptors-to-add-response-caching-dns-lookup-caching-and-connection-retries).

[Agent parameters](https://undici.nodejs.org/#/docs/api/Client?id=parameter-clientoptions).

### Jest

In jest-environment-jsdom there is no fetch. Use jsdom patching with undici’s fetch, as it is the most spec-compatible:

```ts
// FixJSDOMEnvironment.ts

import JSDOMEnvironment from 'jest-environment-jsdom';

// https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string
export default class FixJSDOMEnvironment extends JSDOMEnvironment {
constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
super(...args);

// FIXME https://github.com/jsdom/jsdom/issues/1724
this.global.fetch = fetch;
this.global.Headers = Headers;
this.global.Request = Request;
this.global.Response = Response;
}
```

```ts
// jest.config.js

/** @type {import('jest').Config} */
const config = {
testEnvironment: './FixJSDOMEnvironment.ts',

...
}

module.exports = config;
```

For mocks in Jest tests, use `jest.spyOn()`:

```ts
const fetch = jest.spyOn(globalThis, 'fetch');

it('some test', () => {
(fetch as any).mockImplementation(() =>
createJsonResponse({
resultCode: 'OK',
payload: 'payload',
})
);

const url = 'http://localhost:3000';
// Some actions

expect(fetch).toHaveBeenCalledTimes(1);

const request = (fetch as any).mock.calls[0];
expect(request[0]).toEqual(url);
});
```
6 changes: 2 additions & 4 deletions packages/plugin-protocol-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@
"@tinkoff/request-core": "^0.10.0",
"@tinkoff/request-url-utils": "^0.10.2",
"@tinkoff/utils": "^2.0.0",
"abort-controller": "^3.0.0",
"form-data": "^2.5.0",
"node-fetch": "^2.6.1",
"tslib": "^2.1.3"
},
"devDependencies": {
"@types/node": "^12.7.1",
"jest-fetch-mock": "^2.1.2"
"@types/node": "^18.0.0",
"undici": "^5.0.0"
},
"peerDependencies": {
"@tinkoff/request-core": "0.x"
Expand Down
10 changes: 8 additions & 2 deletions packages/plugin-protocol-http/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import fetch, { Headers, Request, Response } from 'node-fetch';
import type { RequestInit, RequestInfo, fetch as undiciFetch } from "undici";

export { fetch, Headers, Response, Request };
const fetch = (input: RequestInfo, init?: RequestInit) => {
return (<typeof undiciFetch>globalThis.fetch)(input, init);
};

const { Headers, Request, Response } = globalThis;

export { fetch, Headers, Request, Response };
Loading