@sokutils/api-kit 0.0.0-beta.0 → 0.0.0-beta.1
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/dist/index.d.ts +17 -10
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/core.ts +10 -8
- package/src/define.ts +5 -0
- package/src/index.ts +1 -0
- package/src/types.ts +15 -5
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,12 @@ import { ZodMiniType } from "zod/v4-mini";
|
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
4
|
type MaybePromise<P> = P | Promise<P>;
|
|
5
|
-
interface
|
|
5
|
+
interface TApiKit<ResponseType> {
|
|
6
|
+
config: <NextResponseType>(resolver: ApiKitConfigResolver<ResponseType, NextResponseType>) => TApiKit<NextResponseType>;
|
|
7
|
+
fetch: () => Promise<ResponseType>;
|
|
8
|
+
}
|
|
9
|
+
type ApiKitConfigResolver<ResponseType, NextResponseType> = (config: ApiKitConfig<ResponseType>) => ApiKitConfig<NextResponseType>;
|
|
10
|
+
interface ApiKitConfig<ResponseType = any> {
|
|
6
11
|
BaseURL?: string;
|
|
7
12
|
Path?: string;
|
|
8
13
|
Method?: string;
|
|
@@ -10,16 +15,18 @@ interface ApiKitConfig {
|
|
|
10
15
|
Query?: Record<string, any>;
|
|
11
16
|
GetHeaders?: () => MaybePromise<Record<string, any>>;
|
|
12
17
|
ResponseResolver?: (response: Response) => any;
|
|
13
|
-
ResponseZod?: ZodMiniType
|
|
14
|
-
ResponseHandlers?: ApiKitResponseHandler[];
|
|
15
|
-
ErrorHandlers?:
|
|
18
|
+
ResponseZod?: ZodMiniType<ResponseType>;
|
|
19
|
+
ResponseHandlers?: ApiKitResponseHandler<ResponseType>[];
|
|
20
|
+
ErrorHandlers?: ApiKitErrorHandler[];
|
|
21
|
+
Debug?: (...messages: any[]) => void;
|
|
16
22
|
}
|
|
17
|
-
type ApiKitResponseHandler = (response:
|
|
23
|
+
type ApiKitResponseHandler<ResponseType> = (response: ResponseType) => void;
|
|
24
|
+
type ApiKitErrorHandler = (error: Error) => void;
|
|
18
25
|
//#endregion
|
|
19
26
|
//#region src/core.d.ts
|
|
20
|
-
declare const ApiKit: (originConfig?: ApiKitConfig) =>
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
declare const ApiKit: <ResponseType = any>(originConfig?: ApiKitConfig<ResponseType>) => TApiKit<ResponseType>;
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/define.d.ts
|
|
30
|
+
declare const ApiKitDefineConfigResolver: <ResponseType, NextResponseType>(resolver: ApiKitConfigResolver<ResponseType, NextResponseType>) => ApiKitConfigResolver<ResponseType, NextResponseType>;
|
|
24
31
|
//#endregion
|
|
25
|
-
export { ApiKit, ApiKitConfig, ApiKitResponseHandler, MaybePromise };
|
|
32
|
+
export { ApiKit, ApiKitConfig, ApiKitConfigResolver, ApiKitDefineConfigResolver, ApiKitErrorHandler, ApiKitResponseHandler, MaybePromise, TApiKit };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{compact as e,toPairs as t}from"es-toolkit/compat";const
|
|
1
|
+
import{compact as e,toPairs as t}from"es-toolkit/compat";import{z as n}from"zod/v4-mini";const r=(i={})=>({config:e=>r(e(i)),fetch:async()=>{let r=e([e([i?.BaseURL,i?.Path]).join(`/`),new URLSearchParams(t(i?.Query)).toString()]).join(`?`),a=await i?.GetHeaders?.();return i?.Debug?.(a,r),fetch(r,{method:i?.Method,body:i?.Body,headers:a}).then(async e=>{let t=await(i?.ResponseResolver||(e=>e.json()))(e);return(i.ResponseZod||n.any()).parse(t)}).then(e=>(i?.ResponseHandlers?.map(t=>t(e)),e)).catch(e=>{throw i?.ErrorHandlers?.map(t=>t(e)),e})}}),i=e=>e;export{r as ApiKit,i as ApiKitDefineConfigResolver};
|
package/package.json
CHANGED
package/src/core.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { compact, merge, toPairs } from 'es-toolkit/compat';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export const ApiKit = (originConfig?: ApiKitConfig) => {
|
|
1
|
+
import { cloneDeep, compact, merge, toPairs } from 'es-toolkit/compat';
|
|
2
|
+
import { z } from 'zod/v4-mini';
|
|
3
|
+
import { ApiKitConfig, TApiKit } from './types';
|
|
5
4
|
|
|
5
|
+
export const ApiKit = <ResponseType = any>(originConfig: ApiKitConfig<ResponseType> = {}): TApiKit<ResponseType> => {
|
|
6
6
|
return {
|
|
7
|
-
config: (configResolver
|
|
7
|
+
config: (configResolver) => {
|
|
8
8
|
return ApiKit(configResolver(originConfig));
|
|
9
9
|
},
|
|
10
10
|
fetch: async () => {
|
|
@@ -12,15 +12,17 @@ export const ApiKit = (originConfig?: ApiKitConfig) => {
|
|
|
12
12
|
const withQueryUrl = compact([finalUrl, new URLSearchParams(toPairs(originConfig?.Query)).toString()]).join('?');
|
|
13
13
|
const headers = await originConfig?.GetHeaders?.();
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
originConfig?.Debug?.(headers, withQueryUrl);
|
|
16
16
|
|
|
17
17
|
return fetch(withQueryUrl, {
|
|
18
18
|
method: originConfig?.Method,
|
|
19
19
|
body: originConfig?.Body,
|
|
20
20
|
headers,
|
|
21
|
-
}).then(r => {
|
|
21
|
+
}).then(async r => {
|
|
22
22
|
const handler = originConfig?.ResponseResolver || ((r) => r.json());
|
|
23
|
-
|
|
23
|
+
const result = await handler(r);
|
|
24
|
+
const zod = originConfig.ResponseZod || z.any();
|
|
25
|
+
return zod.parse(result);
|
|
24
26
|
}).then(r => {
|
|
25
27
|
originConfig?.ResponseHandlers?.map(handler => handler(r));
|
|
26
28
|
return r;
|
package/src/define.ts
ADDED
package/src/index.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -2,7 +2,14 @@ import { ZodMiniType } from 'zod/v4-mini';
|
|
|
2
2
|
|
|
3
3
|
export type MaybePromise<P> = P | Promise<P>
|
|
4
4
|
|
|
5
|
-
export interface
|
|
5
|
+
export interface TApiKit<ResponseType> {
|
|
6
|
+
config: <NextResponseType>(resolver: ApiKitConfigResolver<ResponseType, NextResponseType>) => TApiKit<NextResponseType>;
|
|
7
|
+
fetch: () => Promise<ResponseType>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type ApiKitConfigResolver<ResponseType, NextResponseType> = (config: ApiKitConfig<ResponseType>) => ApiKitConfig<NextResponseType>
|
|
11
|
+
|
|
12
|
+
export interface ApiKitConfig<ResponseType = any> {
|
|
6
13
|
BaseURL?: string;
|
|
7
14
|
Path?: string;
|
|
8
15
|
Method?: string;
|
|
@@ -10,9 +17,12 @@ export interface ApiKitConfig {
|
|
|
10
17
|
Query?: Record<string, any>;
|
|
11
18
|
GetHeaders?: () => MaybePromise<Record<string, any>>;
|
|
12
19
|
ResponseResolver?: (response: Response) => any;
|
|
13
|
-
ResponseZod?: ZodMiniType
|
|
14
|
-
ResponseHandlers?: ApiKitResponseHandler[];
|
|
15
|
-
ErrorHandlers?:
|
|
20
|
+
ResponseZod?: ZodMiniType<ResponseType>;
|
|
21
|
+
ResponseHandlers?: ApiKitResponseHandler<ResponseType>[];
|
|
22
|
+
ErrorHandlers?: ApiKitErrorHandler[];
|
|
23
|
+
|
|
24
|
+
Debug?: (...messages: any[]) => void;
|
|
16
25
|
}
|
|
17
26
|
|
|
18
|
-
export type ApiKitResponseHandler = (response:
|
|
27
|
+
export type ApiKitResponseHandler<ResponseType> = (response: ResponseType) => void
|
|
28
|
+
export type ApiKitErrorHandler = (error: Error) => void
|