Skip to content

Commit 413397a

Browse files
committed
refactor: more strict types
1 parent 16bf0b8 commit 413397a

File tree

9 files changed

+55
-41
lines changed

9 files changed

+55
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ coverage
66
dist
77
types
88
.conf*
9+
tsconfig.tsbuildinfo

src/base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from "./fetch";
2-
export * from "./error";
1+
export * from "./fetch.ts";
2+
export * from "./error.ts";

src/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FetchContext, IFetchError } from "./types";
1+
import type { FetchContext, IFetchError } from "./types.ts";
22

33
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
44
export class FetchError<T = any> extends Error implements IFetchError<T> {

src/fetch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { Readable } from "node:stream";
22
import destr from "destr";
33
import { withBase, withQuery } from "ufo";
4-
import { createFetchError } from "./error";
4+
import { createFetchError } from "./error.ts";
55
import {
66
isPayloadMethod,
77
isJSONSerializable,
88
detectResponseType,
99
resolveFetchOptions,
1010
callHooks,
11-
} from "./utils";
11+
} from "./utils.ts";
1212
import type {
1313
CreateFetchOptions,
1414
FetchResponse,
@@ -17,7 +17,7 @@ import type {
1717
$Fetch,
1818
FetchRequest,
1919
FetchOptions,
20-
} from "./types";
20+
} from "./types.ts";
2121

