@outfitter/contracts 0.1.0-rc.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/README.md +50 -0
- package/dist/actions.d.ts +388 -0
- package/dist/actions.js +32 -0
- package/dist/adapters.d.ts +182 -0
- package/dist/adapters.js +1 -0
- package/dist/assert/index.d.ts +63 -0
- package/dist/assert/index.js +36 -0
- package/dist/capabilities.d.ts +19 -0
- package/dist/capabilities.js +66 -0
- package/dist/context.d.ts +125 -0
- package/dist/context.js +36 -0
- package/dist/envelope.d.ts +328 -0
- package/dist/envelope.js +56 -0
- package/dist/errors.d.ts +261 -0
- package/dist/errors.js +171 -0
- package/dist/handler.d.ts +318 -0
- package/dist/handler.js +1 -0
- package/dist/index.d.ts +1354 -0
- package/dist/index.js +137 -0
- package/dist/recovery.d.ts +150 -0
- package/dist/recovery.js +56 -0
- package/dist/redactor.d.ts +100 -0
- package/dist/redactor.js +111 -0
- package/dist/resilience.d.ts +299 -0
- package/dist/resilience.js +82 -0
- package/dist/result/index.d.ts +103 -0
- package/dist/result/index.js +13 -0
- package/dist/result/utilities.d.ts +103 -0
- package/dist/result/utilities.js +31 -0
- package/dist/serialization.d.ts +313 -0
- package/dist/serialization.js +270 -0
- package/dist/validation.d.ts +59 -0
- package/dist/validation.js +42 -0
- package/package.json +143 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { TaggedErrorClass } from "better-result";
|
|
2
|
+
declare const ValidationErrorBase: TaggedErrorClass<"ValidationError", {
|
|
3
|
+
message: string;
|
|
4
|
+
field?: string;
|
|
5
|
+
}>;
|
|
6
|
+
declare const AssertionErrorBase: TaggedErrorClass<"AssertionError", {
|
|
7
|
+
message: string;
|
|
8
|
+
}>;
|
|
9
|
+
declare const NotFoundErrorBase: TaggedErrorClass<"NotFoundError", {
|
|
10
|
+
message: string;
|
|
11
|
+
resourceType: string;
|
|
12
|
+
resourceId: string;
|
|
13
|
+
}>;
|
|
14
|
+
declare const ConflictErrorBase: TaggedErrorClass<"ConflictError", {
|
|
15
|
+
message: string;
|
|
16
|
+
context?: Record<string, unknown>;
|
|
17
|
+
}>;
|
|
18
|
+
declare const PermissionErrorBase: TaggedErrorClass<"PermissionError", {
|
|
19
|
+
message: string;
|
|
20
|
+
context?: Record<string, unknown>;
|
|
21
|
+
}>;
|
|
22
|
+
declare const TimeoutErrorBase: TaggedErrorClass<"TimeoutError", {
|
|
23
|
+
message: string;
|
|
24
|
+
operation: string;
|
|
25
|
+
timeoutMs: number;
|
|
26
|
+
}>;
|
|
27
|
+
declare const RateLimitErrorBase: TaggedErrorClass<"RateLimitError", {
|
|
28
|
+
message: string;
|
|
29
|
+
retryAfterSeconds?: number;
|
|
30
|
+
}>;
|
|
31
|
+
declare const NetworkErrorBase: TaggedErrorClass<"NetworkError", {
|
|
32
|
+
message: string;
|
|
33
|
+
context?: Record<string, unknown>;
|
|
34
|
+
}>;
|
|
35
|
+
declare const InternalErrorBase: TaggedErrorClass<"InternalError", {
|
|
36
|
+
message: string;
|
|
37
|
+
context?: Record<string, unknown>;
|
|
38
|
+
}>;
|
|
39
|
+
declare const AuthErrorBase: TaggedErrorClass<"AuthError", {
|
|
40
|
+
message: string;
|
|
41
|
+
reason?: "missing" | "invalid" | "expired";
|
|
42
|
+
}>;
|
|
43
|
+
declare const CancelledErrorBase: TaggedErrorClass<"CancelledError", {
|
|
44
|
+
message: string;
|
|
45
|
+
}>;
|
|
46
|
+
/**
|
|
47
|
+
* Input validation failed.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* new ValidationError({ message: "Email format invalid", field: "email" });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare class ValidationError extends ValidationErrorBase {
|
|
55
|
+
readonly category: "validation";
|
|
56
|
+
exitCode(): number;
|
|
57
|
+
statusCode(): number;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Assertion failed (invariant violation).
|
|
61
|
+
*
|
|
62
|
+
* Used by assertion utilities that return Result types instead of throwing.
|
|
63
|
+
* AssertionError indicates a programming bug — an invariant that should
|
|
64
|
+
* never be violated was broken. These are internal errors, not user input
|
|
65
|
+
* validation failures.
|
|
66
|
+
*
|
|
67
|
+
* **Category rationale**: Uses `internal` (not `validation`) because:
|
|
68
|
+
* - Assertions check **invariants** (programmer assumptions), not user input
|
|
69
|
+
* - A failed assertion means "this should be impossible if the code is correct"
|
|
70
|
+
* - User-facing validation uses {@link ValidationError} with helpful field info
|
|
71
|
+
* - HTTP 500 is correct: this is a server bug, not a client mistake
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* // In domain logic after validation has passed
|
|
76
|
+
* const result = assertDefined(cachedValue, "Cache should always have value after init");
|
|
77
|
+
* if (result.isErr()) {
|
|
78
|
+
* return result; // Propagate as internal error
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @see ValidationError - For user input validation failures (HTTP 400)
|
|
83
|
+
*/
|
|
84
|
+
declare class AssertionError extends AssertionErrorBase {
|
|
85
|
+
readonly category: "internal";
|
|
86
|
+
exitCode(): number;
|
|
87
|
+
statusCode(): number;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Requested resource not found.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* new NotFoundError({ message: "note not found: abc123", resourceType: "note", resourceId: "abc123" });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
declare class NotFoundError extends NotFoundErrorBase {
|
|
98
|
+
readonly category: "not_found";
|
|
99
|
+
exitCode(): number;
|
|
100
|
+
statusCode(): number;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* State conflict (optimistic locking, concurrent modification).
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* new ConflictError({ message: "Resource was modified by another process" });
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
declare class ConflictError extends ConflictErrorBase {
|
|
111
|
+
readonly category: "conflict";
|
|
112
|
+
exitCode(): number;
|
|
113
|
+
statusCode(): number;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Authorization denied.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* new PermissionError({ message: "Cannot delete read-only resource" });
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
declare class PermissionError extends PermissionErrorBase {
|
|
124
|
+
readonly category: "permission";
|
|
125
|
+
exitCode(): number;
|
|
126
|
+
statusCode(): number;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Operation timed out.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* new TimeoutError({ message: "Database query timed out after 5000ms", operation: "Database query", timeoutMs: 5000 });
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
declare class TimeoutError extends TimeoutErrorBase {
|
|
137
|
+
readonly category: "timeout";
|
|
138
|
+
exitCode(): number;
|
|
139
|
+
statusCode(): number;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Rate limit exceeded.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* new RateLimitError({ message: "Rate limit exceeded, retry after 60s", retryAfterSeconds: 60 });
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
declare class RateLimitError extends RateLimitErrorBase {
|
|
150
|
+
readonly category: "rate_limit";
|
|
151
|
+
exitCode(): number;
|
|
152
|
+
statusCode(): number;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Network/transport failure.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* new NetworkError({ message: "Connection refused to api.example.com" });
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
declare class NetworkError extends NetworkErrorBase {
|
|
163
|
+
readonly category: "network";
|
|
164
|
+
exitCode(): number;
|
|
165
|
+
statusCode(): number;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Unexpected internal error.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* new InternalError({ message: "Unexpected state in processor" });
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
declare class InternalError extends InternalErrorBase {
|
|
176
|
+
readonly category: "internal";
|
|
177
|
+
exitCode(): number;
|
|
178
|
+
statusCode(): number;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Authentication failed (missing or invalid credentials).
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* new AuthError({ message: "Invalid API key", reason: "invalid" });
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
declare class AuthError extends AuthErrorBase {
|
|
189
|
+
readonly category: "auth";
|
|
190
|
+
exitCode(): number;
|
|
191
|
+
statusCode(): number;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Operation cancelled by user or signal.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```typescript
|
|
198
|
+
* new CancelledError({ message: "Operation cancelled by user" });
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
declare class CancelledError extends CancelledErrorBase {
|
|
202
|
+
readonly category: "cancelled";
|
|
203
|
+
exitCode(): number;
|
|
204
|
+
statusCode(): number;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Union type of all concrete error class instances.
|
|
208
|
+
*/
|
|
209
|
+
type AnyKitError = InstanceType<typeof ValidationError> | InstanceType<typeof AssertionError> | InstanceType<typeof NotFoundError> | InstanceType<typeof ConflictError> | InstanceType<typeof PermissionError> | InstanceType<typeof TimeoutError> | InstanceType<typeof RateLimitError> | InstanceType<typeof NetworkError> | InstanceType<typeof InternalError> | InstanceType<typeof AuthError> | InstanceType<typeof CancelledError>;
|
|
210
|
+
/**
|
|
211
|
+
* Type alias for backwards compatibility with handler signatures.
|
|
212
|
+
* Use AnyKitError for the union type.
|
|
213
|
+
*/
|
|
214
|
+
type OutfitterError = AnyKitError;
|
|
215
|
+
import { Result } from "better-result";
|
|
216
|
+
/**
|
|
217
|
+
* Logger interface for handler context.
|
|
218
|
+
* Implementations provided by @outfitter/logging.
|
|
219
|
+
*
|
|
220
|
+
* All log methods accept an optional context object that will be merged
|
|
221
|
+
* with any context inherited from parent loggers created via `child()`.
|
|
222
|
+
*/
|
|
223
|
+
interface Logger {
|
|
224
|
+
trace(message: string, metadata?: Record<string, unknown>): void;
|
|
225
|
+
debug(message: string, metadata?: Record<string, unknown>): void;
|
|
226
|
+
info(message: string, metadata?: Record<string, unknown>): void;
|
|
227
|
+
warn(message: string, metadata?: Record<string, unknown>): void;
|
|
228
|
+
error(message: string, metadata?: Record<string, unknown>): void;
|
|
229
|
+
fatal(message: string, metadata?: Record<string, unknown>): void;
|
|
230
|
+
/**
|
|
231
|
+
* Creates a child logger with additional context.
|
|
232
|
+
*
|
|
233
|
+
* Context from the child is merged with the parent's context,
|
|
234
|
+
* with child context taking precedence for duplicate keys.
|
|
235
|
+
* Child loggers are composable (can create nested children).
|
|
236
|
+
*
|
|
237
|
+
* @param context - Additional context to include in all log messages
|
|
238
|
+
* @returns A new Logger instance with the merged context
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```typescript
|
|
242
|
+
* const requestLogger = ctx.logger.child({ requestId: ctx.requestId });
|
|
243
|
+
* requestLogger.info("Processing request"); // includes requestId
|
|
244
|
+
*
|
|
245
|
+
* const opLogger = requestLogger.child({ operation: "create" });
|
|
246
|
+
* opLogger.debug("Starting"); // includes requestId + operation
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
child(context: Record<string, unknown>): Logger;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Resolved configuration interface.
|
|
253
|
+
* Implementations provided by @outfitter/config.
|
|
254
|
+
*/
|
|
255
|
+
interface ResolvedConfig {
|
|
256
|
+
get<T>(key: string): T | undefined;
|
|
257
|
+
getRequired<T>(key: string): T;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Handler context - provides cross-cutting concerns without polluting handler signatures.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```typescript
|
|
264
|
+
* const handler: Handler<Input, Output, NotFoundError> = async (input, ctx) => {
|
|
265
|
+
* ctx.logger.debug("Processing request", { requestId: ctx.requestId });
|
|
266
|
+
* // ... handler logic
|
|
267
|
+
* };
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
interface HandlerContext {
|
|
271
|
+
/** Abort signal for cancellation propagation */
|
|
272
|
+
signal?: AbortSignal;
|
|
273
|
+
/** Unique request identifier for tracing (UUIDv7) */
|
|
274
|
+
requestId: string;
|
|
275
|
+
/** Structured logger with automatic redaction */
|
|
276
|
+
logger: Logger;
|
|
277
|
+
/** Resolved configuration values */
|
|
278
|
+
config?: ResolvedConfig;
|
|
279
|
+
/** Workspace root path, if detected */
|
|
280
|
+
workspaceRoot?: string;
|
|
281
|
+
/** Current working directory */
|
|
282
|
+
cwd: string;
|
|
283
|
+
/** Environment variables (filtered, redacted) */
|
|
284
|
+
env: Record<string, string | undefined>;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Handler - transport-agnostic domain logic unit.
|
|
288
|
+
*
|
|
289
|
+
* Handlers receive typed input, return Results, and know nothing about
|
|
290
|
+
* transport or output format. CLI and MCP are thin adapters over handlers.
|
|
291
|
+
*
|
|
292
|
+
* @typeParam TInput - Validated input parameters
|
|
293
|
+
* @typeParam TOutput - Success return type
|
|
294
|
+
* @typeParam TError - Error type (must extend OutfitterError)
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```typescript
|
|
298
|
+
* const getNote: Handler<{ id: string }, Note, NotFoundError> = async (input, ctx) => {
|
|
299
|
+
* const note = await ctx.db.notes.find(input.id);
|
|
300
|
+
* if (!note) return Result.err(new NotFoundError("note", input.id));
|
|
301
|
+
* return Result.ok(note);
|
|
302
|
+
* };
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
type Handler<
|
|
306
|
+
TInput,
|
|
307
|
+
TOutput,
|
|
308
|
+
TError extends OutfitterError = OutfitterError
|
|
309
|
+
> = (input: TInput, ctx: HandlerContext) => Promise<Result<TOutput, TError>>;
|
|
310
|
+
/**
|
|
311
|
+
* Synchronous handler variant for operations that don't need async.
|
|
312
|
+
*/
|
|
313
|
+
type SyncHandler<
|
|
314
|
+
TInput,
|
|
315
|
+
TOutput,
|
|
316
|
+
TError extends OutfitterError = OutfitterError
|
|
317
|
+
> = (input: TInput, ctx: HandlerContext) => Result<TOutput, TError>;
|
|
318
|
+
export { SyncHandler, ResolvedConfig, Logger, HandlerContext, Handler };
|
package/dist/handler.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// @bun
|