@tahanabavi/typefetch 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +36 -0
- package/README.md +182 -182
- package/dist/__tests__/client.test.d.ts +1 -0
- package/dist/__tests__/client.test.js +108 -0
- package/dist/__tests__/middlewares.test.d.ts +1 -0
- package/dist/__tests__/middlewares.test.js +85 -0
- package/dist/client.d.ts +31 -0
- package/dist/client.js +98 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +22 -0
- package/dist/middlewares/auth.d.ts +5 -0
- package/dist/middlewares/auth.js +17 -0
- package/dist/middlewares/cache.d.ts +5 -0
- package/dist/middlewares/cache.js +25 -0
- package/dist/middlewares/logging.d.ts +7 -0
- package/dist/middlewares/logging.js +13 -0
- package/dist/middlewares/retry.d.ts +6 -0
- package/dist/middlewares/retry.js +22 -0
- package/jest.config.ts +18 -18
- package/package.json +1 -1
- package/src/__tests__/client.test.ts +137 -137
- package/src/__tests__/middlewares.test.ts +108 -108
- package/src/client.ts +142 -142
- package/src/index.ts +6 -6
- package/src/middlewares/auth.ts +19 -19
- package/src/middlewares/cache.ts +26 -26
- package/src/middlewares/logging.ts +19 -19
- package/src/middlewares/retry.ts +25 -25
- package/src/types.d.ts +46 -46
- package/tsconfig.json +13 -13
package/src/middlewares/retry.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { Middleware } from "../types";
|
|
2
|
-
|
|
3
|
-
export type RetryOptions = {
|
|
4
|
-
maxRetries?: number;
|
|
5
|
-
delay?: number; // ms
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export const retryMiddleware = (options?: RetryOptions): Middleware => {
|
|
9
|
-
const { maxRetries = 3, delay = 500 } = options || {};
|
|
10
|
-
|
|
11
|
-
const middleware: Middleware = async (ctx, next) => {
|
|
12
|
-
let attempt = 0;
|
|
13
|
-
while (true) {
|
|
14
|
-
try {
|
|
15
|
-
return await next();
|
|
16
|
-
} catch (err) {
|
|
17
|
-
if (attempt >= maxRetries) throw err;
|
|
18
|
-
attempt++;
|
|
19
|
-
await new Promise(r => setTimeout(r, delay * 2 ** attempt));
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
return middleware;
|
|
25
|
-
};
|
|
1
|
+
import { Middleware } from "../types";
|
|
2
|
+
|
|
3
|
+
export type RetryOptions = {
|
|
4
|
+
maxRetries?: number;
|
|
5
|
+
delay?: number; // ms
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const retryMiddleware = (options?: RetryOptions): Middleware => {
|
|
9
|
+
const { maxRetries = 3, delay = 500 } = options || {};
|
|
10
|
+
|
|
11
|
+
const middleware: Middleware = async (ctx, next) => {
|
|
12
|
+
let attempt = 0;
|
|
13
|
+
while (true) {
|
|
14
|
+
try {
|
|
15
|
+
return await next();
|
|
16
|
+
} catch (err) {
|
|
17
|
+
if (attempt >= maxRetries) throw err;
|
|
18
|
+
attempt++;
|
|
19
|
+
await new Promise(r => setTimeout(r, delay * 2 ** attempt));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return middleware;
|
|
25
|
+
};
|
package/src/types.d.ts
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
|
|
3
|
-
export type EndpointDef<
|
|
4
|
-
TReq extends z.ZodTypeAny,
|
|
5
|
-
TRes extends z.ZodTypeAny
|
|
6
|
-
> = {
|
|
7
|
-
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
8
|
-
path: string;
|
|
9
|
-
auth?: boolean;
|
|
10
|
-
request: TReq;
|
|
11
|
-
response: TRes;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type Contracts = {
|
|
15
|
-
[ModuleName: string]: {
|
|
16
|
-
[EndpointName: string]: EndpointDef<z.ZodTypeAny, z.ZodTypeAny>;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export interface MiddlewareContext {
|
|
21
|
-
url: string;
|
|
22
|
-
init: RequestInit;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export type MiddlewareNext = () => Promise<Response>;
|
|
26
|
-
|
|
27
|
-
export type Middleware<Options = any> = (
|
|
28
|
-
ctx: MiddlewareContext,
|
|
29
|
-
next: MiddlewareNext,
|
|
30
|
-
options?: Options
|
|
31
|
-
) => Promise<Response>;
|
|
32
|
-
|
|
33
|
-
export type ErrorLike = {
|
|
34
|
-
message: string;
|
|
35
|
-
status?: number;
|
|
36
|
-
code?: string;
|
|
37
|
-
[key: string]: any;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export type EndpointDefZ = EndpointDef<z.ZodTypeAny, z.ZodTypeAny>;
|
|
41
|
-
|
|
42
|
-
export type EndpointMethods<M extends Record<string, EndpointDefZ>> = {
|
|
43
|
-
[K in keyof M]: (
|
|
44
|
-
input: z.infer<M[K]["request"]>
|
|
45
|
-
) => Promise<z.infer<M[K]["response"]>>;
|
|
46
|
-
};
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export type EndpointDef<
|
|
4
|
+
TReq extends z.ZodTypeAny,
|
|
5
|
+
TRes extends z.ZodTypeAny
|
|
6
|
+
> = {
|
|
7
|
+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
8
|
+
path: string;
|
|
9
|
+
auth?: boolean;
|
|
10
|
+
request: TReq;
|
|
11
|
+
response: TRes;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type Contracts = {
|
|
15
|
+
[ModuleName: string]: {
|
|
16
|
+
[EndpointName: string]: EndpointDef<z.ZodTypeAny, z.ZodTypeAny>;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export interface MiddlewareContext {
|
|
21
|
+
url: string;
|
|
22
|
+
init: RequestInit;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type MiddlewareNext = () => Promise<Response>;
|
|
26
|
+
|
|
27
|
+
export type Middleware<Options = any> = (
|
|
28
|
+
ctx: MiddlewareContext,
|
|
29
|
+
next: MiddlewareNext,
|
|
30
|
+
options?: Options
|
|
31
|
+
) => Promise<Response>;
|
|
32
|
+
|
|
33
|
+
export type ErrorLike = {
|
|
34
|
+
message: string;
|
|
35
|
+
status?: number;
|
|
36
|
+
code?: string;
|
|
37
|
+
[key: string]: any;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type EndpointDefZ = EndpointDef<z.ZodTypeAny, z.ZodTypeAny>;
|
|
41
|
+
|
|
42
|
+
export type EndpointMethods<M extends Record<string, EndpointDefZ>> = {
|
|
43
|
+
[K in keyof M]: (
|
|
44
|
+
input: z.infer<M[K]["request"]>
|
|
45
|
+
) => Promise<z.infer<M[K]["response"]>>;
|
|
46
|
+
};
|
package/tsconfig.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "NodeNext",
|
|
5
|
-
"moduleResolution": "NodeNext",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"outDir": "dist",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"forceConsistentCasingInFileNames": true
|
|
11
|
-
},
|
|
12
|
-
"include": ["src", "__test__"]
|
|
13
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"outDir": "dist",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["src", "__test__"]
|
|
13
|
+
}
|