2222
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
2323
const retryStatusCodes = new Set([

src/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { createFetch } from "./base";
1+
import { createFetch } from "./base.ts";
2+
import type { $Fetch } from "./types.ts";
23

3-
export * from "./base";
4+
export * from "./base.ts";
45

5-
export type * from "./types";
6+
export type * from "./types.ts";
67

78
// ref: https://github.com/tc39/proposal-global
89
const _globalThis = (function () {
@@ -24,12 +25,13 @@ const _globalThis = (function () {
2425
})();
2526

2627
// ref: https://github.com/unjs/ofetch/issues/295
27-
export const fetch = _globalThis.fetch
28+
export const fetch: typeof globalThis.fetch = _globalThis.fetch
2829
? (...args: Parameters<typeof globalThis.fetch>) => _globalThis.fetch(...args)
2930
: () => Promise.reject(new Error("[ofetch] global.fetch is not supported!"));
3031

31-
export const Headers = _globalThis.Headers;
32-
export const AbortController = _globalThis.AbortController;
32+
export const Headers: typeof globalThis.Headers = _globalThis.Headers;
33+
export const AbortController: typeof globalThis.AbortController =
34+
_globalThis.AbortController;
3335

34-
export const ofetch = createFetch({ fetch, Headers, AbortController });
35-
export const $fetch = ofetch;
36+
export const ofetch: $Fetch = createFetch({ fetch, Headers, AbortController });
37+
export const $fetch: $Fetch = ofetch;

src/node.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import http from "node:http";
2-
import https, { AgentOptions } from "node:https";
2+
import https, { type AgentOptions } from "node:https";
33
import nodeFetch, {
44
Headers as _Headers,
55
AbortController as _AbortController,
66
} from "node-fetch-native";
77

8-
import { createFetch } from "./base";
8+
import { createFetch } from "./base.ts";
9+
import type { $Fetch } from "./types.ts";
910

10-
export * from "./base";
11-
export type * from "./types";
11+
export * from "./base.ts";
12+
export type * from "./types.ts";
1213

13-
export function createNodeFetch() {
14+
export function createNodeFetch(): typeof globalThis.fetch {
1415
const useKeepAlive = JSON.parse(process.env.FETCH_KEEP_ALIVE || "false");
1516
if (!useKeepAlive) {
1617
return nodeFetch;
@@ -31,15 +32,17 @@ export function createNodeFetch() {
3132
init?: RequestInit
3233
) {
3334
return (nodeFetch as any)(input, { ...nodeFetchOptions, ...init });
34-
};
35+
} as any;
3536
}
3637

37-
export const fetch = globalThis.fetch
38+
export const fetch: typeof globalThis.fetch = globalThis.fetch
3839
? (...args: Parameters<typeof globalThis.fetch>) => globalThis.fetch(...args)
3940
: (createNodeFetch() as typeof globalThis.fetch);
4041

41-
export const Headers = globalThis.Headers || _Headers;
42-
export const AbortController = globalThis.AbortController || _AbortController;
42+
export const Headers: typeof globalThis.Headers =
43+
globalThis.Headers || _Headers;
44+
export const AbortController: typeof globalThis.AbortController =
45+
globalThis.AbortController || _AbortController;
4346

44-
export const ofetch = createFetch({ fetch, Headers, AbortController });
45-
export const $fetch = ofetch;
47+
export const ofetch: $Fetch = createFetch({ fetch, Headers, AbortController });
48+
export const $fetch: $Fetch = ofetch;

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import type {
55
FetchRequest,
66
ResolvedFetchOptions,
77
ResponseType,
8-
} from "./types";
8+
} from "./types.ts";
99

1010
const payloadMethods = new Set(
1111
Object.freeze(["PATCH", "POST", "PUT", "DELETE"])
1212
);
13-
export function isPayloadMethod(method = "GET") {
13+
export function isPayloadMethod(method = "GET"): boolean {
1414
return payloadMethods.has(method.toUpperCase());
1515
}
1616

17-
export function isJSONSerializable(value: any) {
17+
export function isJSONSerializable(value: any): boolean {
1818
if (value === undefined) {
1919
return false;
2020
}

test/index.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Readable } from "node:stream";
1111
import { H3, HTTPError, readBody, serve } from "h3";
1212
import { Headers, FormData, Blob } from "node-fetch-native";
1313
import { nodeMajorVersion } from "std-env";
14-
import { $fetch } from "../src/node";
14+
import { $fetch } from "../src/node.ts";
1515

1616
describe("ofetch", () => {
1717
let listener: ReturnType<typeof serve>;
@@ -237,7 +237,7 @@ describe("ofetch", () => {
237237
});
238238

239239
it("404", async () => {
240-
const error = await $fetch(getURL("404")).catch((error_) => error_);
240+
const error = await $fetch(getURL("404")).catch((error_: any) => error_);
241241
expect(error.toString()).toBe(
242242
`FetchError: [GET] "${getURL("404")}": 404 Not Found`
243243
);
@@ -267,7 +267,7 @@ describe("ofetch", () => {
267267

268268
it("baseURL with retry", async () => {
269269
const error = await $fetch("", { baseURL: getURL("404"), retry: 3 }).catch(
270-
(error_) => error_
270+
(error_: any) => error_
271271
);
272272
expect(error.request).to.equal(getURL("404"));
273273
});
@@ -316,7 +316,7 @@ describe("ofetch", () => {
316316

317317
it("passing request obj should return request obj in error", async () => {
318318
const error = await $fetch(getURL("/403"), { method: "post" }).catch(
319-
(error) => error
319+
(error: any) => error
320320
);
321321
expect(error.toString()).toBe(
322322
`FetchError: [POST] "${getURL("403")}": 403 Forbidden`
@@ -340,7 +340,7 @@ describe("ofetch", () => {
340340
await $fetch(getURL("timeout"), {
341341
timeout: 100,
342342
retry: 0,
343-
}).catch((error) => {
343+
}).catch((error: any) => {
344344
expect(error.cause.message).to.include(
345345
"The operation was aborted due to timeout"
346346
);
@@ -468,7 +468,7 @@ describe("ofetch", () => {
468468
onRequestError,
469469
onResponse,
470470
onResponseError,
471-
}).catch((error) => error);
471+
}).catch((error: any) => error);
472472

473473
expect(onRequest).toHaveBeenCalledOnce();
474474
expect(onRequestError).not.toHaveBeenCalled();

tsconfig.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
{
22
"compilerOptions": {
33
"target": "ESNext",
4-
"module": "ESNext",
5-
"moduleResolution": "Node",
6-
"esModuleInterop": true,
7-
"outDir": "dist",
4+
"module": "NodeNext",
5+
"moduleResolution": "NodeNext",
6+
"resolveJsonModule": true,
7+
"esModuleInterop": false,
8+
"allowSyntheticDefaultImports": true,
9+
"skipLibCheck": true,
810
"strict": true,
9-
"declaration": true,
10-
"types": ["node"]
11+
"verbatimModuleSyntax": true,
12+
"isolatedModules": true,
13+
"composite": true,
14+
"allowImportingTsExtensions": true,
15+
"isolatedDeclarations": true,
16+
"forceConsistentCasingInFileNames": true,
17+
"noImplicitOverride": true,
18+
"noEmit": true
1119
},
12-
"include": ["src"]
20+
"include": ["src", "test"]
1321
}

0 commit comments

Comments
 (0)