better-call 1.0.25-beta.3 → 1.0.26-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 CHANGED
@@ -78,7 +78,27 @@ const items = await client("/item", {
78
78
 
79
79
  ### Returning non 200 responses
80
80
 
81
- 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
+ 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-DOTL_ENw.cjs";
1
+ import { Endpoint, HasRequiredKeys, Router, UnionToIntersection } from "./router-CeSGPJlX.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-CC_up1_c.js";
1
+ import { Endpoint, HasRequiredKeys, Router, UnionToIntersection } from "./router-BH6Wndse.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,6 +417,7 @@ 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,
@@ -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
- return context.asResponse ? toResponse(response, { headers }) : context.returnHeaders ? {
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;