@meistrari/auth-core 2.7.0 → 2.9.0

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.d.mts CHANGED
@@ -908,10 +908,10 @@ declare class ApplicationService {
908
908
  * on the verification URI to authorize the device.
909
909
  *
910
910
  * @param requesterApplicationId - The application requesting authorization
911
- * @param targetApplicationId - The target application to gain access to
911
+ * @param targetApplicationId - The target application to gain access to. Omit it to sign the user in to the requester application itself.
912
912
  * @returns Device authorization details including codes and verification URI
913
913
  */
914
- startDeviceAuthorizationFlow(requesterApplicationId: string, targetApplicationId: string): Promise<DeviceAuthorizationResponse>;
914
+ startDeviceAuthorizationFlow(requesterApplicationId: string, targetApplicationId?: string): Promise<DeviceAuthorizationResponse>;
915
915
  /**
916
916
  * Retrieves the context for a pending device authorization request.
917
917
  *
@@ -3423,7 +3423,7 @@ declare function createAPIClient(apiUrl: string, fetchOptions?: BetterFetchOptio
3423
3423
  data: StartAuthorizationFlowResponse;
3424
3424
  error: null;
3425
3425
  }>;
3426
- startDeviceAuthorizationFlow: (requesterApplicationId: string, targetApplicationId: string) => Promise<{
3426
+ startDeviceAuthorizationFlow: (requesterApplicationId: string, targetApplicationId?: string) => Promise<{
3427
3427
  data: null;
3428
3428
  error: {
3429
3429
  message?: string | undefined;
@@ -5123,14 +5123,14 @@ declare class AdminService {
5123
5123
  * Requests an application management route.
5124
5124
  *
5125
5125
  * These routes are served by the API itself rather than by the Better Auth
5126
- * handler, so they sit outside the `/api/auth` base path that the Better
5127
- * Auth client prepends and cannot go through {@link APIClient.$fetch}. They
5128
- * authenticate with the session cookie alone; a bearer token must never be
5129
- * attached, since the API reads one only as an internal API key and would
5130
- * reject the request.
5126
+ * handler, so they sit outside the `/api/auth` base path. The request is
5127
+ * sent to an absolute URL resolved against the API root, which better-fetch
5128
+ * uses as-is instead of prefixing the client's base URL, so it still goes
5129
+ * through {@link APIClient.$fetch} and picks up every fetch option the
5130
+ * client was created with (hooks, default headers, custom fetch, cookies).
5131
5131
  *
5132
- * @param path - Path relative to the API root, without a leading slash
5133
- * @param request - Method, body, and query parameters
5132
+ * @param path - Path relative to the API root, without a leading slash; `:name` segments are filled from `request.params`
5133
+ * @param request - Method, body, query, and path parameters
5134
5134
  * @returns The parsed JSON response
5135
5135
  */
5136
5136
  private requestApplicationRoute;
package/dist/index.d.ts CHANGED
@@ -908,10 +908,10 @@ declare class ApplicationService {
908
908
  * on the verification URI to authorize the device.
909
909
  *
910
910
  * @param requesterApplicationId - The application requesting authorization
911
- * @param targetApplicationId - The target application to gain access to
911
+ * @param targetApplicationId - The target application to gain access to. Omit it to sign the user in to the requester application itself.
912
912
  * @returns Device authorization details including codes and verification URI
913
913
  */
914
- startDeviceAuthorizationFlow(requesterApplicationId: string, targetApplicationId: string): Promise<DeviceAuthorizationResponse>;
914
+ startDeviceAuthorizationFlow(requesterApplicationId: string, targetApplicationId?: string): Promise<DeviceAuthorizationResponse>;
915
915
  /**
916
916
  * Retrieves the context for a pending device authorization request.
917
917
  *
@@ -3423,7 +3423,7 @@ declare function createAPIClient(apiUrl: string, fetchOptions?: BetterFetchOptio
3423
3423
  data: StartAuthorizationFlowResponse;
3424
3424
  error: null;
3425
3425
  }>;
3426
- startDeviceAuthorizationFlow: (requesterApplicationId: string, targetApplicationId: string) => Promise<{
3426
+ startDeviceAuthorizationFlow: (requesterApplicationId: string, targetApplicationId?: string) => Promise<{
3427
3427
  data: null;
3428
3428
  error: {
3429
3429
  message?: string | undefined;
@@ -5123,14 +5123,14 @@ declare class AdminService {
5123
5123
  * Requests an application management route.
5124
5124
  *
5125
5125
  * These routes are served by the API itself rather than by the Better Auth
5126
- * handler, so they sit outside the `/api/auth` base path that the Better
5127
- * Auth client prepends and cannot go through {@link APIClient.$fetch}. They
5128
- * authenticate with the session cookie alone; a bearer token must never be
5129
- * attached, since the API reads one only as an internal API key and would
5130
- * reject the request.
5126
+ * handler, so they sit outside the `/api/auth` base path. The request is
5127
+ * sent to an absolute URL resolved against the API root, which better-fetch
5128
+ * uses as-is instead of prefixing the client's base URL, so it still goes
5129
+ * through {@link APIClient.$fetch} and picks up every fetch option the
5130
+ * client was created with (hooks, default headers, custom fetch, cookies).
5131
5131
  *
5132
- * @param path - Path relative to the API root, without a leading slash
5133
- * @param request - Method, body, and query parameters
5132
+ * @param path - Path relative to the API root, without a leading slash; `:name` segments are filled from `request.params`
5133
+ * @param request - Method, body, query, and path parameters
5134
5134
  * @returns The parsed JSON response
5135
5135
  */
5136
5136
  private requestApplicationRoute;
package/dist/index.mjs CHANGED
@@ -21,7 +21,7 @@ function isTokenExpired(token, expiresInMs) {
21
21
  return nowInSeconds > expirationTime;
22
22
  }
23
23
 
24
- const version = "2.7.0";
24
+ const version = "2.9.0";
25
25
 
26
26
  function parsedEnumValues(schema) {
27
27
  return Object.fromEntries(
@@ -512,19 +512,12 @@ class ApplicationRouteError extends Error {
512
512
  code;
513
513
  details;
514
514
  status;
515
- constructor(response, body) {
516
- super(body?.error ?? body?.message ?? response.statusText);
515
+ constructor(body) {
516
+ super(body.error ?? body.message ?? body.statusText);
517
517
  this.name = "ApplicationRouteError";
518
- this.status = response.status;
519
- this.code = body?.code;
520
- this.details = body?.details;
521
- }
522
- }
523
- async function readErrorBody(response) {
524
- try {
525
- return await response.json();
526
- } catch {
527
- return null;
518
+ this.status = body.status;
519
+ this.code = body.code;
520
+ this.details = body.details;
528
521
  }
529
522
  }
530
523
  class AdminService {
@@ -545,35 +538,35 @@ class AdminService {
545
538
  * Requests an application management route.
546
539
  *
547
540
  * These routes are served by the API itself rather than by the Better Auth
548
- * handler, so they sit outside the `/api/auth` base path that the Better
549
- * Auth client prepends and cannot go through {@link APIClient.$fetch}. They
550
- * authenticate with the session cookie alone; a bearer token must never be
551
- * attached, since the API reads one only as an internal API key and would
552
- * reject the request.
553
- *
554
- * @param path - Path relative to the API root, without a leading slash
555
- * @param request - Method, body, and query parameters
541
+ * handler, so they sit outside the `/api/auth` base path. The request is
542
+ * sent to an absolute URL resolved against the API root, which better-fetch
543
+ * uses as-is instead of prefixing the client's base URL, so it still goes
544
+ * through {@link APIClient.$fetch} and picks up every fetch option the
545
+ * client was created with (hooks, default headers, custom fetch, cookies).
546
+ *
547
+ * @param path - Path relative to the API root, without a leading slash; `:name` segments are filled from `request.params`
548
+ * @param request - Method, body, query, and path parameters
556
549
  * @returns The parsed JSON response
557
550
  */
558
551
  async requestApplicationRoute(path, request = {}) {
559
552
  const base = this.apiUrl.endsWith("/") ? this.apiUrl : `${this.apiUrl}/`;
560
- const url = new URL(path, base);
561
- for (const [key, value] of Object.entries(request.query ?? {})) {
562
- if (value !== void 0)
563
- url.searchParams.set(key, String(value));
564
- }
565
- const headers = new Headers({ Accept: "application/json" });
566
- if (request.body !== void 0)
567
- headers.set("Content-Type", "application/json");
568
- const response = await fetch(url, {
553
+ const hasBody = request.body !== void 0;
554
+ const { data, error } = await this.client.$fetch(`${base}${path}`, {
569
555
  method: request.method ?? "GET",
570
- credentials: "include",
571
- headers,
572
- ...request.body !== void 0 ? { body: JSON.stringify(request.body) } : {}
556
+ params: request.params,
557
+ query: request.query,
558
+ headers: {
559
+ Accept: "application/json",
560
+ ...hasBody ? { "Content-Type": "application/json" } : {}
561
+ },
562
+ ...hasBody ? { body: JSON.stringify(request.body) } : {},
563
+ // The client's parser revives ISO strings into Dates; these routes are typed with plain strings
564
+ jsonParser: (text) => JSON.parse(text),
565
+ throw: false
573
566
  });
574
- if (!response.ok)
575
- throw new ApplicationRouteError(response, await readErrorBody(response));
576
- return await response.json();
567
+ if (error)
568
+ throw new ApplicationRouteError(error);
569
+ return data;
577
570
  }
578
571
  /**
579
572
  * Lists applications, optionally filtered by organization.
@@ -614,7 +607,8 @@ class AdminService {
614
607
  * @returns The updated application
615
608
  */
616
609
  async updateApplication(applicationId, options) {
617
- return await this.requestApplicationRoute(`applications/${encodeURIComponent(applicationId)}`, {
610
+ return await this.requestApplicationRoute("applications/:applicationId", {
611
+ params: { applicationId },
618
612
  method: "PUT",
619
613
  body: options
620
614
  });
@@ -622,28 +616,29 @@ class AdminService {
622
616
  /** Lists the role catalog for an application. */
623
617
  async listApplicationRoles(applicationId) {
624
618
  return await this.requestApplicationRoute(
625
- `applications/${encodeURIComponent(applicationId)}/roles`
619
+ "applications/:applicationId/roles",
620
+ { params: { applicationId } }
626
621
  );
627
622
  }
628
623
  /** Creates an application-scoped role definition. */
629
624
  async createApplicationRole(applicationId, options) {
630
625
  return await this.requestApplicationRoute(
631
- `applications/${encodeURIComponent(applicationId)}/roles`,
632
- { method: "POST", body: options }
626
+ "applications/:applicationId/roles",
627
+ { params: { applicationId }, method: "POST", body: options }
633
628
  );
634
629
  }
635
630
  /** Updates an application-scoped role definition. */
636
631
  async updateApplicationRole(applicationId, currentName, options) {
637
632
  return await this.requestApplicationRoute(
638
- `applications/${encodeURIComponent(applicationId)}/roles/${encodeURIComponent(currentName)}`,
639
- { method: "PUT", body: options }
633
+ "applications/:applicationId/roles/:currentName",
634
+ { params: { applicationId, currentName }, method: "PUT", body: options }
640
635
  );
641
636
  }
642
637
  /** Deletes an application-scoped role definition. */
643
638
  async deleteApplicationRole(applicationId, name) {
644
639
  return await this.requestApplicationRoute(
645
- `applications/${encodeURIComponent(applicationId)}/roles/${encodeURIComponent(name)}`,
646
- { method: "DELETE" }
640
+ "applications/:applicationId/roles/:name",
641
+ { params: { applicationId, name }, method: "DELETE" }
647
642
  );
648
643
  }
649
644
  /**
@@ -654,7 +649,8 @@ class AdminService {
654
649
  * @returns The created entitlement
655
650
  */
656
651
  async createApplicationEntitlement(applicationId, options) {
657
- return await this.requestApplicationRoute(`applications/${encodeURIComponent(applicationId)}/entitlements`, {
652
+ return await this.requestApplicationRoute("applications/:applicationId/entitlements", {
653
+ params: { applicationId },
658
654
  method: "POST",
659
655
  body: options
660
656
  });
@@ -666,7 +662,7 @@ class AdminService {
666
662
  * @returns Secret metadata without plaintext values
667
663
  */
668
664
  async listApplicationSecrets(applicationId) {
669
- return await this.requestApplicationRoute(`applications/${encodeURIComponent(applicationId)}/secrets`);
665
+ return await this.requestApplicationRoute("applications/:applicationId/secrets", { params: { applicationId } });
670
666
  }
671
667
  /**
672
668
  * Creates a client secret for an application.
@@ -676,7 +672,8 @@ class AdminService {
676
672
  * @returns The created secret, including the plaintext value shown only once
677
673
  */
678
674
  async createApplicationSecret(applicationId, options = {}) {
679
- return await this.requestApplicationRoute(`applications/${encodeURIComponent(applicationId)}/secrets`, {
675
+ return await this.requestApplicationRoute("applications/:applicationId/secrets", {
676
+ params: { applicationId },
680
677
  method: "POST",
681
678
  body: options
682
679
  });
@@ -690,8 +687,8 @@ class AdminService {
690
687
  */
691
688
  async deleteApplicationSecret(applicationId, secretId) {
692
689
  return await this.requestApplicationRoute(
693
- `applications/${encodeURIComponent(applicationId)}/secrets/${encodeURIComponent(secretId)}`,
694
- { method: "DELETE" }
690
+ "applications/:applicationId/secrets/:secretId",
691
+ { params: { applicationId, secretId }, method: "DELETE" }
695
692
  );
696
693
  }
697
694
  /**
@@ -702,8 +699,8 @@ class AdminService {
702
699
  */
703
700
  async retireApplicationServiceAccount(applicationId) {
704
701
  return await this.requestApplicationRoute(
705
- `applications/${encodeURIComponent(applicationId)}/service-account`,
706
- { method: "DELETE" }
702
+ "applications/:applicationId/service-account",
703
+ { params: { applicationId }, method: "DELETE" }
707
704
  );
708
705
  }
709
706
  /**
@@ -958,7 +955,8 @@ class AdminService {
958
955
  */
959
956
  async getEntitlement(entitlementId) {
960
957
  return await this.requestApplicationRoute(
961
- `applications/entitlements/${encodeURIComponent(entitlementId)}`
958
+ "applications/entitlements/:entitlementId",
959
+ { params: { entitlementId } }
962
960
  );
963
961
  }
964
962
  /**
@@ -970,8 +968,8 @@ class AdminService {
970
968
  */
971
969
  async setEntitlementRules(entitlementId, options) {
972
970
  return await this.requestApplicationRoute(
973
- `applications/entitlements/${encodeURIComponent(entitlementId)}/rules`,
974
- { method: "PUT", body: options }
971
+ "applications/entitlements/:entitlementId/rules",
972
+ { params: { entitlementId }, method: "PUT", body: options }
975
973
  );
976
974
  }
977
975
  /**
@@ -983,8 +981,8 @@ class AdminService {
983
981
  */
984
982
  async updateEntitlement(entitlementId, options) {
985
983
  return await this.requestApplicationRoute(
986
- `applications/entitlements/${encodeURIComponent(entitlementId)}`,
987
- { method: "PUT", body: options }
984
+ "applications/entitlements/:entitlementId",
985
+ { params: { entitlementId }, method: "PUT", body: options }
988
986
  );
989
987
  }
990
988
  /**
@@ -995,8 +993,8 @@ class AdminService {
995
993
  */
996
994
  async deleteEntitlement(entitlementId) {
997
995
  return await this.requestApplicationRoute(
998
- `applications/entitlements/${encodeURIComponent(entitlementId)}`,
999
- { method: "DELETE" }
996
+ "applications/entitlements/:entitlementId",
997
+ { params: { entitlementId }, method: "DELETE" }
1000
998
  );
1001
999
  }
1002
1000
  /**
@@ -1538,7 +1536,7 @@ class ApplicationService {
1538
1536
  * on the verification URI to authorize the device.
1539
1537
  *
1540
1538
  * @param requesterApplicationId - The application requesting authorization
1541
- * @param targetApplicationId - The target application to gain access to
1539
+ * @param targetApplicationId - The target application to gain access to. Omit it to sign the user in to the requester application itself.
1542
1540
  * @returns Device authorization details including codes and verification URI
1543
1541
  */
1544
1542
  async startDeviceAuthorizationFlow(requesterApplicationId, targetApplicationId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meistrari/auth-core",
3
- "version": "2.7.0",
3
+ "version": "2.9.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {