better-call 1.0.24 → 1.0.25-beta.3
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/client.cjs.map +1 -1
- package/dist/client.d.cts +2 -2
- package/dist/client.d.ts +2 -2
- package/dist/client.js.map +1 -1
- package/dist/index.cjs +42 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +42 -35
- package/dist/index.js.map +1 -1
- package/dist/node.d.cts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/{router-BFJOSfmb.d.cts → router-CC_up1_c.d.ts} +44 -11
- package/dist/{router-D0KeMgM6.d.ts → router-DOTL_ENw.d.cts} +44 -11
- package/package.json +1 -1
package/dist/client.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.cjs","names":["options"],"sources":["../src/client.ts"],"sourcesContent":["import { type BetterFetchOption, type BetterFetchResponse, createFetch } from \"@better-fetch/fetch\";\nimport type { Router } from \"./router\";\nimport type { HasRequiredKeys, Prettify, UnionToIntersection } from \"./helper\";\nimport type { Endpoint } from \"./endpoint\";\n\ntype HasRequired<\n\tT extends {\n\t\tbody?: any;\n\t\tquery?: any;\n\t\tparams?: any;\n\t},\n> =
|
|
1
|
+
{"version":3,"file":"client.cjs","names":["options"],"sources":["../src/client.ts"],"sourcesContent":["import { type BetterFetchOption, type BetterFetchResponse, createFetch } from \"@better-fetch/fetch\";\nimport type { Router } from \"./router\";\nimport type { HasRequiredKeys, Prettify, UnionToIntersection } from \"./helper\";\nimport type { Endpoint } from \"./endpoint\";\n\ntype HasRequired<\n\tT extends {\n\t\tbody?: any;\n\t\tquery?: any;\n\t\tparams?: any;\n\t},\n> = T[\"body\"] extends object\n\t? HasRequiredKeys<T[\"body\"]> extends true\n\t\t? true\n\t\t: T[\"query\"] extends object\n\t\t\t? HasRequiredKeys<T[\"query\"]> extends true\n\t\t\t\t? true\n\t\t\t\t: T[\"params\"] extends object\n\t\t\t\t\t? HasRequiredKeys<T[\"params\"]>\n\t\t\t\t\t: false\n\t\t\t: T[\"params\"] extends object\n\t\t\t\t? HasRequiredKeys<T[\"params\"]>\n\t\t\t\t: false\n\t: T[\"query\"] extends object\n\t\t? HasRequiredKeys<T[\"query\"]> extends true\n\t\t\t? true\n\t\t\t: T[\"params\"] extends object\n\t\t\t\t? HasRequiredKeys<T[\"params\"]>\n\t\t\t\t: false\n\t\t: T[\"params\"] extends object\n\t\t\t? HasRequiredKeys<T[\"params\"]>\n\t\t\t: false;\n\ntype InferContext<T> = T extends (ctx: infer Ctx) => any\n\t? Ctx extends object\n\t\t? Ctx\n\t\t: never\n\t: never;\n\nexport interface ClientOptions extends BetterFetchOption {\n\tbaseURL: string;\n}\n\ntype WithRequired<T, K> = T & {\n\t[P in K extends string ? K : never]-?: T[P extends keyof T ? P : never];\n};\n\ntype WithoutServerOnly<T extends Record<string, Endpoint>> = {\n\t[K in keyof T]: T[K] extends Endpoint<any, infer O>\n\t\t? O extends { metadata: { SERVER_ONLY: true } }\n\t\t\t? never\n\t\t\t: T[K]\n\t\t: T[K];\n};\n\nexport type RequiredOptionKeys<\n\tC extends {\n\t\tbody?: any;\n\t\tquery?: any;\n\t\tparams?: any;\n\t},\n> = (undefined extends C[\"body\"]\n\t? {}\n\t: {\n\t\t\tbody: true;\n\t\t}) &\n\t(undefined extends C[\"query\"]\n\t\t? {}\n\t\t: {\n\t\t\t\tquery: true;\n\t\t\t}) &\n\t(undefined extends C[\"params\"]\n\t\t? {}\n\t\t: {\n\t\t\t\tparams: true;\n\t\t\t});\n\nexport const createClient = <R extends Router | Router[\"endpoints\"]>(options: ClientOptions) => {\n\tconst fetch = createFetch(options);\n\ttype API = WithoutServerOnly<\n\t\tR extends { endpoints: Record<string, Endpoint> } ? R[\"endpoints\"] : R\n\t>;\n\ttype Options = API extends {\n\t\t[key: string]: infer T;\n\t}\n\t\t? T extends Endpoint\n\t\t\t? {\n\t\t\t\t\t[key in T[\"options\"][\"method\"] extends \"GET\"\n\t\t\t\t\t\t? T[\"path\"]\n\t\t\t\t\t\t: `@${T[\"options\"][\"method\"] extends string ? Lowercase<T[\"options\"][\"method\"]> : never}${T[\"path\"]}`]: T;\n\t\t\t\t}\n\t\t\t: {}\n\t\t: {};\n\n\ttype O = Prettify<UnionToIntersection<Options>>;\n\treturn async <OPT extends O, K extends keyof OPT, C extends InferContext<OPT[K]>>(\n\t\tpath: K,\n\t\t...options: HasRequired<C> extends true\n\t\t\t? [\n\t\t\t\t\tWithRequired<\n\t\t\t\t\t\tBetterFetchOption<C[\"body\"], C[\"query\"], C[\"params\"]>,\n\t\t\t\t\t\tkeyof RequiredOptionKeys<C>\n\t\t\t\t\t>,\n\t\t\t\t]\n\t\t\t: [BetterFetchOption<C[\"body\"], C[\"query\"], C[\"params\"]>?]\n\t): Promise<\n\t\tBetterFetchResponse<Awaited<ReturnType<OPT[K] extends Endpoint ? OPT[K] : never>>>\n\t> => {\n\t\treturn (await fetch(path as string, {\n\t\t\t...options[0],\n\t\t})) as any;\n\t};\n};\n"],"mappings":";;;;;AA6EA,MAAa,gBAAwD,YAA2B;CAC/F,MAAM,8CAAoB,QAAQ;AAiBlC,QAAO,OACN,MACA,GAAGA,cAUC;AACJ,SAAQ,MAAM,MAAM,MAAgB,EACnC,GAAGA,UAAQ,IACX,CAAC"}
|
package/dist/client.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Endpoint, HasRequiredKeys, Router, UnionToIntersection } from "./router-
|
|
1
|
+
import { Endpoint, HasRequiredKeys, Router, UnionToIntersection } from "./router-DOTL_ENw.cjs";
|
|
2
2
|
import { BetterFetchOption, BetterFetchResponse } from "@better-fetch/fetch";
|
|
3
3
|
|
|
4
4
|
//#region src/client.d.ts
|
|
@@ -6,7 +6,7 @@ type HasRequired<T extends {
|
|
|
6
6
|
body?: any;
|
|
7
7
|
query?: any;
|
|
8
8
|
params?: any;
|
|
9
|
-
}> =
|
|
9
|
+
}> = T["body"] extends object ? HasRequiredKeys<T["body"]> extends true ? true : T["query"] extends object ? HasRequiredKeys<T["query"]> extends true ? true : T["params"] extends object ? HasRequiredKeys<T["params"]> : false : T["params"] extends object ? HasRequiredKeys<T["params"]> : false : T["query"] extends object ? HasRequiredKeys<T["query"]> extends true ? true : T["params"] extends object ? HasRequiredKeys<T["params"]> : false : T["params"] extends object ? HasRequiredKeys<T["params"]> : false;
|
|
10
10
|
type InferContext<T> = T extends ((ctx: infer Ctx) => any) ? Ctx extends object ? Ctx : never : never;
|
|
11
11
|
interface ClientOptions extends BetterFetchOption {
|
|
12
12
|
baseURL: string;
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Endpoint, HasRequiredKeys, Router, UnionToIntersection } from "./router-
|
|
1
|
+
import { Endpoint, HasRequiredKeys, Router, UnionToIntersection } from "./router-CC_up1_c.js";
|
|
2
2
|
import { BetterFetchOption, BetterFetchResponse } from "@better-fetch/fetch";
|
|
3
3
|
|
|
4
4
|
//#region src/client.d.ts
|
|
@@ -6,7 +6,7 @@ type HasRequired<T extends {
|
|
|
6
6
|
body?: any;
|
|
7
7
|
query?: any;
|
|
8
8
|
params?: any;
|
|
9
|
-
}> =
|
|
9
|
+
}> = T["body"] extends object ? HasRequiredKeys<T["body"]> extends true ? true : T["query"] extends object ? HasRequiredKeys<T["query"]> extends true ? true : T["params"] extends object ? HasRequiredKeys<T["params"]> : false : T["params"] extends object ? HasRequiredKeys<T["params"]> : false : T["query"] extends object ? HasRequiredKeys<T["query"]> extends true ? true : T["params"] extends object ? HasRequiredKeys<T["params"]> : false : T["params"] extends object ? HasRequiredKeys<T["params"]> : false;
|
|
10
10
|
type InferContext<T> = T extends ((ctx: infer Ctx) => any) ? Ctx extends object ? Ctx : never : never;
|
|
11
11
|
interface ClientOptions extends BetterFetchOption {
|
|
12
12
|
baseURL: string;
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","names":["options"],"sources":["../src/client.ts"],"sourcesContent":["import { type BetterFetchOption, type BetterFetchResponse, createFetch } from \"@better-fetch/fetch\";\nimport type { Router } from \"./router\";\nimport type { HasRequiredKeys, Prettify, UnionToIntersection } from \"./helper\";\nimport type { Endpoint } from \"./endpoint\";\n\ntype HasRequired<\n\tT extends {\n\t\tbody?: any;\n\t\tquery?: any;\n\t\tparams?: any;\n\t},\n> =
|
|
1
|
+
{"version":3,"file":"client.js","names":["options"],"sources":["../src/client.ts"],"sourcesContent":["import { type BetterFetchOption, type BetterFetchResponse, createFetch } from \"@better-fetch/fetch\";\nimport type { Router } from \"./router\";\nimport type { HasRequiredKeys, Prettify, UnionToIntersection } from \"./helper\";\nimport type { Endpoint } from \"./endpoint\";\n\ntype HasRequired<\n\tT extends {\n\t\tbody?: any;\n\t\tquery?: any;\n\t\tparams?: any;\n\t},\n> = T[\"body\"] extends object\n\t? HasRequiredKeys<T[\"body\"]> extends true\n\t\t? true\n\t\t: T[\"query\"] extends object\n\t\t\t? HasRequiredKeys<T[\"query\"]> extends true\n\t\t\t\t? true\n\t\t\t\t: T[\"params\"] extends object\n\t\t\t\t\t? HasRequiredKeys<T[\"params\"]>\n\t\t\t\t\t: false\n\t\t\t: T[\"params\"] extends object\n\t\t\t\t? HasRequiredKeys<T[\"params\"]>\n\t\t\t\t: false\n\t: T[\"query\"] extends object\n\t\t? HasRequiredKeys<T[\"query\"]> extends true\n\t\t\t? true\n\t\t\t: T[\"params\"] extends object\n\t\t\t\t? HasRequiredKeys<T[\"params\"]>\n\t\t\t\t: false\n\t\t: T[\"params\"] extends object\n\t\t\t? HasRequiredKeys<T[\"params\"]>\n\t\t\t: false;\n\ntype InferContext<T> = T extends (ctx: infer Ctx) => any\n\t? Ctx extends object\n\t\t? Ctx\n\t\t: never\n\t: never;\n\nexport interface ClientOptions extends BetterFetchOption {\n\tbaseURL: string;\n}\n\ntype WithRequired<T, K> = T & {\n\t[P in K extends string ? K : never]-?: T[P extends keyof T ? P : never];\n};\n\ntype WithoutServerOnly<T extends Record<string, Endpoint>> = {\n\t[K in keyof T]: T[K] extends Endpoint<any, infer O>\n\t\t? O extends { metadata: { SERVER_ONLY: true } }\n\t\t\t? never\n\t\t\t: T[K]\n\t\t: T[K];\n};\n\nexport type RequiredOptionKeys<\n\tC extends {\n\t\tbody?: any;\n\t\tquery?: any;\n\t\tparams?: any;\n\t},\n> = (undefined extends C[\"body\"]\n\t? {}\n\t: {\n\t\t\tbody: true;\n\t\t}) &\n\t(undefined extends C[\"query\"]\n\t\t? {}\n\t\t: {\n\t\t\t\tquery: true;\n\t\t\t}) &\n\t(undefined extends C[\"params\"]\n\t\t? {}\n\t\t: {\n\t\t\t\tparams: true;\n\t\t\t});\n\nexport const createClient = <R extends Router | Router[\"endpoints\"]>(options: ClientOptions) => {\n\tconst fetch = createFetch(options);\n\ttype API = WithoutServerOnly<\n\t\tR extends { endpoints: Record<string, Endpoint> } ? R[\"endpoints\"] : R\n\t>;\n\ttype Options = API extends {\n\t\t[key: string]: infer T;\n\t}\n\t\t? T extends Endpoint\n\t\t\t? {\n\t\t\t\t\t[key in T[\"options\"][\"method\"] extends \"GET\"\n\t\t\t\t\t\t? T[\"path\"]\n\t\t\t\t\t\t: `@${T[\"options\"][\"method\"] extends string ? Lowercase<T[\"options\"][\"method\"]> : never}${T[\"path\"]}`]: T;\n\t\t\t\t}\n\t\t\t: {}\n\t\t: {};\n\n\ttype O = Prettify<UnionToIntersection<Options>>;\n\treturn async <OPT extends O, K extends keyof OPT, C extends InferContext<OPT[K]>>(\n\t\tpath: K,\n\t\t...options: HasRequired<C> extends true\n\t\t\t? [\n\t\t\t\t\tWithRequired<\n\t\t\t\t\t\tBetterFetchOption<C[\"body\"], C[\"query\"], C[\"params\"]>,\n\t\t\t\t\t\tkeyof RequiredOptionKeys<C>\n\t\t\t\t\t>,\n\t\t\t\t]\n\t\t\t: [BetterFetchOption<C[\"body\"], C[\"query\"], C[\"params\"]>?]\n\t): Promise<\n\t\tBetterFetchResponse<Awaited<ReturnType<OPT[K] extends Endpoint ? OPT[K] : never>>>\n\t> => {\n\t\treturn (await fetch(path as string, {\n\t\t\t...options[0],\n\t\t})) as any;\n\t};\n};\n"],"mappings":";;;AA6EA,MAAa,gBAAwD,YAA2B;CAC/F,MAAM,QAAQ,YAAY,QAAQ;AAiBlC,QAAO,OACN,MACA,GAAGA,cAUC;AACJ,SAAQ,MAAM,MAAM,MAAgB,EACnC,GAAGA,UAAQ,IACX,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -117,6 +117,12 @@ var InternalAPIError = class extends Error {
|
|
|
117
117
|
} : void 0;
|
|
118
118
|
}
|
|
119
119
|
};
|
|
120
|
+
var BetterCallError = class extends Error {
|
|
121
|
+
constructor(message) {
|
|
122
|
+
super(message);
|
|
123
|
+
this.name = "BetterCallError";
|
|
124
|
+
}
|
|
125
|
+
};
|
|
120
126
|
const APIError = makeErrorForHideStackFrame(InternalAPIError, Error);
|
|
121
127
|
|
|
122
128
|
//#endregion
|
|
@@ -498,43 +504,10 @@ const createInternalContext = async (context, { options, path }) => {
|
|
|
498
504
|
return internalContext;
|
|
499
505
|
};
|
|
500
506
|
|
|
501
|
-
//#endregion
|
|
502
|
-
//#region src/middleware.ts
|
|
503
|
-
function createMiddleware(optionsOrHandler, handler) {
|
|
504
|
-
const internalHandler = async (inputCtx) => {
|
|
505
|
-
const context = inputCtx;
|
|
506
|
-
const _handler = typeof optionsOrHandler === "function" ? optionsOrHandler : handler;
|
|
507
|
-
const internalContext = await createInternalContext(context, {
|
|
508
|
-
options: typeof optionsOrHandler === "function" ? {} : optionsOrHandler,
|
|
509
|
-
path: "/"
|
|
510
|
-
});
|
|
511
|
-
if (!_handler) throw new Error("handler must be defined");
|
|
512
|
-
const response = await _handler(internalContext);
|
|
513
|
-
const headers = internalContext.responseHeaders;
|
|
514
|
-
return context.returnHeaders ? {
|
|
515
|
-
headers,
|
|
516
|
-
response
|
|
517
|
-
} : response;
|
|
518
|
-
};
|
|
519
|
-
internalHandler.options = typeof optionsOrHandler === "function" ? {} : optionsOrHandler;
|
|
520
|
-
return internalHandler;
|
|
521
|
-
}
|
|
522
|
-
createMiddleware.create = (opts) => {
|
|
523
|
-
function fn(optionsOrHandler, handler) {
|
|
524
|
-
if (typeof optionsOrHandler === "function") return createMiddleware({ use: opts?.use }, optionsOrHandler);
|
|
525
|
-
if (!handler) throw new Error("Middleware handler is required");
|
|
526
|
-
return createMiddleware({
|
|
527
|
-
...optionsOrHandler,
|
|
528
|
-
method: "*",
|
|
529
|
-
use: [...opts?.use || [], ...optionsOrHandler.use || []]
|
|
530
|
-
}, handler);
|
|
531
|
-
}
|
|
532
|
-
return fn;
|
|
533
|
-
};
|
|
534
|
-
|
|
535
507
|
//#endregion
|
|
536
508
|
//#region src/endpoint.ts
|
|
537
509
|
const createEndpoint = (path, options, handler) => {
|
|
510
|
+
if ((options.method === "GET" || options.method === "HEAD") && options.body) throw new BetterCallError("Body is not allowed with GET or HEAD methods");
|
|
538
511
|
const internalHandler = async (...inputCtx) => {
|
|
539
512
|
const context = inputCtx[0] || {};
|
|
540
513
|
const internalContext = await createInternalContext(context, {
|
|
@@ -568,6 +541,40 @@ createEndpoint.create = (opts) => {
|
|
|
568
541
|
};
|
|
569
542
|
};
|
|
570
543
|
|
|
544
|
+
//#endregion
|
|
545
|
+
//#region src/middleware.ts
|
|
546
|
+
function createMiddleware(optionsOrHandler, handler) {
|
|
547
|
+
const internalHandler = async (inputCtx) => {
|
|
548
|
+
const context = inputCtx;
|
|
549
|
+
const _handler = typeof optionsOrHandler === "function" ? optionsOrHandler : handler;
|
|
550
|
+
const internalContext = await createInternalContext(context, {
|
|
551
|
+
options: typeof optionsOrHandler === "function" ? {} : optionsOrHandler,
|
|
552
|
+
path: "/"
|
|
553
|
+
});
|
|
554
|
+
if (!_handler) throw new Error("handler must be defined");
|
|
555
|
+
const response = await _handler(internalContext);
|
|
556
|
+
const headers = internalContext.responseHeaders;
|
|
557
|
+
return context.returnHeaders ? {
|
|
558
|
+
headers,
|
|
559
|
+
response
|
|
560
|
+
} : response;
|
|
561
|
+
};
|
|
562
|
+
internalHandler.options = typeof optionsOrHandler === "function" ? {} : optionsOrHandler;
|
|
563
|
+
return internalHandler;
|
|
564
|
+
}
|
|
565
|
+
createMiddleware.create = (opts) => {
|
|
566
|
+
function fn(optionsOrHandler, handler) {
|
|
567
|
+
if (typeof optionsOrHandler === "function") return createMiddleware({ use: opts?.use }, optionsOrHandler);
|
|
568
|
+
if (!handler) throw new Error("Middleware handler is required");
|
|
569
|
+
return createMiddleware({
|
|
570
|
+
...optionsOrHandler,
|
|
571
|
+
method: "*",
|
|
572
|
+
use: [...opts?.use || [], ...optionsOrHandler.use || []]
|
|
573
|
+
}, handler);
|
|
574
|
+
}
|
|
575
|
+
return fn;
|
|
576
|
+
};
|
|
577
|
+
|
|
571
578
|
//#endregion
|
|
572
579
|
//#region node_modules/.pnpm/zod@4.0.1/node_modules/zod/v4/core/core.js
|
|
573
580
|
/** A special constant with type `never` */
|
|
@@ -2521,6 +2528,7 @@ const createRouter = (endpoints, config$1) => {
|
|
|
2521
2528
|
|
|
2522
2529
|
//#endregion
|
|
2523
2530
|
exports.APIError = APIError;
|
|
2531
|
+
exports.BetterCallError = BetterCallError;
|
|
2524
2532
|
exports._statusCode = _statusCode;
|
|
2525
2533
|
exports.createEndpoint = createEndpoint;
|
|
2526
2534
|
exports.createInternalContext = createInternalContext;
|