From 7dd9a3d0352b7dadaae3932da4f31a7ce3a9544e Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Mon, 24 Jun 2024 20:22:28 +0900 Subject: [PATCH] No more use `node-fetch` due to NodeJS v20 supports native `fetch()` function. --- package.json | 2 +- packages/core/package.json | 6 ++-- packages/fetcher/package.json | 6 +--- packages/fetcher/src/EncryptedFetcher.ts | 2 +- packages/fetcher/src/IConnection.ts | 7 +++- packages/fetcher/src/PlainFetcher.ts | 2 +- packages/fetcher/src/internal/FetcherBase.ts | 37 +++++--------------- packages/fetcher/src/internal/Singleton.ts | 20 ----------- packages/sdk/package.json | 6 ++-- test/package.json | 8 ++--- 10 files changed, 28 insertions(+), 68 deletions(-) delete mode 100644 packages/fetcher/src/internal/Singleton.ts diff --git a/package.json b/package.json index 0a4d72c9d..6ec5c6ad6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "@nestia/station", - "version": "3.2.6", + "version": "3.3.0-dev.20240624", "description": "Nestia station", "scripts": { "build": "node build/index.js", diff --git a/packages/core/package.json b/packages/core/package.json index a90368bb9..044a9f61e 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@nestia/core", - "version": "3.2.6", + "version": "3.3.0-dev.20240624", "description": "Super-fast validation decorators of NestJS", "main": "lib/index.js", "typings": "lib/index.d.ts", @@ -36,7 +36,7 @@ }, "homepage": "https://nestia.io", "dependencies": { - "@nestia/fetcher": "^3.2.6", + "@nestia/fetcher": "^3.3.0-dev.20240624", "@nestjs/common": ">=7.0.1", "@nestjs/core": ">=7.0.1", "@samchon/openapi": "^0.1.21", @@ -53,7 +53,7 @@ "ws": "^7.5.3" }, "peerDependencies": { - "@nestia/fetcher": ">=3.2.6", + "@nestia/fetcher": ">=3.3.0-dev.20240624", "@nestjs/common": ">=7.0.1", "@nestjs/core": ">=7.0.1", "reflect-metadata": ">=0.1.12", diff --git a/packages/fetcher/package.json b/packages/fetcher/package.json index f7cee4696..1e8dd0b4f 100644 --- a/packages/fetcher/package.json +++ b/packages/fetcher/package.json @@ -1,6 +1,6 @@ { "name": "@nestia/fetcher", - "version": "3.2.6", + "version": "3.3.0-dev.20240624", "description": "Fetcher library of Nestia SDK", "main": "lib/index.js", "typings": "lib/index.d.ts", @@ -35,10 +35,6 @@ "peerDependencies": { "typescript": ">= 4.8.0" }, - "dependencies": { - "import2": "^1.0.3", - "node-fetch": "^2.6.7" - }, "files": [ "README.md", "LICENSE", diff --git a/packages/fetcher/src/EncryptedFetcher.ts b/packages/fetcher/src/EncryptedFetcher.ts index b72c20ad0..e197376e6 100644 --- a/packages/fetcher/src/EncryptedFetcher.ts +++ b/packages/fetcher/src/EncryptedFetcher.ts @@ -85,7 +85,7 @@ export namespace EncryptedFetcher { }) : () => () => connection.encryption as IEncryptionPassword; - return FetcherBase.fetch({ + return FetcherBase.request({ className: "EncryptedFetcher", encode: route.request?.encrypted === true diff --git a/packages/fetcher/src/IConnection.ts b/packages/fetcher/src/IConnection.ts index e1048921f..faa4e089f 100644 --- a/packages/fetcher/src/IConnection.ts +++ b/packages/fetcher/src/IConnection.ts @@ -75,8 +75,13 @@ export interface IConnection< /** * Custom fetch function. * - * If you want to use custom `fetch` function instead of built-in function, + * If you want to use custom `fetch` function instead of built-in, * assign your custom `fetch` function into this property. + * + * For reference, the `fetch` function has started to be supported + * since version 20 of NodeJS. Therefore, if you are using NodeJS + * version 19 or lower, you have to assign the `node-fetch` module + * into this property. */ fetch?: typeof fetch; } diff --git a/packages/fetcher/src/PlainFetcher.ts b/packages/fetcher/src/PlainFetcher.ts index d288bb903..5e816718c 100644 --- a/packages/fetcher/src/PlainFetcher.ts +++ b/packages/fetcher/src/PlainFetcher.ts @@ -67,7 +67,7 @@ export namespace PlainFetcher { throw new Error( "Error on PlainFetcher.fetch(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.", ); - return FetcherBase.fetch({ + return FetcherBase.request({ className: "PlainFetcher", encode: (input) => input, decode: (input) => input, diff --git a/packages/fetcher/src/internal/FetcherBase.ts b/packages/fetcher/src/internal/FetcherBase.ts index e6ed651d2..0f8dad313 100644 --- a/packages/fetcher/src/internal/FetcherBase.ts +++ b/packages/fetcher/src/internal/FetcherBase.ts @@ -1,12 +1,12 @@ -import import2 from "import2"; - import { HttpError } from "../HttpError"; import { IConnection } from "../IConnection"; import { IFetchEvent } from "../IFetchEvent"; import { IFetchRoute } from "../IFetchRoute"; import { IPropagation } from "../IPropagation"; -import { Singleton } from "./Singleton"; +/** + * @internal + */ export namespace FetcherBase { export interface IProps { className: string; @@ -20,7 +20,7 @@ export namespace FetcherBase { ) => any; } - export const fetch = + export const request = (props: IProps) => async ( connection: IConnection, @@ -135,9 +135,10 @@ export namespace FetcherBase { }; try { // TRY FETCH - const response: Response = await ( - connection.fetch ?? (await polyfill.get()) - )(url.href, init); + const response: Response = await (connection.fetch ?? fetch)( + url.href, + init, + ); event.respond_at = new Date(); event.status = response.status; @@ -193,28 +194,6 @@ export namespace FetcherBase { }; } -/** - * @internal - */ -const polyfill = new Singleton(async (): Promise => { - function is_node_process(m: typeof global | null): boolean { - return ( - m !== null && - typeof m.process === "object" && - m.process !== null && - typeof m.process.versions === "object" && - m.process.versions !== null && - typeof m.process.versions.node !== "undefined" - ); - } - if (typeof global === "object" && is_node_process(global)) { - const m: any = global as any; - m.fetch ??= ((await import2("node-fetch")) as any).default; - return (m as any).fetch; - } - return self.fetch; -}); - /** * @internal */ diff --git a/packages/fetcher/src/internal/Singleton.ts b/packages/fetcher/src/internal/Singleton.ts deleted file mode 100644 index eee94c509..000000000 --- a/packages/fetcher/src/internal/Singleton.ts +++ /dev/null @@ -1,20 +0,0 @@ -/** - * @internal - */ -export class Singleton { - private value_: T | object; - - public constructor(private readonly closure_: () => T) { - this.value_ = NOT_MOUNTED_YET; - } - - public get(): T { - if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_(); - return this.value_ as T; - } -} - -/** - * @internal - */ -const NOT_MOUNTED_YET = {}; diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 517c682ef..a8a8fe0bc 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@nestia/sdk", - "version": "3.2.6", + "version": "3.3.0-dev.20240624", "description": "Nestia SDK and Swagger generator", "main": "lib/index.js", "typings": "lib/index.d.ts", @@ -32,7 +32,7 @@ }, "homepage": "https://nestia.io", "dependencies": { - "@nestia/fetcher": "^3.2.6", + "@nestia/fetcher": "^3.3.0-dev.20240624", "@samchon/openapi": "^0.1.21", "cli": "^1.0.1", "get-function-location": "^2.0.0", @@ -46,7 +46,7 @@ "typia": "^6.0.3" }, "peerDependencies": { - "@nestia/fetcher": ">=3.2.6", + "@nestia/fetcher": ">=3.3.0-dev.20240624", "@nestjs/common": ">=7.0.1", "@nestjs/core": ">=7.0.1", "reflect-metadata": ">=0.1.12", diff --git a/test/package.json b/test/package.json index 82dcb40e5..d30ded912 100644 --- a/test/package.json +++ b/test/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "@nestia/test", - "version": "3.2.6", + "version": "3.3.0-dev.20240624", "description": "Test program of Nestia", "main": "index.js", "scripts": { @@ -26,7 +26,7 @@ }, "homepage": "https://nestia.io", "devDependencies": { - "@nestia/sdk": "^3.2.6", + "@nestia/sdk": "^3.3.0-dev.20240624", "@nestjs/swagger": "^7.1.2", "@samchon/openapi": "^0.1.21", "@types/express": "^4.17.17", @@ -40,9 +40,9 @@ }, "dependencies": { "@fastify/multipart": "^8.1.0", - "@nestia/core": "^3.2.6", + "@nestia/core": "^3.3.0-dev.20240624", "@nestia/e2e": "^0.3.6", - "@nestia/fetcher": "^3.2.6", + "@nestia/fetcher": "^3.3.0-dev.20240624", "@nestjs/common": "^10.3.5", "@nestjs/core": "^10.3.5", "@nestjs/platform-express": "^10.3.5",