better-call 1.0.25-beta.3 → 1.0.25
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 +38 -1
- package/dist/client.d.cts +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/index.cjs +23 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +23 -5
- package/dist/index.js.map +1 -1
- package/dist/node.d.cts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/{router-CC_up1_c.d.ts → router-DRUBbubk.d.ts} +48 -5
- package/dist/{router-DOTL_ENw.d.cts → router-DeSuoHCx.d.cts} +48 -5
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -78,7 +78,27 @@ const items = await client("/item", {
|
|
|
78
78
|
|
|
79
79
|
### Returning non 200 responses
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
There are several supported ways to a non 200 response:
|
|
82
|
+
|
|
83
|
+
You can use the `ctx.setStatus(status)` helper to change the default status code of a successful response:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const createItem = createEndpoint("/item", {
|
|
87
|
+
method: "POST",
|
|
88
|
+
body: z.object({
|
|
89
|
+
id: z.string()
|
|
90
|
+
})
|
|
91
|
+
}, async (ctx) => {
|
|
92
|
+
ctx.setStatus(201);
|
|
93
|
+
return {
|
|
94
|
+
item: {
|
|
95
|
+
id: ctx.body.id
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Sometimes, you want to respond with an error, in those cases 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.
|
|
82
102
|
|
|
83
103
|
```ts
|
|
84
104
|
const createItem = createEndpoint("/item", {
|
|
@@ -122,6 +142,23 @@ const createItem = createEndpoint("/item", {
|
|
|
122
142
|
})
|
|
123
143
|
```
|
|
124
144
|
|
|
145
|
+
Finally, you can return a new `Response` object. In this case, the `ctx.setStatus()` call will be ignored, as the `Response` will have completely control over the final status code:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const createItem = createEndpoint("/item", {
|
|
149
|
+
method: "POST",
|
|
150
|
+
body: z.object({
|
|
151
|
+
id: z.string()
|
|
152
|
+
})
|
|
153
|
+
}, async (ctx) => {
|
|
154
|
+
return Response.json({
|
|
155
|
+
item: {
|
|
156
|
+
id: ctx.body.id
|
|
157
|
+
}
|
|
158
|
+
}, { status: 201 });
|
|
159
|
+
})
|
|
160
|
+
```
|
|
161
|
+
|
|
125
162
|
### Endpoint
|
|
126
163
|
|
|
127
164
|
Endpoints are building blocks of better-call.
|
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-DeSuoHCx.cjs";
|
|
2
2
|
import { BetterFetchOption, BetterFetchResponse } from "@better-fetch/fetch";
|
|
3
3
|
|
|
4
4
|
//#region src/client.d.ts
|
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-DRUBbubk.js";
|
|
2
2
|
import { BetterFetchOption, BetterFetchResponse } from "@better-fetch/fetch";
|
|
3
3
|
|
|
4
4
|
//#region src/client.d.ts
|
package/dist/index.cjs
CHANGED
|
@@ -417,19 +417,20 @@ const serializeSignedCookie = async (key, value, secret, opt) => {
|
|
|
417
417
|
//#region src/context.ts
|
|
418
418
|
const createInternalContext = async (context, { options, path }) => {
|
|
419
419
|
const headers = new Headers();
|
|
420
|
+
let responseStatus = void 0;
|
|
420
421
|
const { data, error } = await runValidation(options, context);
|
|
421
422
|
if (error) throw new APIError(400, {
|
|
422
423
|
message: error.message,
|
|
423
424
|
code: "VALIDATION_ERROR"
|
|
424
425
|
});
|
|
425
|
-
const requestHeaders = "headers" in context ? context.headers instanceof Headers ? context.headers : new Headers(context.headers) : "request" in context && context.request instanceof Request ? context.request.headers :
|
|
426
|
-
const requestCookies = requestHeaders
|
|
426
|
+
const requestHeaders = "headers" in context ? context.headers instanceof Headers ? context.headers : new Headers(context.headers) : "request" in context && context.request instanceof Request ? context.request.headers : new Headers();
|
|
427
|
+
const requestCookies = requestHeaders.get("cookie");
|
|
427
428
|
const parsedCookies = requestCookies ? parseCookies(requestCookies) : void 0;
|
|
428
429
|
const internalContext = {
|
|
429
430
|
...context,
|
|
430
431
|
body: data.body,
|
|
431
432
|
query: data.query,
|
|
432
|
-
path
|
|
433
|
+
path,
|
|
433
434
|
context: "context" in context && context.context ? context.context : {},
|
|
434
435
|
returned: void 0,
|
|
435
436
|
headers: context?.headers,
|
|
@@ -477,6 +478,9 @@ const createInternalContext = async (context, { options, path }) => {
|
|
|
477
478
|
error: (status, body, headers$1) => {
|
|
478
479
|
return new APIError(status, body, headers$1);
|
|
479
480
|
},
|
|
481
|
+
setStatus: (status) => {
|
|
482
|
+
responseStatus = status;
|
|
483
|
+
},
|
|
480
484
|
json: (json, routerResponse) => {
|
|
481
485
|
if (!context.asResponse) return json;
|
|
482
486
|
return {
|
|
@@ -485,7 +489,10 @@ const createInternalContext = async (context, { options, path }) => {
|
|
|
485
489
|
_flag: "json"
|
|
486
490
|
};
|
|
487
491
|
},
|
|
488
|
-
responseHeaders: headers
|
|
492
|
+
responseHeaders: headers,
|
|
493
|
+
get responseStatus() {
|
|
494
|
+
return responseStatus;
|
|
495
|
+
}
|
|
489
496
|
};
|
|
490
497
|
for (const middleware of options.use || []) {
|
|
491
498
|
const response = await middleware({
|
|
@@ -523,9 +530,20 @@ const createEndpoint = (path, options, handler) => {
|
|
|
523
530
|
throw e;
|
|
524
531
|
});
|
|
525
532
|
const headers = internalContext.responseHeaders;
|
|
526
|
-
|
|
533
|
+
const status = internalContext.responseStatus;
|
|
534
|
+
return context.asResponse ? toResponse(response, {
|
|
535
|
+
headers,
|
|
536
|
+
status
|
|
537
|
+
}) : context.returnHeaders ? context.returnStatus ? {
|
|
538
|
+
headers,
|
|
539
|
+
response,
|
|
540
|
+
status
|
|
541
|
+
} : {
|
|
527
542
|
headers,
|
|
528
543
|
response
|
|
544
|
+
} : context.returnStatus ? {
|
|
545
|
+
response,
|
|
546
|
+
status
|
|
529
547
|
} : response;
|
|
530
548
|
};
|
|
531
549
|
internalHandler.options = options;
|