@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 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 ApiKitConfig {
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?: ApiKitResponseHandler[];
18
+ ResponseZod?: ZodMiniType<ResponseType>;
19
+ ResponseHandlers?: ApiKitResponseHandler<ResponseType>[];
20
+ ErrorHandlers?: ApiKitErrorHandler[];
21
+ Debug?: (...messages: any[]) => void;
16
22
  }
17
- type ApiKitResponseHandler = (response: any) => void;
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
- config: (configResolver: (config?: ApiKitConfig) => ApiKitConfig) => /*elided*/any;
22
- fetch: () => Promise<any>;
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 n=r=>({config:e=>n(e(r)),fetch:async()=>{let n=e([e([r?.BaseURL,r?.Path]).join(`/`),new URLSearchParams(t(r?.Query)).toString()]).join(`?`),i=await r?.GetHeaders?.();return console.log(i,n),fetch(n,{method:r?.Method,body:r?.Body,headers:i}).then(e=>(r?.ResponseResolver||(e=>e.json()))(e)).then(e=>(r?.ResponseHandlers?.map(t=>t(e)),e)).catch(e=>{throw r?.ErrorHandlers?.map(t=>t(e)),e})}});export{n as ApiKit};
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sokutils/api-kit",
3
- "version": "0.0.0-beta.0",
3
+ "version": "0.0.0-beta.1",
4
4
  "description": "Api kit",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",
package/src/core.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { compact, merge, toPairs } from 'es-toolkit/compat';
2
- import { ApiKitConfig } from './types';
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: (config?: ApiKitConfig) => ApiKitConfig) => {
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
- console.log(headers, withQueryUrl);
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
- return handler(r);
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
@@ -0,0 +1,5 @@
1
+ import { ApiKitConfigResolver } from './types';
2
+
3
+ export const ApiKitDefineConfigResolver = <ResponseType, NextResponseType>(resolver: ApiKitConfigResolver<ResponseType, NextResponseType>) => {
4
+ return resolver;
5
+ };
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './core';
2
2
  export * from './types';
3
+ export * from './define';
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 ApiKitConfig {
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?: ApiKitResponseHandler[];
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: any) => void
27
+ export type ApiKitResponseHandler<ResponseType> = (response: ResponseType) => void
28
+ export type ApiKitErrorHandler = (error: Error) => void