@sokutils/api-kit 0.0.0-beta.0

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.
@@ -0,0 +1,25 @@
1
+ import { ZodMiniType } from "zod/v4-mini";
2
+
3
+ //#region src/types.d.ts
4
+ type MaybePromise<P> = P | Promise<P>;
5
+ interface ApiKitConfig {
6
+ BaseURL?: string;
7
+ Path?: string;
8
+ Method?: string;
9
+ Body?: string;
10
+ Query?: Record<string, any>;
11
+ GetHeaders?: () => MaybePromise<Record<string, any>>;
12
+ ResponseResolver?: (response: Response) => any;
13
+ ResponseZod?: ZodMiniType;
14
+ ResponseHandlers?: ApiKitResponseHandler[];
15
+ ErrorHandlers?: ApiKitResponseHandler[];
16
+ }
17
+ type ApiKitResponseHandler = (response: any) => void;
18
+ //#endregion
19
+ //#region src/core.d.ts
20
+ declare const ApiKit: (originConfig?: ApiKitConfig) => {
21
+ config: (configResolver: (config?: ApiKitConfig) => ApiKitConfig) => /*elided*/any;
22
+ fetch: () => Promise<any>;
23
+ };
24
+ //#endregion
25
+ export { ApiKit, ApiKitConfig, ApiKitResponseHandler, MaybePromise };
package/dist/index.js ADDED
@@ -0,0 +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};
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@sokutils/api-kit",
3
+ "version": "0.0.0-beta.0",
4
+ "description": "Api kit",
5
+ "type": "module",
6
+ "types": "dist/index.d.ts",
7
+ "main": "dist/index.js",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "keywords": [
15
+ "cache",
16
+ "memoization",
17
+ "lru",
18
+ "typescript"
19
+ ],
20
+ "author": "",
21
+ "license": "ISC",
22
+ "dependencies": {
23
+ "es-toolkit": "1.49.0",
24
+ "zod": "4.4.3"
25
+ },
26
+ "devDependencies": {
27
+ "typescript": "6.0.2",
28
+ "vitest": "^3.2.4"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/SokuRitszZ/sokutils.git",
33
+ "directory": "packages/api-kit"
34
+ },
35
+ "scripts": {
36
+ "build": "tsdown",
37
+ "dev": "tsdown --watch",
38
+ "test:dev": "vitest",
39
+ "test": "vitest run"
40
+ }
41
+ }
package/src/core.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { compact, merge, toPairs } from 'es-toolkit/compat';
2
+ import { ApiKitConfig } from './types';
3
+
4
+ export const ApiKit = (originConfig?: ApiKitConfig) => {
5
+
6
+ return {
7
+ config: (configResolver: (config?: ApiKitConfig) => ApiKitConfig) => {
8
+ return ApiKit(configResolver(originConfig));
9
+ },
10
+ fetch: async () => {
11
+ const finalUrl = compact([originConfig?.BaseURL, originConfig?.Path]).join('/');
12
+ const withQueryUrl = compact([finalUrl, new URLSearchParams(toPairs(originConfig?.Query)).toString()]).join('?');
13
+ const headers = await originConfig?.GetHeaders?.();
14
+
15
+ console.log(headers, withQueryUrl);
16
+
17
+ return fetch(withQueryUrl, {
18
+ method: originConfig?.Method,
19
+ body: originConfig?.Body,
20
+ headers,
21
+ }).then(r => {
22
+ const handler = originConfig?.ResponseResolver || ((r) => r.json());
23
+ return handler(r);
24
+ }).then(r => {
25
+ originConfig?.ResponseHandlers?.map(handler => handler(r));
26
+ return r;
27
+ }).catch(e => {
28
+ originConfig?.ErrorHandlers?.map(handler => handler(e));
29
+ throw e;
30
+ });
31
+ },
32
+ };
33
+ };
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './core';
2
+ export * from './types';
package/src/types.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { ZodMiniType } from 'zod/v4-mini';
2
+
3
+ export type MaybePromise<P> = P | Promise<P>
4
+
5
+ export interface ApiKitConfig {
6
+ BaseURL?: string;
7
+ Path?: string;
8
+ Method?: string;
9
+ Body?: string;
10
+ Query?: Record<string, any>;
11
+ GetHeaders?: () => MaybePromise<Record<string, any>>;
12
+ ResponseResolver?: (response: Response) => any;
13
+ ResponseZod?: ZodMiniType
14
+ ResponseHandlers?: ApiKitResponseHandler[];
15
+ ErrorHandlers?: ApiKitResponseHandler[];
16
+ }
17
+
18
+ export type ApiKitResponseHandler = (response: any) => void
package/tsconfig.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": ["../../configs/tsconfig.node.json"],
3
+ "include": ["src"],
4
+ }
@@ -0,0 +1,6 @@
1
+ import { defineConfig, mergeConfig } from 'tsdown';
2
+ import commonLibConfig from '../../configs/tsdown-purelib.config.mts';
3
+
4
+ export default defineConfig(
5
+ mergeConfig(commonLibConfig, {}),
6
+ );
@@ -0,0 +1,6 @@
1
+ import { defineConfig, mergeConfig } from 'vitest/config';
2
+ import commonLibConfig from '../../configs/vitest.config.ts';
3
+
4
+ export default defineConfig(
5
+ mergeConfig(commonLibConfig, {}),
6
+ );