@veloxts/router 0.6.56 → 0.6.58
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 +18 -0
- package/GUIDE.md +237 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +36 -0
- package/dist/openapi/generator.d.ts +52 -0
- package/dist/openapi/generator.js +442 -0
- package/dist/openapi/index.d.ts +37 -0
- package/dist/openapi/index.js +51 -0
- package/dist/openapi/path-extractor.d.ts +212 -0
- package/dist/openapi/path-extractor.js +256 -0
- package/dist/openapi/plugin.d.ts +101 -0
- package/dist/openapi/plugin.js +228 -0
- package/dist/openapi/schema-converter.d.ts +119 -0
- package/dist/openapi/schema-converter.js +246 -0
- package/dist/openapi/security-mapper.d.ts +131 -0
- package/dist/openapi/security-mapper.js +226 -0
- package/dist/openapi/types.d.ts +437 -0
- package/dist/openapi/types.js +8 -0
- package/dist/providers.d.ts +90 -0
- package/dist/providers.js +145 -0
- package/dist/tokens.d.ts +107 -0
- package/dist/tokens.js +78 -0
- package/package.json +8 -3
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI 3.0.3 Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Type-safe definitions for generating OpenAPI specifications from VeloxTS procedures.
|
|
5
|
+
*
|
|
6
|
+
* @module @veloxts/router/openapi/types
|
|
7
|
+
*/
|
|
8
|
+
import type { ProcedureCollection } from '../types.js';
|
|
9
|
+
/**
|
|
10
|
+
* JSON Schema representation for OpenAPI
|
|
11
|
+
*/
|
|
12
|
+
export type JSONSchema = {
|
|
13
|
+
type?: string | string[];
|
|
14
|
+
format?: string;
|
|
15
|
+
title?: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
default?: unknown;
|
|
18
|
+
enum?: unknown[];
|
|
19
|
+
const?: unknown;
|
|
20
|
+
nullable?: boolean;
|
|
21
|
+
properties?: Record<string, JSONSchema>;
|
|
22
|
+
required?: string[];
|
|
23
|
+
additionalProperties?: boolean | JSONSchema;
|
|
24
|
+
patternProperties?: Record<string, JSONSchema>;
|
|
25
|
+
minProperties?: number;
|
|
26
|
+
maxProperties?: number;
|
|
27
|
+
items?: JSONSchema | JSONSchema[];
|
|
28
|
+
minItems?: number;
|
|
29
|
+
maxItems?: number;
|
|
30
|
+
uniqueItems?: boolean;
|
|
31
|
+
minLength?: number;
|
|
32
|
+
maxLength?: number;
|
|
33
|
+
pattern?: string;
|
|
34
|
+
minimum?: number;
|
|
35
|
+
maximum?: number;
|
|
36
|
+
exclusiveMinimum?: number;
|
|
37
|
+
exclusiveMaximum?: number;
|
|
38
|
+
multipleOf?: number;
|
|
39
|
+
allOf?: JSONSchema[];
|
|
40
|
+
anyOf?: JSONSchema[];
|
|
41
|
+
oneOf?: JSONSchema[];
|
|
42
|
+
not?: JSONSchema;
|
|
43
|
+
$ref?: string;
|
|
44
|
+
example?: unknown;
|
|
45
|
+
deprecated?: boolean;
|
|
46
|
+
readOnly?: boolean;
|
|
47
|
+
writeOnly?: boolean;
|
|
48
|
+
[key: string]: unknown;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Contact information for the API
|
|
52
|
+
*/
|
|
53
|
+
export interface OpenAPIContact {
|
|
54
|
+
name?: string;
|
|
55
|
+
url?: string;
|
|
56
|
+
email?: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* License information for the API
|
|
60
|
+
*/
|
|
61
|
+
export interface OpenAPILicense {
|
|
62
|
+
name: string;
|
|
63
|
+
url?: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* API metadata
|
|
67
|
+
*/
|
|
68
|
+
export interface OpenAPIInfo {
|
|
69
|
+
title: string;
|
|
70
|
+
version: string;
|
|
71
|
+
description?: string;
|
|
72
|
+
termsOfService?: string;
|
|
73
|
+
contact?: OpenAPIContact;
|
|
74
|
+
license?: OpenAPILicense;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Server information
|
|
78
|
+
*/
|
|
79
|
+
export interface OpenAPIServer {
|
|
80
|
+
url: string;
|
|
81
|
+
description?: string;
|
|
82
|
+
variables?: Record<string, {
|
|
83
|
+
default: string;
|
|
84
|
+
enum?: string[];
|
|
85
|
+
description?: string;
|
|
86
|
+
}>;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Parameter location
|
|
90
|
+
*/
|
|
91
|
+
export type ParameterIn = 'path' | 'query' | 'header' | 'cookie';
|
|
92
|
+
/**
|
|
93
|
+
* Parameter definition
|
|
94
|
+
*/
|
|
95
|
+
export interface OpenAPIParameter {
|
|
96
|
+
name: string;
|
|
97
|
+
in: ParameterIn;
|
|
98
|
+
description?: string;
|
|
99
|
+
required?: boolean;
|
|
100
|
+
deprecated?: boolean;
|
|
101
|
+
allowEmptyValue?: boolean;
|
|
102
|
+
schema: JSONSchema;
|
|
103
|
+
example?: unknown;
|
|
104
|
+
examples?: Record<string, OpenAPIExample>;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Example object
|
|
108
|
+
*/
|
|
109
|
+
export interface OpenAPIExample {
|
|
110
|
+
summary?: string;
|
|
111
|
+
description?: string;
|
|
112
|
+
value?: unknown;
|
|
113
|
+
externalValue?: string;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Media type object
|
|
117
|
+
*/
|
|
118
|
+
export interface OpenAPIMediaType {
|
|
119
|
+
schema?: JSONSchema;
|
|
120
|
+
example?: unknown;
|
|
121
|
+
examples?: Record<string, OpenAPIExample>;
|
|
122
|
+
encoding?: Record<string, OpenAPIEncoding>;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Encoding object for multipart requests
|
|
126
|
+
*/
|
|
127
|
+
export interface OpenAPIEncoding {
|
|
128
|
+
contentType?: string;
|
|
129
|
+
headers?: Record<string, OpenAPIHeader>;
|
|
130
|
+
style?: string;
|
|
131
|
+
explode?: boolean;
|
|
132
|
+
allowReserved?: boolean;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Header object
|
|
136
|
+
*/
|
|
137
|
+
export interface OpenAPIHeader {
|
|
138
|
+
description?: string;
|
|
139
|
+
required?: boolean;
|
|
140
|
+
deprecated?: boolean;
|
|
141
|
+
schema?: JSONSchema;
|
|
142
|
+
example?: unknown;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Request body definition
|
|
146
|
+
*/
|
|
147
|
+
export interface OpenAPIRequestBody {
|
|
148
|
+
description?: string;
|
|
149
|
+
required?: boolean;
|
|
150
|
+
content: Record<string, OpenAPIMediaType>;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Response definition
|
|
154
|
+
*/
|
|
155
|
+
export interface OpenAPIResponse {
|
|
156
|
+
description: string;
|
|
157
|
+
headers?: Record<string, OpenAPIHeader>;
|
|
158
|
+
content?: Record<string, OpenAPIMediaType>;
|
|
159
|
+
links?: Record<string, OpenAPILink>;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Link object for response linking
|
|
163
|
+
*/
|
|
164
|
+
export interface OpenAPILink {
|
|
165
|
+
operationRef?: string;
|
|
166
|
+
operationId?: string;
|
|
167
|
+
parameters?: Record<string, unknown>;
|
|
168
|
+
requestBody?: unknown;
|
|
169
|
+
description?: string;
|
|
170
|
+
server?: OpenAPIServer;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Security scheme types
|
|
174
|
+
*/
|
|
175
|
+
export type SecuritySchemeType = 'apiKey' | 'http' | 'oauth2' | 'openIdConnect';
|
|
176
|
+
/**
|
|
177
|
+
* OAuth2 flow object
|
|
178
|
+
*/
|
|
179
|
+
export interface OpenAPIOAuthFlow {
|
|
180
|
+
authorizationUrl?: string;
|
|
181
|
+
tokenUrl?: string;
|
|
182
|
+
refreshUrl?: string;
|
|
183
|
+
scopes: Record<string, string>;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* OAuth2 flows object
|
|
187
|
+
*/
|
|
188
|
+
export interface OpenAPIOAuthFlows {
|
|
189
|
+
implicit?: OpenAPIOAuthFlow;
|
|
190
|
+
password?: OpenAPIOAuthFlow;
|
|
191
|
+
clientCredentials?: OpenAPIOAuthFlow;
|
|
192
|
+
authorizationCode?: OpenAPIOAuthFlow;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Security scheme definition
|
|
196
|
+
*/
|
|
197
|
+
export interface OpenAPISecurityScheme {
|
|
198
|
+
type: SecuritySchemeType;
|
|
199
|
+
description?: string;
|
|
200
|
+
name?: string;
|
|
201
|
+
in?: 'query' | 'header' | 'cookie';
|
|
202
|
+
scheme?: string;
|
|
203
|
+
bearerFormat?: string;
|
|
204
|
+
flows?: OpenAPIOAuthFlows;
|
|
205
|
+
openIdConnectUrl?: string;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Security requirement object
|
|
209
|
+
*/
|
|
210
|
+
export type OpenAPISecurityRequirement = Record<string, string[]>;
|
|
211
|
+
/**
|
|
212
|
+
* External documentation object
|
|
213
|
+
*/
|
|
214
|
+
export interface OpenAPIExternalDocs {
|
|
215
|
+
url: string;
|
|
216
|
+
description?: string;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Operation object (endpoint definition)
|
|
220
|
+
*/
|
|
221
|
+
export interface OpenAPIOperation {
|
|
222
|
+
operationId?: string;
|
|
223
|
+
summary?: string;
|
|
224
|
+
description?: string;
|
|
225
|
+
tags?: string[];
|
|
226
|
+
externalDocs?: OpenAPIExternalDocs;
|
|
227
|
+
parameters?: OpenAPIParameter[];
|
|
228
|
+
requestBody?: OpenAPIRequestBody;
|
|
229
|
+
responses: Record<string, OpenAPIResponse>;
|
|
230
|
+
callbacks?: Record<string, Record<string, OpenAPIPathItem>>;
|
|
231
|
+
deprecated?: boolean;
|
|
232
|
+
security?: OpenAPISecurityRequirement[];
|
|
233
|
+
servers?: OpenAPIServer[];
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Path item object (all operations for a path)
|
|
237
|
+
*/
|
|
238
|
+
export interface OpenAPIPathItem {
|
|
239
|
+
$ref?: string;
|
|
240
|
+
summary?: string;
|
|
241
|
+
description?: string;
|
|
242
|
+
get?: OpenAPIOperation;
|
|
243
|
+
put?: OpenAPIOperation;
|
|
244
|
+
post?: OpenAPIOperation;
|
|
245
|
+
delete?: OpenAPIOperation;
|
|
246
|
+
options?: OpenAPIOperation;
|
|
247
|
+
head?: OpenAPIOperation;
|
|
248
|
+
patch?: OpenAPIOperation;
|
|
249
|
+
trace?: OpenAPIOperation;
|
|
250
|
+
servers?: OpenAPIServer[];
|
|
251
|
+
parameters?: OpenAPIParameter[];
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Components object for reusable definitions
|
|
255
|
+
*/
|
|
256
|
+
export interface OpenAPIComponents {
|
|
257
|
+
schemas?: Record<string, JSONSchema>;
|
|
258
|
+
responses?: Record<string, OpenAPIResponse>;
|
|
259
|
+
parameters?: Record<string, OpenAPIParameter>;
|
|
260
|
+
examples?: Record<string, OpenAPIExample>;
|
|
261
|
+
requestBodies?: Record<string, OpenAPIRequestBody>;
|
|
262
|
+
headers?: Record<string, OpenAPIHeader>;
|
|
263
|
+
securitySchemes?: Record<string, OpenAPISecurityScheme>;
|
|
264
|
+
links?: Record<string, OpenAPILink>;
|
|
265
|
+
callbacks?: Record<string, Record<string, OpenAPIPathItem>>;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Tag object for grouping operations
|
|
269
|
+
*/
|
|
270
|
+
export interface OpenAPITag {
|
|
271
|
+
name: string;
|
|
272
|
+
description?: string;
|
|
273
|
+
externalDocs?: OpenAPIExternalDocs;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Complete OpenAPI 3.0.3 specification
|
|
277
|
+
*/
|
|
278
|
+
export interface OpenAPISpec {
|
|
279
|
+
openapi: '3.0.3';
|
|
280
|
+
info: OpenAPIInfo;
|
|
281
|
+
servers?: OpenAPIServer[];
|
|
282
|
+
paths: Record<string, OpenAPIPathItem>;
|
|
283
|
+
components?: OpenAPIComponents;
|
|
284
|
+
security?: OpenAPISecurityRequirement[];
|
|
285
|
+
tags?: OpenAPITag[];
|
|
286
|
+
externalDocs?: OpenAPIExternalDocs;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Options for OpenAPI specification generation
|
|
290
|
+
*/
|
|
291
|
+
export interface OpenAPIGeneratorOptions {
|
|
292
|
+
/**
|
|
293
|
+
* API info metadata (required)
|
|
294
|
+
*/
|
|
295
|
+
info: OpenAPIInfo;
|
|
296
|
+
/**
|
|
297
|
+
* Server URLs for the API
|
|
298
|
+
*/
|
|
299
|
+
servers?: OpenAPIServer[];
|
|
300
|
+
/**
|
|
301
|
+
* API prefix prepended to all paths
|
|
302
|
+
* @default '/api'
|
|
303
|
+
*/
|
|
304
|
+
prefix?: string;
|
|
305
|
+
/**
|
|
306
|
+
* Security scheme definitions
|
|
307
|
+
* Merged with default schemes (bearerAuth)
|
|
308
|
+
*/
|
|
309
|
+
securitySchemes?: Record<string, OpenAPISecurityScheme>;
|
|
310
|
+
/**
|
|
311
|
+
* Default security for all endpoints
|
|
312
|
+
* Applied when no guards are present
|
|
313
|
+
*/
|
|
314
|
+
defaultSecurity?: OpenAPISecurityRequirement[];
|
|
315
|
+
/**
|
|
316
|
+
* Custom guard name to security scheme mapping
|
|
317
|
+
* @example { 'authenticated': 'bearerAuth', 'apiKey': 'apiKeyAuth' }
|
|
318
|
+
*/
|
|
319
|
+
guardToSecurityMap?: Record<string, string>;
|
|
320
|
+
/**
|
|
321
|
+
* Tag descriptions for namespaces
|
|
322
|
+
* @example { 'users': 'User management endpoints' }
|
|
323
|
+
*/
|
|
324
|
+
tagDescriptions?: Record<string, string>;
|
|
325
|
+
/**
|
|
326
|
+
* External documentation link
|
|
327
|
+
*/
|
|
328
|
+
externalDocs?: OpenAPIExternalDocs;
|
|
329
|
+
/**
|
|
330
|
+
* Additional OpenAPI extensions (x-* properties)
|
|
331
|
+
*/
|
|
332
|
+
extensions?: Record<string, unknown>;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Swagger UI configuration options
|
|
336
|
+
*/
|
|
337
|
+
export interface SwaggerUIConfig {
|
|
338
|
+
/**
|
|
339
|
+
* Enable deep linking for tags and operations
|
|
340
|
+
* @default true
|
|
341
|
+
*/
|
|
342
|
+
deepLinking?: boolean;
|
|
343
|
+
/**
|
|
344
|
+
* Display operationId in the UI
|
|
345
|
+
* @default false
|
|
346
|
+
*/
|
|
347
|
+
displayOperationId?: boolean;
|
|
348
|
+
/**
|
|
349
|
+
* Default expand depth for models section
|
|
350
|
+
* @default 1
|
|
351
|
+
*/
|
|
352
|
+
defaultModelsExpandDepth?: number;
|
|
353
|
+
/**
|
|
354
|
+
* Default expand depth for model properties
|
|
355
|
+
* @default 1
|
|
356
|
+
*/
|
|
357
|
+
defaultModelExpandDepth?: number;
|
|
358
|
+
/**
|
|
359
|
+
* Controls expansion of operations and tags
|
|
360
|
+
* @default 'list'
|
|
361
|
+
*/
|
|
362
|
+
docExpansion?: 'list' | 'full' | 'none';
|
|
363
|
+
/**
|
|
364
|
+
* Enable filter/search box
|
|
365
|
+
* @default false
|
|
366
|
+
*/
|
|
367
|
+
filter?: boolean | string;
|
|
368
|
+
/**
|
|
369
|
+
* Show extensions (x-* fields)
|
|
370
|
+
* @default false
|
|
371
|
+
*/
|
|
372
|
+
showExtensions?: boolean;
|
|
373
|
+
/**
|
|
374
|
+
* Enable "Try it out" by default
|
|
375
|
+
* @default true
|
|
376
|
+
*/
|
|
377
|
+
tryItOutEnabled?: boolean;
|
|
378
|
+
/**
|
|
379
|
+
* Persist authorization data across browser sessions
|
|
380
|
+
* @default false
|
|
381
|
+
*/
|
|
382
|
+
persistAuthorization?: boolean;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Options for the Swagger UI Fastify plugin
|
|
386
|
+
*/
|
|
387
|
+
export interface SwaggerUIPluginOptions {
|
|
388
|
+
/**
|
|
389
|
+
* Route prefix for Swagger UI
|
|
390
|
+
* @default '/docs'
|
|
391
|
+
*/
|
|
392
|
+
routePrefix?: string;
|
|
393
|
+
/**
|
|
394
|
+
* Route for raw OpenAPI JSON
|
|
395
|
+
* @default '/docs/openapi.json'
|
|
396
|
+
*/
|
|
397
|
+
specRoute?: string;
|
|
398
|
+
/**
|
|
399
|
+
* Swagger UI configuration
|
|
400
|
+
*/
|
|
401
|
+
uiConfig?: SwaggerUIConfig;
|
|
402
|
+
/**
|
|
403
|
+
* OpenAPI generator options
|
|
404
|
+
*/
|
|
405
|
+
openapi: OpenAPIGeneratorOptions;
|
|
406
|
+
/**
|
|
407
|
+
* Procedure collections to document
|
|
408
|
+
*/
|
|
409
|
+
collections: ProcedureCollection[];
|
|
410
|
+
/**
|
|
411
|
+
* Custom page title
|
|
412
|
+
* @default 'API Documentation'
|
|
413
|
+
*/
|
|
414
|
+
title?: string;
|
|
415
|
+
/**
|
|
416
|
+
* Custom favicon URL
|
|
417
|
+
*/
|
|
418
|
+
favicon?: string;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* HTTP methods supported by OpenAPI
|
|
422
|
+
*/
|
|
423
|
+
export type OpenAPIHttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'options' | 'head';
|
|
424
|
+
/**
|
|
425
|
+
* Route information for generation
|
|
426
|
+
*/
|
|
427
|
+
export interface RouteInfo {
|
|
428
|
+
method: OpenAPIHttpMethod;
|
|
429
|
+
path: string;
|
|
430
|
+
operationId: string;
|
|
431
|
+
namespace: string;
|
|
432
|
+
procedureName: string;
|
|
433
|
+
inputSchema?: JSONSchema;
|
|
434
|
+
outputSchema?: JSONSchema;
|
|
435
|
+
guards: string[];
|
|
436
|
+
deprecated?: boolean;
|
|
437
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DI Providers for @veloxts/router
|
|
3
|
+
*
|
|
4
|
+
* Factory provider functions for registering router services with the DI container.
|
|
5
|
+
* These providers allow services to be managed by the container for testability and flexibility.
|
|
6
|
+
*
|
|
7
|
+
* @module router/providers
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Container } from '@veloxts/core';
|
|
12
|
+
* import { registerRouterProviders, TRPC_INSTANCE, APP_ROUTER } from '@veloxts/router';
|
|
13
|
+
*
|
|
14
|
+
* const container = new Container();
|
|
15
|
+
* registerRouterProviders(container, {
|
|
16
|
+
* procedures: [userProcedures, postProcedures],
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* const t = container.resolve(TRPC_INSTANCE);
|
|
20
|
+
* const router = container.resolve(APP_ROUTER);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
import { type Container, type FactoryProvider } from '@veloxts/core';
|
|
24
|
+
import type { RouterConfig } from './tokens.js';
|
|
25
|
+
import type { AnyRouter, TRPCInstance, TRPCPluginOptions } from './trpc/index.js';
|
|
26
|
+
/**
|
|
27
|
+
* Creates a factory provider for the tRPC instance
|
|
28
|
+
*
|
|
29
|
+
* The tRPC instance is the foundation for building type-safe routers.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* container.register(trpcInstanceProvider());
|
|
34
|
+
* const t = container.resolve(TRPC_INSTANCE);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function trpcInstanceProvider(): FactoryProvider<TRPCInstance>;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a factory provider for the app router
|
|
40
|
+
*
|
|
41
|
+
* Requires TRPC_INSTANCE and PROCEDURE_COLLECTIONS to be registered.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* container.register({ provide: PROCEDURE_COLLECTIONS, useValue: [userProcedures] });
|
|
46
|
+
* container.register(trpcInstanceProvider());
|
|
47
|
+
* container.register(appRouterProvider());
|
|
48
|
+
*
|
|
49
|
+
* const router = container.resolve(APP_ROUTER);
|
|
50
|
+
* export type AppRouter = typeof router;
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function appRouterProvider(): FactoryProvider<AnyRouter>;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a factory provider for tRPC plugin options
|
|
56
|
+
*
|
|
57
|
+
* Requires APP_ROUTER and ROUTER_CONFIG to be registered.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const options = container.resolve(TRPC_PLUGIN_OPTIONS);
|
|
62
|
+
* await registerTRPCPlugin(server, options);
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function trpcPluginOptionsProvider(): FactoryProvider<TRPCPluginOptions>;
|
|
66
|
+
/**
|
|
67
|
+
* Registers core router providers with a container
|
|
68
|
+
*
|
|
69
|
+
* This registers the tRPC instance and app router for DI-enabled routing.
|
|
70
|
+
*
|
|
71
|
+
* @param container - The DI container to register providers with
|
|
72
|
+
* @param config - Router configuration with procedure collections
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* import { Container } from '@veloxts/core';
|
|
77
|
+
* import { registerRouterProviders, TRPC_INSTANCE, APP_ROUTER } from '@veloxts/router';
|
|
78
|
+
*
|
|
79
|
+
* const container = new Container();
|
|
80
|
+
* registerRouterProviders(container, {
|
|
81
|
+
* procedures: [userProcedures, postProcedures],
|
|
82
|
+
* apiPrefix: '/api',
|
|
83
|
+
* rpcPrefix: '/trpc',
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* const t = container.resolve(TRPC_INSTANCE);
|
|
87
|
+
* const router = container.resolve(APP_ROUTER);
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function registerRouterProviders(container: Container, config?: RouterConfig): void;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DI Providers for @veloxts/router
|
|
3
|
+
*
|
|
4
|
+
* Factory provider functions for registering router services with the DI container.
|
|
5
|
+
* These providers allow services to be managed by the container for testability and flexibility.
|
|
6
|
+
*
|
|
7
|
+
* @module router/providers
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Container } from '@veloxts/core';
|
|
12
|
+
* import { registerRouterProviders, TRPC_INSTANCE, APP_ROUTER } from '@veloxts/router';
|
|
13
|
+
*
|
|
14
|
+
* const container = new Container();
|
|
15
|
+
* registerRouterProviders(container, {
|
|
16
|
+
* procedures: [userProcedures, postProcedures],
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* const t = container.resolve(TRPC_INSTANCE);
|
|
20
|
+
* const router = container.resolve(APP_ROUTER);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
import { Scope } from '@veloxts/core';
|
|
24
|
+
import { APP_ROUTER, PROCEDURE_COLLECTIONS, REST_ADAPTER_CONFIG, ROUTER_CONFIG, TRPC_INSTANCE, TRPC_PLUGIN_OPTIONS, } from './tokens.js';
|
|
25
|
+
import { appRouter, trpc } from './trpc/index.js';
|
|
26
|
+
// ============================================================================
|
|
27
|
+
// Core Router Providers
|
|
28
|
+
// ============================================================================
|
|
29
|
+
/**
|
|
30
|
+
* Creates a factory provider for the tRPC instance
|
|
31
|
+
*
|
|
32
|
+
* The tRPC instance is the foundation for building type-safe routers.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* container.register(trpcInstanceProvider());
|
|
37
|
+
* const t = container.resolve(TRPC_INSTANCE);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function trpcInstanceProvider() {
|
|
41
|
+
return {
|
|
42
|
+
provide: TRPC_INSTANCE,
|
|
43
|
+
useFactory: () => trpc(),
|
|
44
|
+
inject: [],
|
|
45
|
+
scope: Scope.SINGLETON,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Creates a factory provider for the app router
|
|
50
|
+
*
|
|
51
|
+
* Requires TRPC_INSTANCE and PROCEDURE_COLLECTIONS to be registered.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* container.register({ provide: PROCEDURE_COLLECTIONS, useValue: [userProcedures] });
|
|
56
|
+
* container.register(trpcInstanceProvider());
|
|
57
|
+
* container.register(appRouterProvider());
|
|
58
|
+
*
|
|
59
|
+
* const router = container.resolve(APP_ROUTER);
|
|
60
|
+
* export type AppRouter = typeof router;
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export function appRouterProvider() {
|
|
64
|
+
return {
|
|
65
|
+
provide: APP_ROUTER,
|
|
66
|
+
useFactory: (t, collections) => appRouter(t, collections),
|
|
67
|
+
inject: [TRPC_INSTANCE, PROCEDURE_COLLECTIONS],
|
|
68
|
+
scope: Scope.SINGLETON,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Creates a factory provider for tRPC plugin options
|
|
73
|
+
*
|
|
74
|
+
* Requires APP_ROUTER and ROUTER_CONFIG to be registered.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const options = container.resolve(TRPC_PLUGIN_OPTIONS);
|
|
79
|
+
* await registerTRPCPlugin(server, options);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export function trpcPluginOptionsProvider() {
|
|
83
|
+
return {
|
|
84
|
+
provide: TRPC_PLUGIN_OPTIONS,
|
|
85
|
+
useFactory: (router, config) => ({
|
|
86
|
+
prefix: config.rpcPrefix ?? '/trpc',
|
|
87
|
+
router,
|
|
88
|
+
}),
|
|
89
|
+
inject: [APP_ROUTER, ROUTER_CONFIG],
|
|
90
|
+
scope: Scope.SINGLETON,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
// ============================================================================
|
|
94
|
+
// Bulk Registration Helpers
|
|
95
|
+
// ============================================================================
|
|
96
|
+
/**
|
|
97
|
+
* Registers core router providers with a container
|
|
98
|
+
*
|
|
99
|
+
* This registers the tRPC instance and app router for DI-enabled routing.
|
|
100
|
+
*
|
|
101
|
+
* @param container - The DI container to register providers with
|
|
102
|
+
* @param config - Router configuration with procedure collections
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* import { Container } from '@veloxts/core';
|
|
107
|
+
* import { registerRouterProviders, TRPC_INSTANCE, APP_ROUTER } from '@veloxts/router';
|
|
108
|
+
*
|
|
109
|
+
* const container = new Container();
|
|
110
|
+
* registerRouterProviders(container, {
|
|
111
|
+
* procedures: [userProcedures, postProcedures],
|
|
112
|
+
* apiPrefix: '/api',
|
|
113
|
+
* rpcPrefix: '/trpc',
|
|
114
|
+
* });
|
|
115
|
+
*
|
|
116
|
+
* const t = container.resolve(TRPC_INSTANCE);
|
|
117
|
+
* const router = container.resolve(APP_ROUTER);
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export function registerRouterProviders(container, config = {}) {
|
|
121
|
+
// Register config
|
|
122
|
+
container.register({
|
|
123
|
+
provide: ROUTER_CONFIG,
|
|
124
|
+
useValue: config,
|
|
125
|
+
});
|
|
126
|
+
// Register procedure collections
|
|
127
|
+
container.register({
|
|
128
|
+
provide: PROCEDURE_COLLECTIONS,
|
|
129
|
+
useValue: config.procedures ?? [],
|
|
130
|
+
});
|
|
131
|
+
// Register REST adapter config
|
|
132
|
+
container.register({
|
|
133
|
+
provide: REST_ADAPTER_CONFIG,
|
|
134
|
+
useValue: {
|
|
135
|
+
prefix: config.apiPrefix ?? '/api',
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
// Register tRPC instance provider
|
|
139
|
+
container.register(trpcInstanceProvider());
|
|
140
|
+
// Only register app router if procedures are provided
|
|
141
|
+
if (config.procedures && config.procedures.length > 0) {
|
|
142
|
+
container.register(appRouterProvider());
|
|
143
|
+
container.register(trpcPluginOptionsProvider());
|
|
144
|
+
}
|
|
145
|
+
}
|