better-call 1.0.4-beta.1 → 1.0.4

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/index.js CHANGED
@@ -70,6 +70,50 @@ var APIError = class extends Error {
70
70
  }
71
71
  };
72
72
 
73
+ // src/utils.ts
74
+ async function getBody(request) {
75
+ const contentType = request.headers.get("content-type") || "";
76
+ if (!request.body) {
77
+ return void 0;
78
+ }
79
+ if (contentType.includes("application/json")) {
80
+ return await request.json();
81
+ }
82
+ if (contentType.includes("application/x-www-form-urlencoded")) {
83
+ const formData = await request.formData();
84
+ const result = {};
85
+ formData.forEach((value, key) => {
86
+ result[key] = value.toString();
87
+ });
88
+ return result;
89
+ }
90
+ if (contentType.includes("multipart/form-data")) {
91
+ const formData = await request.formData();
92
+ const result = {};
93
+ formData.forEach((value, key) => {
94
+ result[key] = value;
95
+ });
96
+ return result;
97
+ }
98
+ if (contentType.includes("text/plain")) {
99
+ return await request.text();
100
+ }
101
+ if (contentType.includes("application/octet-stream")) {
102
+ return await request.arrayBuffer();
103
+ }
104
+ if (contentType.includes("application/pdf") || contentType.includes("image/") || contentType.includes("video/")) {
105
+ const blob = await request.blob();
106
+ return blob;
107
+ }
108
+ if (contentType.includes("application/stream") || request.body instanceof ReadableStream) {
109
+ return request.body;
110
+ }
111
+ return await request.text();
112
+ }
113
+ function isAPIError(error) {
114
+ return error instanceof APIError || error?.name === "APIError";
115
+ }
116
+
73
117
  // src/to-response.ts
74
118
  function isJSONSerializable(value) {
75
119
  if (value === void 0) {
@@ -109,7 +153,7 @@ function toResponse(data, init) {
109
153
  status: data.status
110
154
  });
111
155
  }
112
- if (data instanceof APIError) {
156
+ if (isAPIError(data)) {
113
157
  return toResponse(data.body, {
114
158
  status: data.statusCode,
115
159
  statusText: data.status.toString(),
@@ -494,7 +538,7 @@ var createEndpoint2 = (path, options, handler) => {
494
538
  path
495
539
  });
496
540
  const response = await handler(internalContext).catch((e) => {
497
- if (e instanceof APIError && context.asResponse) {
541
+ if (isAPIError(e) && context.asResponse) {
498
542
  return e;
499
543
  }
500
544
  throw e;
@@ -4706,47 +4750,6 @@ var getHTML = (apiReference, config) => `<!doctype html>
4706
4750
  </body>
4707
4751
  </html>`;
4708
4752
 
4709
- // src/utils.ts
4710
- async function getBody(request) {
4711
- const contentType = request.headers.get("content-type") || "";
4712
- if (!request.body) {
4713
- return void 0;
4714
- }
4715
- if (contentType.includes("application/json")) {
4716
- return await request.json();
4717
- }
4718
- if (contentType.includes("application/x-www-form-urlencoded")) {
4719
- const formData = await request.formData();
4720
- const result = {};
4721
- formData.forEach((value, key) => {
4722
- result[key] = value.toString();
4723
- });
4724
- return result;
4725
- }
4726
- if (contentType.includes("multipart/form-data")) {
4727
- const formData = await request.formData();
4728
- const result = {};
4729
- formData.forEach((value, key) => {
4730
- result[key] = value;
4731
- });
4732
- return result;
4733
- }
4734
- if (contentType.includes("text/plain")) {
4735
- return await request.text();
4736
- }
4737
- if (contentType.includes("application/octet-stream")) {
4738
- return await request.arrayBuffer();
4739
- }
4740
- if (contentType.includes("application/pdf") || contentType.includes("image/") || contentType.includes("video/")) {
4741
- const blob = await request.blob();
4742
- return blob;
4743
- }
4744
- if (contentType.includes("application/stream") || request.body instanceof ReadableStream) {
4745
- return request.body;
4746
- }
4747
- return await request.text();
4748
- }
4749
-
4750
4753
  // src/router.ts
4751
4754
  var createRouter = (endpoints, config) => {
4752
4755
  if (!config?.openapi?.disabled) {
@@ -4825,7 +4828,7 @@ var createRouter = (endpoints, config) => {
4825
4828
  const response = await handler(context);
4826
4829
  return response;
4827
4830
  } catch (error) {
4828
- if (error instanceof APIError) {
4831
+ if (isAPIError(error)) {
4829
4832
  return toResponse(error);
4830
4833
  }
4831
4834
  console.error(`# SERVER_ERROR: `, error);