@choiceform/shared-auth 0.1.28 → 0.1.29

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.
@@ -1 +1 @@
1
- {"version":3,"file":"auth-api.d.ts","sourceRoot":"","sources":["../../src/api/auth-api.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,OAAO,EACP,gBAAgB,EAChB,gBAAgB,EAChB,oBAAoB,EACpB,WAAW,EACX,iBAAiB,EACjB,WAAW,EACZ,MAAM,UAAU,CAAA;AAEjB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAEzC;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,SAAS;IAM9C;;;OAGG;kBACiB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAW/C;;;OAGG;+BAC8B,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAgBrE;;;OAGG;mBACkB,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAc3C;;OAEG;qBACoB,iBAAiB,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAetE;;OAEG;0BACyB,gBAAgB,GAAG,OAAO,CAAC;QAAE,MAAM,EAAE,OAAO,CAAA;KAAE,CAAC;IAiB3E;;OAEG;mCAEM,MAAM,gBACC,MAAM,GACnB,OAAO,CAAC;QAAE,MAAM,EAAE,OAAO,CAAA;KAAE,CAAC;IAmB/B;;;;OAIG;4BAC2B,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAqBvD;;;OAGG;gCAC+B,MAAM,UAAU,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBxE;;OAEG;oCAEgB,MAAM,eACV,MAAM,UACX,MAAM,GACb,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC;IAsBhC;;OAEG;0BAES,MAAM,gBACF,MAAM,UACZ,MAAM,GACb,OAAO,C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AAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAA"}
@@ -5,6 +5,7 @@
5
5
  * 统一使用 apiClient 发送请求
6
6
  */
7
7
  import { extractSessionUser } from "../utils";
8
+ import { createErrorWithSchema } from "../utils/error-schema";
8
9
  /**
9
10
  * 创建认证 API
10
11
  *
@@ -39,7 +40,7 @@ export function createAuthApi(apiClient) {
39
40
  headers: { Authorization: `Bearer ${token}` },
40
41
  });
41
42
  if (!response.ok) {
42
- throw new Error(`Failed to fetch session: ${response.status}`);
43
+ throw createErrorWithSchema("Failed to fetch session", response.status, response.data);
43
44
  }
44
45
  return extractSessionUser(response.data);
45
46
  },
@@ -55,7 +56,7 @@ export function createAuthApi(apiClient) {
55
56
  headers: { Authorization: `Bearer ${token}` },
56
57
  });
57
58
  if (!response.ok) {
58
- throw new Error(`Failed to onboard: ${response.status}`);
59
+ throw createErrorWithSchema("Failed to onboard", response.status, response.data);
59
60
  }
60
61
  },
61
62
  // ============================================================
@@ -67,7 +68,7 @@ export function createAuthApi(apiClient) {
67
68
  async updateUser(data) {
68
69
  const response = await apiClient.post("/v1/auth/update-user", data);
69
70
  if (!response.ok) {
70
- throw new Error(`Failed to update user: ${response.status}`);
71
+ throw createErrorWithSchema("Failed to update user", response.status, response.data);
71
72
  }
72
73
  return this.getSession();
73
74
  },
@@ -80,7 +81,7 @@ export function createAuthApi(apiClient) {
80
81
  async sendMagicLink(params) {
81
82
  const response = await apiClient.post("/v1/auth/sign-in/magic-link", params);
82
83
  if (!response.ok) {
83
- throw new Error(`Failed to send magic link: ${response.status}`);
84
+ throw createErrorWithSchema("Failed to send magic link", response.status, response.data);
84
85
  }
85
86
  return response.data;
86
87
  },
@@ -97,7 +98,7 @@ export function createAuthApi(apiClient) {
97
98
  : `/auth/callback?type=signup&email=${encodeURIComponent(email)}`);
98
99
  const response = await apiClient.post("/v1/auth/send-verification-email", { email, callbackURL: finalCallbackURL });
99
100
  if (!response.ok) {
100
- throw new Error(`Failed to resend verification email: ${response.status}`);
101
+ throw createErrorWithSchema("Failed to resend verification email", response.status, response.data);
101
102
  }
102
103
  return response.data;
103
104
  },
@@ -132,7 +133,7 @@ export function createAuthApi(apiClient) {
132
133
  : undefined;
133
134
  const response = await apiClient.post("/v1/auth/link-credential", { newPassword }, options);
134
135
  if (!response.ok) {
135
- throw new Error(`Failed to link credential: ${response.status}`);
136
+ throw createErrorWithSchema("Failed to link credential", response.status, response.data);
136
137
  }
137
138
  },
138
139
  /**
@@ -144,7 +145,7 @@ export function createAuthApi(apiClient) {
144
145
  : undefined;
145
146
  const response = await apiClient.post("/v1/auth/change-password", { currentPassword, newPassword }, options);
146
147
  if (!response.ok) {
147
- throw new Error(`Failed to change password: ${response.status}`);
148
+ throw createErrorWithSchema("Failed to change password", response.status, response.data);
148
149
  }
149
150
  return response.data;
150
151
  },
@@ -160,12 +161,7 @@ export function createAuthApi(apiClient) {
160
161
  : undefined;
161
162
  const response = await apiClient.post("/v1/auth/change-email", { newEmail, callbackURL }, options);
162
163
  if (!response.ok) {
163
- // 提取服务器返回的错误代码
164
- const errorData = response.data;
165
- if (errorData?.code) {
166
- throw new Error(JSON.stringify({ code: errorData.code, message: errorData.message }));
167
- }
168
- throw new Error(`Failed to change email: ${response.status}`);
164
+ throw createErrorWithSchema("Failed to change email", response.status, response.data);
169
165
  }
170
166
  return response.data;
171
167
  },
@@ -186,7 +182,7 @@ export function createAuthApi(apiClient) {
186
182
  : undefined;
187
183
  const response = await apiClient.get("/v1/auth/list-accounts", options);
188
184
  if (!response.ok) {
189
- throw new Error(`Failed to list accounts: ${response.status}`);
185
+ throw createErrorWithSchema("Failed to list accounts", response.status, response.data);
190
186
  }
191
187
  return response.data;
192
188
  },
@@ -222,7 +218,7 @@ export function createAuthApi(apiClient) {
222
218
  : undefined;
223
219
  const response = await apiClient.post("/v1/auth/unlink-account", { accountId, providerId }, options);
224
220
  if (!response.ok) {
225
- throw new Error(`Failed to unlink account: ${response.status}`);
221
+ throw createErrorWithSchema("Failed to unlink account", response.status, response.data);
226
222
  }
227
223
  return response.data;
228
224
  },
@@ -235,7 +231,7 @@ export function createAuthApi(apiClient) {
235
231
  : undefined;
236
232
  const response = await apiClient.post("/v1/auth/delete-user", { password, callbackURL }, options);
237
233
  if (!response.ok) {
238
- throw new Error(`Failed to delete user: ${response.status}`);
234
+ throw createErrorWithSchema("Failed to delete user", response.status, response.data);
239
235
  }
240
236
  return response.data;
241
237
  },
@@ -251,7 +247,7 @@ export function createAuthApi(apiClient) {
251
247
  : undefined;
252
248
  const response = await apiClient.get("/v1/auth/list-sessions", options);
253
249
  if (!response.ok) {
254
- throw new Error(`Failed to list sessions: ${response.status}`);
250
+ throw createErrorWithSchema("Failed to list sessions", response.status, response.data);
255
251
  }
256
252
  return response.data;
257
253
  },
@@ -264,7 +260,7 @@ export function createAuthApi(apiClient) {
264
260
  : undefined;
265
261
  const response = await apiClient.post("/v1/auth/revoke-session", params, options);
266
262
  if (!response.ok) {
267
- throw new Error(`Failed to revoke session: ${response.status}`);
263
+ throw createErrorWithSchema("Failed to revoke session", response.status, response.data);
268
264
  }
269
265
  return response.data;
270
266
  },
@@ -1 +1 @@
1
- {"version":3,"file":"organization-api.d.ts","sourceRoot":"","sources":["../../src/api/organization-api.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EAChB,yBAAyB,EACzB,yBAAyB,EACzB,yBAAyB,EACzB,gBAAgB,EAChB,MAAM,EACN,mBAAmB,EACnB,uBAAuB,EACvB,UAAU,EACV,gBAAgB,EAChB,mBAAmB,EACnB,uBAAuB,EACvB,uBAAuB,EACvB,uBAAuB,EACvB,kBAAkB,EACnB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAEzC;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM;IAQxE;;OAEG;oBACmB,yB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CAAC,IAAI,CAAC;IAOvE;;OAEG;8BAC6B,uBAAuB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAQrF;;OAEG;8BAC6B,uBAAuB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAQrF;;;;OAIG;0BACyB,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;EAOrD;AAED,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,qBAAqB,CAAC,CAAA"}
@@ -4,6 +4,7 @@
4
4
  * 封装所有组织管理的 API 调用
5
5
  * 基于 Core AI Auth API 文档
6
6
  */
7
+ import { createErrorWithSchema } from "../utils/error-schema";
7
8
  /**
8
9
  * 创建组织 API
9
10
  *
@@ -22,7 +23,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
22
23
  async create(request) {
23
24
  const response = await apiClient.post(`${basePath}/create`, request);
24
25
  if (!response.ok) {
25
- throw new Error(`Failed to create organization: ${response.status}`);
26
+ throw createErrorWithSchema("Failed to create organization", response.status, response.data);
26
27
  }
27
28
  return response.data;
28
29
  },
@@ -32,7 +33,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
32
33
  async update(request) {
33
34
  const response = await apiClient.post(`${basePath}/update`, request);
34
35
  if (!response.ok) {
35
- throw new Error(`Failed to update organization: ${response.status}`);
36
+ throw createErrorWithSchema("Failed to update organization", response.status, response.data);
36
37
  }
37
38
  return response.data;
38
39
  },
@@ -42,7 +43,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
42
43
  async delete(request) {
43
44
  const response = await apiClient.post(`${basePath}/delete`, request);
44
45
  if (!response.ok) {
45
- throw new Error(`Failed to delete organization: ${response.status}`);
46
+ throw createErrorWithSchema("Failed to delete organization", response.status, response.data);
46
47
  }
47
48
  return response.data;
48
49
  },
@@ -52,7 +53,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
52
53
  async list() {
53
54
  const response = await apiClient.get(`${basePath}/list`);
54
55
  if (!response.ok) {
55
- throw new Error(`Failed to list organizations: ${response.status}`);
56
+ throw createErrorWithSchema("Failed to list organizations", response.status, response.data);
56
57
  }
57
58
  return response.data || [];
58
59
  },
@@ -65,7 +66,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
65
66
  if (response.status === 404) {
66
67
  return null;
67
68
  }
68
- throw new Error(`Failed to get organization: ${response.status}`);
69
+ throw createErrorWithSchema("Failed to get organization", response.status, response.data);
69
70
  }
70
71
  return response.data;
71
72
  },
@@ -79,7 +80,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
79
80
  if (response.status === 400) {
80
81
  return false;
81
82
  }
82
- throw new Error(`Failed to check slug: ${response.status}`);
83
+ throw createErrorWithSchema("Failed to check slug", response.status, response.data);
83
84
  }
84
85
  return true;
85
86
  },
@@ -95,7 +96,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
95
96
  if (response.status === 404) {
96
97
  return null;
97
98
  }
98
- throw new Error(`Failed to get active member: ${response.status}`);
99
+ throw createErrorWithSchema("Failed to get active member", response.status, response.data);
99
100
  }
100
101
  return response.data;
101
102
  },
@@ -108,7 +109,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
108
109
  if (response.status === 404) {
109
110
  return null;
110
111
  }
111
- throw new Error(`Failed to get active member role: ${response.status}`);
112
+ throw createErrorWithSchema("Failed to get active member role", response.status, response.data);
112
113
  }
113
114
  return response.data;
114
115
  },
@@ -118,7 +119,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
118
119
  async removeMember(request) {
119
120
  const response = await apiClient.post(`${basePath}/remove-member`, request);
120
121
  if (!response.ok) {
121
- throw new Error(`Failed to remove member: ${response.status}`);
122
+ throw createErrorWithSchema("Failed to remove member", response.status, response.data);
122
123
  }
123
124
  return response.data.member;
124
125
  },
@@ -128,7 +129,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
128
129
  async updateMemberRole(request) {
129
130
  const response = await apiClient.post(`${basePath}/update-member-role`, request);
130
131
  if (!response.ok) {
131
- throw new Error(`Failed to update member role: ${response.status}`);
132
+ throw createErrorWithSchema("Failed to update member role", response.status, response.data);
132
133
  }
133
134
  return response.data.member;
134
135
  },
@@ -142,7 +143,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
142
143
  async inviteMember(request) {
143
144
  const response = await apiClient.post(`${basePath}/invite-member`, request);
144
145
  if (!response.ok) {
145
- throw new Error(`Failed to invite member: ${response.status}`);
146
+ throw createErrorWithSchema("Failed to invite member", response.status, response.data);
146
147
  }
147
148
  return response.data;
148
149
  },
@@ -152,7 +153,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
152
153
  async listInvitations() {
153
154
  const response = await apiClient.get(`${basePath}/list-invitations`);
154
155
  if (!response.ok) {
155
- throw new Error(`Failed to list invitations: ${response.status}`);
156
+ throw createErrorWithSchema("Failed to list invitations", response.status, response.data);
156
157
  }
157
158
  return response.data || [];
158
159
  },
@@ -165,7 +166,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
165
166
  if (response.status === 404) {
166
167
  return null;
167
168
  }
168
- throw new Error(`Failed to get invitation: ${response.status}`);
169
+ throw createErrorWithSchema("Failed to get invitation", response.status, response.data);
169
170
  }
170
171
  return response.data;
171
172
  },
@@ -175,7 +176,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
175
176
  async cancelInvitation(request) {
176
177
  const response = await apiClient.post(`${basePath}/cancel-invitation`, request);
177
178
  if (!response.ok) {
178
- throw new Error(`Failed to cancel invitation: ${response.status}`);
179
+ throw createErrorWithSchema("Failed to cancel invitation", response.status, response.data);
179
180
  }
180
181
  },
181
182
  /**
@@ -184,7 +185,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
184
185
  async acceptInvitation(request) {
185
186
  const response = await apiClient.post(`${basePath}/accept-invitation`, request);
186
187
  if (!response.ok) {
187
- throw new Error(`Failed to accept invitation: ${response.status}`);
188
+ throw createErrorWithSchema("Failed to accept invitation", response.status, response.data);
188
189
  }
189
190
  return response.data;
190
191
  },
@@ -194,7 +195,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
194
195
  async rejectInvitation(request) {
195
196
  const response = await apiClient.post(`${basePath}/reject-invitation`, request);
196
197
  if (!response.ok) {
197
- throw new Error(`Failed to reject invitation: ${response.status}`);
198
+ throw createErrorWithSchema("Failed to reject invitation", response.status, response.data);
198
199
  }
199
200
  return response.data;
200
201
  },
@@ -206,7 +207,7 @@ export function createOrganizationApi(apiClient, _baseURL) {
206
207
  async leave(organizationId) {
207
208
  const response = await apiClient.post(`${basePath}/leave`, { organizationId });
208
209
  if (!response.ok) {
209
- throw new Error(`Failed to leave organization: ${response.status}`);
210
+ throw createErrorWithSchema("Failed to leave organization", response.status, response.data);
210
211
  }
211
212
  },
212
213
  };
@@ -1 +1 @@
1
- {"version":3,"file":"team-api.d.ts","sourceRoot":"","sources":["../../src/api/team-api.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,EACvB,IAAI,EACJ,UAAU,EACV,iBAAiB,EAClB,MAAM,eAAe,CAAA;AACtB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAEzC;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,SAAS;IAQ9C;;OAEG;oBACmB,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAWvD;;OAEG;YACW,OAAO,CAAC,IAAI,EAAE,CAAC;IAQ7B;;OAEG;oBACmB,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAQvD;;OAEG;oBACmB,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAOvD;;OAEG;qBACoB,OAAO,CAAC,IAAI,EAAE,CAAC;IAYtC;;OAEG;wBACuB,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAUxD;;OAEG;uBACsB,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC;IAQnE;;OAEG;0BACyB,uBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC;IAOnE;;OAEG;sBACqB,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;EAOjD;AAED,MAAM,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAA"}
1
+ {"version":3,"file":"team-api.d.ts","sourceRoot":"","sources":["../../src/api/team-api.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,EACvB,IAAI,EACJ,UAAU,EACV,iBAAiB,EAClB,MAAM,eAAe,CAAA;AAEtB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAEzC;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,SAAS;IAQ9C;;OAEG;oBACmB,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAWvD;;OAEG;YACW,OAAO,CAAC,IAAI,EAAE,CAAC;IAQ7B;;OAEG;oBACmB,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAQvD;;OAEG;oBACmB,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAOvD;;OAEG;qBACoB,OAAO,CAAC,IAAI,EAAE,CAAC;IAYtC;;OAEG;wBACuB,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAUxD;;OAEG;uBACsB,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC;IAQnE;;OAEG;0BACyB,uBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC;IAOnE;;OAEG;sBACqB,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;EAOjD;AAED,MAAM,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAA"}
@@ -4,6 +4,7 @@
4
4
  * 封装所有团队管理的 API 调用
5
5
  * 基于 Core AI Auth API 文档
6
6
  */
7
+ import { createErrorWithSchema } from "../utils/error-schema";
7
8
  /**
8
9
  * 创建团队 API
9
10
  *
@@ -24,7 +25,7 @@ export function createTeamApi(apiClient) {
24
25
  metadata: request.metadata ?? {},
25
26
  });
26
27
  if (!response.ok) {
27
- throw new Error(`Failed to create team: ${response.status}`);
28
+ throw createErrorWithSchema("Failed to create team", response.status, response.data);
28
29
  }
29
30
  return response.data;
30
31
  },
@@ -34,7 +35,7 @@ export function createTeamApi(apiClient) {
34
35
  async list() {
35
36
  const response = await apiClient.get(`${basePath}/list-teams`);
36
37
  if (!response.ok) {
37
- throw new Error(`Failed to list teams: ${response.status}`);
38
+ throw createErrorWithSchema("Failed to list teams", response.status, response.data);
38
39
  }
39
40
  return response.data || [];
40
41
  },
@@ -44,7 +45,7 @@ export function createTeamApi(apiClient) {
44
45
  async update(request) {
45
46
  const response = await apiClient.post(`${basePath}/update-team`, request);
46
47
  if (!response.ok) {
47
- throw new Error(`Failed to update team: ${response.status}`);
48
+ throw createErrorWithSchema("Failed to update team", response.status, response.data);
48
49
  }
49
50
  return response.data;
50
51
  },
@@ -54,7 +55,7 @@ export function createTeamApi(apiClient) {
54
55
  async delete(request) {
55
56
  const response = await apiClient.post(`${basePath}/remove-team`, request);
56
57
  if (!response.ok) {
57
- throw new Error(`Failed to delete team: ${response.status}`);
58
+ throw createErrorWithSchema("Failed to delete team", response.status, response.data);
58
59
  }
59
60
  },
60
61
  /**
@@ -63,7 +64,7 @@ export function createTeamApi(apiClient) {
63
64
  async listUserTeams() {
64
65
  const response = await apiClient.get(`${basePath}/list-user-teams`);
65
66
  if (!response.ok) {
66
- throw new Error(`Failed to list user teams: ${response.status}`);
67
+ throw createErrorWithSchema("Failed to list user teams", response.status, response.data);
67
68
  }
68
69
  return response.data || [];
69
70
  },
@@ -76,7 +77,7 @@ export function createTeamApi(apiClient) {
76
77
  async listMembers(teamId) {
77
78
  const response = await apiClient.get(`${basePath}/list-team-members?teamId=${encodeURIComponent(teamId)}`);
78
79
  if (!response.ok) {
79
- throw new Error(`Failed to list team members: ${response.status}`);
80
+ throw createErrorWithSchema("Failed to list team members", response.status, response.data);
80
81
  }
81
82
  return response.data || [];
82
83
  },
@@ -86,7 +87,7 @@ export function createTeamApi(apiClient) {
86
87
  async addMember(request) {
87
88
  const response = await apiClient.post(`${basePath}/add-team-member`, request);
88
89
  if (!response.ok) {
89
- throw new Error(`Failed to add team member: ${response.status}`);
90
+ throw createErrorWithSchema("Failed to add team member", response.status, response.data);
90
91
  }
91
92
  return response.data;
92
93
  },
@@ -96,7 +97,7 @@ export function createTeamApi(apiClient) {
96
97
  async removeMember(request) {
97
98
  const response = await apiClient.post(`${basePath}/remove-team-member`, request);
98
99
  if (!response.ok) {
99
- throw new Error(`Failed to remove team member: ${response.status}`);
100
+ throw createErrorWithSchema("Failed to remove team member", response.status, response.data);
100
101
  }
101
102
  },
102
103
  /**
@@ -105,7 +106,7 @@ export function createTeamApi(apiClient) {
105
106
  async leaveTeam(teamId) {
106
107
  const response = await apiClient.post("/v1/auth/leave-team", { teamId });
107
108
  if (!response.ok) {
108
- throw new Error(`Failed to leave team: ${response.status}`);
109
+ throw createErrorWithSchema("Failed to leave team", response.status, response.data);
109
110
  }
110
111
  },
111
112
  };
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Error Schema Validation
3
+ *
4
+ * Provides utilities to parse error code and message from unknown data
5
+ */
6
+ export interface ErrorSchema {
7
+ code?: string;
8
+ message?: string;
9
+ }
10
+ /**
11
+ * Validates and extracts error code and message from unknown data
12
+ *
13
+ * @param data - Unknown data that might contain error information
14
+ * @returns Object with code and message if found, otherwise undefined values
15
+ */
16
+ export declare function parseErrorSchema(data: unknown): ErrorSchema;
17
+ /**
18
+ * Creates an Error with code and message appended
19
+ *
20
+ * @param baseMessage - Base error message
21
+ * @param status - HTTP status code
22
+ * @param data - Response data that might contain error code and message
23
+ * @returns Error instance with code and message appended
24
+ */
25
+ export declare function createErrorWithSchema(baseMessage: string, status: number, data?: unknown): Error;
26
+ //# sourceMappingURL=error-schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-schema.d.ts","sourceRoot":"","sources":["../../src/utils/error-schema.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,GAAG,WAAW,CAqB3D;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,OAAO,GACb,KAAK,CAuBP"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Error Schema Validation
3
+ *
4
+ * Provides utilities to parse error code and message from unknown data
5
+ */
6
+ /**
7
+ * Validates and extracts error code and message from unknown data
8
+ *
9
+ * @param data - Unknown data that might contain error information
10
+ * @returns Object with code and message if found, otherwise undefined values
11
+ */
12
+ export function parseErrorSchema(data) {
13
+ if (!data || typeof data !== "object") {
14
+ return { code: undefined, message: undefined };
15
+ }
16
+ const obj = data;
17
+ // Check for direct code and message properties
18
+ const code = typeof obj.code === "string" ? obj.code : undefined;
19
+ const message = typeof obj.message === "string" ? obj.message : undefined;
20
+ // Check for nested error object
21
+ if (!code && !message && typeof obj.error === "object" && obj.error !== null) {
22
+ const errorObj = obj.error;
23
+ return {
24
+ code: typeof errorObj.code === "string" ? errorObj.code : undefined,
25
+ message: typeof errorObj.message === "string" ? errorObj.message : undefined,
26
+ };
27
+ }
28
+ return { code, message };
29
+ }
30
+ /**
31
+ * Creates an Error with code and message appended
32
+ *
33
+ * @param baseMessage - Base error message
34
+ * @param status - HTTP status code
35
+ * @param data - Response data that might contain error code and message
36
+ * @returns Error instance with code and message appended
37
+ */
38
+ export function createErrorWithSchema(baseMessage, status, data) {
39
+ const schema = parseErrorSchema(data);
40
+ const parts = [baseMessage];
41
+ if (schema.code) {
42
+ parts.push(`code: ${schema.code}`);
43
+ }
44
+ if (schema.message) {
45
+ parts.push(`message: ${schema.message}`);
46
+ }
47
+ parts.push(`status: ${status}`);
48
+ const error = new Error(parts.join(", "));
49
+ // Attach code and message as properties for programmatic access
50
+ if (schema.code) {
51
+ ;
52
+ error.code = schema.code;
53
+ }
54
+ if (schema.message) {
55
+ ;
56
+ error.message = schema.message;
57
+ }
58
+ ;
59
+ error.status = status;
60
+ return error;
61
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@choiceform/shared-auth",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "description": "Shared authentication package for Choiceform projects",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -16,14 +16,6 @@
16
16
  "dist",
17
17
  "README.md"
18
18
  ],
19
- "scripts": {
20
- "build": "tsc",
21
- "dev": "tsc --watch",
22
- "clean": "rimraf dist",
23
- "test": "vitest run",
24
- "test:watch": "vitest",
25
- "prepublishOnly": "pnpm run build"
26
- },
27
19
  "repository": {
28
20
  "type": "git",
29
21
  "url": "git+https://github.com/choiceform/automation.git",
@@ -65,5 +57,11 @@
65
57
  "typescript": "^5.9.3",
66
58
  "vitest": "^3.2.4"
67
59
  },
68
- "packageManager": "pnpm@10.25.0+sha512.5e82639027af37cf832061bcc6d639c219634488e0f2baebe785028a793de7b525ffcd3f7ff574f5e9860654e098fe852ba8ac5dd5cefe1767d23a020a92f501"
69
- }
60
+ "scripts": {
61
+ "build": "tsc",
62
+ "dev": "tsc --watch",
63
+ "clean": "rimraf dist",
64
+ "test": "vitest run",
65
+ "test:watch": "vitest"
66
+ }
67
+ }