@coherent.js/api 1.0.0-beta.2
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 +91 -0
- package/dist/api/errors.d.ts +92 -0
- package/dist/api/errors.d.ts.map +1 -0
- package/dist/api/errors.js +161 -0
- package/dist/api/errors.js.map +1 -0
- package/dist/api/index.d.ts +61 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +41 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/middleware.d.ts +57 -0
- package/dist/api/middleware.d.ts.map +1 -0
- package/dist/api/middleware.js +244 -0
- package/dist/api/middleware.js.map +1 -0
- package/dist/api/openapi.d.ts +54 -0
- package/dist/api/openapi.d.ts.map +1 -0
- package/dist/api/openapi.js +144 -0
- package/dist/api/openapi.js.map +1 -0
- package/dist/api/router.d.ts +368 -0
- package/dist/api/router.d.ts.map +1 -0
- package/dist/api/router.js +1508 -0
- package/dist/api/router.js.map +1 -0
- package/dist/api/security.d.ts +64 -0
- package/dist/api/security.d.ts.map +1 -0
- package/dist/api/security.js +239 -0
- package/dist/api/security.js.map +1 -0
- package/dist/api/serialization.d.ts +86 -0
- package/dist/api/serialization.d.ts.map +1 -0
- package/dist/api/serialization.js +151 -0
- package/dist/api/serialization.js.map +1 -0
- package/dist/api/validation.d.ts +34 -0
- package/dist/api/validation.d.ts.map +1 -0
- package/dist/api/validation.js +172 -0
- package/dist/api/validation.js.map +1 -0
- package/dist/index.cjs +1776 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.js +1722 -0
- package/dist/index.js.map +7 -0
- package/package.json +46 -0
- package/types/index.d.ts +720 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coherent.js API Types
|
|
3
|
+
* TypeScript definitions for the API framework
|
|
4
|
+
*
|
|
5
|
+
* @version 1.0.0-beta.1
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { IncomingMessage, ServerResponse } from 'http';
|
|
9
|
+
|
|
10
|
+
// ============================================================================
|
|
11
|
+
// HTTP Types
|
|
12
|
+
// ============================================================================
|
|
13
|
+
|
|
14
|
+
/** HTTP methods */
|
|
15
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD';
|
|
16
|
+
|
|
17
|
+
/** HTTP status codes */
|
|
18
|
+
export type HttpStatusCode = number;
|
|
19
|
+
|
|
20
|
+
/** Request headers */
|
|
21
|
+
export interface RequestHeaders {
|
|
22
|
+
[key: string]: string | string[] | undefined;
|
|
23
|
+
'content-type'?: string;
|
|
24
|
+
'authorization'?: string;
|
|
25
|
+
'accept'?: string;
|
|
26
|
+
'user-agent'?: string;
|
|
27
|
+
'x-api-key'?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Response headers */
|
|
31
|
+
export interface ResponseHeaders {
|
|
32
|
+
[key: string]: string | number | string[];
|
|
33
|
+
'content-type'?: string;
|
|
34
|
+
'cache-control'?: string;
|
|
35
|
+
'access-control-allow-origin'?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Query parameters */
|
|
39
|
+
export interface QueryParams {
|
|
40
|
+
[key: string]: string | string[] | undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** URL parameters */
|
|
44
|
+
export interface UrlParams {
|
|
45
|
+
[key: string]: string | undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Request body types */
|
|
49
|
+
export type RequestBody = any;
|
|
50
|
+
|
|
51
|
+
// ============================================================================
|
|
52
|
+
// API Request and Response
|
|
53
|
+
// ============================================================================
|
|
54
|
+
|
|
55
|
+
/** Enhanced API request object */
|
|
56
|
+
export interface ApiRequest extends IncomingMessage {
|
|
57
|
+
method: HttpMethod;
|
|
58
|
+
url: string;
|
|
59
|
+
headers: RequestHeaders;
|
|
60
|
+
query: QueryParams;
|
|
61
|
+
params: UrlParams;
|
|
62
|
+
body: RequestBody;
|
|
63
|
+
originalUrl?: string;
|
|
64
|
+
path?: string;
|
|
65
|
+
protocol?: string;
|
|
66
|
+
secure?: boolean;
|
|
67
|
+
ip?: string;
|
|
68
|
+
ips?: string[];
|
|
69
|
+
hostname?: string;
|
|
70
|
+
fresh?: boolean;
|
|
71
|
+
stale?: boolean;
|
|
72
|
+
xhr?: boolean;
|
|
73
|
+
user?: any;
|
|
74
|
+
session?: any;
|
|
75
|
+
cookies?: Record<string, string>;
|
|
76
|
+
signedCookies?: Record<string, string>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Enhanced API response object */
|
|
80
|
+
export interface ApiResponse extends ServerResponse {
|
|
81
|
+
json(obj: any): ApiResponse;
|
|
82
|
+
send(body: any): ApiResponse;
|
|
83
|
+
status(code: HttpStatusCode): ApiResponse;
|
|
84
|
+
set(field: string, val: string | string[]): ApiResponse;
|
|
85
|
+
set(field: ResponseHeaders): ApiResponse;
|
|
86
|
+
get(field: string): string | undefined;
|
|
87
|
+
header(field: string, val: string | string[]): ApiResponse;
|
|
88
|
+
header(field: ResponseHeaders): ApiResponse;
|
|
89
|
+
type(type: string): ApiResponse;
|
|
90
|
+
format(obj: Record<string, Function>): ApiResponse;
|
|
91
|
+
attachment(filename?: string): ApiResponse;
|
|
92
|
+
sendFile(path: string, options?: any, fn?: Function): void;
|
|
93
|
+
download(path: string, filename?: string, options?: any, fn?: Function): void;
|
|
94
|
+
contentType(type: string): ApiResponse;
|
|
95
|
+
sendStatus(code: HttpStatusCode): ApiResponse;
|
|
96
|
+
links(links: Record<string, string>): ApiResponse;
|
|
97
|
+
location(url: string): ApiResponse;
|
|
98
|
+
redirect(status: number, url: string): void;
|
|
99
|
+
redirect(url: string): void;
|
|
100
|
+
vary(field: string): ApiResponse;
|
|
101
|
+
render(view: string, locals?: any, callback?: Function): void;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ============================================================================
|
|
105
|
+
// Route Handler Types
|
|
106
|
+
// ============================================================================
|
|
107
|
+
|
|
108
|
+
/** Route handler function */
|
|
109
|
+
export type RouteHandler = (
|
|
110
|
+
req: ApiRequest,
|
|
111
|
+
res: ApiResponse,
|
|
112
|
+
next?: NextFunction
|
|
113
|
+
) => void | Promise<void> | any;
|
|
114
|
+
|
|
115
|
+
/** Next function for middleware */
|
|
116
|
+
export interface NextFunction {
|
|
117
|
+
(err?: any): void;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Middleware function */
|
|
121
|
+
export type Middleware = (
|
|
122
|
+
req: ApiRequest,
|
|
123
|
+
res: ApiResponse,
|
|
124
|
+
next: NextFunction
|
|
125
|
+
) => void | Promise<void>;
|
|
126
|
+
|
|
127
|
+
/** Error handling middleware */
|
|
128
|
+
export type ErrorMiddleware = (
|
|
129
|
+
err: any,
|
|
130
|
+
req: ApiRequest,
|
|
131
|
+
res: ApiResponse,
|
|
132
|
+
next: NextFunction
|
|
133
|
+
) => void | Promise<void>;
|
|
134
|
+
|
|
135
|
+
// ============================================================================
|
|
136
|
+
// Object-Based Routing
|
|
137
|
+
// ============================================================================
|
|
138
|
+
|
|
139
|
+
/** Route definition for object-based routing */
|
|
140
|
+
export interface RouteDefinition {
|
|
141
|
+
GET?: RouteHandler;
|
|
142
|
+
POST?: RouteHandler;
|
|
143
|
+
PUT?: RouteHandler;
|
|
144
|
+
DELETE?: RouteHandler;
|
|
145
|
+
PATCH?: RouteHandler;
|
|
146
|
+
OPTIONS?: RouteHandler;
|
|
147
|
+
HEAD?: RouteHandler;
|
|
148
|
+
middleware?: Middleware | Middleware[];
|
|
149
|
+
validation?: ValidationSchema;
|
|
150
|
+
serialization?: SerializationConfig;
|
|
151
|
+
auth?: AuthConfig;
|
|
152
|
+
rateLimit?: RateLimitConfig;
|
|
153
|
+
cache?: CacheConfig;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Nested route object */
|
|
157
|
+
export interface RouteObject {
|
|
158
|
+
[path: string]: RouteDefinition | RouteObject;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Router configuration */
|
|
162
|
+
export interface RouterConfig {
|
|
163
|
+
prefix?: string;
|
|
164
|
+
middleware?: Middleware[];
|
|
165
|
+
errorHandler?: ErrorMiddleware;
|
|
166
|
+
notFoundHandler?: RouteHandler;
|
|
167
|
+
caseSensitive?: boolean;
|
|
168
|
+
mergeParams?: boolean;
|
|
169
|
+
strict?: boolean;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Object router interface */
|
|
173
|
+
export interface ObjectRouter {
|
|
174
|
+
routes: RouteObject;
|
|
175
|
+
config: RouterConfig;
|
|
176
|
+
addRoute(path: string, definition: RouteDefinition): void;
|
|
177
|
+
addRoutes(routes: RouteObject): void;
|
|
178
|
+
use(middleware: Middleware): void;
|
|
179
|
+
use(path: string, middleware: Middleware): void;
|
|
180
|
+
handle(req: ApiRequest, res: ApiResponse, next?: NextFunction): void;
|
|
181
|
+
getRoutes(): RouteObject;
|
|
182
|
+
mount(app: any): void;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ============================================================================
|
|
186
|
+
// Validation
|
|
187
|
+
// ============================================================================
|
|
188
|
+
|
|
189
|
+
/** Field validation rule */
|
|
190
|
+
export interface ValidationRule {
|
|
191
|
+
type?: 'string' | 'number' | 'boolean' | 'array' | 'object' | 'email' | 'url' | 'date';
|
|
192
|
+
required?: boolean;
|
|
193
|
+
min?: number;
|
|
194
|
+
max?: number;
|
|
195
|
+
minLength?: number;
|
|
196
|
+
maxLength?: number;
|
|
197
|
+
pattern?: RegExp | string;
|
|
198
|
+
enum?: any[];
|
|
199
|
+
custom?: (value: any, field: string, data: any) => boolean | string;
|
|
200
|
+
message?: string;
|
|
201
|
+
transform?: (value: any) => any;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Validation schema */
|
|
205
|
+
export interface ValidationSchema {
|
|
206
|
+
[field: string]: ValidationRule | ValidationSchema;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Validation result */
|
|
210
|
+
export interface ValidationResult {
|
|
211
|
+
valid: boolean;
|
|
212
|
+
errors: ValidationError[];
|
|
213
|
+
data: any;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Validation error */
|
|
217
|
+
export interface ValidationError {
|
|
218
|
+
field: string;
|
|
219
|
+
message: string;
|
|
220
|
+
value: any;
|
|
221
|
+
rule: string;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Validation options */
|
|
225
|
+
export interface ValidationOptions {
|
|
226
|
+
abortEarly?: boolean;
|
|
227
|
+
stripUnknown?: boolean;
|
|
228
|
+
allowUnknown?: boolean;
|
|
229
|
+
skipMissing?: boolean;
|
|
230
|
+
context?: any;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ============================================================================
|
|
234
|
+
// Authentication and Authorization
|
|
235
|
+
// ============================================================================
|
|
236
|
+
|
|
237
|
+
/** Authentication configuration */
|
|
238
|
+
export interface AuthConfig {
|
|
239
|
+
required?: boolean;
|
|
240
|
+
roles?: string[];
|
|
241
|
+
permissions?: string[];
|
|
242
|
+
strategy?: 'jwt' | 'session' | 'basic' | 'custom';
|
|
243
|
+
verify?: (req: ApiRequest) => Promise<any> | any;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** JWT options */
|
|
247
|
+
export interface JwtOptions {
|
|
248
|
+
secret: string;
|
|
249
|
+
algorithm?: string;
|
|
250
|
+
expiresIn?: string | number;
|
|
251
|
+
issuer?: string;
|
|
252
|
+
audience?: string;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** User authentication info */
|
|
256
|
+
export interface AuthUser {
|
|
257
|
+
id: string | number;
|
|
258
|
+
username?: string;
|
|
259
|
+
email?: string;
|
|
260
|
+
roles?: string[];
|
|
261
|
+
permissions?: string[];
|
|
262
|
+
[key: string]: any;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// ============================================================================
|
|
266
|
+
// Rate Limiting
|
|
267
|
+
// ============================================================================
|
|
268
|
+
|
|
269
|
+
/** Rate limit configuration */
|
|
270
|
+
export interface RateLimitConfig {
|
|
271
|
+
windowMs?: number;
|
|
272
|
+
max?: number;
|
|
273
|
+
keyGenerator?: (req: ApiRequest) => string;
|
|
274
|
+
handler?: RouteHandler;
|
|
275
|
+
skip?: (req: ApiRequest) => boolean;
|
|
276
|
+
message?: string | any;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// ============================================================================
|
|
280
|
+
// Caching
|
|
281
|
+
// ============================================================================
|
|
282
|
+
|
|
283
|
+
/** Cache configuration */
|
|
284
|
+
export interface CacheConfig {
|
|
285
|
+
ttl?: number;
|
|
286
|
+
key?: string | ((req: ApiRequest) => string);
|
|
287
|
+
varies?: string[];
|
|
288
|
+
condition?: (req: ApiRequest, res: ApiResponse) => boolean;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// ============================================================================
|
|
292
|
+
// Serialization
|
|
293
|
+
// ============================================================================
|
|
294
|
+
|
|
295
|
+
/** Serialization configuration */
|
|
296
|
+
export interface SerializationConfig {
|
|
297
|
+
include?: string[];
|
|
298
|
+
exclude?: string[];
|
|
299
|
+
transform?: Record<string, (value: any) => any>;
|
|
300
|
+
dateFormat?: string;
|
|
301
|
+
nullValues?: boolean;
|
|
302
|
+
undefinedValues?: boolean;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Serialization options */
|
|
306
|
+
export interface SerializationOptions {
|
|
307
|
+
space?: number;
|
|
308
|
+
replacer?: (key: string, value: any) => any;
|
|
309
|
+
dateHandler?: (date: Date) => any;
|
|
310
|
+
errorHandler?: (error: Error) => any;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ============================================================================
|
|
314
|
+
// Error Handling
|
|
315
|
+
// ============================================================================
|
|
316
|
+
|
|
317
|
+
/** Base API error */
|
|
318
|
+
export class ApiError extends Error {
|
|
319
|
+
constructor(message: string, statusCode?: number, code?: string);
|
|
320
|
+
statusCode: number;
|
|
321
|
+
code: string;
|
|
322
|
+
details?: any;
|
|
323
|
+
toJSON(): object;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** Validation error class */
|
|
327
|
+
export class ValidationError extends ApiError {
|
|
328
|
+
constructor(message: string, errors?: ValidationError[]);
|
|
329
|
+
errors: ValidationError[];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Authentication error class */
|
|
333
|
+
export class AuthenticationError extends ApiError {
|
|
334
|
+
constructor(message?: string);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** Authorization error class */
|
|
338
|
+
export class AuthorizationError extends ApiError {
|
|
339
|
+
constructor(message?: string);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Not found error class */
|
|
343
|
+
export class NotFoundError extends ApiError {
|
|
344
|
+
constructor(message?: string);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** Conflict error class */
|
|
348
|
+
export class ConflictError extends ApiError {
|
|
349
|
+
constructor(message?: string);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Error handler options */
|
|
353
|
+
export interface ErrorHandlerOptions {
|
|
354
|
+
includeStack?: boolean;
|
|
355
|
+
logger?: (error: Error, req: ApiRequest) => void;
|
|
356
|
+
transform?: (error: Error) => any;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// ============================================================================
|
|
360
|
+
// Middleware Types
|
|
361
|
+
// ============================================================================
|
|
362
|
+
|
|
363
|
+
/** CORS configuration */
|
|
364
|
+
export interface CorsConfig {
|
|
365
|
+
origin?: string | string[] | boolean | ((req: ApiRequest) => boolean);
|
|
366
|
+
methods?: HttpMethod[];
|
|
367
|
+
allowedHeaders?: string[];
|
|
368
|
+
exposedHeaders?: string[];
|
|
369
|
+
credentials?: boolean;
|
|
370
|
+
maxAge?: number;
|
|
371
|
+
preflightContinue?: boolean;
|
|
372
|
+
optionsSuccessStatus?: number;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** Body parser options */
|
|
376
|
+
export interface BodyParserOptions {
|
|
377
|
+
limit?: string;
|
|
378
|
+
extended?: boolean;
|
|
379
|
+
inflate?: boolean;
|
|
380
|
+
strict?: boolean;
|
|
381
|
+
type?: string | string[] | ((req: ApiRequest) => boolean);
|
|
382
|
+
verify?: (req: ApiRequest, res: ApiResponse, buf: Buffer, encoding: string) => void;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Security headers configuration */
|
|
386
|
+
export interface SecurityConfig {
|
|
387
|
+
contentSecurityPolicy?: string | boolean;
|
|
388
|
+
crossOriginEmbedderPolicy?: boolean;
|
|
389
|
+
crossOriginOpenerPolicy?: boolean;
|
|
390
|
+
crossOriginResourcePolicy?: string | boolean;
|
|
391
|
+
dnsPrefetchControl?: boolean;
|
|
392
|
+
expectCt?: boolean;
|
|
393
|
+
frameguard?: boolean | string;
|
|
394
|
+
hidePoweredBy?: boolean;
|
|
395
|
+
hsts?: boolean | object;
|
|
396
|
+
ieNoOpen?: boolean;
|
|
397
|
+
noSniff?: boolean;
|
|
398
|
+
originAgentCluster?: boolean;
|
|
399
|
+
permittedCrossDomainPolicies?: boolean | string;
|
|
400
|
+
referrerPolicy?: boolean | string;
|
|
401
|
+
xssFilter?: boolean;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// ============================================================================
|
|
405
|
+
// OpenAPI/Swagger Types
|
|
406
|
+
// ============================================================================
|
|
407
|
+
|
|
408
|
+
/** OpenAPI specification */
|
|
409
|
+
export interface OpenAPISpec {
|
|
410
|
+
openapi: string;
|
|
411
|
+
info: OpenAPIInfo;
|
|
412
|
+
paths: OpenAPIPaths;
|
|
413
|
+
components?: OpenAPIComponents;
|
|
414
|
+
security?: OpenAPISecurityRequirement[];
|
|
415
|
+
tags?: OpenAPITag[];
|
|
416
|
+
servers?: OpenAPIServer[];
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** OpenAPI info object */
|
|
420
|
+
export interface OpenAPIInfo {
|
|
421
|
+
title: string;
|
|
422
|
+
version: string;
|
|
423
|
+
description?: string;
|
|
424
|
+
contact?: OpenAPIContact;
|
|
425
|
+
license?: OpenAPILicense;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** OpenAPI contact object */
|
|
429
|
+
export interface OpenAPIContact {
|
|
430
|
+
name?: string;
|
|
431
|
+
url?: string;
|
|
432
|
+
email?: string;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** OpenAPI license object */
|
|
436
|
+
export interface OpenAPILicense {
|
|
437
|
+
name: string;
|
|
438
|
+
url?: string;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** OpenAPI paths object */
|
|
442
|
+
export interface OpenAPIPaths {
|
|
443
|
+
[path: string]: OpenAPIPathItem;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** OpenAPI path item */
|
|
447
|
+
export interface OpenAPIPathItem {
|
|
448
|
+
summary?: string;
|
|
449
|
+
description?: string;
|
|
450
|
+
get?: OpenAPIOperation;
|
|
451
|
+
post?: OpenAPIOperation;
|
|
452
|
+
put?: OpenAPIOperation;
|
|
453
|
+
delete?: OpenAPIOperation;
|
|
454
|
+
options?: OpenAPIOperation;
|
|
455
|
+
head?: OpenAPIOperation;
|
|
456
|
+
patch?: OpenAPIOperation;
|
|
457
|
+
parameters?: OpenAPIParameter[];
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** OpenAPI operation */
|
|
461
|
+
export interface OpenAPIOperation {
|
|
462
|
+
tags?: string[];
|
|
463
|
+
summary?: string;
|
|
464
|
+
description?: string;
|
|
465
|
+
operationId?: string;
|
|
466
|
+
parameters?: OpenAPIParameter[];
|
|
467
|
+
requestBody?: OpenAPIRequestBody;
|
|
468
|
+
responses: OpenAPIResponses;
|
|
469
|
+
security?: OpenAPISecurityRequirement[];
|
|
470
|
+
deprecated?: boolean;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/** OpenAPI parameter */
|
|
474
|
+
export interface OpenAPIParameter {
|
|
475
|
+
name: string;
|
|
476
|
+
in: 'query' | 'header' | 'path' | 'cookie';
|
|
477
|
+
description?: string;
|
|
478
|
+
required?: boolean;
|
|
479
|
+
deprecated?: boolean;
|
|
480
|
+
schema?: OpenAPISchema;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** OpenAPI request body */
|
|
484
|
+
export interface OpenAPIRequestBody {
|
|
485
|
+
description?: string;
|
|
486
|
+
content: OpenAPIMediaType;
|
|
487
|
+
required?: boolean;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/** OpenAPI responses */
|
|
491
|
+
export interface OpenAPIResponses {
|
|
492
|
+
[statusCode: string]: OpenAPIResponse;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** OpenAPI response */
|
|
496
|
+
export interface OpenAPIResponse {
|
|
497
|
+
description: string;
|
|
498
|
+
headers?: Record<string, OpenAPIHeader>;
|
|
499
|
+
content?: OpenAPIMediaType;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/** OpenAPI header */
|
|
503
|
+
export interface OpenAPIHeader {
|
|
504
|
+
description?: string;
|
|
505
|
+
schema?: OpenAPISchema;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/** OpenAPI media type */
|
|
509
|
+
export interface OpenAPIMediaType {
|
|
510
|
+
[mediaType: string]: {
|
|
511
|
+
schema?: OpenAPISchema;
|
|
512
|
+
example?: any;
|
|
513
|
+
examples?: Record<string, OpenAPIExample>;
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** OpenAPI example */
|
|
518
|
+
export interface OpenAPIExample {
|
|
519
|
+
summary?: string;
|
|
520
|
+
description?: string;
|
|
521
|
+
value?: any;
|
|
522
|
+
externalValue?: string;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** OpenAPI schema */
|
|
526
|
+
export interface OpenAPISchema {
|
|
527
|
+
type?: string;
|
|
528
|
+
format?: string;
|
|
529
|
+
title?: string;
|
|
530
|
+
description?: string;
|
|
531
|
+
default?: any;
|
|
532
|
+
example?: any;
|
|
533
|
+
enum?: any[];
|
|
534
|
+
const?: any;
|
|
535
|
+
minimum?: number;
|
|
536
|
+
maximum?: number;
|
|
537
|
+
exclusiveMinimum?: number;
|
|
538
|
+
exclusiveMaximum?: number;
|
|
539
|
+
minLength?: number;
|
|
540
|
+
maxLength?: number;
|
|
541
|
+
pattern?: string;
|
|
542
|
+
minItems?: number;
|
|
543
|
+
maxItems?: number;
|
|
544
|
+
uniqueItems?: boolean;
|
|
545
|
+
minProperties?: number;
|
|
546
|
+
maxProperties?: number;
|
|
547
|
+
required?: string[];
|
|
548
|
+
properties?: Record<string, OpenAPISchema>;
|
|
549
|
+
additionalProperties?: boolean | OpenAPISchema;
|
|
550
|
+
items?: OpenAPISchema;
|
|
551
|
+
allOf?: OpenAPISchema[];
|
|
552
|
+
oneOf?: OpenAPISchema[];
|
|
553
|
+
anyOf?: OpenAPISchema[];
|
|
554
|
+
not?: OpenAPISchema;
|
|
555
|
+
nullable?: boolean;
|
|
556
|
+
readOnly?: boolean;
|
|
557
|
+
writeOnly?: boolean;
|
|
558
|
+
deprecated?: boolean;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/** OpenAPI components */
|
|
562
|
+
export interface OpenAPIComponents {
|
|
563
|
+
schemas?: Record<string, OpenAPISchema>;
|
|
564
|
+
responses?: Record<string, OpenAPIResponse>;
|
|
565
|
+
parameters?: Record<string, OpenAPIParameter>;
|
|
566
|
+
requestBodies?: Record<string, OpenAPIRequestBody>;
|
|
567
|
+
headers?: Record<string, OpenAPIHeader>;
|
|
568
|
+
securitySchemes?: Record<string, OpenAPISecurityScheme>;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/** OpenAPI security scheme */
|
|
572
|
+
export interface OpenAPISecurityScheme {
|
|
573
|
+
type: 'apiKey' | 'http' | 'oauth2' | 'openIdConnect';
|
|
574
|
+
description?: string;
|
|
575
|
+
name?: string;
|
|
576
|
+
in?: 'query' | 'header' | 'cookie';
|
|
577
|
+
scheme?: string;
|
|
578
|
+
bearerFormat?: string;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** OpenAPI security requirement */
|
|
582
|
+
export interface OpenAPISecurityRequirement {
|
|
583
|
+
[name: string]: string[];
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** OpenAPI tag */
|
|
587
|
+
export interface OpenAPITag {
|
|
588
|
+
name: string;
|
|
589
|
+
description?: string;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/** OpenAPI server */
|
|
593
|
+
export interface OpenAPIServer {
|
|
594
|
+
url: string;
|
|
595
|
+
description?: string;
|
|
596
|
+
variables?: Record<string, OpenAPIServerVariable>;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/** OpenAPI server variable */
|
|
600
|
+
export interface OpenAPIServerVariable {
|
|
601
|
+
enum?: string[];
|
|
602
|
+
default: string;
|
|
603
|
+
description?: string;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// ============================================================================
|
|
607
|
+
// Main Functions
|
|
608
|
+
// ============================================================================
|
|
609
|
+
|
|
610
|
+
/** Create an object-based router */
|
|
611
|
+
export function createRouter(routes: RouteObject, config?: RouterConfig): ObjectRouter;
|
|
612
|
+
|
|
613
|
+
/** Error handling HOC */
|
|
614
|
+
export function withErrorHandling(options?: ErrorHandlerOptions): (handler: RouteHandler) => RouteHandler;
|
|
615
|
+
|
|
616
|
+
/** Create error handler middleware */
|
|
617
|
+
export function createErrorHandler(options?: ErrorHandlerOptions): ErrorMiddleware;
|
|
618
|
+
|
|
619
|
+
/** Validate against schema */
|
|
620
|
+
export function validateAgainstSchema(
|
|
621
|
+
schema: ValidationSchema,
|
|
622
|
+
data: any,
|
|
623
|
+
options?: ValidationOptions
|
|
624
|
+
): ValidationResult;
|
|
625
|
+
|
|
626
|
+
/** Validate a single field */
|
|
627
|
+
export function validateField(
|
|
628
|
+
rule: ValidationRule,
|
|
629
|
+
value: any,
|
|
630
|
+
field: string,
|
|
631
|
+
data?: any
|
|
632
|
+
): ValidationError | null;
|
|
633
|
+
|
|
634
|
+
/** Validation middleware */
|
|
635
|
+
export function withValidation(schema: ValidationSchema): Middleware;
|
|
636
|
+
|
|
637
|
+
/** Query validation middleware */
|
|
638
|
+
export function withQueryValidation(schema: ValidationSchema): Middleware;
|
|
639
|
+
|
|
640
|
+
/** Params validation middleware */
|
|
641
|
+
export function withParamsValidation(schema: ValidationSchema): Middleware;
|
|
642
|
+
|
|
643
|
+
/** Authentication middleware */
|
|
644
|
+
export function withAuth(config?: AuthConfig): Middleware;
|
|
645
|
+
|
|
646
|
+
/** Role-based authorization middleware */
|
|
647
|
+
export function withRole(roles: string | string[]): Middleware;
|
|
648
|
+
|
|
649
|
+
/** Input validation middleware */
|
|
650
|
+
export function withInputValidation(schema: ValidationSchema): Middleware;
|
|
651
|
+
|
|
652
|
+
/** Hash password */
|
|
653
|
+
export function hashPassword(password: string, saltRounds?: number): Promise<string>;
|
|
654
|
+
|
|
655
|
+
/** Verify password */
|
|
656
|
+
export function verifyPassword(password: string, hash: string): Promise<boolean>;
|
|
657
|
+
|
|
658
|
+
/** Generate JWT token */
|
|
659
|
+
export function generateToken(payload: any, options?: JwtOptions): string;
|
|
660
|
+
|
|
661
|
+
/** Serialization middleware */
|
|
662
|
+
export function withSerialization(config: SerializationConfig): Middleware;
|
|
663
|
+
|
|
664
|
+
/** Serialize for JSON */
|
|
665
|
+
export function serializeForJSON(obj: any, options?: SerializationOptions): any;
|
|
666
|
+
|
|
667
|
+
/** Serialize date */
|
|
668
|
+
export function serializeDate(date: Date): string;
|
|
669
|
+
|
|
670
|
+
/** Deserialize date */
|
|
671
|
+
export function deserializeDate(dateString: string): Date;
|
|
672
|
+
|
|
673
|
+
/** Serialize Map */
|
|
674
|
+
export function serializeMap(map: Map<any, any>): any;
|
|
675
|
+
|
|
676
|
+
/** Deserialize Map */
|
|
677
|
+
export function deserializeMap(obj: any): Map<any, any>;
|
|
678
|
+
|
|
679
|
+
/** Serialize Set */
|
|
680
|
+
export function serializeSet(set: Set<any>): any;
|
|
681
|
+
|
|
682
|
+
/** Deserialize Set */
|
|
683
|
+
export function deserializeSet(arr: any[]): Set<any>;
|
|
684
|
+
|
|
685
|
+
// ============================================================================
|
|
686
|
+
// Default Export
|
|
687
|
+
// ============================================================================
|
|
688
|
+
|
|
689
|
+
declare const coherentApi: {
|
|
690
|
+
createRouter: typeof createRouter;
|
|
691
|
+
ApiError: typeof ApiError;
|
|
692
|
+
ValidationError: typeof ValidationError;
|
|
693
|
+
AuthenticationError: typeof AuthenticationError;
|
|
694
|
+
AuthorizationError: typeof AuthorizationError;
|
|
695
|
+
NotFoundError: typeof NotFoundError;
|
|
696
|
+
ConflictError: typeof ConflictError;
|
|
697
|
+
withErrorHandling: typeof withErrorHandling;
|
|
698
|
+
createErrorHandler: typeof createErrorHandler;
|
|
699
|
+
validateAgainstSchema: typeof validateAgainstSchema;
|
|
700
|
+
validateField: typeof validateField;
|
|
701
|
+
withValidation: typeof withValidation;
|
|
702
|
+
withQueryValidation: typeof withQueryValidation;
|
|
703
|
+
withParamsValidation: typeof withParamsValidation;
|
|
704
|
+
serializeDate: typeof serializeDate;
|
|
705
|
+
deserializeDate: typeof deserializeDate;
|
|
706
|
+
serializeMap: typeof serializeMap;
|
|
707
|
+
deserializeMap: typeof deserializeMap;
|
|
708
|
+
serializeSet: typeof serializeSet;
|
|
709
|
+
deserializeSet: typeof deserializeSet;
|
|
710
|
+
withSerialization: typeof withSerialization;
|
|
711
|
+
serializeForJSON: typeof serializeForJSON;
|
|
712
|
+
withAuth: typeof withAuth;
|
|
713
|
+
withRole: typeof withRole;
|
|
714
|
+
hashPassword: typeof hashPassword;
|
|
715
|
+
verifyPassword: typeof verifyPassword;
|
|
716
|
+
generateToken: typeof generateToken;
|
|
717
|
+
withInputValidation: typeof withInputValidation;
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
export default coherentApi;
|