-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-factory.ts
105 lines (87 loc) · 4.51 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
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 {
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 } 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 type { Config } from '../config/production.js';
import { createPingHandler } from './handler.js';
import type { CleanDirectoriesCommand } from './command.js';
import { createCleanDirectoriesCommand } from './command.js';
export const cleanDirectoriesCommandServiceFactory = (container: Container): CleanDirectoriesCommand => {
return createCleanDirectoriesCommand(container.get<Config>('config').directories, container.get<Logger>('logger'));
};
export const errorMiddlewareServiceFactory = (container: Container): Middleware => {
return createErrorMiddleware(
container.get<ResponseFactory>('responseFactory'),
container.get<Config>('config').debug,
container.get<Logger>('logger'),
);
};
export const loggerServiceFactory = (container: Container): Logger => {
const { options, stream } = container.get<Config>('config').pino;
return createLogger(createPinoAdapter(pino(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('routeMatcherMiddleware')];
};
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'),
}),
];
};
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;