@hirefinn/sdk 0.5.0 → 0.5.2

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.
@@ -24,6 +24,28 @@ const CODE_FOR_STATUS = {
24
24
  503: 'unavailable',
25
25
  504: 'unavailable',
26
26
  };
27
+ /**
28
+ * Error codes the API renamed: old code -> its neutral successor. The new paths (`/assistants`, ...) answer the neutral
29
+ * code; the old paths answered the old one. {@link FinnError.hasCode} matches either name.
30
+ */
31
+ export const RENAMED_ERROR_CODES = Object.freeze({
32
+ finn_not_found: 'assistant_not_found',
33
+ finn_in_use: 'assistant_in_use',
34
+ finn_paused: 'assistant_paused',
35
+ finn_not_draft: 'assistant_not_draft',
36
+ finn_not_yours: 'assistant_not_yours',
37
+ invalid_finn_id: 'invalid_assistant_id',
38
+ invalid_finn_version: 'invalid_api_version',
39
+ });
40
+ const LEGACY_FOR_NEUTRAL = Object.freeze(Object.fromEntries(Object.entries(RENAMED_ERROR_CODES).map(([old, neutral]) => [neutral, old])));
41
+ /** The neutral name of an error code: `finn_not_found` -> `assistant_not_found`; any other code as it is. */
42
+ export const neutralErrorCode = (code) => Object.hasOwn(RENAMED_ERROR_CODES, code) ? RENAMED_ERROR_CODES[code] : code;
43
+ /** The old name of a renamed error code: `assistant_not_found` -> `finn_not_found`; null when it was never renamed. */
44
+ export const legacyErrorCode = (code) => Object.hasOwn(LEGACY_FOR_NEUTRAL, code)
45
+ ? LEGACY_FOR_NEUTRAL[code]
46
+ : Object.hasOwn(RENAMED_ERROR_CODES, code)
47
+ ? code
48
+ : null;
27
49
  /** The code an error gets when the body does not name one (mirrors the backend's codeForStatus). */
28
50
  export const codeForStatus = (status) => CODE_FOR_STATUS[status] ?? (status >= 500 ? 'internal_error' : 'invalid_request');
29
51
  /** Base class for every error the SDK throws. */
@@ -42,6 +64,7 @@ export class FinnError extends FinnSdkError {
42
64
  this.name = 'FinnError';
43
65
  this.status = init.status;
44
66
  this.code = init.code;
67
+ this.legacyCode = legacyErrorCode(init.code);
45
68
  this.requestId = init.requestId ?? null;
46
69
  this.fields = init.fields ?? [];
47
70
  this.docUrl = init.docUrl ?? null;
@@ -52,6 +75,13 @@ export class FinnError extends FinnSdkError {
52
75
  this.body = init.body;
53
76
  this.headers = init.headers ?? new Headers();
54
77
  }
78
+ /**
79
+ * True when this error's code is `code`, by its old or its neutral name: `hasCode('finn_not_found')` and
80
+ * `hasCode('assistant_not_found')` both match either answer.
81
+ */
82
+ hasCode(code) {
83
+ return neutralErrorCode(this.code) === neutralErrorCode(code);
84
+ }
55
85
  /** Builds a FinnError from a failed response and its already-read body. */
56
86
  static fromResponse(status, headers, body, statusText = '') {
57
87
  const headerRequestId = headers.get('x-request-id');
@@ -164,3 +194,14 @@ function legacyFields(v, code, message) {
164
194
  function isFieldError(v) {
165
195
  return isRecord(v) && typeof v.field === 'string';
166
196
  }
197
+ // Neutral names of the error classes: the same classes, so `instanceof` works with either name.
198
+ /** The neutral name of {@link FinnSdkError}. */
199
+ export const SdkError = FinnSdkError;
200
+ /** The neutral name of {@link FinnError}. */
201
+ export const ApiError = FinnError;
202
+ /** The neutral name of {@link FinnConnectionError}. */
203
+ export const ApiConnectionError = FinnConnectionError;
204
+ /** The neutral name of {@link FinnTimeoutError}. */
205
+ export const ApiTimeoutError = FinnTimeoutError;
206
+ /** The neutral name of {@link FinnApprovalTimeoutError}. */
207
+ export const ApprovalTimeoutError = FinnApprovalTimeoutError;