better-call 0.2.13-beta.8 → 0.2.14-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/README.md +378 -2
- package/dist/client.cjs +33 -8
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +3 -7
- package/dist/client.d.ts +3 -7
- package/dist/client.js +14 -0
- package/dist/client.js.map +1 -0
- package/dist/index.cjs +524 -393
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +115 -20
- package/dist/index.d.ts +115 -20
- package/dist/{index.mjs → index.js} +463 -368
- package/dist/index.js.map +1 -0
- package/dist/router-Bn7zn81P.d.cts +342 -0
- package/dist/router-Bn7zn81P.d.ts +342 -0
- package/package.json +24 -28
- package/dist/adapter/node.cjs +0 -210
- package/dist/adapter/node.d.cts +0 -19
- package/dist/adapter/node.d.mts +0 -19
- package/dist/adapter/node.d.ts +0 -19
- package/dist/adapter/node.mjs +0 -206
- package/dist/client.d.mts +0 -42
- package/dist/client.mjs +0 -12
- package/dist/index.d.mts +0 -32
- package/dist/shared/better-call.25f0dd59.d.cts +0 -677
- package/dist/shared/better-call.25f0dd59.d.mts +0 -677
- package/dist/shared/better-call.25f0dd59.d.ts +0 -677
package/README.md
CHANGED
|
@@ -1,9 +1,385 @@
|
|
|
1
|
-
#
|
|
1
|
+
# better-call
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Better call is a tiny web framework for creating endpoints that can be invoked as a normal function or mounted to a router to be served by any web standard compatible server (like Bun, node, nextjs, sveltekit...) and also includes a typed RPC client for typesafe client-side invocation of these endpoints.
|
|
4
|
+
|
|
5
|
+
Built for typescript and it comes with a very high performance router based on [rou3](https://github.com/unjs/rou3).
|
|
6
|
+
|
|
7
|
+
> ⚠️ This project in development and not ready for production use. But feel free to try it out and give feedback.
|
|
4
8
|
|
|
5
9
|
## Install
|
|
6
10
|
|
|
7
11
|
```bash
|
|
8
12
|
pnpm i better-call
|
|
9
13
|
```
|
|
14
|
+
|
|
15
|
+
make sure to install zod if you haven't
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm i zod
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
The building blocks for better-call are endpoints. You can create an endpoint by calling `createEndpoint` and passing it a path, [options](#endpointoptions) and a handler that will be invoked when the endpoint is called.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { createEndpoint, createRouter } from "better-call"
|
|
27
|
+
import { z } from "zod"
|
|
28
|
+
|
|
29
|
+
const createItem = createEndpoint("/item", {
|
|
30
|
+
method: "POST",
|
|
31
|
+
body: z.object({
|
|
32
|
+
id: z.string()
|
|
33
|
+
})
|
|
34
|
+
}, async (ctx) => {
|
|
35
|
+
return {
|
|
36
|
+
item: {
|
|
37
|
+
id: ctx.body.id
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
// Now you can call the endpoint just as a normal function.
|
|
43
|
+
const item = await createItem({
|
|
44
|
+
body: {
|
|
45
|
+
id: "123"
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
OR you can mount the endpoint to a router and serve it with any web standard compatible server.
|
|
51
|
+
|
|
52
|
+
> The example below uses [Bun](https://bun.sh/)
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const router = createRouter({
|
|
56
|
+
createItem
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
Bun.serve({
|
|
60
|
+
fetch: router.handler
|
|
61
|
+
})
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Then you can use the rpc client to call the endpoints on client.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
//client.ts
|
|
68
|
+
import { createClient } from "better-call/client";
|
|
69
|
+
|
|
70
|
+
const client = createClient<typeof router>();
|
|
71
|
+
const items = await client("/item", {
|
|
72
|
+
body: {
|
|
73
|
+
id: "123"
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Returning non 200 responses
|
|
79
|
+
|
|
80
|
+
To return a non 200 response, you will need to throw Better Call's `APIError` error. If the endpoint is called as a function, the error will be thrown but if it's mounted to a router, the error will be converted to a response object with the correct status code and headers.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
const createItem = createEndpoint("/item", {
|
|
84
|
+
method: "POST",
|
|
85
|
+
body: z.object({
|
|
86
|
+
id: z.string()
|
|
87
|
+
})
|
|
88
|
+
}, async (ctx) => {
|
|
89
|
+
if(ctx.body.id === "123") {
|
|
90
|
+
throw new APIError("Bad Request", {
|
|
91
|
+
message: "Id is not allowed"
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
item: {
|
|
96
|
+
id: ctx.body.id
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Endpoint
|
|
103
|
+
|
|
104
|
+
Endpoints are building blocks of better-call.
|
|
105
|
+
|
|
106
|
+
#### Path
|
|
107
|
+
|
|
108
|
+
The path is the URL path that the endpoint will respond to. It can be a direct path or a path with parameters and wildcards.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
//direct path
|
|
112
|
+
const endpoint = createEndpoint("/item", {
|
|
113
|
+
method: "GET",
|
|
114
|
+
}, async (ctx) => {})
|
|
115
|
+
|
|
116
|
+
//path with parameters
|
|
117
|
+
const endpoint = createEndpoint("/item/:id", {
|
|
118
|
+
method: "GET",
|
|
119
|
+
}, async (ctx) => {
|
|
120
|
+
return {
|
|
121
|
+
item: {
|
|
122
|
+
id: ctx.params.id
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
//path with wildcards
|
|
128
|
+
const endpoint = createEndpoint("/item/**:name", {
|
|
129
|
+
method: "GET",
|
|
130
|
+
}, async (ctx) => {
|
|
131
|
+
//the name will be the remaining path
|
|
132
|
+
ctx.params.name
|
|
133
|
+
})
|
|
134
|
+
```
|
|
135
|
+
#### Body Schema
|
|
136
|
+
|
|
137
|
+
The `body` option accepts a zod schema and will validate the request body. If the request body doesn't match the schema, the endpoint will throw an error. If it's mounted to a router, it'll return a 400 error.
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
const createItem = createEndpoint("/item", {
|
|
141
|
+
method: "POST",
|
|
142
|
+
body: z.object({
|
|
143
|
+
id: z.string()
|
|
144
|
+
})
|
|
145
|
+
}, async (ctx) => {
|
|
146
|
+
return {
|
|
147
|
+
item: {
|
|
148
|
+
id: ctx.body.id
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
#### Query Schema
|
|
155
|
+
|
|
156
|
+
The `query` option accepts a zod schema and will validate the request query. If the request query doesn't match the schema, the endpoint will throw an error. If it's mounted to a router, it'll return a 400 error.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
const createItem = createEndpoint("/item", {
|
|
160
|
+
method: "GET",
|
|
161
|
+
query: z.object({
|
|
162
|
+
id: z.string()
|
|
163
|
+
})
|
|
164
|
+
}, async (ctx) => {
|
|
165
|
+
return {
|
|
166
|
+
item: {
|
|
167
|
+
id: ctx.query.id
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
#### Require Headers
|
|
174
|
+
|
|
175
|
+
The `requireHeaders` option is used to require the request to have headers. If the request doesn't have headers, the endpoint will throw an error. And even when you call the endpoint as a function, it will require headers to be passed in the context.
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
const createItem = createEndpoint("/item", {
|
|
179
|
+
method: "GET",
|
|
180
|
+
requireHeaders: true
|
|
181
|
+
}, async (ctx) => {
|
|
182
|
+
return {
|
|
183
|
+
item: {
|
|
184
|
+
id: ctx.headers.get("id")
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
createItem({
|
|
189
|
+
headers: new Headers()
|
|
190
|
+
})
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
#### Require Request
|
|
194
|
+
|
|
195
|
+
The `requireRequest` option is used to require the request to have a request object. If the request doesn't have a request object, the endpoint will throw an error. And even when you call the endpoint as a function, it will require a request to be passed in the context.
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
const createItem = createEndpoint("/item", {
|
|
199
|
+
method: "GET",
|
|
200
|
+
requireRequest: true
|
|
201
|
+
}, async (ctx) => {
|
|
202
|
+
return {
|
|
203
|
+
item: {
|
|
204
|
+
id: ctx.request.id
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
createItem({
|
|
210
|
+
request: new Request()
|
|
211
|
+
})
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
### Handler
|
|
216
|
+
|
|
217
|
+
this is the function that will be invoked when the endpoint is called. It accepts a context object that contains the request, headers, body, query, params and other information.
|
|
218
|
+
|
|
219
|
+
It can return a response object, a string, a boolean, a number, an object with a status, body, headers and other properties or undefined.
|
|
220
|
+
|
|
221
|
+
If you return a response object, it will be returned as is even when it's mounted to a router.
|
|
222
|
+
|
|
223
|
+
It can also throw an error and if it throws APIError, it will be converted to a response object with the correct status code and headers.
|
|
224
|
+
|
|
225
|
+
- **Context**: the context object contains the request, headers, body, query, params and a helper function to set headers, cookies and get cookies. If there is a middleware, the context will be extended with the middleware context.
|
|
226
|
+
|
|
227
|
+
### Middleware
|
|
228
|
+
|
|
229
|
+
Endpoints can use middleware by passing the `use` option to the endpoint. To create a middleware, you can call `createMiddleware` and pass it a function or an options object and a handler function.
|
|
230
|
+
|
|
231
|
+
If you return a context object from the middleware, it will be available in the endpoint context.
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
const middleware = createMiddleware(async (ctx) => {
|
|
235
|
+
return {
|
|
236
|
+
name: "hello"
|
|
237
|
+
}
|
|
238
|
+
})
|
|
239
|
+
const endpoint = createEndpoint("/", {
|
|
240
|
+
method: "GET",
|
|
241
|
+
use: [middleware],
|
|
242
|
+
}, async (ctx) => {
|
|
243
|
+
//this will be the context object returned by the middleware with the name property
|
|
244
|
+
ctx.context
|
|
245
|
+
})
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
You can also pass an options object to the middleware and a handler function.
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
const middleware = createMiddleware({
|
|
252
|
+
body: z.object({
|
|
253
|
+
name: z.string()
|
|
254
|
+
})
|
|
255
|
+
}, async (ctx) => {
|
|
256
|
+
return {
|
|
257
|
+
name: "hello"
|
|
258
|
+
}
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
const endpoint = createEndpoint("/", {
|
|
262
|
+
method: "GET",
|
|
263
|
+
use: [middleware],
|
|
264
|
+
}, async (ctx) => {
|
|
265
|
+
//the body will also contain the middleware body
|
|
266
|
+
ctx.body
|
|
267
|
+
})
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Router
|
|
271
|
+
|
|
272
|
+
You can create a router by calling `createRouter` and passing it an array of endpoints. It returns a router object that has a `handler` method that can be used to serve the endpoints.
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
import { createRouter } from "better-call"
|
|
276
|
+
import { createItem } from "./item"
|
|
277
|
+
|
|
278
|
+
const router = createRouter({
|
|
279
|
+
createItem
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
Bun.serve({
|
|
283
|
+
fetch: router.handler
|
|
284
|
+
})
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Behind the scenes, the router uses [rou3](https://github.com/unjs/rou3) to match the endpoints and invoke the correct endpoint. You can look at the [rou3 documentation](https://github.com/unjs/rou3) for more information.
|
|
288
|
+
|
|
289
|
+
#### Router Options
|
|
290
|
+
|
|
291
|
+
**routerMiddleware:**
|
|
292
|
+
|
|
293
|
+
A router middleware is similar to an endpoint middleware but it's applied to any path that matches the route. It's like any traditional middleware. You have to pass endpoints to the router middleware as an array.
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
const routeMiddleware = createEndpoint("/api/**", {
|
|
297
|
+
method: "GET",
|
|
298
|
+
}, async (ctx) => {
|
|
299
|
+
return {
|
|
300
|
+
name: "hello"
|
|
301
|
+
}
|
|
302
|
+
})
|
|
303
|
+
const router = createRouter({
|
|
304
|
+
createItem
|
|
305
|
+
}, {
|
|
306
|
+
routerMiddleware: [{
|
|
307
|
+
path: "/api/**",
|
|
308
|
+
middleware:routeMiddleware
|
|
309
|
+
}]
|
|
310
|
+
})
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
**basePath**: The base path for the router. All paths will be relative to this path.
|
|
314
|
+
|
|
315
|
+
**onError**: The router will call this function if an error occurs in the middleware or the endpoint.
|
|
316
|
+
|
|
317
|
+
**throwError**: If true, the router will throw an error if an error occurs in the middleware or the endpoint.
|
|
318
|
+
|
|
319
|
+
### RPC Client
|
|
320
|
+
|
|
321
|
+
better-call comes with a rpc client that can be used to call endpoints from the client. The client wraps over better-fetch so you can pass any options that are supported by better-fetch.
|
|
322
|
+
|
|
323
|
+
```ts
|
|
324
|
+
import { createClient } from "better-call/client";
|
|
325
|
+
import { router } from "@serve/router";
|
|
326
|
+
|
|
327
|
+
const client = createClient<typeof router>({
|
|
328
|
+
/**
|
|
329
|
+
* if you add custom path like `http://
|
|
330
|
+
* localhost:3000/api` make sure to add the
|
|
331
|
+
* custom path on the router config as well.
|
|
332
|
+
*/
|
|
333
|
+
baseURL: "http://localhost:3000"
|
|
334
|
+
});
|
|
335
|
+
const items = await client("/item", {
|
|
336
|
+
body: {
|
|
337
|
+
id: "123"
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
```
|
|
341
|
+
> You can also pass object that contains endpoints as a generic type to create client.
|
|
342
|
+
|
|
343
|
+
### Headers and Cookies
|
|
344
|
+
|
|
345
|
+
If you return a response object from an endpoint, the headers and cookies will be set on the response object. But You can set headers and cookies for the context object.
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
const createItem = createEndpoint("/item", {
|
|
349
|
+
method: "POST",
|
|
350
|
+
body: z.object({
|
|
351
|
+
id: z.string()
|
|
352
|
+
})
|
|
353
|
+
}, async (ctx) => {
|
|
354
|
+
ctx.setHeader("X-Custom-Header", "Hello World")
|
|
355
|
+
ctx.setCookie("my-cookie", "hello world")
|
|
356
|
+
return {
|
|
357
|
+
item: {
|
|
358
|
+
id: ctx.body.id
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
})
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
You can also get cookies from the context object.
|
|
365
|
+
|
|
366
|
+
```ts
|
|
367
|
+
const createItem = createEndpoint("/item", {
|
|
368
|
+
method: "POST",
|
|
369
|
+
body: z.object({
|
|
370
|
+
id: z.string()
|
|
371
|
+
})
|
|
372
|
+
}, async (ctx) => {
|
|
373
|
+
const cookie = ctx.getCookie("my-cookie")
|
|
374
|
+
return {
|
|
375
|
+
item: {
|
|
376
|
+
id: ctx.body.id
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
})
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
> other than normal cookies the ctx object also exposes signed cookies.
|
|
383
|
+
|
|
384
|
+
## License
|
|
385
|
+
MIT
|
package/dist/client.cjs
CHANGED
|
@@ -1,14 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
4
19
|
|
|
5
|
-
|
|
6
|
-
|
|
20
|
+
// src/client.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
createClient: () => createClient
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(client_exports);
|
|
26
|
+
var import_fetch = require("@better-fetch/fetch");
|
|
27
|
+
var createClient = (options) => {
|
|
28
|
+
const fetch = (0, import_fetch.createFetch)(options);
|
|
7
29
|
return async (path, ...options2) => {
|
|
8
|
-
return await fetch
|
|
30
|
+
return await fetch(path, {
|
|
9
31
|
...options2[0]
|
|
10
32
|
});
|
|
11
33
|
};
|
|
12
34
|
};
|
|
13
|
-
|
|
14
|
-
exports
|
|
35
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
+
0 && (module.exports = {
|
|
37
|
+
createClient
|
|
38
|
+
});
|
|
39
|
+
//# sourceMappingURL=client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["import type { Endpoint, Prettify } from \"./types\";\n\nimport { type BetterFetchOption, type BetterFetchResponse, createFetch } from \"@better-fetch/fetch\";\nimport type { Router } from \"./router\";\nimport type { HasRequiredKeys, UnionToIntersection } from \"./helper\";\n\ntype HasRequired<\n\tT extends {\n\t\tbody?: any;\n\t\tquery?: any;\n\t\tparams?: any;\n\t},\n> = HasRequiredKeys<T> extends true\n\t? HasRequiredKeys<T[\"body\"]> extends false\n\t\t? HasRequiredKeys<T[\"query\"]> extends false\n\t\t\t? HasRequiredKeys<T[\"params\"]> extends false\n\t\t\t\t? false\n\t\t\t\t: true\n\t\t\t: true\n\t\t: true\n\t: true;\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\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 = R extends { endpoints: Record<string, Endpoint> } ? R[\"endpoints\"] : R;\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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAA8E;AAwDvE,IAAM,eAAe,CAAyC,YAA2B;AAC/F,QAAM,YAAQ,0BAAY,OAAO;AAejC,SAAO,OACN,SACGA,aAUC;AACJ,WAAQ,MAAM,MAAM,MAAgB;AAAA,MACnC,GAAGA,SAAQ,CAAC;AAAA,IACb,CAAC;AAAA,EACF;AACD;","names":["options"]}
|
package/dist/client.d.cts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
+
import { R as Router, U as UnionToIntersection, E as Endpoint, H as HasRequiredKeys } from './router-Bn7zn81P.cjs';
|
|
1
2
|
import { BetterFetchOption, BetterFetchResponse } from '@better-fetch/fetch';
|
|
2
|
-
import { R as Router, U as UnionToIntersection, b as Endpoint, H as HasRequiredKeys } from './shared/better-call.25f0dd59.cjs';
|
|
3
3
|
import 'zod';
|
|
4
|
-
import '@asteasolutions/zod-to-openapi/dist/zod-extensions';
|
|
5
|
-
import '@asteasolutions/zod-to-openapi';
|
|
6
|
-
import '@asteasolutions/zod-to-openapi/dist/openapi-registry';
|
|
7
|
-
import 'stream/web';
|
|
8
4
|
|
|
9
5
|
type HasRequired<T extends {
|
|
10
6
|
body?: any;
|
|
@@ -33,10 +29,10 @@ declare const createClient: <R extends Router | Router["endpoints"]>(options: Cl
|
|
|
33
29
|
endpoints: Record<string, Endpoint>;
|
|
34
30
|
} ? R["endpoints"] : R) extends {
|
|
35
31
|
[key: string]: infer T_1;
|
|
36
|
-
} ? T_1 extends Endpoint ? { [
|
|
32
|
+
} ? T_1 extends Endpoint ? { [key_1 in T_1["options"]["method"] extends "GET" ? T_1["path"] : `@${T_1["options"]["method"] extends string ? Lowercase<T_1["options"]["method"]> : never}${T_1["path"]}`]: T_1; } : {} : {}> extends infer T ? { [key in keyof T]: UnionToIntersection<(R extends {
|
|
37
33
|
endpoints: Record<string, Endpoint>;
|
|
38
34
|
} ? R["endpoints"] : R) extends {
|
|
39
35
|
[key: string]: infer T_1;
|
|
40
|
-
} ? T_1 extends Endpoint ? { [
|
|
36
|
+
} ? T_1 extends Endpoint ? { [key_1 in T_1["options"]["method"] extends "GET" ? T_1["path"] : `@${T_1["options"]["method"] extends string ? Lowercase<T_1["options"]["method"]> : never}${T_1["path"]}`]: T_1; } : {} : {}>[key]; } : never, K extends keyof OPT, C extends InferContext<OPT[K]>>(path: K, ...options: HasRequired<C> extends true ? [WithRequired<BetterFetchOption<C["body"], C["query"], C["params"]>, keyof RequiredOptionKeys<C>>] : [BetterFetchOption<C["body"], C["query"], C["params"]>?]) => Promise<BetterFetchResponse<Awaited<ReturnType<OPT[K] extends Endpoint ? OPT[K] : never>>>>;
|
|
41
37
|
|
|
42
38
|
export { type ClientOptions, type RequiredOptionKeys, createClient };
|
package/dist/client.d.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
+
import { R as Router, U as UnionToIntersection, E as Endpoint, H as HasRequiredKeys } from './router-Bn7zn81P.js';
|
|
1
2
|
import { BetterFetchOption, BetterFetchResponse } from '@better-fetch/fetch';
|
|
2
|
-
import { R as Router, U as UnionToIntersection, b as Endpoint, H as HasRequiredKeys } from './shared/better-call.25f0dd59.js';
|
|
3
3
|
import 'zod';
|
|
4
|
-
import '@asteasolutions/zod-to-openapi/dist/zod-extensions';
|
|
5
|
-
import '@asteasolutions/zod-to-openapi';
|
|
6
|
-
import '@asteasolutions/zod-to-openapi/dist/openapi-registry';
|
|
7
|
-
import 'stream/web';
|
|
8
4
|
|
|
9
5
|
type HasRequired<T extends {
|
|
10
6
|
body?: any;
|
|
@@ -33,10 +29,10 @@ declare const createClient: <R extends Router | Router["endpoints"]>(options: Cl
|
|
|
33
29
|
endpoints: Record<string, Endpoint>;
|
|
34
30
|
} ? R["endpoints"] : R) extends {
|
|
35
31
|
[key: string]: infer T_1;
|
|
36
|
-
} ? T_1 extends Endpoint ? { [
|
|
32
|
+
} ? T_1 extends Endpoint ? { [key_1 in T_1["options"]["method"] extends "GET" ? T_1["path"] : `@${T_1["options"]["method"] extends string ? Lowercase<T_1["options"]["method"]> : never}${T_1["path"]}`]: T_1; } : {} : {}> extends infer T ? { [key in keyof T]: UnionToIntersection<(R extends {
|
|
37
33
|
endpoints: Record<string, Endpoint>;
|
|
38
34
|
} ? R["endpoints"] : R) extends {
|
|
39
35
|
[key: string]: infer T_1;
|
|
40
|
-
} ? T_1 extends Endpoint ? { [
|
|
36
|
+
} ? T_1 extends Endpoint ? { [key_1 in T_1["options"]["method"] extends "GET" ? T_1["path"] : `@${T_1["options"]["method"] extends string ? Lowercase<T_1["options"]["method"]> : never}${T_1["path"]}`]: T_1; } : {} : {}>[key]; } : never, K extends keyof OPT, C extends InferContext<OPT[K]>>(path: K, ...options: HasRequired<C> extends true ? [WithRequired<BetterFetchOption<C["body"], C["query"], C["params"]>, keyof RequiredOptionKeys<C>>] : [BetterFetchOption<C["body"], C["query"], C["params"]>?]) => Promise<BetterFetchResponse<Awaited<ReturnType<OPT[K] extends Endpoint ? OPT[K] : never>>>>;
|
|
41
37
|
|
|
42
38
|
export { type ClientOptions, type RequiredOptionKeys, createClient };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import { createFetch } from "@better-fetch/fetch";
|
|
3
|
+
var createClient = (options) => {
|
|
4
|
+
const fetch = createFetch(options);
|
|
5
|
+
return async (path, ...options2) => {
|
|
6
|
+
return await fetch(path, {
|
|
7
|
+
...options2[0]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
createClient
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["import type { Endpoint, Prettify } from \"./types\";\n\nimport { type BetterFetchOption, type BetterFetchResponse, createFetch } from \"@better-fetch/fetch\";\nimport type { Router } from \"./router\";\nimport type { HasRequiredKeys, UnionToIntersection } from \"./helper\";\n\ntype HasRequired<\n\tT extends {\n\t\tbody?: any;\n\t\tquery?: any;\n\t\tparams?: any;\n\t},\n> = HasRequiredKeys<T> extends true\n\t? HasRequiredKeys<T[\"body\"]> extends false\n\t\t? HasRequiredKeys<T[\"query\"]> extends false\n\t\t\t? HasRequiredKeys<T[\"params\"]> extends false\n\t\t\t\t? false\n\t\t\t\t: true\n\t\t\t: true\n\t\t: true\n\t: true;\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\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 = R extends { endpoints: Record<string, Endpoint> } ? R[\"endpoints\"] : R;\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":";AAEA,SAA2D,mBAAmB;AAwDvE,IAAM,eAAe,CAAyC,YAA2B;AAC/F,QAAM,QAAQ,YAAY,OAAO;AAejC,SAAO,OACN,SACGA,aAUC;AACJ,WAAQ,MAAM,MAAM,MAAgB;AAAA,MACnC,GAAGA,SAAQ,CAAC;AAAA,IACb,CAAC;AAAA,EACF;AACD;","names":["options"]}
|