@svadmin/core 0.70.0 → 0.72.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/package.json +21 -1
- package/src/create-hooks.svelte.ts +11 -1
- package/src/create-many-hooks.svelte.ts +11 -1
- package/src/delete-hooks.svelte.ts +11 -1
- package/src/delete-many-hooks.svelte.ts +11 -1
- package/src/domain-errors.ts +418 -0
- package/src/index.ts +53 -0
- package/src/inferencer-schema.ts +325 -0
- package/src/mock-provider.ts +307 -0
- package/src/resilience.ts +185 -0
- package/src/result.ts +99 -0
- package/src/update-hooks.svelte.ts +11 -1
- package/src/update-many-hooks.svelte.ts +11 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@svadmin/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.72.0",
|
|
4
4
|
"author": "zuohuadong",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -107,6 +107,14 @@
|
|
|
107
107
|
"types": "./src/inferencer.ts",
|
|
108
108
|
"default": "./src/inferencer.ts"
|
|
109
109
|
},
|
|
110
|
+
"./inferencer-schema": {
|
|
111
|
+
"types": "./src/inferencer-schema.ts",
|
|
112
|
+
"default": "./src/inferencer-schema.ts"
|
|
113
|
+
},
|
|
114
|
+
"./mock-provider": {
|
|
115
|
+
"types": "./src/mock-provider.ts",
|
|
116
|
+
"default": "./src/mock-provider.ts"
|
|
117
|
+
},
|
|
110
118
|
"./inferencer-openapi": {
|
|
111
119
|
"types": "./src/inferencer-openapi.ts",
|
|
112
120
|
"default": "./src/inferencer-openapi.ts"
|
|
@@ -142,6 +150,18 @@
|
|
|
142
150
|
"./contracts": {
|
|
143
151
|
"types": "./src/contracts.ts",
|
|
144
152
|
"default": "./src/contracts.ts"
|
|
153
|
+
},
|
|
154
|
+
"./result": {
|
|
155
|
+
"types": "./src/result.ts",
|
|
156
|
+
"default": "./src/result.ts"
|
|
157
|
+
},
|
|
158
|
+
"./domain-errors": {
|
|
159
|
+
"types": "./src/domain-errors.ts",
|
|
160
|
+
"default": "./src/domain-errors.ts"
|
|
161
|
+
},
|
|
162
|
+
"./resilience": {
|
|
163
|
+
"types": "./src/resilience.ts",
|
|
164
|
+
"default": "./src/resilience.ts"
|
|
145
165
|
}
|
|
146
166
|
},
|
|
147
167
|
"bugs": {
|
|
@@ -18,6 +18,8 @@ import { auditWithProvider } from './audit';
|
|
|
18
18
|
import { HttpError } from './types';
|
|
19
19
|
import { useTranslation } from './i18n.svelte';
|
|
20
20
|
import { toast } from './toast.svelte';
|
|
21
|
+
import { ok, err, type Result as SafeResult } from './result';
|
|
22
|
+
import { toDomainError, type DomainError } from './domain-errors';
|
|
21
23
|
|
|
22
24
|
export interface ContractCreateOptions<S extends ContractSchemas> {
|
|
23
25
|
resource: ResourceContract<S>;
|
|
@@ -331,13 +333,21 @@ export function useContractCreate<S extends ContractSchemas>(options: ContractCr
|
|
|
331
333
|
mutate(params: ContractCreateParams<S>, callbacks?: Callbacks<S>): void {
|
|
332
334
|
void mutateAsync(params, callbacks).catch(() => {});
|
|
333
335
|
},
|
|
336
|
+
async safeMutate(params: ContractCreateParams<S>, callbacks?: Callbacks<S>): Promise<SafeResult<ContractRecord<S>, DomainError>> {
|
|
337
|
+
try {
|
|
338
|
+
const res = await mutateAsync(params, callbacks);
|
|
339
|
+
return ok(res.data);
|
|
340
|
+
} catch (cause) {
|
|
341
|
+
return err(toDomainError(cause));
|
|
342
|
+
}
|
|
343
|
+
},
|
|
334
344
|
reset() { active = undefined; latest = undefined; latestToken = undefined; mutation.reset(); },
|
|
335
345
|
});
|
|
336
346
|
const idle: typeof result = {
|
|
337
347
|
context: undefined, data: undefined, error: null, failureCount: 0, failureReason: null,
|
|
338
348
|
isError: false, isIdle: true, isPending: false, isPaused: false, isSuccess: false,
|
|
339
349
|
status: 'idle', submittedAt: 0, variables: undefined,
|
|
340
|
-
mutate: result.mutate, mutateAsync: result.mutateAsync, reset: result.reset,
|
|
350
|
+
mutate: result.mutate, mutateAsync: result.mutateAsync, safeMutate: result.safeMutate, reset: result.reset,
|
|
341
351
|
};
|
|
342
352
|
const view = createQueryResultView(() => {
|
|
343
353
|
const invocation = mutation.variables;
|
|
@@ -15,6 +15,8 @@ import { getAdminOptions } from './options.svelte';
|
|
|
15
15
|
import { captureAuthLiveScope, clearAuthQueries, handleAuthError } from './auth-hooks.svelte';
|
|
16
16
|
import { auditWithProvider } from './audit';
|
|
17
17
|
import { HttpError } from './types';
|
|
18
|
+
import { ok, err, type Result as SafeResult } from './result';
|
|
19
|
+
import { toDomainError, type DomainError } from './domain-errors';
|
|
18
20
|
import { useTranslation } from './i18n.svelte';
|
|
19
21
|
import { toast } from './toast.svelte';
|
|
20
22
|
|
|
@@ -377,13 +379,21 @@ export function useContractCreateMany<S extends ContractSchemas>(options: Contra
|
|
|
377
379
|
mutate(params: ContractCreateManyParams<S>, callbacks?: Callbacks<S>): void {
|
|
378
380
|
void mutateAsync(params, callbacks).catch(() => {});
|
|
379
381
|
},
|
|
382
|
+
async safeMutate(params: ContractCreateManyParams<S>, callbacks?: Callbacks<S>): Promise<SafeResult<ContractRecord<S>[], DomainError>> {
|
|
383
|
+
try {
|
|
384
|
+
const res = await mutateAsync(params, callbacks);
|
|
385
|
+
return ok(res.data);
|
|
386
|
+
} catch (cause) {
|
|
387
|
+
return err(toDomainError(cause));
|
|
388
|
+
}
|
|
389
|
+
},
|
|
380
390
|
reset() { active = undefined; latest = undefined; latestToken = undefined; mutation.reset(); },
|
|
381
391
|
});
|
|
382
392
|
const idle: typeof result = {
|
|
383
393
|
context: undefined, data: undefined, error: null, failureCount: 0, failureReason: null,
|
|
384
394
|
isError: false, isIdle: true, isPending: false, isPaused: false, isSuccess: false,
|
|
385
395
|
status: 'idle', submittedAt: 0, variables: undefined,
|
|
386
|
-
mutate: result.mutate, mutateAsync: result.mutateAsync, reset: result.reset,
|
|
396
|
+
mutate: result.mutate, mutateAsync: result.mutateAsync, safeMutate: result.safeMutate, reset: result.reset,
|
|
387
397
|
};
|
|
388
398
|
const view = createQueryResultView(() => {
|
|
389
399
|
const invocation = mutation.variables;
|
|
@@ -17,6 +17,8 @@ import { auditWithProvider } from './audit';
|
|
|
17
17
|
import { HttpError, UndoError } from './types';
|
|
18
18
|
import { useTranslation } from './i18n.svelte';
|
|
19
19
|
import { toast, getToasts, removeToast } from './toast.svelte';
|
|
20
|
+
import { ok, err, type Result as SafeResult } from './result';
|
|
21
|
+
import { toDomainError, type DomainError } from './domain-errors';
|
|
20
22
|
|
|
21
23
|
export type { ContractDeleteParams } from './delete-contract';
|
|
22
24
|
export interface ContractDeleteOptions<S extends ContractSchemas> {
|
|
@@ -384,13 +386,21 @@ export function useContractDelete<S extends ContractSchemas>(options: ContractDe
|
|
|
384
386
|
mutate(params: ContractDeleteParams<S>, callbacks?: Callbacks<S>): void {
|
|
385
387
|
void mutateAsync(params, callbacks).catch(() => {});
|
|
386
388
|
},
|
|
389
|
+
async safeMutate(params: ContractDeleteParams<S>, callbacks?: Callbacks<S>): Promise<SafeResult<ContractRecord<S>, DomainError>> {
|
|
390
|
+
try {
|
|
391
|
+
const res = await mutateAsync(params, callbacks);
|
|
392
|
+
return ok(res.data);
|
|
393
|
+
} catch (cause) {
|
|
394
|
+
return err(toDomainError(cause));
|
|
395
|
+
}
|
|
396
|
+
},
|
|
387
397
|
reset() { latestToken = undefined; clearActive(); mutation.reset(); },
|
|
388
398
|
});
|
|
389
399
|
const idle: typeof result = {
|
|
390
400
|
context: undefined, data: undefined, error: null, failureCount: 0, failureReason: null,
|
|
391
401
|
isError: false, isIdle: true, isPending: false, isPaused: false, isSuccess: false,
|
|
392
402
|
status: 'idle', submittedAt: 0, variables: undefined,
|
|
393
|
-
mutate: result.mutate, mutateAsync: result.mutateAsync, reset: result.reset,
|
|
403
|
+
mutate: result.mutate, mutateAsync: result.mutateAsync, safeMutate: result.safeMutate, reset: result.reset,
|
|
394
404
|
};
|
|
395
405
|
const view = createQueryResultView(() => {
|
|
396
406
|
const invocation = mutation.variables;
|
|
@@ -15,6 +15,8 @@ import { getAdminOptions } from './options.svelte';
|
|
|
15
15
|
import { captureAuthLiveScope, clearAuthQueries, handleAuthError } from './auth-hooks.svelte';
|
|
16
16
|
import { auditWithProvider } from './audit';
|
|
17
17
|
import { DeleteManyPartialError, HttpError } from './types';
|
|
18
|
+
import { ok, err, type Result as SafeResult } from './result';
|
|
19
|
+
import { toDomainError, type DomainError } from './domain-errors';
|
|
18
20
|
import { useTranslation } from './i18n.svelte';
|
|
19
21
|
import { toast } from './toast.svelte';
|
|
20
22
|
|
|
@@ -386,13 +388,21 @@ export function useContractDeleteMany<S extends ContractSchemas>(options: Contra
|
|
|
386
388
|
mutate(params: ContractDeleteManyParams<S>, callbacks?: Callbacks<S>): void {
|
|
387
389
|
void mutateAsync(params, callbacks).catch(() => {});
|
|
388
390
|
},
|
|
391
|
+
async safeMutate(params: ContractDeleteManyParams<S>, callbacks?: Callbacks<S>): Promise<SafeResult<ContractRecord<S>[], DomainError>> {
|
|
392
|
+
try {
|
|
393
|
+
const res = await mutateAsync(params, callbacks);
|
|
394
|
+
return ok(res.data);
|
|
395
|
+
} catch (cause) {
|
|
396
|
+
return err(toDomainError(cause));
|
|
397
|
+
}
|
|
398
|
+
},
|
|
389
399
|
reset() { active = undefined; latest = undefined; latestToken = undefined; mutation.reset(); },
|
|
390
400
|
});
|
|
391
401
|
const idle: typeof result = {
|
|
392
402
|
context: undefined, data: undefined, error: null, failureCount: 0, failureReason: null,
|
|
393
403
|
isError: false, isIdle: true, isPending: false, isPaused: false, isSuccess: false,
|
|
394
404
|
status: 'idle', submittedAt: 0, variables: undefined,
|
|
395
|
-
mutate: result.mutate, mutateAsync: result.mutateAsync, reset: result.reset,
|
|
405
|
+
mutate: result.mutate, mutateAsync: result.mutateAsync, safeMutate: result.safeMutate, reset: result.reset,
|
|
396
406
|
};
|
|
397
407
|
const view = createQueryResultView(() => {
|
|
398
408
|
const invocation = mutation.variables;
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import { HttpError, UndoError, type ValidationErrors, type HttpErrorOptions } from './types';
|
|
2
|
+
import { definedOptions } from './defined-options';
|
|
3
|
+
|
|
4
|
+
export class ValidationError extends HttpError {
|
|
5
|
+
readonly _tag = 'ValidationError' as const;
|
|
6
|
+
readonly fieldErrors: ValidationErrors;
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
message: string = 'Validation failed',
|
|
10
|
+
fieldErrors: ValidationErrors = {},
|
|
11
|
+
options: HttpErrorOptions = {},
|
|
12
|
+
) {
|
|
13
|
+
super(message, 422, fieldErrors, definedOptions({
|
|
14
|
+
code: options.code ?? 'VALIDATION_FAILED',
|
|
15
|
+
details: options.details,
|
|
16
|
+
body: options.body,
|
|
17
|
+
cause: options.cause,
|
|
18
|
+
}));
|
|
19
|
+
this.name = 'ValidationError';
|
|
20
|
+
this.fieldErrors = fieldErrors;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ConflictErrorOptions extends HttpErrorOptions {
|
|
25
|
+
resource?: string | undefined;
|
|
26
|
+
recordId?: string | number | undefined;
|
|
27
|
+
currentVersion?: string | number | undefined;
|
|
28
|
+
expectedVersion?: string | number | undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class ConflictError extends HttpError {
|
|
32
|
+
readonly _tag = 'ConflictError' as const;
|
|
33
|
+
readonly resource?: string | undefined;
|
|
34
|
+
readonly recordId?: string | number | undefined;
|
|
35
|
+
readonly currentVersion?: string | number | undefined;
|
|
36
|
+
readonly expectedVersion?: string | number | undefined;
|
|
37
|
+
|
|
38
|
+
constructor(
|
|
39
|
+
message: string = 'Resource conflict detected',
|
|
40
|
+
options: ConflictErrorOptions = {},
|
|
41
|
+
) {
|
|
42
|
+
super(message, 409, undefined, definedOptions({
|
|
43
|
+
code: options.code ?? 'RESOURCE_CONFLICT',
|
|
44
|
+
details: options.details,
|
|
45
|
+
body: options.body,
|
|
46
|
+
cause: options.cause,
|
|
47
|
+
}));
|
|
48
|
+
this.name = 'ConflictError';
|
|
49
|
+
this.resource = options.resource;
|
|
50
|
+
this.recordId = options.recordId;
|
|
51
|
+
this.currentVersion = options.currentVersion;
|
|
52
|
+
this.expectedVersion = options.expectedVersion;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface AuthErrorOptions extends HttpErrorOptions {
|
|
57
|
+
resource?: string | undefined;
|
|
58
|
+
action?: string | undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class AuthError extends HttpError {
|
|
62
|
+
readonly _tag = 'AuthError' as const;
|
|
63
|
+
readonly kind: 'unauthorized' | 'forbidden';
|
|
64
|
+
readonly resource?: string | undefined;
|
|
65
|
+
readonly action?: string | undefined;
|
|
66
|
+
|
|
67
|
+
constructor(
|
|
68
|
+
kind: 'unauthorized' | 'forbidden',
|
|
69
|
+
message?: string,
|
|
70
|
+
options: AuthErrorOptions = {},
|
|
71
|
+
) {
|
|
72
|
+
const statusCode = kind === 'unauthorized' ? 401 : 403;
|
|
73
|
+
const defaultMsg = kind === 'unauthorized' ? 'Authentication required' : 'Permission denied';
|
|
74
|
+
super(message ?? defaultMsg, statusCode, undefined, definedOptions({
|
|
75
|
+
code: options.code ?? (kind === 'unauthorized' ? 'UNAUTHORIZED' : 'FORBIDDEN'),
|
|
76
|
+
details: options.details,
|
|
77
|
+
body: options.body,
|
|
78
|
+
cause: options.cause,
|
|
79
|
+
}));
|
|
80
|
+
this.name = 'AuthError';
|
|
81
|
+
this.kind = kind;
|
|
82
|
+
this.resource = options.resource;
|
|
83
|
+
this.action = options.action;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface NetworkErrorOptions extends HttpErrorOptions {
|
|
88
|
+
isTimeout?: boolean | undefined;
|
|
89
|
+
isOffline?: boolean | undefined;
|
|
90
|
+
statusCode?: number | undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export class NetworkError extends HttpError {
|
|
94
|
+
readonly _tag = 'NetworkError' as const;
|
|
95
|
+
readonly isTimeout: boolean;
|
|
96
|
+
readonly isOffline: boolean;
|
|
97
|
+
readonly retryable = true as const;
|
|
98
|
+
|
|
99
|
+
constructor(
|
|
100
|
+
message: string = 'Network communication failure',
|
|
101
|
+
options: NetworkErrorOptions = {},
|
|
102
|
+
) {
|
|
103
|
+
const isTimeout = options.isTimeout ?? false;
|
|
104
|
+
super(message, options.statusCode ?? (isTimeout ? 504 : 0), undefined, definedOptions({
|
|
105
|
+
code: options.code ?? (isTimeout ? 'NETWORK_TIMEOUT' : 'NETWORK_ERROR'),
|
|
106
|
+
details: options.details,
|
|
107
|
+
body: options.body,
|
|
108
|
+
cause: options.cause,
|
|
109
|
+
}));
|
|
110
|
+
this.name = 'NetworkError';
|
|
111
|
+
this.isTimeout = isTimeout;
|
|
112
|
+
this.isOffline = options.isOffline ?? false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface ContractDecodingErrorOptions extends HttpErrorOptions {
|
|
117
|
+
resource?: string | undefined;
|
|
118
|
+
issues?: readonly unknown[] | undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export class ContractDecodingError extends HttpError {
|
|
122
|
+
readonly _tag = 'ContractDecodingError' as const;
|
|
123
|
+
readonly resource?: string | undefined;
|
|
124
|
+
readonly issues?: readonly unknown[] | undefined;
|
|
125
|
+
|
|
126
|
+
constructor(
|
|
127
|
+
message: string = 'Contract decoding mismatch',
|
|
128
|
+
options: ContractDecodingErrorOptions = {},
|
|
129
|
+
) {
|
|
130
|
+
super(message, 502, undefined, definedOptions({
|
|
131
|
+
code: options.code ?? 'CONTRACT_DECODING_ERROR',
|
|
132
|
+
details: options.details ?? options.issues,
|
|
133
|
+
body: options.body,
|
|
134
|
+
cause: options.cause,
|
|
135
|
+
}));
|
|
136
|
+
this.name = 'ContractDecodingError';
|
|
137
|
+
this.resource = options.resource;
|
|
138
|
+
this.issues = options.issues;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface NotFoundErrorOptions extends HttpErrorOptions {
|
|
143
|
+
resource?: string | undefined;
|
|
144
|
+
id?: string | number | undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export class NotFoundError extends HttpError {
|
|
148
|
+
readonly _tag = 'NotFoundError' as const;
|
|
149
|
+
readonly resource?: string | undefined;
|
|
150
|
+
readonly id?: string | number | undefined;
|
|
151
|
+
|
|
152
|
+
constructor(
|
|
153
|
+
message: string = 'Resource not found',
|
|
154
|
+
options: NotFoundErrorOptions = {},
|
|
155
|
+
) {
|
|
156
|
+
super(message, 404, undefined, definedOptions({
|
|
157
|
+
code: options.code ?? 'RESOURCE_NOT_FOUND',
|
|
158
|
+
details: options.details,
|
|
159
|
+
body: options.body,
|
|
160
|
+
cause: options.cause,
|
|
161
|
+
}));
|
|
162
|
+
this.name = 'NotFoundError';
|
|
163
|
+
this.resource = options.resource;
|
|
164
|
+
this.id = options.id;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface ServerInternalErrorOptions extends HttpErrorOptions {
|
|
169
|
+
traceId?: string | undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export class ServerInternalError extends HttpError {
|
|
173
|
+
readonly _tag = 'ServerInternalError' as const;
|
|
174
|
+
readonly traceId?: string | undefined;
|
|
175
|
+
|
|
176
|
+
constructor(
|
|
177
|
+
message: string = 'Internal server failure',
|
|
178
|
+
statusCode: number = 500,
|
|
179
|
+
options: ServerInternalErrorOptions = {},
|
|
180
|
+
) {
|
|
181
|
+
super(message, statusCode, undefined, definedOptions({
|
|
182
|
+
code: options.code ?? 'INTERNAL_SERVER_ERROR',
|
|
183
|
+
details: options.details,
|
|
184
|
+
body: options.body,
|
|
185
|
+
cause: options.cause,
|
|
186
|
+
}));
|
|
187
|
+
this.name = 'ServerInternalError';
|
|
188
|
+
this.traceId = options.traceId;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export class BadRequestError extends HttpError {
|
|
193
|
+
readonly _tag = 'BadRequestError' as const;
|
|
194
|
+
readonly fieldErrors: ValidationErrors;
|
|
195
|
+
|
|
196
|
+
constructor(
|
|
197
|
+
message: string = 'Bad request',
|
|
198
|
+
fieldErrors: ValidationErrors = {},
|
|
199
|
+
options: HttpErrorOptions = {},
|
|
200
|
+
) {
|
|
201
|
+
super(message, 400, fieldErrors, definedOptions({
|
|
202
|
+
code: options.code ?? 'BAD_REQUEST',
|
|
203
|
+
details: options.details,
|
|
204
|
+
body: options.body,
|
|
205
|
+
cause: options.cause,
|
|
206
|
+
}));
|
|
207
|
+
this.name = 'BadRequestError';
|
|
208
|
+
this.fieldErrors = fieldErrors;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export class RateLimitError extends HttpError {
|
|
213
|
+
readonly _tag = 'RateLimitError' as const;
|
|
214
|
+
readonly retryable = true as const;
|
|
215
|
+
|
|
216
|
+
constructor(
|
|
217
|
+
message: string = 'Rate limit exceeded',
|
|
218
|
+
options: HttpErrorOptions = {},
|
|
219
|
+
) {
|
|
220
|
+
super(message, 429, undefined, definedOptions({
|
|
221
|
+
code: options.code ?? 'RATE_LIMITED',
|
|
222
|
+
details: options.details,
|
|
223
|
+
body: options.body,
|
|
224
|
+
cause: options.cause,
|
|
225
|
+
}));
|
|
226
|
+
this.name = 'RateLimitError';
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export class UnavailableError extends HttpError {
|
|
231
|
+
readonly _tag = 'UnavailableError' as const;
|
|
232
|
+
readonly retryable = true as const;
|
|
233
|
+
|
|
234
|
+
constructor(
|
|
235
|
+
message: string = 'Service unavailable',
|
|
236
|
+
statusCode: number = 503,
|
|
237
|
+
options: HttpErrorOptions = {},
|
|
238
|
+
) {
|
|
239
|
+
super(message, statusCode, undefined, definedOptions({
|
|
240
|
+
code: options.code ?? 'SERVICE_UNAVAILABLE',
|
|
241
|
+
details: options.details,
|
|
242
|
+
body: options.body,
|
|
243
|
+
cause: options.cause,
|
|
244
|
+
}));
|
|
245
|
+
this.name = 'UnavailableError';
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export class CancelledError extends HttpError {
|
|
250
|
+
readonly _tag = 'CancelledError' as const;
|
|
251
|
+
readonly writeMayHaveSucceeded: boolean;
|
|
252
|
+
|
|
253
|
+
constructor(
|
|
254
|
+
message: string = 'Operation cancelled',
|
|
255
|
+
statusCode: number = 409,
|
|
256
|
+
options: HttpErrorOptions = {},
|
|
257
|
+
) {
|
|
258
|
+
super(message, statusCode, undefined, definedOptions({
|
|
259
|
+
code: options.code ?? 'OPERATION_CANCELLED',
|
|
260
|
+
details: options.details,
|
|
261
|
+
body: options.body,
|
|
262
|
+
cause: options.cause,
|
|
263
|
+
}));
|
|
264
|
+
this.name = 'CancelledError';
|
|
265
|
+
this.writeMayHaveSucceeded = writeMayHaveSucceeded(options.details);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export class AbortedError extends HttpError {
|
|
270
|
+
readonly _tag = 'AbortedError' as const;
|
|
271
|
+
readonly retryable = false as const;
|
|
272
|
+
|
|
273
|
+
constructor(
|
|
274
|
+
message: string = 'Operation aborted',
|
|
275
|
+
options: HttpErrorOptions = {},
|
|
276
|
+
) {
|
|
277
|
+
super(message, 0, undefined, definedOptions({
|
|
278
|
+
code: options.code ?? 'ABORTED',
|
|
279
|
+
details: options.details,
|
|
280
|
+
body: options.body,
|
|
281
|
+
cause: options.cause,
|
|
282
|
+
}));
|
|
283
|
+
this.name = 'AbortedError';
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** 用户撤销不是服务器失败,不能伪装成 500。 */
|
|
288
|
+
export class UndoneError extends Error {
|
|
289
|
+
readonly _tag = 'UndoneError' as const;
|
|
290
|
+
|
|
291
|
+
constructor(
|
|
292
|
+
message: string = 'Mutation undone',
|
|
293
|
+
options: { cause?: unknown } = {},
|
|
294
|
+
) {
|
|
295
|
+
super(message, options.cause === undefined ? undefined : { cause: options.cause });
|
|
296
|
+
this.name = 'UndoneError';
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export type DomainError =
|
|
301
|
+
| ValidationError
|
|
302
|
+
| ConflictError
|
|
303
|
+
| AuthError
|
|
304
|
+
| NetworkError
|
|
305
|
+
| ContractDecodingError
|
|
306
|
+
| NotFoundError
|
|
307
|
+
| ServerInternalError
|
|
308
|
+
| BadRequestError
|
|
309
|
+
| RateLimitError
|
|
310
|
+
| UnavailableError
|
|
311
|
+
| CancelledError
|
|
312
|
+
| AbortedError
|
|
313
|
+
| UndoneError;
|
|
314
|
+
|
|
315
|
+
export type DomainErrorTag = DomainError['_tag'];
|
|
316
|
+
|
|
317
|
+
type ErrorHandlers<E extends { readonly _tag: string }, R> = {
|
|
318
|
+
[K in E['_tag']]: (error: Extract<E, { readonly _tag: K }>) => R;
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
/** 要么覆盖全部分支,要么提供 `_` 兜底。缺分支在编译期失败。 */
|
|
322
|
+
export type ErrorPattern<E extends { readonly _tag: string }, R> =
|
|
323
|
+
| ErrorHandlers<E, R>
|
|
324
|
+
| (Partial<ErrorHandlers<E, R>> & { _: (error: E) => R });
|
|
325
|
+
|
|
326
|
+
export function matchError<E extends { readonly _tag: string }, R>(
|
|
327
|
+
error: E,
|
|
328
|
+
patterns: ErrorPattern<E, R>,
|
|
329
|
+
): R {
|
|
330
|
+
const tag = error._tag as E['_tag'];
|
|
331
|
+
const handler = (patterns as Partial<ErrorHandlers<E, R>>)[tag];
|
|
332
|
+
if (typeof handler === 'function') {
|
|
333
|
+
return handler(error as never);
|
|
334
|
+
}
|
|
335
|
+
if ('_' in patterns && typeof patterns._ === 'function') {
|
|
336
|
+
return patterns._(error);
|
|
337
|
+
}
|
|
338
|
+
throw new Error(`Unhandled domain error tag: ${tag}`);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function writeMayHaveSucceeded(details: unknown): boolean {
|
|
342
|
+
return typeof details === 'object'
|
|
343
|
+
&& details !== null
|
|
344
|
+
&& 'writeMayHaveSucceeded' in details
|
|
345
|
+
&& details.writeMayHaveSucceeded === true;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function carried(cause: HttpError): HttpErrorOptions {
|
|
349
|
+
return definedOptions({
|
|
350
|
+
code: cause.code,
|
|
351
|
+
details: cause.details,
|
|
352
|
+
body: cause.body,
|
|
353
|
+
cause,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function isDomainError(cause: unknown): cause is DomainError {
|
|
358
|
+
return cause instanceof ValidationError
|
|
359
|
+
|| cause instanceof ConflictError
|
|
360
|
+
|| cause instanceof AuthError
|
|
361
|
+
|| cause instanceof NetworkError
|
|
362
|
+
|| cause instanceof ContractDecodingError
|
|
363
|
+
|| cause instanceof NotFoundError
|
|
364
|
+
|| cause instanceof ServerInternalError
|
|
365
|
+
|| cause instanceof BadRequestError
|
|
366
|
+
|| cause instanceof RateLimitError
|
|
367
|
+
|| cause instanceof UnavailableError
|
|
368
|
+
|| cause instanceof CancelledError
|
|
369
|
+
|| cause instanceof AbortedError
|
|
370
|
+
|| cause instanceof UndoneError;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* 保留 HttpError 的 code、details、body。
|
|
375
|
+
* 取消和撤销不能改写成冲突或 500,否则 safeMutate 会丢掉 writeMayHaveSucceeded。
|
|
376
|
+
*/
|
|
377
|
+
export function toDomainError(cause: unknown): DomainError {
|
|
378
|
+
if (isDomainError(cause)) return cause;
|
|
379
|
+
|
|
380
|
+
if (cause instanceof UndoError) {
|
|
381
|
+
return new UndoneError(cause.message, { cause });
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (cause instanceof HttpError) {
|
|
385
|
+
const options = carried(cause);
|
|
386
|
+
if (typeof cause.code === 'string' && cause.code.endsWith('_CANCELLED')) {
|
|
387
|
+
return new CancelledError(cause.message, cause.statusCode, options);
|
|
388
|
+
}
|
|
389
|
+
const status = cause.statusCode;
|
|
390
|
+
if (status === 422) return new ValidationError(cause.message, cause.errors ?? {}, options);
|
|
391
|
+
if (status === 409) return new ConflictError(cause.message, options);
|
|
392
|
+
if (status === 401) return new AuthError('unauthorized', cause.message, options);
|
|
393
|
+
if (status === 403) return new AuthError('forbidden', cause.message, options);
|
|
394
|
+
if (status === 404) return new NotFoundError(cause.message, options);
|
|
395
|
+
if (status === 400) return new BadRequestError(cause.message, cause.errors ?? {}, options);
|
|
396
|
+
if (status === 408 || status === 504 || status === 0) {
|
|
397
|
+
return new NetworkError(cause.message, definedOptions({
|
|
398
|
+
isTimeout: status === 408 || status === 504,
|
|
399
|
+
statusCode: status,
|
|
400
|
+
code: cause.code,
|
|
401
|
+
details: cause.details,
|
|
402
|
+
body: cause.body,
|
|
403
|
+
cause,
|
|
404
|
+
}));
|
|
405
|
+
}
|
|
406
|
+
if (status === 429) return new RateLimitError(cause.message, options);
|
|
407
|
+
if (status === 503) return new UnavailableError(cause.message, status, options);
|
|
408
|
+
return new ServerInternalError(cause.message, status, options);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (cause instanceof Error) {
|
|
412
|
+
if (cause.name === 'AbortError') return new AbortedError(cause.message, { cause });
|
|
413
|
+
if (cause.name === 'TimeoutError') return new NetworkError(cause.message, { isTimeout: true, cause });
|
|
414
|
+
return new ServerInternalError(cause.message, 500, { cause });
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return new ServerInternalError(typeof cause === 'string' ? cause : 'Unknown system error', 500, { cause });
|
|
418
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -333,12 +333,16 @@ export {
|
|
|
333
333
|
generateResourceBundle,
|
|
334
334
|
} from './inferencer';
|
|
335
335
|
export type { InferResult, InferReview, InferReviewWarning, InferReviewWarningCode } from './inferencer';
|
|
336
|
+
export { createMockDataProvider } from './mock-provider';
|
|
337
|
+
export type { MockDataProviderOptions, MockDataProviderInstance } from './mock-provider';
|
|
336
338
|
export { createWebSocketLiveProvider } from './live-websocket';
|
|
337
339
|
export type { WebSocketLiveProviderOptions } from './live-websocket';
|
|
338
340
|
export { createSSELiveProvider } from './live-sse';
|
|
339
341
|
export type { SSELiveProviderOptions } from './live-sse';
|
|
340
342
|
export { inferFromOpenAPI } from './inferencer-openapi';
|
|
341
343
|
export type { InferFromOpenAPIOptions } from './inferencer-openapi';
|
|
344
|
+
export { inferFromDatabaseColumns, inferFromJSONSchema } from './inferencer-schema';
|
|
345
|
+
export type { DatabaseColumnMeta, InferFromDatabaseOptions, JSONSchemaInput, JSONSchemaProperty } from './inferencer-schema';
|
|
342
346
|
export { inferFromGraphQL, GRAPHQL_INTROSPECTION_QUERY } from './inferencer-graphql';
|
|
343
347
|
export type { InferFromGraphQLOptions, GraphQLIntrospectionSchema, GraphQLTypeDescriptor } from './inferencer-graphql';
|
|
344
348
|
export {
|
|
@@ -419,3 +423,52 @@ export { formatProductPreview, runProductPipeline } from './product-pipeline';
|
|
|
419
423
|
export type { ProductPipelineInput, ProductPipelineOutput } from './product-pipeline';
|
|
420
424
|
export { reviewProductPlan } from './product-review';
|
|
421
425
|
export type { ProductReviewIssue, ProductReviewMetrics, ProductReviewReport, ProductReviewSeverity } from './product-review';
|
|
426
|
+
|
|
427
|
+
// High-Rigor Typed Result, Domain Errors & Resilience
|
|
428
|
+
export {
|
|
429
|
+
ok, err, isOk, isErr, unwrapResult, unwrapOr,
|
|
430
|
+
mapResult, mapErr, flatMapResult, matchResult,
|
|
431
|
+
fromThrowable, fromPromise,
|
|
432
|
+
} from './result';
|
|
433
|
+
export type { Result, Ok, Err } from './result';
|
|
434
|
+
|
|
435
|
+
export {
|
|
436
|
+
ValidationError,
|
|
437
|
+
ConflictError,
|
|
438
|
+
AuthError,
|
|
439
|
+
NetworkError,
|
|
440
|
+
ContractDecodingError,
|
|
441
|
+
NotFoundError,
|
|
442
|
+
ServerInternalError,
|
|
443
|
+
BadRequestError,
|
|
444
|
+
RateLimitError,
|
|
445
|
+
UnavailableError,
|
|
446
|
+
CancelledError,
|
|
447
|
+
AbortedError,
|
|
448
|
+
UndoneError,
|
|
449
|
+
matchError,
|
|
450
|
+
toDomainError,
|
|
451
|
+
} from './domain-errors';
|
|
452
|
+
export type {
|
|
453
|
+
DomainError,
|
|
454
|
+
DomainErrorTag,
|
|
455
|
+
ErrorPattern,
|
|
456
|
+
ConflictErrorOptions,
|
|
457
|
+
AuthErrorOptions,
|
|
458
|
+
NetworkErrorOptions,
|
|
459
|
+
ContractDecodingErrorOptions,
|
|
460
|
+
NotFoundErrorOptions,
|
|
461
|
+
ServerInternalErrorOptions,
|
|
462
|
+
} from './domain-errors';
|
|
463
|
+
|
|
464
|
+
export {
|
|
465
|
+
ConcurrencyLimitClearedError,
|
|
466
|
+
createConcurrencyLimiter,
|
|
467
|
+
withRetry,
|
|
468
|
+
wrapDataProviderWithResult,
|
|
469
|
+
} from './resilience';
|
|
470
|
+
export type {
|
|
471
|
+
ConcurrencyLimiter,
|
|
472
|
+
RetryOptions,
|
|
473
|
+
SafeDataProvider,
|
|
474
|
+
} from './resilience';
|