@tracken/cli 0.0.0-stage → 0.2.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.
- package/CHANGELOG.md +404 -0
- package/LICENSE +21 -0
- package/README.md +310 -2
- package/dist/bin/tracken.d.ts +2 -0
- package/dist/bin/tracken.js +5 -0
- package/dist/cli/args.d.ts +17 -0
- package/dist/cli/args.js +114 -0
- package/dist/cli/commands/api.d.ts +2 -0
- package/dist/cli/commands/api.js +73 -0
- package/dist/cli/commands/auth.d.ts +8 -0
- package/dist/cli/commands/auth.js +141 -0
- package/dist/cli/commands/mcp.d.ts +19 -0
- package/dist/cli/commands/mcp.js +89 -0
- package/dist/cli/commands/org.d.ts +24 -0
- package/dist/cli/commands/org.js +123 -0
- package/dist/cli/context.d.ts +54 -0
- package/dist/cli/context.js +135 -0
- package/dist/cli/execute.d.ts +16 -0
- package/dist/cli/execute.js +112 -0
- package/dist/cli/help.d.ts +8 -0
- package/dist/cli/help.js +153 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +114 -0
- package/dist/cli/operation-args.d.ts +25 -0
- package/dist/cli/operation-args.js +245 -0
- package/dist/core/auth/oauth.d.ts +39 -0
- package/dist/core/auth/oauth.js +102 -0
- package/dist/core/auth/scopes.d.ts +19 -0
- package/dist/core/auth/scopes.js +49 -0
- package/dist/core/auth/secret-store.d.ts +60 -0
- package/dist/core/auth/secret-store.js +141 -0
- package/dist/core/auth/session.d.ts +75 -0
- package/dist/core/auth/session.js +200 -0
- package/dist/core/catalog.d.ts +12 -0
- package/dist/core/catalog.js +33 -0
- package/dist/core/client.d.ts +22 -0
- package/dist/core/client.js +45 -0
- package/dist/core/config.d.ts +27 -0
- package/dist/core/config.js +109 -0
- package/dist/core/environments.d.ts +21 -0
- package/dist/core/environments.js +53 -0
- package/dist/core/errors.d.ts +96 -0
- package/dist/core/errors.js +248 -0
- package/dist/core/index.d.ts +21 -0
- package/dist/core/index.js +22 -0
- package/dist/core/jobs.d.ts +15 -0
- package/dist/core/jobs.js +19 -0
- package/dist/core/operations.d.ts +108 -0
- package/dist/core/operations.js +7 -0
- package/dist/core/output/format.d.ts +19 -0
- package/dist/core/output/format.js +62 -0
- package/dist/core/output/render.d.ts +53 -0
- package/dist/core/output/render.js +229 -0
- package/dist/core/pagination.d.ts +2 -0
- package/dist/core/pagination.js +3 -0
- package/dist/core/request.d.ts +29 -0
- package/dist/core/request.js +109 -0
- package/dist/core/version.d.ts +10 -0
- package/dist/core/version.js +13 -0
- package/dist/generated/mcp-tools.d.ts +2 -0
- package/dist/generated/mcp-tools.js +5937 -0
- package/dist/generated/operations.d.ts +4 -0
- package/dist/generated/operations.js +17701 -0
- package/dist/mcp/errors.d.ts +34 -0
- package/dist/mcp/errors.js +100 -0
- package/dist/mcp/live.d.ts +3 -0
- package/dist/mcp/live.js +5 -0
- package/dist/mcp/prompts.d.ts +23 -0
- package/dist/mcp/prompts.js +101 -0
- package/dist/mcp/resources.d.ts +25 -0
- package/dist/mcp/resources.js +125 -0
- package/dist/mcp/server.d.ts +20 -0
- package/dist/mcp/server.js +59 -0
- package/dist/mcp/session.d.ts +60 -0
- package/dist/mcp/session.js +105 -0
- package/dist/mcp/tools.d.ts +17 -0
- package/dist/mcp/tools.js +181 -0
- package/dist/mcp/types.d.ts +25 -0
- package/dist/mcp/types.js +2 -0
- package/node_modules/@tracken/sdk/LICENSE +21 -0
- package/node_modules/@tracken/sdk/README.md +76 -0
- package/node_modules/@tracken/sdk/SOURCE.json +10 -0
- package/node_modules/@tracken/sdk/dist/client.d.ts +94 -0
- package/node_modules/@tracken/sdk/dist/client.js +258 -0
- package/node_modules/@tracken/sdk/dist/credentials.d.ts +60 -0
- package/node_modules/@tracken/sdk/dist/credentials.js +122 -0
- package/node_modules/@tracken/sdk/dist/decimal.d.ts +20 -0
- package/node_modules/@tracken/sdk/dist/decimal.js +67 -0
- package/node_modules/@tracken/sdk/dist/environments.d.ts +14 -0
- package/node_modules/@tracken/sdk/dist/environments.js +16 -0
- package/node_modules/@tracken/sdk/dist/errors.d.ts +150 -0
- package/node_modules/@tracken/sdk/dist/errors.js +190 -0
- package/node_modules/@tracken/sdk/dist/generated/operations.d.ts +1955 -0
- package/node_modules/@tracken/sdk/dist/generated/operations.js +2746 -0
- package/node_modules/@tracken/sdk/dist/generated/schema.d.ts +17988 -0
- package/node_modules/@tracken/sdk/dist/generated/schema.js +5 -0
- package/node_modules/@tracken/sdk/dist/index.d.ts +27 -0
- package/node_modules/@tracken/sdk/dist/index.js +24 -0
- package/node_modules/@tracken/sdk/dist/jobs.d.ts +23 -0
- package/node_modules/@tracken/sdk/dist/jobs.js +80 -0
- package/node_modules/@tracken/sdk/dist/operations.d.ts +23 -0
- package/node_modules/@tracken/sdk/dist/operations.js +2 -0
- package/node_modules/@tracken/sdk/dist/pagination.d.ts +19 -0
- package/node_modules/@tracken/sdk/dist/pagination.js +49 -0
- package/node_modules/@tracken/sdk/dist/request.d.ts +27 -0
- package/node_modules/@tracken/sdk/dist/request.js +10 -0
- package/node_modules/@tracken/sdk/dist/runtime.d.ts +22 -0
- package/node_modules/@tracken/sdk/dist/runtime.js +33 -0
- package/node_modules/@tracken/sdk/dist/tracken.d.ts +74 -0
- package/node_modules/@tracken/sdk/dist/tracken.js +203 -0
- package/node_modules/@tracken/sdk/dist/types.d.ts +130 -0
- package/node_modules/@tracken/sdk/dist/types.js +1 -0
- package/node_modules/@tracken/sdk/dist/version.d.ts +2 -0
- package/node_modules/@tracken/sdk/dist/version.js +2 -0
- package/node_modules/@tracken/sdk/package.json +42 -0
- package/node_modules/openapi-fetch/CHANGELOG.md +565 -0
- package/node_modules/openapi-fetch/CONTRIBUTING.md +102 -0
- package/node_modules/openapi-fetch/LICENSE +21 -0
- package/node_modules/openapi-fetch/README.md +122 -0
- package/node_modules/openapi-fetch/dist/index.cjs +547 -0
- package/node_modules/openapi-fetch/dist/index.cjs.map +1 -0
- package/node_modules/openapi-fetch/dist/index.d.cts +337 -0
- package/node_modules/openapi-fetch/dist/index.d.mts +335 -0
- package/node_modules/openapi-fetch/dist/index.d.ts +337 -0
- package/node_modules/openapi-fetch/dist/index.mjs +531 -0
- package/node_modules/openapi-fetch/dist/index.mjs.map +1 -0
- package/node_modules/openapi-fetch/package.json +77 -0
- package/node_modules/openapi-fetch/src/index.d.ts +345 -0
- package/node_modules/openapi-fetch/src/index.js +689 -0
- package/node_modules/openapi-typescript-helpers/CHANGELOG.md +98 -0
- package/node_modules/openapi-typescript-helpers/LICENSE +21 -0
- package/node_modules/openapi-typescript-helpers/README.md +5 -0
- package/node_modules/openapi-typescript-helpers/dist/index.cjs +3 -0
- package/node_modules/openapi-typescript-helpers/dist/index.cjs.map +1 -0
- package/node_modules/openapi-typescript-helpers/dist/index.d.cts +167 -0
- package/node_modules/openapi-typescript-helpers/dist/index.d.mts +167 -0
- package/node_modules/openapi-typescript-helpers/dist/index.d.ts +167 -0
- package/node_modules/openapi-typescript-helpers/dist/index.mjs +2 -0
- package/node_modules/openapi-typescript-helpers/dist/index.mjs.map +1 -0
- package/node_modules/openapi-typescript-helpers/package.json +39 -0
- package/openapi/SOURCE.json +10 -0
- package/openapi/openapi.yaml +30514 -0
- package/package.json +63 -4
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every failure the SDK raises is a `TrackenError`. An answer from the API that is not a success is a
|
|
3
|
+
* `TrackenApiError`, of the subclass its status names, so a caller can tell "sign in again" from "you
|
|
4
|
+
* may not" from "it changed under you" with `instanceof`, and a specific case by its stable `code`.
|
|
5
|
+
*/
|
|
6
|
+
import type { KNOWN_ERROR_CODES } from "./generated/operations.js";
|
|
7
|
+
/** A code the public document names. The API may add codes, so any string can arrive. */
|
|
8
|
+
export type KnownErrorCode = (typeof KNOWN_ERROR_CODES)[number];
|
|
9
|
+
export type ErrorCode = KnownErrorCode | (string & {});
|
|
10
|
+
export declare class TrackenError extends Error {
|
|
11
|
+
constructor(message: string, options?: {
|
|
12
|
+
cause?: unknown;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/** The request was refused before it was sent: a missing path parameter, a number where money goes. */
|
|
16
|
+
export declare class TrackenUsageError extends TrackenError {
|
|
17
|
+
}
|
|
18
|
+
/** The API (or Tracken ID) could not be reached, after the client's own retries. */
|
|
19
|
+
export declare class TrackenNetworkError extends TrackenError {
|
|
20
|
+
readonly cause?: unknown | undefined;
|
|
21
|
+
constructor(message: string, cause?: unknown | undefined);
|
|
22
|
+
}
|
|
23
|
+
/** Tracken ID refused a credential (RFC 6749 §5.2): `error` is its OAuth code, such as `invalid_client`. */
|
|
24
|
+
export declare class TrackenAuthError extends TrackenError {
|
|
25
|
+
readonly error: string;
|
|
26
|
+
readonly description: string | undefined;
|
|
27
|
+
readonly status: number;
|
|
28
|
+
constructor(error: string, description: string | undefined, status: number);
|
|
29
|
+
}
|
|
30
|
+
/** An asynchronous job finished as `failed`; `job` is the API's last answer about it. */
|
|
31
|
+
export declare class TrackenJobFailedError extends TrackenError {
|
|
32
|
+
readonly job: unknown;
|
|
33
|
+
constructor(message: string, job: unknown);
|
|
34
|
+
}
|
|
35
|
+
/** `waitForJob` gave up before the job finished; poll `path` later to see how it ended. */
|
|
36
|
+
export declare class TrackenJobTimeoutError extends TrackenError {
|
|
37
|
+
readonly path: string;
|
|
38
|
+
readonly jobStatus: string | undefined;
|
|
39
|
+
readonly timeoutMs: number;
|
|
40
|
+
constructor(path: string, jobStatus: string | undefined, timeoutMs: number);
|
|
41
|
+
}
|
|
42
|
+
export interface ErrorDetail {
|
|
43
|
+
code?: string;
|
|
44
|
+
message?: string;
|
|
45
|
+
/** JSON Pointer to the offending request field, or `?name` for a query parameter. */
|
|
46
|
+
pointer?: string;
|
|
47
|
+
}
|
|
48
|
+
/** A non-2xx answer from the API, read from its `{error}` envelope. */
|
|
49
|
+
export declare class TrackenApiError extends TrackenError {
|
|
50
|
+
readonly status: number;
|
|
51
|
+
/** The answer as it came: the parsed `{error}` envelope, or text. */
|
|
52
|
+
readonly body: unknown;
|
|
53
|
+
readonly code: ErrorCode;
|
|
54
|
+
readonly details: ErrorDetail[];
|
|
55
|
+
/** Machine-readable figures of the error, keyed by `code`, for a caller's own localized message. */
|
|
56
|
+
readonly context: Record<string, unknown> | undefined;
|
|
57
|
+
readonly requestId: string | undefined;
|
|
58
|
+
readonly docUrl: string | undefined;
|
|
59
|
+
/** On a conflict with an existing record (a taken `external_ref`): that record's id. */
|
|
60
|
+
readonly existingId: string | undefined;
|
|
61
|
+
/** For 403: the `scope` of `WWW-Authenticate`, as sent: the scopes any one of which would admit the call. */
|
|
62
|
+
readonly missingScope: string | undefined;
|
|
63
|
+
/** `missingScope` as a list. */
|
|
64
|
+
readonly admittingScopes: string[];
|
|
65
|
+
/**
|
|
66
|
+
* For a 401: why the client's one renewal failed (Tracken ID refused the refresh or the
|
|
67
|
+
* credential). Absent when the renewed token was refused too.
|
|
68
|
+
*/
|
|
69
|
+
renewalError: unknown;
|
|
70
|
+
/** What to do about it, when the caller knows better than the API's message (set by a wrapping client). */
|
|
71
|
+
hint: string | undefined;
|
|
72
|
+
/** For 429: seconds the API asked to wait. */
|
|
73
|
+
readonly retryAfter: number | undefined;
|
|
74
|
+
constructor(status: number,
|
|
75
|
+
/** The answer as it came: the parsed `{error}` envelope, or text. */
|
|
76
|
+
body: unknown, headers: Headers);
|
|
77
|
+
}
|
|
78
|
+
/** 400 or 422: the request is invalid; `details` point at every field at fault. */
|
|
79
|
+
export declare class TrackenValidationError extends TrackenApiError {
|
|
80
|
+
}
|
|
81
|
+
/** 401: no token, an expired one, or one for another audience. */
|
|
82
|
+
export declare class TrackenUnauthenticatedError extends TrackenApiError {
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Why an organization's subscription is not active (`context.reason` of `SUBSCRIPTION_INACTIVE`). The
|
|
86
|
+
* document names these four in prose and keeps the list open: accept a value you do not know.
|
|
87
|
+
*/
|
|
88
|
+
export type SubscriptionInactiveReason = "trial_ended" | "not_paid" | "cancelled" | "payer_stopped" | (string & {});
|
|
89
|
+
/**
|
|
90
|
+
* 402 (`SUBSCRIPTION_INACTIVE`): the organization's subscription is not active, so every operation but
|
|
91
|
+
* reading the current user and the organization (and the data export) is refused, reads included. Not
|
|
92
|
+
* retried: the same request fails the same way until an owner reopens the subscription in the app.
|
|
93
|
+
*/
|
|
94
|
+
export declare class TrackenPaymentRequiredError extends TrackenApiError {
|
|
95
|
+
/** `context.reason`, as sent. */
|
|
96
|
+
readonly reason: SubscriptionInactiveReason | undefined;
|
|
97
|
+
/** `context.export_until`: the last day (ISO date) the organization's data can still be exported. */
|
|
98
|
+
readonly exportUntil: string | undefined;
|
|
99
|
+
constructor(status: number, body: unknown, headers: Headers);
|
|
100
|
+
}
|
|
101
|
+
/** 403: a scope or the role does not allow it; `missingScope` names the scope when that is why. */
|
|
102
|
+
export declare class TrackenForbiddenError extends TrackenApiError {
|
|
103
|
+
}
|
|
104
|
+
export declare class TrackenNotFoundError extends TrackenApiError {
|
|
105
|
+
}
|
|
106
|
+
/** 409: the record's state does not allow it, or it exists already (`existingId`). */
|
|
107
|
+
export declare class TrackenConflictError extends TrackenApiError {
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Which trial allowance a `TRIAL_LIMIT_REACHED` names (`context.limit`). Public operations answer
|
|
111
|
+
* `emails_per_day` (sending a sales invoice or a quote) and `ocr_per_month` (digitizing a document);
|
|
112
|
+
* the app's own operations also `companies`, `firm_companies` and `ai_per_month`. The list is open:
|
|
113
|
+
* accept a value you do not know.
|
|
114
|
+
*/
|
|
115
|
+
export type TrialLimit = "emails_per_day" | "ocr_per_month" | "companies" | "firm_companies" | "ai_per_month" | (string & {});
|
|
116
|
+
/**
|
|
117
|
+
* 409 `TRIAL_LIMIT_REACHED`: a company on a free trial has used one of the trial's allowances, and
|
|
118
|
+
* nothing was sent or started. Not retried: do not retry before `resetsOn`; an owner choosing a plan in
|
|
119
|
+
* the app lifts every allowance at once.
|
|
120
|
+
*/
|
|
121
|
+
export declare class TrackenTrialLimitError extends TrackenConflictError {
|
|
122
|
+
/** `context.limit`, as sent. */
|
|
123
|
+
readonly limit: TrialLimit | undefined;
|
|
124
|
+
/** `context.max`: how many the allowance holds. */
|
|
125
|
+
readonly max: number | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* `context.resets_on`: the ISO date the count starts again (days and months counted in
|
|
128
|
+
* Europe/Tallinn). Absent for an allowance only a plan lifts.
|
|
129
|
+
*/
|
|
130
|
+
readonly resetsOn: string | undefined;
|
|
131
|
+
constructor(status: number, body: unknown, headers: Headers);
|
|
132
|
+
}
|
|
133
|
+
/** 412: `If-Match` did not match; the record changed since it was read. */
|
|
134
|
+
export declare class TrackenPreconditionFailedError extends TrackenApiError {
|
|
135
|
+
}
|
|
136
|
+
/** 429 after the client's own retries; `retryAfter` says how long to wait. */
|
|
137
|
+
export declare class TrackenRateLimitedError extends TrackenApiError {
|
|
138
|
+
}
|
|
139
|
+
/** 5xx after the client's own retries. */
|
|
140
|
+
export declare class TrackenServerError extends TrackenApiError {
|
|
141
|
+
}
|
|
142
|
+
/** The `TrackenApiError` subclass for a status (and, for a 409, the trial's limit by its code). */
|
|
143
|
+
export declare function apiError(status: number, body: unknown, headers: Headers): TrackenApiError;
|
|
144
|
+
/**
|
|
145
|
+
* Whether `error` is an API error, and when `code` is given, one with that code:
|
|
146
|
+
* `if (isApiError(error, "INVOICE_NOT_DRAFT")) …`.
|
|
147
|
+
*/
|
|
148
|
+
export declare function isApiError<C extends ErrorCode>(error: unknown, code?: C): error is TrackenApiError & {
|
|
149
|
+
code: C;
|
|
150
|
+
};
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
export class TrackenError extends Error {
|
|
2
|
+
constructor(message, options) {
|
|
3
|
+
super(message, options);
|
|
4
|
+
this.name = new.target.name;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
/** The request was refused before it was sent: a missing path parameter, a number where money goes. */
|
|
8
|
+
export class TrackenUsageError extends TrackenError {
|
|
9
|
+
}
|
|
10
|
+
/** The API (or Tracken ID) could not be reached, after the client's own retries. */
|
|
11
|
+
export class TrackenNetworkError extends TrackenError {
|
|
12
|
+
cause;
|
|
13
|
+
constructor(message, cause) {
|
|
14
|
+
super(message, { cause });
|
|
15
|
+
this.cause = cause;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Tracken ID refused a credential (RFC 6749 §5.2): `error` is its OAuth code, such as `invalid_client`. */
|
|
19
|
+
export class TrackenAuthError extends TrackenError {
|
|
20
|
+
error;
|
|
21
|
+
description;
|
|
22
|
+
status;
|
|
23
|
+
constructor(error, description, status) {
|
|
24
|
+
super(description ? `${error}: ${description}` : error);
|
|
25
|
+
this.error = error;
|
|
26
|
+
this.description = description;
|
|
27
|
+
this.status = status;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** An asynchronous job finished as `failed`; `job` is the API's last answer about it. */
|
|
31
|
+
export class TrackenJobFailedError extends TrackenError {
|
|
32
|
+
job;
|
|
33
|
+
constructor(message, job) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.job = job;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** `waitForJob` gave up before the job finished; poll `path` later to see how it ended. */
|
|
39
|
+
export class TrackenJobTimeoutError extends TrackenError {
|
|
40
|
+
path;
|
|
41
|
+
jobStatus;
|
|
42
|
+
timeoutMs;
|
|
43
|
+
constructor(path, jobStatus, timeoutMs) {
|
|
44
|
+
super(`Gave up waiting after ${Math.round(timeoutMs / 1000)} s; the job is still ${jobStatus ?? "pending"}. Poll GET ${path} to follow it.`);
|
|
45
|
+
this.path = path;
|
|
46
|
+
this.jobStatus = jobStatus;
|
|
47
|
+
this.timeoutMs = timeoutMs;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const text = (value) => (typeof value === "string" && value ? value : undefined);
|
|
51
|
+
/** A non-2xx answer from the API, read from its `{error}` envelope. */
|
|
52
|
+
export class TrackenApiError extends TrackenError {
|
|
53
|
+
status;
|
|
54
|
+
body;
|
|
55
|
+
code;
|
|
56
|
+
details;
|
|
57
|
+
/** Machine-readable figures of the error, keyed by `code`, for a caller's own localized message. */
|
|
58
|
+
context;
|
|
59
|
+
requestId;
|
|
60
|
+
docUrl;
|
|
61
|
+
/** On a conflict with an existing record (a taken `external_ref`): that record's id. */
|
|
62
|
+
existingId;
|
|
63
|
+
/** For 403: the `scope` of `WWW-Authenticate`, as sent: the scopes any one of which would admit the call. */
|
|
64
|
+
missingScope;
|
|
65
|
+
/** `missingScope` as a list. */
|
|
66
|
+
admittingScopes;
|
|
67
|
+
/**
|
|
68
|
+
* For a 401: why the client's one renewal failed (Tracken ID refused the refresh or the
|
|
69
|
+
* credential). Absent when the renewed token was refused too.
|
|
70
|
+
*/
|
|
71
|
+
renewalError;
|
|
72
|
+
/** What to do about it, when the caller knows better than the API's message (set by a wrapping client). */
|
|
73
|
+
hint;
|
|
74
|
+
/** For 429: seconds the API asked to wait. */
|
|
75
|
+
retryAfter;
|
|
76
|
+
constructor(status,
|
|
77
|
+
/** The answer as it came: the parsed `{error}` envelope, or text. */
|
|
78
|
+
body, headers) {
|
|
79
|
+
const envelope = typeof body === "object" && body !== null ? body.error : undefined;
|
|
80
|
+
super(text(envelope?.message) ?? (typeof body === "string" && body.trim() ? body.trim().slice(0, 500) : `HTTP ${status}`));
|
|
81
|
+
this.status = status;
|
|
82
|
+
this.body = body;
|
|
83
|
+
this.code = text(envelope?.code) ?? `HTTP_${status}`;
|
|
84
|
+
this.details = Array.isArray(envelope?.details) ? envelope.details : [];
|
|
85
|
+
const context = envelope?.context;
|
|
86
|
+
this.context = typeof context === "object" && context !== null && !Array.isArray(context) ? context : undefined;
|
|
87
|
+
this.requestId = text(envelope?.request_id) ?? headers.get("x-request-id") ?? undefined;
|
|
88
|
+
this.docUrl = text(envelope?.doc_url);
|
|
89
|
+
this.existingId = text(envelope?.existing_id);
|
|
90
|
+
this.missingScope = headers.get("www-authenticate")?.match(/scope="([^"]+)"/)?.[1];
|
|
91
|
+
this.admittingScopes = (this.missingScope ?? "").split(/\s+/).filter(Boolean);
|
|
92
|
+
this.renewalError = undefined;
|
|
93
|
+
this.hint = undefined;
|
|
94
|
+
const retry = Number(headers.get("retry-after") ?? Number.NaN);
|
|
95
|
+
this.retryAfter = Number.isFinite(retry) && retry >= 0 ? retry : undefined;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** 400 or 422: the request is invalid; `details` point at every field at fault. */
|
|
99
|
+
export class TrackenValidationError extends TrackenApiError {
|
|
100
|
+
}
|
|
101
|
+
/** 401: no token, an expired one, or one for another audience. */
|
|
102
|
+
export class TrackenUnauthenticatedError extends TrackenApiError {
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* 402 (`SUBSCRIPTION_INACTIVE`): the organization's subscription is not active, so every operation but
|
|
106
|
+
* reading the current user and the organization (and the data export) is refused, reads included. Not
|
|
107
|
+
* retried: the same request fails the same way until an owner reopens the subscription in the app.
|
|
108
|
+
*/
|
|
109
|
+
export class TrackenPaymentRequiredError extends TrackenApiError {
|
|
110
|
+
/** `context.reason`, as sent. */
|
|
111
|
+
reason;
|
|
112
|
+
/** `context.export_until`: the last day (ISO date) the organization's data can still be exported. */
|
|
113
|
+
exportUntil;
|
|
114
|
+
constructor(status, body, headers) {
|
|
115
|
+
super(status, body, headers);
|
|
116
|
+
this.reason = text(this.context?.reason);
|
|
117
|
+
this.exportUntil = text(this.context?.export_until);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** 403: a scope or the role does not allow it; `missingScope` names the scope when that is why. */
|
|
121
|
+
export class TrackenForbiddenError extends TrackenApiError {
|
|
122
|
+
}
|
|
123
|
+
export class TrackenNotFoundError extends TrackenApiError {
|
|
124
|
+
}
|
|
125
|
+
/** 409: the record's state does not allow it, or it exists already (`existingId`). */
|
|
126
|
+
export class TrackenConflictError extends TrackenApiError {
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 409 `TRIAL_LIMIT_REACHED`: a company on a free trial has used one of the trial's allowances, and
|
|
130
|
+
* nothing was sent or started. Not retried: do not retry before `resetsOn`; an owner choosing a plan in
|
|
131
|
+
* the app lifts every allowance at once.
|
|
132
|
+
*/
|
|
133
|
+
export class TrackenTrialLimitError extends TrackenConflictError {
|
|
134
|
+
/** `context.limit`, as sent. */
|
|
135
|
+
limit;
|
|
136
|
+
/** `context.max`: how many the allowance holds. */
|
|
137
|
+
max;
|
|
138
|
+
/**
|
|
139
|
+
* `context.resets_on`: the ISO date the count starts again (days and months counted in
|
|
140
|
+
* Europe/Tallinn). Absent for an allowance only a plan lifts.
|
|
141
|
+
*/
|
|
142
|
+
resetsOn;
|
|
143
|
+
constructor(status, body, headers) {
|
|
144
|
+
super(status, body, headers);
|
|
145
|
+
this.limit = text(this.context?.limit);
|
|
146
|
+
const max = this.context?.max;
|
|
147
|
+
this.max = typeof max === "number" && Number.isFinite(max) ? max : undefined;
|
|
148
|
+
this.resetsOn = text(this.context?.resets_on);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** 412: `If-Match` did not match; the record changed since it was read. */
|
|
152
|
+
export class TrackenPreconditionFailedError extends TrackenApiError {
|
|
153
|
+
}
|
|
154
|
+
/** 429 after the client's own retries; `retryAfter` says how long to wait. */
|
|
155
|
+
export class TrackenRateLimitedError extends TrackenApiError {
|
|
156
|
+
}
|
|
157
|
+
/** 5xx after the client's own retries. */
|
|
158
|
+
export class TrackenServerError extends TrackenApiError {
|
|
159
|
+
}
|
|
160
|
+
const envelopeCode = (body) => typeof body === "object" && body !== null ? text(body.error?.code) : undefined;
|
|
161
|
+
/** The `TrackenApiError` subclass for a status (and, for a 409, the trial's limit by its code). */
|
|
162
|
+
export function apiError(status, body, headers) {
|
|
163
|
+
if (status === 400 || status === 422)
|
|
164
|
+
return new TrackenValidationError(status, body, headers);
|
|
165
|
+
if (status === 401)
|
|
166
|
+
return new TrackenUnauthenticatedError(status, body, headers);
|
|
167
|
+
if (status === 402)
|
|
168
|
+
return new TrackenPaymentRequiredError(status, body, headers);
|
|
169
|
+
if (status === 403)
|
|
170
|
+
return new TrackenForbiddenError(status, body, headers);
|
|
171
|
+
if (status === 404)
|
|
172
|
+
return new TrackenNotFoundError(status, body, headers);
|
|
173
|
+
if (status === 409) {
|
|
174
|
+
return envelopeCode(body) === "TRIAL_LIMIT_REACHED" ? new TrackenTrialLimitError(status, body, headers) : new TrackenConflictError(status, body, headers);
|
|
175
|
+
}
|
|
176
|
+
if (status === 412)
|
|
177
|
+
return new TrackenPreconditionFailedError(status, body, headers);
|
|
178
|
+
if (status === 429)
|
|
179
|
+
return new TrackenRateLimitedError(status, body, headers);
|
|
180
|
+
if (status >= 500)
|
|
181
|
+
return new TrackenServerError(status, body, headers);
|
|
182
|
+
return new TrackenApiError(status, body, headers);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Whether `error` is an API error, and when `code` is given, one with that code:
|
|
186
|
+
* `if (isApiError(error, "INVOICE_NOT_DRAFT")) …`.
|
|
187
|
+
*/
|
|
188
|
+
export function isApiError(error, code) {
|
|
189
|
+
return error instanceof TrackenApiError && (code === undefined || error.code === code);
|
|
190
|
+
}
|