@cstar.help/js 0.14.0 → 0.15.1

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.
@@ -55,6 +55,403 @@ interface QuickHelpAskResult {
55
55
  turnNumber: number;
56
56
  }
57
57
 
58
+ /**
59
+ * Error classes for the cStar SDK.
60
+ *
61
+ * NEW-v3-01 — Cross-subpath instanceof identity.
62
+ *
63
+ * Each public subpath (`/library`, `/auth`, `/community`, `/quickhelp`, ...)
64
+ * is bundled as a separate tsup entry with `splitting: false`, so each one
65
+ * carries its own copy of this module. Without deduplication, errors thrown
66
+ * by `LibraryClient` carry the LibraryClient-bundled `CStarError` class —
67
+ * not the one a consumer imports from `@cstar.help/js`. That makes
68
+ * `instanceof CStarError` checks fail silently, invalidating every "did the
69
+ * SDK throw a typed cStar error?" branch in user code.
70
+ *
71
+ * Fix: register each class on a `Symbol.for(...)` slot of `globalThis`. The
72
+ * symbol is realm-shared, so every bundled copy of this module resolves to
73
+ * the SAME class identity at runtime — the first copy to load wins, and all
74
+ * subsequent copies surface the canonical class via the same slot. The
75
+ * inline `Impl` definitions are only used as the seed for the very first
76
+ * copy to register. Internal `parseError` / `parsePublicError` reference the
77
+ * EXPORTED canonical names so instances they construct also land on the
78
+ * canonical class.
79
+ */
80
+ /** Error type constants matching the cStar API. */
81
+ declare const ErrorType: {
82
+ readonly AUTHENTICATION_ERROR: "authentication_error";
83
+ readonly PERMISSION_ERROR: "permission_error";
84
+ readonly INVALID_REQUEST_ERROR: "invalid_request_error";
85
+ readonly NOT_FOUND_ERROR: "not_found_error";
86
+ readonly CONFLICT_ERROR: "conflict_error";
87
+ readonly RATE_LIMIT_ERROR: "rate_limit_error";
88
+ readonly FEATURE_NOT_CONFIGURED: "feature_not_configured";
89
+ readonly API_ERROR: "api_error";
90
+ readonly SERVER_ERROR: "server_error";
91
+ };
92
+ /** Shape of the error object returned by the cStar API. */
93
+ interface ApiErrorBody {
94
+ type: string;
95
+ code: string;
96
+ message: string;
97
+ param?: string;
98
+ doc_url: string;
99
+ request_id?: string;
100
+ feature?: string;
101
+ enable_url?: string;
102
+ }
103
+ /** Base error class for all cStar API errors. */
104
+ declare const CStarErrorImpl: {
105
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
106
+ readonly type: string;
107
+ readonly code: string;
108
+ readonly param?: string;
109
+ readonly docUrl: string;
110
+ readonly requestId?: string;
111
+ readonly statusCode: number;
112
+ name: string;
113
+ message: string;
114
+ stack?: string;
115
+ };
116
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
117
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
118
+ stackTraceLimit: number;
119
+ };
120
+ declare const CStarAuthenticationErrorImpl: {
121
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
122
+ readonly type: string;
123
+ readonly code: string;
124
+ readonly param?: string;
125
+ readonly docUrl: string;
126
+ readonly requestId?: string;
127
+ readonly statusCode: number;
128
+ name: string;
129
+ message: string;
130
+ stack?: string;
131
+ };
132
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
133
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
134
+ stackTraceLimit: number;
135
+ };
136
+ declare const CStarRateLimitErrorImpl: {
137
+ new (messageOrStatus: string | number, body?: ApiErrorBody, retryAfter?: number): {
138
+ readonly retryAfter: number;
139
+ readonly type: string;
140
+ readonly code: string;
141
+ readonly param?: string;
142
+ readonly docUrl: string;
143
+ readonly requestId?: string;
144
+ readonly statusCode: number;
145
+ name: string;
146
+ message: string;
147
+ stack?: string;
148
+ };
149
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
150
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
151
+ stackTraceLimit: number;
152
+ };
153
+ declare const CStarNotFoundErrorImpl: {
154
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
155
+ readonly type: string;
156
+ readonly code: string;
157
+ readonly param?: string;
158
+ readonly docUrl: string;
159
+ readonly requestId?: string;
160
+ readonly statusCode: number;
161
+ name: string;
162
+ message: string;
163
+ stack?: string;
164
+ };
165
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
166
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
167
+ stackTraceLimit: number;
168
+ };
169
+ declare const CStarValidationErrorImpl: {
170
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
171
+ readonly type: string;
172
+ readonly code: string;
173
+ readonly param?: string;
174
+ readonly docUrl: string;
175
+ readonly requestId?: string;
176
+ readonly statusCode: number;
177
+ name: string;
178
+ message: string;
179
+ stack?: string;
180
+ };
181
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
182
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
183
+ stackTraceLimit: number;
184
+ };
185
+ declare const CStarPermissionErrorImpl: {
186
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
187
+ readonly type: string;
188
+ readonly code: string;
189
+ readonly param?: string;
190
+ readonly docUrl: string;
191
+ readonly requestId?: string;
192
+ readonly statusCode: number;
193
+ name: string;
194
+ message: string;
195
+ stack?: string;
196
+ };
197
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
198
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
199
+ stackTraceLimit: number;
200
+ };
201
+ declare const CStarConflictErrorImpl: {
202
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
203
+ readonly type: string;
204
+ readonly code: string;
205
+ readonly param?: string;
206
+ readonly docUrl: string;
207
+ readonly requestId?: string;
208
+ readonly statusCode: number;
209
+ name: string;
210
+ message: string;
211
+ stack?: string;
212
+ };
213
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
214
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
215
+ stackTraceLimit: number;
216
+ };
217
+ declare const CStarServerErrorImpl: {
218
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
219
+ readonly type: string;
220
+ readonly code: string;
221
+ readonly param?: string;
222
+ readonly docUrl: string;
223
+ readonly requestId?: string;
224
+ readonly statusCode: number;
225
+ name: string;
226
+ message: string;
227
+ stack?: string;
228
+ };
229
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
230
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
231
+ stackTraceLimit: number;
232
+ };
233
+ declare const CStarFeatureDisabledErrorImpl: {
234
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
235
+ readonly feature: string;
236
+ readonly enableUrl: string;
237
+ readonly type: string;
238
+ readonly code: string;
239
+ readonly param?: string;
240
+ readonly docUrl: string;
241
+ readonly requestId?: string;
242
+ readonly statusCode: number;
243
+ name: string;
244
+ message: string;
245
+ stack?: string;
246
+ };
247
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
248
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
249
+ stackTraceLimit: number;
250
+ };
251
+ declare const CStarRealtimeUnavailableErrorImpl: {
252
+ new (message?: string): {
253
+ readonly type: string;
254
+ readonly code: string;
255
+ readonly param?: string;
256
+ readonly docUrl: string;
257
+ readonly requestId?: string;
258
+ readonly statusCode: number;
259
+ name: string;
260
+ message: string;
261
+ stack?: string;
262
+ };
263
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
264
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
265
+ stackTraceLimit: number;
266
+ };
267
+ declare const CStarError: {
268
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
269
+ readonly type: string;
270
+ readonly code: string;
271
+ readonly param?: string;
272
+ readonly docUrl: string;
273
+ readonly requestId?: string;
274
+ readonly statusCode: number;
275
+ name: string;
276
+ message: string;
277
+ stack?: string;
278
+ };
279
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
280
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
281
+ stackTraceLimit: number;
282
+ };
283
+ type CStarError = InstanceType<typeof CStarErrorImpl>;
284
+ declare const CStarAuthenticationError: {
285
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
286
+ readonly type: string;
287
+ readonly code: string;
288
+ readonly param?: string;
289
+ readonly docUrl: string;
290
+ readonly requestId?: string;
291
+ readonly statusCode: number;
292
+ name: string;
293
+ message: string;
294
+ stack?: string;
295
+ };
296
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
297
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
298
+ stackTraceLimit: number;
299
+ };
300
+ type CStarAuthenticationError = InstanceType<typeof CStarAuthenticationErrorImpl>;
301
+ declare const CStarRateLimitError: {
302
+ new (messageOrStatus: string | number, body?: ApiErrorBody, retryAfter?: number): {
303
+ readonly retryAfter: number;
304
+ readonly type: string;
305
+ readonly code: string;
306
+ readonly param?: string;
307
+ readonly docUrl: string;
308
+ readonly requestId?: string;
309
+ readonly statusCode: number;
310
+ name: string;
311
+ message: string;
312
+ stack?: string;
313
+ };
314
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
315
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
316
+ stackTraceLimit: number;
317
+ };
318
+ type CStarRateLimitError = InstanceType<typeof CStarRateLimitErrorImpl>;
319
+ declare const CStarNotFoundError: {
320
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
321
+ readonly type: string;
322
+ readonly code: string;
323
+ readonly param?: string;
324
+ readonly docUrl: string;
325
+ readonly requestId?: string;
326
+ readonly statusCode: number;
327
+ name: string;
328
+ message: string;
329
+ stack?: string;
330
+ };
331
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
332
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
333
+ stackTraceLimit: number;
334
+ };
335
+ type CStarNotFoundError = InstanceType<typeof CStarNotFoundErrorImpl>;
336
+ declare const CStarValidationError: {
337
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
338
+ readonly type: string;
339
+ readonly code: string;
340
+ readonly param?: string;
341
+ readonly docUrl: string;
342
+ readonly requestId?: string;
343
+ readonly statusCode: number;
344
+ name: string;
345
+ message: string;
346
+ stack?: string;
347
+ };
348
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
349
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
350
+ stackTraceLimit: number;
351
+ };
352
+ type CStarValidationError = InstanceType<typeof CStarValidationErrorImpl>;
353
+ declare const CStarPermissionError: {
354
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
355
+ readonly type: string;
356
+ readonly code: string;
357
+ readonly param?: string;
358
+ readonly docUrl: string;
359
+ readonly requestId?: string;
360
+ readonly statusCode: number;
361
+ name: string;
362
+ message: string;
363
+ stack?: string;
364
+ };
365
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
366
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
367
+ stackTraceLimit: number;
368
+ };
369
+ type CStarPermissionError = InstanceType<typeof CStarPermissionErrorImpl>;
370
+ declare const CStarConflictError: {
371
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
372
+ readonly type: string;
373
+ readonly code: string;
374
+ readonly param?: string;
375
+ readonly docUrl: string;
376
+ readonly requestId?: string;
377
+ readonly statusCode: number;
378
+ name: string;
379
+ message: string;
380
+ stack?: string;
381
+ };
382
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
383
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
384
+ stackTraceLimit: number;
385
+ };
386
+ type CStarConflictError = InstanceType<typeof CStarConflictErrorImpl>;
387
+ declare const CStarServerError: {
388
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
389
+ readonly type: string;
390
+ readonly code: string;
391
+ readonly param?: string;
392
+ readonly docUrl: string;
393
+ readonly requestId?: string;
394
+ readonly statusCode: number;
395
+ name: string;
396
+ message: string;
397
+ stack?: string;
398
+ };
399
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
400
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
401
+ stackTraceLimit: number;
402
+ };
403
+ type CStarServerError = InstanceType<typeof CStarServerErrorImpl>;
404
+ declare const CStarFeatureDisabledError: {
405
+ new (messageOrStatus: string | number, body?: ApiErrorBody): {
406
+ readonly feature: string;
407
+ readonly enableUrl: string;
408
+ readonly type: string;
409
+ readonly code: string;
410
+ readonly param?: string;
411
+ readonly docUrl: string;
412
+ readonly requestId?: string;
413
+ readonly statusCode: number;
414
+ name: string;
415
+ message: string;
416
+ stack?: string;
417
+ };
418
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
419
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
420
+ stackTraceLimit: number;
421
+ };
422
+ type CStarFeatureDisabledError = InstanceType<typeof CStarFeatureDisabledErrorImpl>;
423
+ declare const CStarRealtimeUnavailableError: {
424
+ new (message?: string): {
425
+ readonly type: string;
426
+ readonly code: string;
427
+ readonly param?: string;
428
+ readonly docUrl: string;
429
+ readonly requestId?: string;
430
+ readonly statusCode: number;
431
+ name: string;
432
+ message: string;
433
+ stack?: string;
434
+ };
435
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
436
+ prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
437
+ stackTraceLimit: number;
438
+ };
439
+ type CStarRealtimeUnavailableError = InstanceType<typeof CStarRealtimeUnavailableErrorImpl>;
440
+ /**
441
+ * Parse a public-route response (anonymous LibraryClient, CommunityClient,
442
+ * QuickHelpClient, AuthClient) into a typed `CStarError`.
443
+ *
444
+ * Public routes don't always emit the structured `{error: {type, code,
445
+ * doc_url, ...}}` envelope — many emit `{error: 'message string'}` or
446
+ * `{message: '...'}`. This helper normalizes both shapes and dispatches
447
+ * on HTTP status (closes F-026 and F-034 — Library/Auth used to throw
448
+ * plain `Error`).
449
+ */
450
+ declare function parsePublicError(status: number, body: unknown, fallbackMessage: string): CStarError;
451
+ declare function parseError(statusCode: number, body: {
452
+ error?: ApiErrorBody;
453
+ } | null, retryAfter?: number): CStarError;
454
+
58
455
  /**
59
456
  * Quick Help — anonymous AI Q&A client.
60
457
  *
@@ -127,4 +524,4 @@ declare class QuickHelpError extends Error {
127
524
  });
128
525
  }
129
526
 
130
- export { type QuickHelpAskParams, type QuickHelpAskResult, QuickHelpClient, type QuickHelpConfig, QuickHelpError, type QuickHelpPublicConfig, type QuickHelpTurnOutcome };
527
+ export { type ApiErrorBody, CStarAuthenticationError, CStarConflictError, CStarError, CStarFeatureDisabledError, CStarNotFoundError, CStarPermissionError, CStarRateLimitError, CStarRealtimeUnavailableError, CStarServerError, CStarValidationError, ErrorType, type QuickHelpAskParams, type QuickHelpAskResult, QuickHelpClient, type QuickHelpConfig, QuickHelpError, type QuickHelpPublicConfig, type QuickHelpTurnOutcome, parseError, parsePublicError };
@@ -35,7 +35,7 @@ function normalizeArgs(messageOrStatus, maybeBody, defaults) {
35
35
  }
36
36
  };
37
37
  }
38
- var CStarError = class extends Error {
38
+ var CStarErrorImpl = class extends Error {
39
39
  constructor(messageOrStatus, body) {
40
40
  const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body);
41
41
  super(resolved.message);
@@ -48,7 +48,7 @@ var CStarError = class extends Error {
48
48
  this.statusCode = statusCode;
49
49
  }
50
50
  };
51
- var CStarAuthenticationError = class extends CStarError {
51
+ var CStarAuthenticationErrorImpl = class extends CStarErrorImpl {
52
52
  constructor(messageOrStatus, body) {
53
53
  const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
54
54
  type: ErrorType.AUTHENTICATION_ERROR,
@@ -59,7 +59,7 @@ var CStarAuthenticationError = class extends CStarError {
59
59
  this.name = "CStarAuthenticationError";
60
60
  }
61
61
  };
62
- var CStarRateLimitError = class extends CStarError {
62
+ var CStarRateLimitErrorImpl = class extends CStarErrorImpl {
63
63
  constructor(messageOrStatus, body, retryAfter) {
64
64
  const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
65
65
  type: ErrorType.RATE_LIMIT_ERROR,
@@ -71,7 +71,7 @@ var CStarRateLimitError = class extends CStarError {
71
71
  this.retryAfter = retryAfter ?? 60;
72
72
  }
73
73
  };
74
- var CStarNotFoundError = class extends CStarError {
74
+ var CStarNotFoundErrorImpl = class extends CStarErrorImpl {
75
75
  constructor(messageOrStatus, body) {
76
76
  const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
77
77
  type: ErrorType.NOT_FOUND_ERROR,
@@ -82,7 +82,7 @@ var CStarNotFoundError = class extends CStarError {
82
82
  this.name = "CStarNotFoundError";
83
83
  }
84
84
  };
85
- var CStarValidationError = class extends CStarError {
85
+ var CStarValidationErrorImpl = class extends CStarErrorImpl {
86
86
  constructor(messageOrStatus, body) {
87
87
  const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
88
88
  type: ErrorType.INVALID_REQUEST_ERROR,
@@ -93,7 +93,7 @@ var CStarValidationError = class extends CStarError {
93
93
  this.name = "CStarValidationError";
94
94
  }
95
95
  };
96
- var CStarPermissionError = class extends CStarError {
96
+ var CStarPermissionErrorImpl = class extends CStarErrorImpl {
97
97
  constructor(messageOrStatus, body) {
98
98
  const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
99
99
  type: ErrorType.PERMISSION_ERROR,
@@ -104,7 +104,7 @@ var CStarPermissionError = class extends CStarError {
104
104
  this.name = "CStarPermissionError";
105
105
  }
106
106
  };
107
- var CStarConflictError = class extends CStarError {
107
+ var CStarConflictErrorImpl = class extends CStarErrorImpl {
108
108
  constructor(messageOrStatus, body) {
109
109
  const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
110
110
  type: ErrorType.CONFLICT_ERROR,
@@ -115,7 +115,7 @@ var CStarConflictError = class extends CStarError {
115
115
  this.name = "CStarConflictError";
116
116
  }
117
117
  };
118
- var CStarServerError = class extends CStarError {
118
+ var CStarServerErrorImpl = class extends CStarErrorImpl {
119
119
  constructor(messageOrStatus, body) {
120
120
  const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
121
121
  type: ErrorType.SERVER_ERROR,
@@ -126,7 +126,7 @@ var CStarServerError = class extends CStarError {
126
126
  this.name = "CStarServerError";
127
127
  }
128
128
  };
129
- var CStarFeatureDisabledError = class extends CStarError {
129
+ var CStarFeatureDisabledErrorImpl = class extends CStarErrorImpl {
130
130
  constructor(messageOrStatus, body) {
131
131
  const { statusCode, body: resolved } = normalizeArgs(messageOrStatus, body, {
132
132
  type: ErrorType.FEATURE_NOT_CONFIGURED,
@@ -139,6 +139,102 @@ var CStarFeatureDisabledError = class extends CStarError {
139
139
  this.enableUrl = resolved.enable_url ?? "";
140
140
  }
141
141
  };
142
+ var CStarRealtimeUnavailableErrorImpl = class extends CStarErrorImpl {
143
+ constructor(message) {
144
+ const resolved = message ?? "EventSource is not available in this environment. Real-time subscriptions only work in the browser. Move the call to a client-only path (`useEffect` / `onMount`), or pass an `eventSource` polyfill via the realtime options. See https://cstar.help/developers/sdk/realtime#ssr";
145
+ super(0, {
146
+ type: ErrorType.API_ERROR,
147
+ code: "realtime_unavailable",
148
+ message: resolved,
149
+ doc_url: "https://cstar.help/developers/sdk/realtime#ssr"
150
+ });
151
+ this.name = "CStarRealtimeUnavailableError";
152
+ }
153
+ };
154
+ var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@cstar.help/js/errors/v1");
155
+ var globalSlot = globalThis;
156
+ var registry = globalSlot[REGISTRY_KEY] ?? (globalSlot[REGISTRY_KEY] = {
157
+ CStarError: CStarErrorImpl,
158
+ CStarAuthenticationError: CStarAuthenticationErrorImpl,
159
+ CStarRateLimitError: CStarRateLimitErrorImpl,
160
+ CStarNotFoundError: CStarNotFoundErrorImpl,
161
+ CStarValidationError: CStarValidationErrorImpl,
162
+ CStarPermissionError: CStarPermissionErrorImpl,
163
+ CStarConflictError: CStarConflictErrorImpl,
164
+ CStarServerError: CStarServerErrorImpl,
165
+ CStarFeatureDisabledError: CStarFeatureDisabledErrorImpl,
166
+ CStarRealtimeUnavailableError: CStarRealtimeUnavailableErrorImpl
167
+ });
168
+ var NAME_FIXES = [
169
+ "CStarError",
170
+ "CStarAuthenticationError",
171
+ "CStarRateLimitError",
172
+ "CStarNotFoundError",
173
+ "CStarValidationError",
174
+ "CStarPermissionError",
175
+ "CStarConflictError",
176
+ "CStarServerError",
177
+ "CStarFeatureDisabledError",
178
+ "CStarRealtimeUnavailableError"
179
+ ];
180
+ for (const key of NAME_FIXES) {
181
+ if (registry[key].name !== key) {
182
+ Object.defineProperty(registry[key], "name", {
183
+ value: key,
184
+ configurable: true
185
+ });
186
+ }
187
+ }
188
+ var CStarError = registry.CStarError;
189
+ var CStarAuthenticationError = registry.CStarAuthenticationError;
190
+ var CStarRateLimitError = registry.CStarRateLimitError;
191
+ var CStarNotFoundError = registry.CStarNotFoundError;
192
+ var CStarValidationError = registry.CStarValidationError;
193
+ var CStarPermissionError = registry.CStarPermissionError;
194
+ var CStarConflictError = registry.CStarConflictError;
195
+ var CStarServerError = registry.CStarServerError;
196
+ var CStarFeatureDisabledError = registry.CStarFeatureDisabledError;
197
+ var CStarRealtimeUnavailableError = registry.CStarRealtimeUnavailableError;
198
+ function parsePublicError(status, body, fallbackMessage) {
199
+ const obj = body && typeof body === "object" ? body : null;
200
+ const errVal = obj?.error;
201
+ let message = fallbackMessage;
202
+ let structured = null;
203
+ if (typeof errVal === "string") {
204
+ message = errVal;
205
+ } else if (errVal && typeof errVal === "object") {
206
+ const e = errVal;
207
+ if (typeof e.message === "string") message = e.message;
208
+ structured = {
209
+ type: typeof e.type === "string" ? e.type : ErrorType.API_ERROR,
210
+ code: typeof e.code === "string" ? e.code : "api_error",
211
+ message,
212
+ param: typeof e.param === "string" ? e.param : void 0,
213
+ doc_url: typeof e.doc_url === "string" ? e.doc_url : DEFAULT_DOC_URL,
214
+ request_id: typeof e.request_id === "string" ? e.request_id : void 0,
215
+ feature: typeof e.feature === "string" ? e.feature : void 0,
216
+ enable_url: typeof e.enable_url === "string" ? e.enable_url : void 0
217
+ };
218
+ } else if (typeof obj?.message === "string") {
219
+ message = obj.message;
220
+ }
221
+ const errorBody = structured ?? {
222
+ type: ErrorType.API_ERROR,
223
+ code: status >= 500 ? "server_error" : "request_failed",
224
+ message,
225
+ doc_url: DEFAULT_DOC_URL
226
+ };
227
+ return parseError(status, { error: errorBody });
228
+ }
229
+ var STATUS_TO_CLASS = {
230
+ 400: CStarValidationError,
231
+ 401: CStarAuthenticationError,
232
+ 403: CStarPermissionError,
233
+ 404: CStarNotFoundError,
234
+ 409: CStarConflictError,
235
+ 422: CStarValidationError,
236
+ 429: CStarRateLimitError
237
+ };
142
238
  var TYPE_TO_CLASS = {
143
239
  [ErrorType.AUTHENTICATION_ERROR]: CStarAuthenticationError,
144
240
  [ErrorType.PERMISSION_ERROR]: CStarPermissionError,
@@ -149,6 +245,27 @@ var TYPE_TO_CLASS = {
149
245
  [ErrorType.SERVER_ERROR]: CStarServerError,
150
246
  [ErrorType.FEATURE_NOT_CONFIGURED]: CStarFeatureDisabledError
151
247
  };
248
+ function parseError(statusCode, body, retryAfter) {
249
+ const errorBody = body?.error ?? {
250
+ type: ErrorType.API_ERROR,
251
+ code: "unknown_error",
252
+ message: `Request failed with status ${statusCode}`,
253
+ doc_url: DEFAULT_DOC_URL
254
+ };
255
+ if (statusCode === 429) {
256
+ return new CStarRateLimitError(statusCode, errorBody, retryAfter ?? 60);
257
+ }
258
+ const StatusCls = STATUS_TO_CLASS[statusCode];
259
+ if (StatusCls) {
260
+ return new StatusCls(statusCode, errorBody);
261
+ }
262
+ const TypeCls = TYPE_TO_CLASS[errorBody.type];
263
+ if (TypeCls) {
264
+ return new TypeCls(statusCode, errorBody);
265
+ }
266
+ if (statusCode >= 500) return new CStarServerError(statusCode, errorBody);
267
+ return new CStarError(statusCode, errorBody);
268
+ }
152
269
 
153
270
  // src/quickhelp/index.ts
154
271
  var DEFAULT_BASE_URL = "https://www.cstar.help";
@@ -282,6 +399,19 @@ function parseRetryAfter(header) {
282
399
  return Number.isFinite(n) && n > 0 ? n : void 0;
283
400
  }
284
401
  export {
402
+ CStarAuthenticationError,
403
+ CStarConflictError,
404
+ CStarError,
405
+ CStarFeatureDisabledError,
406
+ CStarNotFoundError,
407
+ CStarPermissionError,
408
+ CStarRateLimitError,
409
+ CStarRealtimeUnavailableError,
410
+ CStarServerError,
411
+ CStarValidationError,
412
+ ErrorType,
285
413
  QuickHelpClient,
286
- QuickHelpError
414
+ QuickHelpError,
415
+ parseError,
416
+ parsePublicError
287
417
  };