-
Notifications
You must be signed in to change notification settings - Fork 1
/
service-factory.ts
277 lines (240 loc) · 11.2 KB
/
service-factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import type { Container } from '@chubbyts/chubbyts-dic-types/dist/container';
import type { Middleware } from '@chubbyts/chubbyts-http-types/dist/middleware';
import { createLazyMiddleware } from '@chubbyts/chubbyts-framework/dist/middleware/lazy-middleware';
import { createErrorMiddleware } from '@chubbyts/chubbyts-framework/dist/middleware/error-middleware';
import { createRouteMatcherMiddleware } from '@chubbyts/chubbyts-framework/dist/middleware/route-matcher-middleware';
import type {
RequestFactory,
ResponseFactory,
ServerRequestFactory,
StreamFactory,
UriFactory,
} from '@chubbyts/chubbyts-http-types/dist/message-factory';
import type { Logger } from '@chubbyts/chubbyts-log-types/dist/log';
import { createLogger } from '@chubbyts/chubbyts-log-types/dist/log';
import type { Match } from '@chubbyts/chubbyts-framework/dist/router/route-matcher';
import type { GeneratePath } from '@chubbyts/chubbyts-framework/dist/router/url-generator';
import {
createRequestFactory,
createResponseFactory,
createServerRequestFactory,
createStreamFactory,
createStreamFromResourceFactory,
createUriFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory';
import { createPinoAdapter } from '@chubbyts/chubbyts-pino-adapter/dist/pino-adapter';
import {
createPathToRegexpRouteMatcher,
createPathToRegexpPathGenerator,
} from '@chubbyts/chubbyts-framework-router-path-to-regexp/dist/path-to-regexp-router';
import { pino } from 'pino';
import type { RoutesByName } from '@chubbyts/chubbyts-framework/dist/router/routes-by-name';
import { createRoutesByName } from '@chubbyts/chubbyts-framework/dist/router/routes-by-name';
import { createLazyHandler } from '@chubbyts/chubbyts-framework/dist/handler/lazy-handler';
import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route';
import { createGetRoute } from '@chubbyts/chubbyts-framework/dist/router/route';
import { MongoClient } from 'mongodb';
import { createAcceptNegotiationMiddleware } from '@chubbyts/chubbyts-api/dist/middleware/accept-negotiation-middleware';
import { createContentTypeNegotiationMiddleware } from '@chubbyts/chubbyts-api/dist/middleware/content-type-negotiation-middleware';
import { createErrorMiddleware as createApiErrorMiddleware } from '@chubbyts/chubbyts-api/dist/middleware/error-middleware';
import { createAcceptNegotiator } from '@chubbyts/chubbyts-negotiation/dist/accept-negotiator';
import { createContentTypeNegotiator } from '@chubbyts/chubbyts-negotiation/dist/content-type-negotiator';
import type { Negotiator } from '@chubbyts/chubbyts-negotiation/dist/negotiation';
import type { Decoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder';
import { createDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder';
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';
import { createJsonxTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/jsonx-type-decoder';
import { createUrlEncodedTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/url-encoded-type-decoder';
import { createYamlTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/yaml-type-decoder';
import type { Encoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder';
import { createEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder';
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder';
import { createJsonxTypeEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/jsonx-type-encoder';
import { createUrlEncodedTypeEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/url-encoded-type-encoder';
import { createYamlTypeEncoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/yaml-type-encoder';
import { upsertIndexes } from '@chubbyts/chubbyts-mongodb/dist/mongo';
import { createCorsMiddleware } from '@chubbyts/chubbyts-http-cors/dist/middleware';
import {
createAllowOriginExact,
createAllowOriginRegex,
createHeadersNegotiator,
createMethodNegotiator,
createOriginNegotiator,
} from '@chubbyts/chubbyts-http-cors/dist/negotiation';
import { extendZodWithOpenApi, OpenApiGeneratorV3, OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import { z } from 'zod';
import type { OpenAPIComponentObject } from '@asteasolutions/zod-to-openapi/dist/openapi-registry.ts';
import type { Config } from '../config/production.js';
import { createCleanDirectoriesCommand } from './command.js';
import type { CleanDirectoriesCommand } from './command.js';
import { createOpenApiHandler, createPingHandler } from './handler.js';
extendZodWithOpenApi(z);
export const acceptNegotiationMiddlewareServiceFactory = (container: Container) => {
return createAcceptNegotiationMiddleware(container.get<Negotiator>('acceptNegotiator'));
};
export const acceptNegotiatorServiceFactory = (container: Container) => {
return createAcceptNegotiator(container.get<Encoder>('encoder').contentTypes);
};
export const apiErrorMiddlewareServiceFactory = (container: Container) => {
return createApiErrorMiddleware(
container.get<ResponseFactory>('responseFactory'),
container.get<Encoder>('encoder'),
undefined,
container.get<Config>('config').debug,
container.get<Logger>('logger'),
);
};
export const cleanDirectoriesCommandServiceFactory = (container: Container): CleanDirectoriesCommand => {
return createCleanDirectoriesCommand(container.get<Config>('config').directories, container.get<Logger>('logger'));
};
export const contentTypeNegotiationMiddlewareServiceFactory = (container: Container): Middleware => {
return createContentTypeNegotiationMiddleware(container.get<Negotiator>('contentTypeNegotiator'));
};
export const contentTypeNegotiatorServiceFactory = (container: Container): Negotiator => {
return createContentTypeNegotiator(container.get<Decoder>('decoder').contentTypes);
};
export const corsMiddlewareServiceFactory = (container: Container) => {
const cors = container.get<Config>('config').cors;
return createCorsMiddleware(
container.get<ResponseFactory>('responseFactory'),
createOriginNegotiator([
...(cors.allowOrigins.createAllowOriginExact
? cors.allowOrigins.createAllowOriginExact.map(createAllowOriginExact)
: []),
...(cors.allowOrigins.createAllowOriginRegex
? cors.allowOrigins.createAllowOriginRegex.map(createAllowOriginRegex)
: []),
]),
createMethodNegotiator(cors.allowMethods),
createHeadersNegotiator(cors.allowHeaders),
cors.exposeHeaders,
cors.allowCredentials,
cors.maxAge,
);
};
export const decoderServiceFactory = (): Decoder => {
return createDecoder([
createJsonTypeDecoder(),
createJsonxTypeDecoder(),
createUrlEncodedTypeDecoder(),
createYamlTypeDecoder(),
]);
};
export const encoderServiceFactory = (): Encoder => {
return createEncoder([
createJsonTypeEncoder(),
createJsonxTypeEncoder(),
createUrlEncodedTypeEncoder(),
createYamlTypeEncoder(),
]);
};
export const errorMiddlewareServiceFactory = (container: Container): Middleware => {
return createErrorMiddleware(
container.get<ResponseFactory>('responseFactory'),
container.get<Config>('config').debug,
container.get<Logger>('logger'),
);
};
export const generatePathServiceFactory = (container: Container): GeneratePath => {
return createPathToRegexpPathGenerator(container.get<RoutesByName>('routesByName'));
};
export const loggerServiceFactory = (container: Container): Logger => {
const { options, stream } = container.get<Config>('config').pino;
return createLogger(
createPinoAdapter(
pino(
{
timestamp: () => `,"time":${(Date.now() / 1000).toString()}`,
messageKey: 'message',
formatters: {
level: (label: string, number: number) => ({ level: label, level_number: number }),
},
...options,
},
stream,
),
),
);
};
export const matchServiceFactory = (container: Container): Match => {
return createPathToRegexpRouteMatcher(container.get<RoutesByName>('routesByName'));
};
export const middlewaresServiceFactory = (container: Container): Array<Middleware> => {
const m = (name: string) => createLazyMiddleware(container, name);
return [m('errorMiddleware'), m('corsMiddleware'), m('routeMatcherMiddleware')];
};
export const mongoClientServiceFactory = async (container: Container): Promise<MongoClient> => {
const mongoConfig = container.get<Config>('config').mongodb;
const mongoClient = await MongoClient.connect(mongoConfig.uri);
await upsertIndexes(mongoClient, mongoConfig.indexes);
return mongoClient;
};
export const openApiHandlerServiceFactory = (container: Container) => {
return createOpenApiHandler(
container.get<OpenAPIComponentObject>('openApiObject'),
container.get<ResponseFactory>('responseFactory'),
);
};
export const openApiObjectServiceFactory = (container: Container): OpenAPIComponentObject => {
const openApi = container.get<Config>('config').openApi;
const generator = new OpenApiGeneratorV3(container.get<OpenAPIRegistry>('openApiRegistry').definitions);
return generator.generateDocument(openApi);
};
export const openApiRegistryServiceFactory = (): OpenAPIRegistry => {
const registry = new OpenAPIRegistry();
registry.registerPath({
path: '/ping',
method: 'get',
operationId: 'ping',
tags: ['system'],
responses: {
200: {
description: 'Ping response with current date',
content: {
'application/json': {
schema: z.object({ data: z.string() }).openapi({
description: 'Ping',
}),
},
},
},
},
});
return registry;
};
export const pingHandlerServiceFactory = (container: Container) => {
return createPingHandler(container.get<ResponseFactory>('responseFactory'));
};
export const requestFactoryServiceFactory = (container: Container): RequestFactory => {
return createRequestFactory(container.get<UriFactory>('uriFactory'), container.get<StreamFactory>('streamFactory'));
};
export const responseFactoryServiceFactory = (container: Container): ResponseFactory => {
return createResponseFactory(container.get<StreamFactory>('streamFactory'));
};
export const routeMatcherMiddlewareServiceFactory = (container: Container): Middleware => {
return createRouteMatcherMiddleware(container.get<Match>('match'));
};
export const routesServiceFactory = (container: Container): Array<Route> => {
const h = (name: string) => createLazyHandler(container, name);
return [
createGetRoute({
path: '/ping',
name: 'ping',
handler: h('pingHandler'),
}),
createGetRoute({
path: '/openapi',
name: 'openapi',
handler: h('openApiHandler'),
}),
];
};
export const routesByNameServiceFactory = (container: Container): RoutesByName => {
return createRoutesByName(container.get<Array<Route>>('routes'));
};
export const serverRequestFactoryServiceFactory = (container: Container): ServerRequestFactory => {
return createServerRequestFactory(container.get<RequestFactory>('requestFactory'));
};
export const streamFactoryServiceFactory = createStreamFactory;
export const streamFromResourceFactoryServiceFactory = createStreamFromResourceFactory;
export const uriFactoryServiceFactory = createUriFactory;