better-call 1.0.18 → 1.0.20

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-DcqXHY8X.cjs';
2
+ import { j as Router, Z as UnionToIntersection, b as Endpoint, W as HasRequiredKeys } from './router-BDQGRZdc.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-DcqXHY8X.js';
2
+ import { j as Router, Z as UnionToIntersection, b as Endpoint, W as HasRequiredKeys } from './router-BDQGRZdc.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) => {
@@ -688,6 +700,7 @@ createMiddleware.create = (opts) => {
688
700
  };
689
701
 
690
702
  // src/endpoint.ts
703
+ var originalHandlerSymbol = Symbol("kOriginalHandler");
691
704
  var createEndpoint2 = (path, options, handler) => {
692
705
  const internalHandler = async (...inputCtx) => {
693
706
  const context = inputCtx[0] || {};
@@ -695,7 +708,9 @@ var createEndpoint2 = (path, options, handler) => {
695
708
  options,
696
709
  path
697
710
  });
698
- const response = await handler(internalContext).catch(async (e) => {
711
+ const response = await internalHandler[originalHandlerSymbol](
712
+ internalContext
713
+ ).catch(async (e) => {
699
714
  if (isAPIError(e)) {
700
715
  const onAPIError = options.onAPIError;
701
716
  if (onAPIError) {
@@ -715,6 +730,13 @@ var createEndpoint2 = (path, options, handler) => {
715
730
  response
716
731
  } : response;
717
732
  };
733
+ internalHandler[originalHandlerSymbol] = handler;
734
+ internalHandler.wrap = (fn) => {
735
+ const wrappedFn = async (context) => {
736
+ return fn(context, internalHandler[originalHandlerSymbol]);
737
+ };
738
+ return createEndpoint2(path, options, wrappedFn);
739
+ };
718
740
  internalHandler.options = options;
719
741
  internalHandler.path = path;
720
742
  return internalHandler;
@@ -3174,6 +3196,22 @@ var createRouter = (endpoints, config2) => {
3174
3196
  const response = await handler(context);
3175
3197
  return response;
3176
3198
  } catch (error) {
3199
+ if (config2?.onError) {
3200
+ try {
3201
+ const errorResponse = await config2.onError(error);
3202
+ if (errorResponse instanceof Response) {
3203
+ return toResponse(errorResponse);
3204
+ }
3205
+ } catch (error2) {
3206
+ if (isAPIError(error2)) {
3207
+ return toResponse(error2);
3208
+ }
3209
+ throw error2;
3210
+ }
3211
+ }
3212
+ if (config2?.throwError) {
3213
+ throw error;
3214
+ }
3177
3215
  if (isAPIError(error)) {
3178
3216
  return toResponse(error);
3179
3217
  }