@meistrari/auth-core 1.15.0 → 1.16.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.mjs CHANGED
@@ -8,7 +8,7 @@ import { defaultStatements } from 'better-auth/plugins/organization/access';
8
8
  import { z } from 'zod';
9
9
  export { APIError } from 'better-auth';
10
10
 
11
- const version = "1.15.0";
11
+ const version = "1.16.0";
12
12
 
13
13
  const statements = {
14
14
  ...defaultStatements,
@@ -171,12 +171,17 @@ function applicationsPluginClient() {
171
171
  headers
172
172
  });
173
173
  },
174
- whoAmI: async (accessToken) => {
174
+ whoAmI: async (accessToken, options = {}) => {
175
175
  const headers = new Headers();
176
176
  headers.set("x-tela-access-token", accessToken);
177
+ const query = {};
178
+ if (options.include && options.include.length > 0) {
179
+ query.include = options.include.join(",");
180
+ }
177
181
  return await $fetch("/applications/whoami", {
178
182
  method: "GET",
179
- headers
183
+ headers,
184
+ query
180
185
  });
181
186
  },
182
187
  switchOrganization: async (organizationId, accessToken) => {
@@ -255,7 +260,13 @@ function createAPIClient(apiUrl, fetchOptions = {}) {
255
260
  }
256
261
 
257
262
  class BaseError extends Error {
263
+ /** Machine-readable error code (e.g. `"INVALID_SOCIAL_PROVIDER"`). */
258
264
  code;
265
+ /**
266
+ * @param code - A machine-readable error code
267
+ * @param message - A human-readable error message
268
+ * @param options - Standard `ErrorOptions` (e.g. `cause`)
269
+ */
259
270
  constructor(code, message, options) {
260
271
  super(message, options);
261
272
  this.code = code;
@@ -389,6 +400,16 @@ class ApplicationService {
389
400
  }
390
401
  return response.data;
391
402
  }
403
+ /**
404
+ * Starts a device authorization flow (RFC 8628).
405
+ *
406
+ * Returns a device code and user code that the end user must enter
407
+ * on the verification URI to authorize the device.
408
+ *
409
+ * @param requesterApplicationId - The application requesting authorization
410
+ * @param targetApplicationId - The target application to gain access to
411
+ * @returns Device authorization details including codes and verification URI
412
+ */
392
413
  async startDeviceAuthorizationFlow(requesterApplicationId, targetApplicationId) {
393
414
  const response = await this.client.applications.startDeviceAuthorizationFlow(requesterApplicationId, targetApplicationId);
394
415
  if (!response.data) {
@@ -396,6 +417,15 @@ class ApplicationService {
396
417
  }
397
418
  return response.data;
398
419
  }
420
+ /**
421
+ * Retrieves the context for a pending device authorization request.
422
+ *
423
+ * Used by the authorization page to display details about the requesting
424
+ * and target applications before the user approves or denies.
425
+ *
426
+ * @param userCode - The user code from the device authorization response
427
+ * @returns Context including application info, available organizations, and status
428
+ */
399
429
  async getDeviceAuthorizationContext(userCode) {
400
430
  const response = await this.client.applications.getDeviceAuthorizationContext(userCode);
401
431
  if (!response.data) {
@@ -403,6 +433,13 @@ class ApplicationService {
403
433
  }
404
434
  return response.data;
405
435
  }
436
+ /**
437
+ * Approves a pending device authorization request.
438
+ *
439
+ * @param userCode - The user code identifying the device authorization request
440
+ * @param organizationId - The organization to authorize the device for
441
+ * @returns Confirmation of the approval action
442
+ */
406
443
  async approveDeviceAuthorizationFlow(userCode, organizationId) {
407
444
  const response = await this.client.applications.approveDeviceAuthorizationFlow(userCode, organizationId);
408
445
  if (!response.data) {
@@ -410,6 +447,12 @@ class ApplicationService {
410
447
  }
411
448
  return response.data;
412
449
  }
450
+ /**
451
+ * Denies a pending device authorization request.
452
+ *
453
+ * @param userCode - The user code identifying the device authorization request
454
+ * @returns Confirmation of the denial action
455
+ */
413
456
  async denyDeviceAuthorizationFlow(userCode) {
414
457
  const response = await this.client.applications.denyDeviceAuthorizationFlow(userCode);
415
458
  if (!response.data) {
@@ -417,6 +460,21 @@ class ApplicationService {
417
460
  }
418
461
  return response.data;
419
462
  }
463
+ /**
464
+ * Exchanges a device code for access and refresh tokens.
465
+ *
466
+ * This should be called by the device client while polling. It will throw
467
+ * typed errors to indicate the polling state (pending, slow down, denied, expired).
468
+ *
469
+ * @param deviceCode - The device code obtained from {@link startDeviceAuthorizationFlow}
470
+ * @returns Access and refresh tokens along with user and organization info
471
+ *
472
+ * @throws {DeviceAuthorizationPendingError} The user hasn't authorized yet — keep polling
473
+ * @throws {DeviceAuthorizationSlowDownError} Polling too fast — increase the interval
474
+ * @throws {DeviceAccessDeniedError} The user denied the request
475
+ * @throws {DeviceCodeExpiredError} The device code has expired
476
+ * @throws {DeviceTransientServerError} Transient server error — safe to retry
477
+ */
420
478
  async exchangeDeviceCodeForTokens(deviceCode) {
421
479
  try {
422
480
  const response = await this.client.applications.exchangeDeviceCodeForTokens(deviceCode);
@@ -474,11 +532,16 @@ class ApplicationService {
474
532
  /**
475
533
  * Gets the current user and organization for a specific application.
476
534
  *
535
+ * By default, the returned organization does not include its `members`,
536
+ * `teams`, or `invitations` collections. Pass `options.include` to request
537
+ * any of them explicitly.
538
+ *
477
539
  * @param accessToken - The access token to use for the who am I request
540
+ * @param options - Optional include list controlling which relations are loaded
478
541
  * @returns The current user and organization
479
542
  */
480
- async whoAmI(accessToken) {
481
- const response = await this.client.applications.whoAmI(accessToken);
543
+ async whoAmI(accessToken, options = {}) {
544
+ const response = await this.client.applications.whoAmI(accessToken, options);
482
545
  if (!response.data) {
483
546
  throw new Error("No data returned from the API", { cause: response.error });
484
547
  }
@@ -560,10 +623,7 @@ class OrganizationService {
560
623
  /**
561
624
  * Updates an organization's details.
562
625
  *
563
- * @param payload - The organization fields to update
564
- * @param payload.name - New organization name
565
- * @param payload.logo - New organization logo URL
566
- * @param payload.settings - New organization settings
626
+ * @param payload - The organization fields to update (name, logo, settings)
567
627
  * @returns The updated organization
568
628
  */
569
629
  async updateOrganization(payload) {
@@ -638,9 +698,7 @@ class OrganizationService {
638
698
  /**
639
699
  * Removes a user from the active organization.
640
700
  *
641
- * @param options - User identifier (either memberId or userEmail must be provided)
642
- * @param options.memberId - The member ID to remove
643
- * @param options.userEmail - The user email to remove
701
+ * @param options - Provide either `memberId` or `userEmail` to identify the user
644
702
  */
645
703
  async removeUserFromOrganization({ memberId, userEmail }) {
646
704
  await this.client.organization.removeMember({
@@ -663,8 +721,7 @@ class OrganizationService {
663
721
  /**
664
722
  * Creates a new team within the active organization.
665
723
  *
666
- * @param payload - Team configuration
667
- * @param payload.name - The name of the team
724
+ * @param payload - Team configuration with a `name` field
668
725
  * @returns The created team
669
726
  */
670
727
  async createTeam(payload) {
@@ -674,8 +731,7 @@ class OrganizationService {
674
731
  * Updates an existing team's details.
675
732
  *
676
733
  * @param id - The team ID to update
677
- * @param payload - Team fields to update
678
- * @param payload.name - The new team name
734
+ * @param payload - Team fields to update (currently supports `name`)
679
735
  * @returns The updated team
680
736
  */
681
737
  async updateTeam(id, payload) {
@@ -934,6 +990,15 @@ class ApiKeyService {
934
990
  constructor(client) {
935
991
  this.client = client;
936
992
  }
993
+ /**
994
+ * Creates a new API key.
995
+ *
996
+ * The returned object includes the secret `key` value, which is only
997
+ * available at creation time and cannot be retrieved later.
998
+ *
999
+ * @param payload - The API key configuration
1000
+ * @returns The created API key including the secret key value
1001
+ */
937
1002
  async createApiKey(payload) {
938
1003
  return await this.client.apiKey.create({
939
1004
  name: payload.name,
@@ -942,6 +1007,12 @@ class ApiKeyService {
942
1007
  metadata: payload.metadata
943
1008
  });
944
1009
  }
1010
+ /**
1011
+ * Retrieves an API key by its ID.
1012
+ *
1013
+ * @param id - The API key ID
1014
+ * @returns The API key (without the secret key value)
1015
+ */
945
1016
  async getApiKey(id) {
946
1017
  return await this.client.apiKey.get({
947
1018
  query: {
@@ -949,18 +1020,39 @@ class ApiKeyService {
949
1020
  }
950
1021
  });
951
1022
  }
1023
+ /**
1024
+ * Lists all API keys owned by the authenticated user.
1025
+ *
1026
+ * @returns An object containing the API keys array and pagination metadata
1027
+ */
952
1028
  async listApiKeys() {
953
1029
  return await this.client.apiKey.list();
954
1030
  }
1031
+ /**
1032
+ * Lists all API keys belonging to the active organization.
1033
+ *
1034
+ * @returns An array of API keys (without secret key values)
1035
+ */
955
1036
  async listOrganizationApiKeys() {
956
1037
  return await this.client.customEndpoints.organizations.apiKeys();
957
1038
  }
1039
+ /**
1040
+ * Updates an existing API key.
1041
+ *
1042
+ * @param payload - The fields to update
1043
+ * @returns The updated API key (without the secret key value)
1044
+ */
958
1045
  async updateApiKey(payload) {
959
1046
  return await this.client.apiKey.update({
960
1047
  keyId: payload.id,
961
1048
  name: payload.name
962
1049
  });
963
1050
  }
1051
+ /**
1052
+ * Permanently deletes an API key.
1053
+ *
1054
+ * @param id - The API key ID to delete
1055
+ */
964
1056
  async deleteApiKey(id) {
965
1057
  return await this.client.apiKey.delete({
966
1058
  keyId: id
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meistrari/auth-core",
3
- "version": "1.15.0",
3
+ "version": "1.16.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -14,7 +14,8 @@
14
14
  "dist"
15
15
  ],
16
16
  "scripts": {
17
- "build": "unbuild"
17
+ "build": "unbuild",
18
+ "docs": "typedoc"
18
19
  },
19
20
  "dependencies": {
20
21
  "@better-auth/api-key": "1.5.4",
@@ -25,6 +26,8 @@
25
26
  },
26
27
  "devDependencies": {
27
28
  "@types/node": "latest",
29
+ "typedoc": "0.28.18",
30
+ "typedoc-plugin-markdown": "4.11.0",
28
31
  "typescript": "5.9.2",
29
32
  "unbuild": "3.6.1"
30
33
  }