@atscript/db-client 0.1.164 → 0.1.165

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.
@@ -0,0 +1,222 @@
1
+ //#region src/client-error.ts
2
+ /**
3
+ * Error thrown by `Client` when the server responds with a non-2xx status code.
4
+ * Captures the HTTP status and the structured error body from moost-db.
5
+ */
6
+ var ClientError = class extends Error {
7
+ status;
8
+ body;
9
+ name = "ClientError";
10
+ constructor(status, body) {
11
+ super(body.message || `HTTP ${status}`);
12
+ this.status = status;
13
+ this.body = body;
14
+ }
15
+ /** Shortcut to structured validation/DB errors from the server. */
16
+ get errors() {
17
+ return this.body.errors ?? [];
18
+ }
19
+ };
20
+ /**
21
+ * Thrown before the request is sent when the client-side preflight
22
+ * (`validator` option, `ClientValidator`) rejects a write. A
23
+ * {@link ClientError} since 0.1.165 — status `400`, `body` shaped like the
24
+ * server's validation envelope (`{ message, statusCode: 400, errors }`) — so
25
+ * one `instanceof ClientError` branch handles a preflight failure and the
26
+ * server's 400 alike; `preflight` (`true`) tells them apart. The same class
27
+ * is exported from `@atscript/db-client` and `@atscript/db-client/validator`.
28
+ */
29
+ var ClientValidationError = class extends ClientError {
30
+ name = "ClientValidationError";
31
+ /** `true`: the request never left the client. */
32
+ preflight = true;
33
+ constructor(errors) {
34
+ const message = errors.length === 1 ? errors[0].message : `Validation failed with ${errors.length} errors`;
35
+ super(400, {
36
+ message,
37
+ statusCode: 400,
38
+ errors
39
+ });
40
+ }
41
+ };
42
+ /**
43
+ * Typed marker thrown by `Client._send` when the server response body's
44
+ * `name === 'ActionDisabledError'`. The transport / status / base body are
45
+ * identical to a generic `ClientError`; this subclass adds typed accessors
46
+ * so consumers can write `catch (e) { if (e instanceof ActionDisabledError) … }`
47
+ * to access `action` / `id` / `ids` / `reason` / `reasons` without indexing
48
+ * into `body`.
49
+ */
50
+ var ActionDisabledError = class extends ClientError {
51
+ name = "ActionDisabledError";
52
+ /** The `@DbAction` name that rejected the request. */
53
+ get action() {
54
+ return this.body.action;
55
+ }
56
+ /** Present only for `'row'`-level rejections. */
57
+ get id() {
58
+ return this.body.id;
59
+ }
60
+ /** Present only for `'rows'`-level rejections (full list of failing IDs). */
61
+ get ids() {
62
+ return this.body.ids;
63
+ }
64
+ /**
65
+ * Human-readable reason shared by every rejected row — present only when
66
+ * the server's `disabled` predicate returned one. `message` already
67
+ * carries it.
68
+ *
69
+ * @since 0.1.141
70
+ */
71
+ get reason() {
72
+ return this.body.reason;
73
+ }
74
+ /**
75
+ * `'rows'`-level only: per-row reasons index-aligned with {@link ids}
76
+ * (`null` where that row has none). Present only when at least one
77
+ * rejected row carries a reason.
78
+ *
79
+ * @since 0.1.141
80
+ */
81
+ get reasons() {
82
+ return this.body.reasons;
83
+ }
84
+ };
85
+ /**
86
+ * Typed marker thrown by `Client` when a query-targeted action request is
87
+ * refused (body `name === 'ActionTargetError'`): `TARGET_INVALID` (400),
88
+ * `TARGET_TOO_LARGE` (400, `cap`) or `TARGET_CHANGED` (409, `matched` — the
89
+ * rows the query matches now; re-confirm and retry with that count).
90
+ *
91
+ * @since 0.1.147
92
+ */
93
+ var ActionTargetError = class extends ClientError {
94
+ name = "ActionTargetError";
95
+ get code() {
96
+ return this.body.code;
97
+ }
98
+ get action() {
99
+ return this.body.action;
100
+ }
101
+ get matched() {
102
+ return this.body.matched;
103
+ }
104
+ get cap() {
105
+ return this.body.cap;
106
+ }
107
+ };
108
+ /**
109
+ * Typed marker thrown by `Client._send` when the server response body's
110
+ * `kind === 'version_mismatch'`. The transport / status / base body are
111
+ * identical to a generic `ClientError`; this subclass adds a typed
112
+ * `currentVersion` accessor so consumers can write
113
+ * `catch (e) { if (e instanceof VersionMismatchError) … }` without casting.
114
+ */
115
+ var VersionMismatchError = class extends ClientError {
116
+ name = "VersionMismatchError";
117
+ /** Row's current server-side version — clients refresh + retry against it. */
118
+ get currentVersion() {
119
+ return this.body.currentVersion;
120
+ }
121
+ };
122
+ /**
123
+ * Thrown by `Client` when NO server verdict was obtained: `fetch` itself
124
+ * rejected (network down, DNS, CORS, `AbortError`, …) or a 2xx response
125
+ * carried a body that is not JSON. Unlike {@link ClientError} — where the
126
+ * server answered and rejected — the request's outcome is unknown: a write
127
+ * may have committed. Reload before retrying a non-idempotent write.
128
+ * The underlying error is the standard `cause`.
129
+ */
130
+ var TransportError = class extends Error {
131
+ method;
132
+ url;
133
+ name = "TransportError";
134
+ constructor(method, url, detail, cause) {
135
+ super(`${method} ${url}: ${detail}`, { cause });
136
+ this.method = method;
137
+ this.url = url;
138
+ }
139
+ /**
140
+ * `true` when the request was cancelled — its `cause` is an `AbortError`
141
+ * (an aborted `signal`) or a `TimeoutError` (`AbortSignal.timeout`) —
142
+ * rather than a network failure. @since 0.1.165
143
+ */
144
+ get aborted() {
145
+ const name = this.cause?.name;
146
+ return name === "AbortError" || name === "TimeoutError";
147
+ }
148
+ };
149
+ /** Thrown by `Client.action()` when the action name is not present in `/meta`. */
150
+ var ActionNotFoundError = class extends Error {
151
+ action;
152
+ name = "ActionNotFoundError";
153
+ constructor(action) {
154
+ super(`Action "${action}" is not declared on this controller`);
155
+ this.action = action;
156
+ }
157
+ };
158
+ /**
159
+ * Thrown by `Client.action()` for actions that cannot be invoked through
160
+ * the client — currently `processor: 'custom'` (UI-dispatched events,
161
+ * which the application is responsible for handling) and `processor: 'navigate'`
162
+ * when no browser environment and no `navigate` option are configured.
163
+ */
164
+ var ActionUnsupportedError = class extends Error {
165
+ action;
166
+ processor;
167
+ name = "ActionUnsupportedError";
168
+ constructor(action, processor, message) {
169
+ super(message);
170
+ this.action = action;
171
+ this.processor = processor;
172
+ }
173
+ };
174
+ //#endregion
175
+ Object.defineProperty(exports, "ActionDisabledError", {
176
+ enumerable: true,
177
+ get: function() {
178
+ return ActionDisabledError;
179
+ }
180
+ });
181
+ Object.defineProperty(exports, "ActionNotFoundError", {
182
+ enumerable: true,
183
+ get: function() {
184
+ return ActionNotFoundError;
185
+ }
186
+ });
187
+ Object.defineProperty(exports, "ActionTargetError", {
188
+ enumerable: true,
189
+ get: function() {
190
+ return ActionTargetError;
191
+ }
192
+ });
193
+ Object.defineProperty(exports, "ActionUnsupportedError", {
194
+ enumerable: true,
195
+ get: function() {
196
+ return ActionUnsupportedError;
197
+ }
198
+ });
199
+ Object.defineProperty(exports, "ClientError", {
200
+ enumerable: true,
201
+ get: function() {
202
+ return ClientError;
203
+ }
204
+ });
205
+ Object.defineProperty(exports, "ClientValidationError", {
206
+ enumerable: true,
207
+ get: function() {
208
+ return ClientValidationError;
209
+ }
210
+ });
211
+ Object.defineProperty(exports, "TransportError", {
212
+ enumerable: true,
213
+ get: function() {
214
+ return TransportError;
215
+ }
216
+ });
217
+ Object.defineProperty(exports, "VersionMismatchError", {
218
+ enumerable: true,
219
+ get: function() {
220
+ return VersionMismatchError;
221
+ }
222
+ });
@@ -0,0 +1,175 @@
1
+ //#region src/client-error.ts
2
+ /**
3
+ * Error thrown by `Client` when the server responds with a non-2xx status code.
4
+ * Captures the HTTP status and the structured error body from moost-db.
5
+ */
6
+ var ClientError = class extends Error {
7
+ status;
8
+ body;
9
+ name = "ClientError";
10
+ constructor(status, body) {
11
+ super(body.message || `HTTP ${status}`);
12
+ this.status = status;
13
+ this.body = body;
14
+ }
15
+ /** Shortcut to structured validation/DB errors from the server. */
16
+ get errors() {
17
+ return this.body.errors ?? [];
18
+ }
19
+ };
20
+ /**
21
+ * Thrown before the request is sent when the client-side preflight
22
+ * (`validator` option, `ClientValidator`) rejects a write. A
23
+ * {@link ClientError} since 0.1.165 — status `400`, `body` shaped like the
24
+ * server's validation envelope (`{ message, statusCode: 400, errors }`) — so
25
+ * one `instanceof ClientError` branch handles a preflight failure and the
26
+ * server's 400 alike; `preflight` (`true`) tells them apart. The same class
27
+ * is exported from `@atscript/db-client` and `@atscript/db-client/validator`.
28
+ */
29
+ var ClientValidationError = class extends ClientError {
30
+ name = "ClientValidationError";
31
+ /** `true`: the request never left the client. */
32
+ preflight = true;
33
+ constructor(errors) {
34
+ const message = errors.length === 1 ? errors[0].message : `Validation failed with ${errors.length} errors`;
35
+ super(400, {
36
+ message,
37
+ statusCode: 400,
38
+ errors
39
+ });
40
+ }
41
+ };
42
+ /**
43
+ * Typed marker thrown by `Client._send` when the server response body's
44
+ * `name === 'ActionDisabledError'`. The transport / status / base body are
45
+ * identical to a generic `ClientError`; this subclass adds typed accessors
46
+ * so consumers can write `catch (e) { if (e instanceof ActionDisabledError) … }`
47
+ * to access `action` / `id` / `ids` / `reason` / `reasons` without indexing
48
+ * into `body`.
49
+ */
50
+ var ActionDisabledError = class extends ClientError {
51
+ name = "ActionDisabledError";
52
+ /** The `@DbAction` name that rejected the request. */
53
+ get action() {
54
+ return this.body.action;
55
+ }
56
+ /** Present only for `'row'`-level rejections. */
57
+ get id() {
58
+ return this.body.id;
59
+ }
60
+ /** Present only for `'rows'`-level rejections (full list of failing IDs). */
61
+ get ids() {
62
+ return this.body.ids;
63
+ }
64
+ /**
65
+ * Human-readable reason shared by every rejected row — present only when
66
+ * the server's `disabled` predicate returned one. `message` already
67
+ * carries it.
68
+ *
69
+ * @since 0.1.141
70
+ */
71
+ get reason() {
72
+ return this.body.reason;
73
+ }
74
+ /**
75
+ * `'rows'`-level only: per-row reasons index-aligned with {@link ids}
76
+ * (`null` where that row has none). Present only when at least one
77
+ * rejected row carries a reason.
78
+ *
79
+ * @since 0.1.141
80
+ */
81
+ get reasons() {
82
+ return this.body.reasons;
83
+ }
84
+ };
85
+ /**
86
+ * Typed marker thrown by `Client` when a query-targeted action request is
87
+ * refused (body `name === 'ActionTargetError'`): `TARGET_INVALID` (400),
88
+ * `TARGET_TOO_LARGE` (400, `cap`) or `TARGET_CHANGED` (409, `matched` — the
89
+ * rows the query matches now; re-confirm and retry with that count).
90
+ *
91
+ * @since 0.1.147
92
+ */
93
+ var ActionTargetError = class extends ClientError {
94
+ name = "ActionTargetError";
95
+ get code() {
96
+ return this.body.code;
97
+ }
98
+ get action() {
99
+ return this.body.action;
100
+ }
101
+ get matched() {
102
+ return this.body.matched;
103
+ }
104
+ get cap() {
105
+ return this.body.cap;
106
+ }
107
+ };
108
+ /**
109
+ * Typed marker thrown by `Client._send` when the server response body's
110
+ * `kind === 'version_mismatch'`. The transport / status / base body are
111
+ * identical to a generic `ClientError`; this subclass adds a typed
112
+ * `currentVersion` accessor so consumers can write
113
+ * `catch (e) { if (e instanceof VersionMismatchError) … }` without casting.
114
+ */
115
+ var VersionMismatchError = class extends ClientError {
116
+ name = "VersionMismatchError";
117
+ /** Row's current server-side version — clients refresh + retry against it. */
118
+ get currentVersion() {
119
+ return this.body.currentVersion;
120
+ }
121
+ };
122
+ /**
123
+ * Thrown by `Client` when NO server verdict was obtained: `fetch` itself
124
+ * rejected (network down, DNS, CORS, `AbortError`, …) or a 2xx response
125
+ * carried a body that is not JSON. Unlike {@link ClientError} — where the
126
+ * server answered and rejected — the request's outcome is unknown: a write
127
+ * may have committed. Reload before retrying a non-idempotent write.
128
+ * The underlying error is the standard `cause`.
129
+ */
130
+ var TransportError = class extends Error {
131
+ method;
132
+ url;
133
+ name = "TransportError";
134
+ constructor(method, url, detail, cause) {
135
+ super(`${method} ${url}: ${detail}`, { cause });
136
+ this.method = method;
137
+ this.url = url;
138
+ }
139
+ /**
140
+ * `true` when the request was cancelled — its `cause` is an `AbortError`
141
+ * (an aborted `signal`) or a `TimeoutError` (`AbortSignal.timeout`) —
142
+ * rather than a network failure. @since 0.1.165
143
+ */
144
+ get aborted() {
145
+ const name = this.cause?.name;
146
+ return name === "AbortError" || name === "TimeoutError";
147
+ }
148
+ };
149
+ /** Thrown by `Client.action()` when the action name is not present in `/meta`. */
150
+ var ActionNotFoundError = class extends Error {
151
+ action;
152
+ name = "ActionNotFoundError";
153
+ constructor(action) {
154
+ super(`Action "${action}" is not declared on this controller`);
155
+ this.action = action;
156
+ }
157
+ };
158
+ /**
159
+ * Thrown by `Client.action()` for actions that cannot be invoked through
160
+ * the client — currently `processor: 'custom'` (UI-dispatched events,
161
+ * which the application is responsible for handling) and `processor: 'navigate'`
162
+ * when no browser environment and no `navigate` option are configured.
163
+ */
164
+ var ActionUnsupportedError = class extends Error {
165
+ action;
166
+ processor;
167
+ name = "ActionUnsupportedError";
168
+ constructor(action, processor, message) {
169
+ super(message);
170
+ this.action = action;
171
+ this.processor = processor;
172
+ }
173
+ };
174
+ //#endregion
175
+ export { ClientError as a, VersionMismatchError as c, ActionUnsupportedError as i, ActionNotFoundError as n, ClientValidationError as o, ActionTargetError as r, TransportError as s, ActionDisabledError as t };