better-call 1.0.17 → 1.0.19

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
@@ -314,9 +314,48 @@ const router = createRouter({
314
314
 
315
315
  **basePath**: The base path for the router. All paths will be relative to this path.
316
316
 
317
- **onError**: The router will call this function if an error occurs in the middleware or the endpoint.
317
+ **onError**: The router will call this function if an error occurs in the middleware or the endpoint. This function receives the error as a parameter and can return different types of values:
318
318
 
319
- **throwError**: If true, the router will throw an error if an error occurs in the middleware or the endpoint.
319
+ - If it returns a `Response` object, the router will use it as the HTTP response.
320
+ - If it throws a new error, the router will handle it based on its type (if it's an `APIError`, it will be converted to a response; otherwise, it will be re-thrown).
321
+ - If it returns nothing (void), the router will proceed with default error handling (checking `throwError` setting).
322
+
323
+ ```ts
324
+ const router = createRouter({
325
+ /**
326
+ * This error handler can be set as async function or not.
327
+ */
328
+ onError: async (error) => {
329
+ // Log the error
330
+ console.error("An error occurred:", error);
331
+
332
+ // Return a custom response
333
+ return new Response(JSON.stringify({ message: "Something went wrong" }), {
334
+ status: 500,
335
+ headers: { "Content-Type": "application/json" }
336
+ });
337
+ }
338
+ });
339
+ ```
340
+
341
+ **throwError**: If true, the router will throw an error if an error occurs in the middleware or the endpoint. If false (default), the router will handle errors internally. This setting is still relevant even when `onError` is provided, as it determines the behavior when:
342
+
343
+ 1. No `onError` handler is provided, or
344
+ 2. The `onError` handler returns void (doesn't return a Response or throw an error)
345
+
346
+ - For `APIError` instances, it will convert them to appropriate HTTP responses.
347
+ - For other errors, it will return a 500 Internal Server Error response.
348
+
349
+ ```ts
350
+ const router = createRouter({
351
+ throwError: true, // Errors will be propagated to higher-level handlers
352
+ onError: (error) => {
353
+ // Log the error but let throwError handle it
354
+ console.error("An error occurred:", error);
355
+ // No return value, so throwError setting will determine behavior
356
+ }
357
+ });
358
+ ```
320
359
 
321
360
  #### Node Adapter
322
361
 
package/dist/client.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BetterFetchOption, BetterFetchResponse } from '@better-fetch/fetch';
2
- import { j as Router, Z as UnionToIntersection, b as Endpoint, W as HasRequiredKeys } from './router-CLKBSbjV.cjs';
2
+ import { j as Router, Z as UnionToIntersection, b as Endpoint, W as HasRequiredKeys } from './router-DcqXHY8X.cjs';
3
3
 
4
4
  type HasRequired<T extends {
5
5
  body?: any;
package/dist/client.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BetterFetchOption, BetterFetchResponse } from '@better-fetch/fetch';
2
- import { j as Router, Z as UnionToIntersection, b as Endpoint, W as HasRequiredKeys } from './router-CLKBSbjV.js';
2
+ import { j as Router, Z as UnionToIntersection, b as Endpoint, W as HasRequiredKeys } from './router-DcqXHY8X.js';
3
3
 
4
4
  type HasRequired<T extends {
5
5
  body?: any;
package/dist/index.cjs CHANGED
@@ -383,11 +383,14 @@ function fromError(error, validating) {
383
383
  }
384
384
 
385
385
  // src/crypto.ts
386
- var import_uncrypto = require("uncrypto");
386
+ var import_utils2 = require("@better-auth/utils");
387
387
  var algorithm = { name: "HMAC", hash: "SHA-256" };
388
388
  var getCryptoKey = async (secret) => {
389
389
  const secretBuf = typeof secret === "string" ? new TextEncoder().encode(secret) : secret;
390
- return await import_uncrypto.subtle.importKey("raw", secretBuf, algorithm, false, ["sign", "verify"]);
390
+ return await (0, import_utils2.getWebcryptoSubtle)().importKey("raw", secretBuf, algorithm, false, [
391
+ "sign",
392
+ "verify"
393
+ ]);
391
394
  };
392
395
  var verifySignature = async (base64Signature, value, secret) => {
393
396
  try {
@@ -396,14 +399,23 @@ var verifySignature = async (base64Signature, value, secret) => {
396
399
  for (let i = 0, len = signatureBinStr.length; i < len; i++) {
397
400
  signature[i] = signatureBinStr.charCodeAt(i);
398
401
  }
399
- return await import_uncrypto.subtle.verify(algorithm, secret, signature, new TextEncoder().encode(value));
402
+ return await (0, import_utils2.getWebcryptoSubtle)().verify(
403
+ algorithm,
404
+ secret,
405
+ signature,
406
+ new TextEncoder().encode(value)
407
+ );
400
408
  } catch (e) {
401
409
  return false;
402
410
  }
403
411
  };
404
412
  var makeSignature = async (value, secret) => {
405
413
  const key = await getCryptoKey(secret);
406
- const signature = await import_uncrypto.subtle.sign(algorithm.name, key, new TextEncoder().encode(value));
414
+ const signature = await (0, import_utils2.getWebcryptoSubtle)().sign(
415
+ algorithm.name,
416
+ key,
417
+ new TextEncoder().encode(value)
418
+ );
407
419
  return btoa(String.fromCharCode(...new Uint8Array(signature)));
408
420
  };
409
421
  var signCookieValue = async (value, secret) => {
@@ -3174,6 +3186,22 @@ var createRouter = (endpoints, config2) => {
3174
3186
  const response = await handler(context);
3175
3187
  return response;
3176
3188
  } catch (error) {
3189
+ if (config2?.onError) {
3190
+ try {
3191
+ const errorResponse = await config2.onError(error);
3192
+ if (errorResponse instanceof Response) {
3193
+ return toResponse(errorResponse);
3194
+ }
3195
+ } catch (error2) {
3196
+ if (isAPIError(error2)) {
3197
+ return toResponse(error2);
3198
+ }
3199
+ throw error2;
3200
+ }
3201
+ }
3202
+ if (config2?.throwError) {
3203
+ throw error;
3204
+ }
3177
3205
  if (isAPIError(error)) {
3178
3206
  return toResponse(error);
3179
3207
  }