Skip to content

Commit

Permalink
Merge pull request #933 from samchon/features/fetch
Browse files Browse the repository at this point in the history
Fix #931: no more use `node-fetch` for React Native
  • Loading branch information
samchon authored Jun 27, 2024
2 parents 3cc636d + 7dd9a3d commit 7b887b6
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 68 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
6 changes: 1 addition & 5 deletions packages/fetcher/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -35,10 +35,6 @@
"peerDependencies": {
"typescript": ">= 4.8.0"
},
"dependencies": {
"import2": "^1.0.3",
"node-fetch": "^2.6.7"
},
"files": [
"README.md",
"LICENSE",
Expand Down
2 changes: 1 addition & 1 deletion packages/fetcher/src/EncryptedFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export namespace EncryptedFetcher {
})
: () => () => connection.encryption as IEncryptionPassword;

return FetcherBase.fetch({
return FetcherBase.request({
className: "EncryptedFetcher",
encode:
route.request?.encrypted === true
Expand Down
7 changes: 6 additions & 1 deletion packages/fetcher/src/IConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/fetcher/src/PlainFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 8 additions & 29 deletions packages/fetcher/src/internal/FetcherBase.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,7 +20,7 @@ export namespace FetcherBase {
) => any;
}

export const fetch =
export const request =
(props: IProps) =>
async <Input, Output>(
connection: IConnection,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -193,28 +194,6 @@ export namespace FetcherBase {
};
}

/**
* @internal
*/
const polyfill = new Singleton(async (): Promise<typeof fetch> => {
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
*/
Expand Down
20 changes: 0 additions & 20 deletions packages/fetcher/src/internal/Singleton.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 7b887b6

Please sign in to comment.