@hirefinn/sdk 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +177 -2
- package/dist/cjs/approval.d.ts +12 -0
- package/dist/cjs/approval.js +19 -0
- package/dist/cjs/client.d.ts +30 -0
- package/dist/cjs/client.js +35 -0
- package/dist/cjs/core.d.ts +69 -0
- package/dist/cjs/core.js +265 -0
- package/dist/cjs/errors.d.ts +90 -0
- package/dist/cjs/errors.js +165 -0
- package/dist/cjs/generated/openapi.d.ts +10729 -0
- package/dist/cjs/generated/openapi.js +2 -0
- package/dist/cjs/index.d.ts +11 -0
- package/dist/cjs/index.js +44 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/pagination.d.ts +36 -0
- package/dist/cjs/pagination.js +124 -0
- package/dist/cjs/resources.d.ts +845 -0
- package/dist/cjs/resources.js +395 -0
- package/dist/cjs/types.d.ts +144 -0
- package/dist/cjs/types.js +2 -0
- package/dist/esm/approval.d.ts +12 -0
- package/dist/esm/approval.js +16 -0
- package/dist/esm/client.d.ts +30 -0
- package/dist/esm/client.js +31 -0
- package/dist/esm/core.d.ts +69 -0
- package/dist/esm/core.js +260 -0
- package/dist/esm/errors.d.ts +90 -0
- package/dist/esm/errors.js +155 -0
- package/dist/esm/generated/openapi.d.ts +10729 -0
- package/dist/esm/generated/openapi.js +1 -0
- package/dist/esm/index.d.ts +11 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/pagination.d.ts +36 -0
- package/dist/esm/pagination.js +116 -0
- package/dist/esm/resources.d.ts +845 -0
- package/dist/esm/resources.js +377 -0
- package/dist/esm/types.d.ts +144 -0
- package/dist/esm/types.js +1 -0
- package/package.json +51 -4
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors thrown by the SDK.
|
|
3
|
+
*
|
|
4
|
+
* The API answers errors in one of three shapes, and FinnError reads all of them:
|
|
5
|
+
*
|
|
6
|
+
* - unified (newer endpoints: Agents, Chat, and errors raised by shared middleware on them):
|
|
7
|
+
* `{ "error": { "code", "message", "fields", "request_id", "doc_url" } }`
|
|
8
|
+
* - legacy (older /v1 endpoints): `{ "error": "<code>", "message": "...", ...extra }`
|
|
9
|
+
* - Operator pass-through (Cases, Approvals): `{ "code": "<code>", "message": "...", "details": ... }`
|
|
10
|
+
*
|
|
11
|
+
* Branch on `code`, never on `message`.
|
|
12
|
+
*/
|
|
13
|
+
export interface FinnFieldError {
|
|
14
|
+
/** Dotted path into the body or query, e.g. `variables.first_name` or `limit`. */
|
|
15
|
+
field: string;
|
|
16
|
+
code: string;
|
|
17
|
+
message: string;
|
|
18
|
+
}
|
|
19
|
+
/** The shared error codes. Endpoints may also answer resource-specific codes such as `finn_not_found`. */
|
|
20
|
+
export type FinnErrorCode = 'invalid_request' | 'invalid_cursor' | 'unauthorized' | 'api_key_required' | 'invalid_api_key' | 'insufficient_scope' | 'forbidden' | 'not_found' | 'conflict' | 'idempotency_in_progress' | 'payload_too_large' | 'idempotency_key_reused' | 'rate_limited' | 'internal_error' | 'unavailable' | (string & {});
|
|
21
|
+
/** The code an error gets when the body does not name one (mirrors the backend's codeForStatus). */
|
|
22
|
+
export declare const codeForStatus: (status: number) => string;
|
|
23
|
+
/** Base class for every error the SDK throws. */
|
|
24
|
+
export declare class FinnSdkError extends Error {
|
|
25
|
+
constructor(message: string, options?: {
|
|
26
|
+
cause?: unknown;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/** The API answered with a 4xx or 5xx status. */
|
|
30
|
+
export declare class FinnError extends FinnSdkError {
|
|
31
|
+
/** HTTP status. */
|
|
32
|
+
readonly status: number;
|
|
33
|
+
/** Stable error code. */
|
|
34
|
+
readonly code: FinnErrorCode;
|
|
35
|
+
/** Matches the `X-Request-Id` response header; quote it to support. */
|
|
36
|
+
readonly requestId: string | null;
|
|
37
|
+
/** Per-field problems (unified shape only; `[]` otherwise). */
|
|
38
|
+
readonly fields: FinnFieldError[];
|
|
39
|
+
/** Link to the error's documentation, when the API sent one. */
|
|
40
|
+
readonly docUrl: string | null;
|
|
41
|
+
/** Dashboard page that fixes the problem (`compliance_required`, `insufficient_credits`, ...), when sent. */
|
|
42
|
+
readonly actionUrl: string | null;
|
|
43
|
+
/** The scope the key lacks, on `403 insufficient_scope`. */
|
|
44
|
+
readonly requiredScope: string | null;
|
|
45
|
+
/** Seconds the API asked us to wait (Retry-After), when sent. */
|
|
46
|
+
readonly retryAfter: number | null;
|
|
47
|
+
/** Extra fields from legacy and Operator bodies (`details`, `deployment_id`, `required_credits`, ...). */
|
|
48
|
+
readonly details: Record<string, unknown>;
|
|
49
|
+
/** The parsed response body (or raw text when it was not JSON). */
|
|
50
|
+
readonly body: unknown;
|
|
51
|
+
/** Response headers. */
|
|
52
|
+
readonly headers: Headers;
|
|
53
|
+
constructor(init: {
|
|
54
|
+
status: number;
|
|
55
|
+
code: string;
|
|
56
|
+
message: string;
|
|
57
|
+
requestId?: string | null;
|
|
58
|
+
fields?: FinnFieldError[];
|
|
59
|
+
docUrl?: string | null;
|
|
60
|
+
actionUrl?: string | null;
|
|
61
|
+
requiredScope?: string | null;
|
|
62
|
+
retryAfter?: number | null;
|
|
63
|
+
details?: Record<string, unknown>;
|
|
64
|
+
body?: unknown;
|
|
65
|
+
headers?: Headers;
|
|
66
|
+
});
|
|
67
|
+
/** Builds a FinnError from a failed response and its already-read body. */
|
|
68
|
+
static fromResponse(status: number, headers: Headers, body: unknown, statusText?: string): FinnError;
|
|
69
|
+
}
|
|
70
|
+
/** The request never got an HTTP answer (DNS, connection reset, CORS, ...). */
|
|
71
|
+
export declare class FinnConnectionError extends FinnSdkError {
|
|
72
|
+
constructor(message?: string, options?: {
|
|
73
|
+
cause?: unknown;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** The request took longer than `timeoutMs`. */
|
|
77
|
+
export declare class FinnTimeoutError extends FinnConnectionError {
|
|
78
|
+
constructor(message?: string, options?: {
|
|
79
|
+
cause?: unknown;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/** waitForApproval gave up before the approval reached a final status. */
|
|
83
|
+
export declare class FinnApprovalTimeoutError extends FinnSdkError {
|
|
84
|
+
readonly approvalId: string;
|
|
85
|
+
/** The last status seen. */
|
|
86
|
+
readonly lastStatus: string | null;
|
|
87
|
+
constructor(approvalId: string, lastStatus: string | null);
|
|
88
|
+
}
|
|
89
|
+
/** Retry-After as seconds: accepts delta-seconds or an HTTP date. */
|
|
90
|
+
export declare function parseRetryAfterSeconds(value: string | null, now?: number): number | null;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Errors thrown by the SDK.
|
|
4
|
+
*
|
|
5
|
+
* The API answers errors in one of three shapes, and FinnError reads all of them:
|
|
6
|
+
*
|
|
7
|
+
* - unified (newer endpoints: Agents, Chat, and errors raised by shared middleware on them):
|
|
8
|
+
* `{ "error": { "code", "message", "fields", "request_id", "doc_url" } }`
|
|
9
|
+
* - legacy (older /v1 endpoints): `{ "error": "<code>", "message": "...", ...extra }`
|
|
10
|
+
* - Operator pass-through (Cases, Approvals): `{ "code": "<code>", "message": "...", "details": ... }`
|
|
11
|
+
*
|
|
12
|
+
* Branch on `code`, never on `message`.
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.FinnApprovalTimeoutError = exports.FinnTimeoutError = exports.FinnConnectionError = exports.FinnError = exports.FinnSdkError = exports.codeForStatus = void 0;
|
|
16
|
+
exports.parseRetryAfterSeconds = parseRetryAfterSeconds;
|
|
17
|
+
const CODE_FOR_STATUS = {
|
|
18
|
+
400: 'invalid_request',
|
|
19
|
+
401: 'unauthorized',
|
|
20
|
+
403: 'forbidden',
|
|
21
|
+
404: 'not_found',
|
|
22
|
+
409: 'conflict',
|
|
23
|
+
413: 'payload_too_large',
|
|
24
|
+
422: 'invalid_request',
|
|
25
|
+
429: 'rate_limited',
|
|
26
|
+
500: 'internal_error',
|
|
27
|
+
502: 'unavailable',
|
|
28
|
+
503: 'unavailable',
|
|
29
|
+
504: 'unavailable',
|
|
30
|
+
};
|
|
31
|
+
/** The code an error gets when the body does not name one (mirrors the backend's codeForStatus). */
|
|
32
|
+
const codeForStatus = (status) => CODE_FOR_STATUS[status] ?? (status >= 500 ? 'internal_error' : 'invalid_request');
|
|
33
|
+
exports.codeForStatus = codeForStatus;
|
|
34
|
+
/** Base class for every error the SDK throws. */
|
|
35
|
+
class FinnSdkError extends Error {
|
|
36
|
+
constructor(message, options) {
|
|
37
|
+
super(message);
|
|
38
|
+
this.name = 'FinnSdkError';
|
|
39
|
+
if (options && 'cause' in options)
|
|
40
|
+
this.cause = options.cause;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.FinnSdkError = FinnSdkError;
|
|
44
|
+
/** The API answered with a 4xx or 5xx status. */
|
|
45
|
+
class FinnError extends FinnSdkError {
|
|
46
|
+
constructor(init) {
|
|
47
|
+
super(init.message);
|
|
48
|
+
this.name = 'FinnError';
|
|
49
|
+
this.status = init.status;
|
|
50
|
+
this.code = init.code;
|
|
51
|
+
this.requestId = init.requestId ?? null;
|
|
52
|
+
this.fields = init.fields ?? [];
|
|
53
|
+
this.docUrl = init.docUrl ?? null;
|
|
54
|
+
this.actionUrl = init.actionUrl ?? null;
|
|
55
|
+
this.requiredScope = init.requiredScope ?? null;
|
|
56
|
+
this.retryAfter = init.retryAfter ?? null;
|
|
57
|
+
this.details = init.details ?? {};
|
|
58
|
+
this.body = init.body;
|
|
59
|
+
this.headers = init.headers ?? new Headers();
|
|
60
|
+
}
|
|
61
|
+
/** Builds a FinnError from a failed response and its already-read body. */
|
|
62
|
+
static fromResponse(status, headers, body, statusText = '') {
|
|
63
|
+
const headerRequestId = headers.get('x-request-id');
|
|
64
|
+
const retryAfter = parseRetryAfterSeconds(headers.get('retry-after'));
|
|
65
|
+
const base = { status, headers, body, retryAfter };
|
|
66
|
+
const obj = isRecord(body) ? body : null;
|
|
67
|
+
// Unified: { error: { code, message, fields, request_id, doc_url } }
|
|
68
|
+
if (obj && isRecord(obj.error)) {
|
|
69
|
+
const e = obj.error;
|
|
70
|
+
const { error: _e, ...rest } = obj;
|
|
71
|
+
return new FinnError({
|
|
72
|
+
...base,
|
|
73
|
+
code: str(e.code) ?? (0, exports.codeForStatus)(status),
|
|
74
|
+
message: str(e.message) ?? defaultMessage(status, statusText),
|
|
75
|
+
requestId: str(e.request_id) ?? headerRequestId,
|
|
76
|
+
fields: Array.isArray(e.fields) ? e.fields.filter(isFieldError) : [],
|
|
77
|
+
docUrl: str(e.doc_url),
|
|
78
|
+
actionUrl: str(e.action_url) ?? str(obj.action_url),
|
|
79
|
+
requiredScope: str(e.required_scope) ?? str(obj.required_scope),
|
|
80
|
+
details: rest,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
// Legacy: { error: "<code>", message, ...extra }
|
|
84
|
+
if (obj && typeof obj.error === 'string') {
|
|
85
|
+
const { error, message, action_url, required_scope, request_id, ...rest } = obj;
|
|
86
|
+
return new FinnError({
|
|
87
|
+
...base,
|
|
88
|
+
code: error || (0, exports.codeForStatus)(status),
|
|
89
|
+
message: str(message) ?? defaultMessage(status, statusText),
|
|
90
|
+
requestId: str(request_id) ?? headerRequestId,
|
|
91
|
+
actionUrl: str(action_url),
|
|
92
|
+
requiredScope: str(required_scope),
|
|
93
|
+
details: rest,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
// Operator pass-through: { code, message, details }
|
|
97
|
+
if (obj && typeof obj.code === 'string') {
|
|
98
|
+
const { code, message, request_id, ...rest } = obj;
|
|
99
|
+
return new FinnError({
|
|
100
|
+
...base,
|
|
101
|
+
code: code,
|
|
102
|
+
message: str(message) ?? defaultMessage(status, statusText),
|
|
103
|
+
requestId: str(request_id) ?? headerRequestId,
|
|
104
|
+
details: rest,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return new FinnError({
|
|
108
|
+
...base,
|
|
109
|
+
code: (0, exports.codeForStatus)(status),
|
|
110
|
+
message: typeof body === 'string' && body.trim() && body.length < 500 ? body.trim() : defaultMessage(status, statusText),
|
|
111
|
+
requestId: headerRequestId,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.FinnError = FinnError;
|
|
116
|
+
/** The request never got an HTTP answer (DNS, connection reset, CORS, ...). */
|
|
117
|
+
class FinnConnectionError extends FinnSdkError {
|
|
118
|
+
constructor(message = 'Could not reach the Finn API.', options) {
|
|
119
|
+
super(message, options);
|
|
120
|
+
this.name = 'FinnConnectionError';
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
exports.FinnConnectionError = FinnConnectionError;
|
|
124
|
+
/** The request took longer than `timeoutMs`. */
|
|
125
|
+
class FinnTimeoutError extends FinnConnectionError {
|
|
126
|
+
constructor(message = 'The request to the Finn API timed out.', options) {
|
|
127
|
+
super(message, options);
|
|
128
|
+
this.name = 'FinnTimeoutError';
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
exports.FinnTimeoutError = FinnTimeoutError;
|
|
132
|
+
/** waitForApproval gave up before the approval reached a final status. */
|
|
133
|
+
class FinnApprovalTimeoutError extends FinnSdkError {
|
|
134
|
+
constructor(approvalId, lastStatus) {
|
|
135
|
+
super(`Approval ${approvalId} was still ${lastStatus ?? 'unknown'} when waitForApproval timed out.`);
|
|
136
|
+
this.name = 'FinnApprovalTimeoutError';
|
|
137
|
+
this.approvalId = approvalId;
|
|
138
|
+
this.lastStatus = lastStatus;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
exports.FinnApprovalTimeoutError = FinnApprovalTimeoutError;
|
|
142
|
+
/** Retry-After as seconds: accepts delta-seconds or an HTTP date. */
|
|
143
|
+
function parseRetryAfterSeconds(value, now = Date.now()) {
|
|
144
|
+
if (value == null || value.trim() === '')
|
|
145
|
+
return null;
|
|
146
|
+
const v = value.trim();
|
|
147
|
+
if (/^\d+(\.\d+)?$/.test(v))
|
|
148
|
+
return Number(v);
|
|
149
|
+
const date = Date.parse(v);
|
|
150
|
+
if (Number.isNaN(date))
|
|
151
|
+
return null;
|
|
152
|
+
return Math.max(0, (date - now) / 1000);
|
|
153
|
+
}
|
|
154
|
+
function defaultMessage(status, statusText) {
|
|
155
|
+
return `Finn API request failed with status ${status}${statusText ? ` ${statusText}` : ''}.`;
|
|
156
|
+
}
|
|
157
|
+
function isRecord(v) {
|
|
158
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
159
|
+
}
|
|
160
|
+
function str(v) {
|
|
161
|
+
return typeof v === 'string' && v !== '' ? v : null;
|
|
162
|
+
}
|
|
163
|
+
function isFieldError(v) {
|
|
164
|
+
return isRecord(v) && typeof v.field === 'string';
|
|
165
|
+
}
|