@tramvai/module-server 2.70.0 → 2.72.0

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.
Files changed (48) hide show
  1. package/lib/browser.js +2 -31
  2. package/lib/modules/debugRequests.es.js +143 -0
  3. package/lib/modules/debugRequests.js +151 -0
  4. package/lib/modules/dependenciesVersion.es.js +47 -0
  5. package/lib/modules/dependenciesVersion.js +49 -0
  6. package/lib/modules/gracefulShutdown.es.js +104 -0
  7. package/lib/modules/gracefulShutdown.js +106 -0
  8. package/lib/modules/keepAlive.es.js +47 -0
  9. package/lib/modules/keepAlive.js +49 -0
  10. package/lib/modules/papi/api/index.es.js +80 -0
  11. package/lib/modules/papi/api/index.js +88 -0
  12. package/lib/modules/papi/papi.browser.browser.js +13 -0
  13. package/lib/modules/papi/papi.browser.es.js +13 -0
  14. package/lib/modules/papi/papi.browser.js +15 -0
  15. package/lib/modules/papi/papi.es.js +111 -0
  16. package/lib/modules/papi/papi.js +118 -0
  17. package/lib/modules/papi/server/executor.es.js +39 -0
  18. package/lib/modules/papi/server/executor.js +43 -0
  19. package/lib/modules/papi/server/fileApi.es.js +44 -0
  20. package/lib/modules/papi/server/fileApi.js +52 -0
  21. package/lib/modules/papi/shared.browser.js +25 -0
  22. package/lib/modules/papi/shared.es.js +25 -0
  23. package/lib/modules/papi/shared.js +29 -0
  24. package/lib/modules/proxy.es.js +65 -0
  25. package/lib/modules/proxy.js +73 -0
  26. package/lib/modules/serverTiming.es.js +30 -0
  27. package/lib/modules/serverTiming.js +34 -0
  28. package/lib/modules/statics.es.js +48 -0
  29. package/lib/modules/statics.js +54 -0
  30. package/lib/modules/utilityServer.es.js +99 -0
  31. package/lib/modules/utilityServer.js +101 -0
  32. package/lib/modules/utils/require.es.js +11 -0
  33. package/lib/modules/utils/require.js +16 -0
  34. package/lib/modules/utils/tramvaiDepsFilter.es.js +8 -0
  35. package/lib/modules/utils/tramvaiDepsFilter.js +16 -0
  36. package/lib/server/error.es.js +54 -0
  37. package/lib/server/error.js +62 -0
  38. package/lib/server/server.es.js +17 -0
  39. package/lib/server/server.js +26 -0
  40. package/lib/server/static.es.js +34 -0
  41. package/lib/server/static.js +44 -0
  42. package/lib/server/webApp.es.js +103 -0
  43. package/lib/server/webApp.js +113 -0
  44. package/lib/server/xHeaders.es.js +21 -0
  45. package/lib/server/xHeaders.js +30 -0
  46. package/lib/server.es.js +24 -1035
  47. package/lib/server.js +29 -1055
  48. package/package.json +19 -20
@@ -0,0 +1,47 @@
1
+ import { __decorate } from 'tslib';
2
+ import { Module, provide, commandLineListTokens } from '@tramvai/core';
3
+ import { ENV_USED_TOKEN } from '@tramvai/tokens-common';
4
+ import { ENV_MANAGER_TOKEN } from '@tramvai/module-common';
5
+ import { SERVER_TOKEN } from '@tramvai/tokens-server';
6
+
7
+ let KeepAliveModule = class KeepAliveModule {
8
+ };
9
+ KeepAliveModule = __decorate([
10
+ Module({
11
+ providers: [
12
+ provide({
13
+ provide: ENV_USED_TOKEN,
14
+ useValue: [
15
+ {
16
+ key: 'NODE_KEEPALIVE_TIMEOUT',
17
+ optional: true,
18
+ validator(keepAliveTimeout) {
19
+ const value = Number(keepAliveTimeout);
20
+ if (Number.isNaN(value)) {
21
+ return 'Env variable NODE_KEEPALIVE_TIMEOUT should be a number';
22
+ }
23
+ if (value < 0) {
24
+ return 'Env variable NODE_KEEPALIVE_TIMEOUT should be greater or equal than 0';
25
+ }
26
+ return true;
27
+ },
28
+ },
29
+ ],
30
+ }),
31
+ provide({
32
+ provide: commandLineListTokens.init,
33
+ useFactory: ({ server, envManager, }) => () => {
34
+ const externalKeepAliveTimeout = envManager.get('NODE_KEEPALIVE_TIMEOUT');
35
+ if (externalKeepAliveTimeout !== 'undefined') {
36
+ // eslint-disable-next-line no-param-reassign
37
+ server.keepAliveTimeout = Number(externalKeepAliveTimeout);
38
+ }
39
+ },
40
+ multi: true,
41
+ deps: { server: SERVER_TOKEN, envManager: ENV_MANAGER_TOKEN },
42
+ }),
43
+ ],
44
+ })
45
+ ], KeepAliveModule);
46
+
47
+ export { KeepAliveModule };
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var core = require('@tramvai/core');
7
+ var tokensCommon = require('@tramvai/tokens-common');
8
+ var moduleCommon = require('@tramvai/module-common');
9
+ var tokensServer = require('@tramvai/tokens-server');
10
+
11
+ exports.KeepAliveModule = class KeepAliveModule {
12
+ };
13
+ exports.KeepAliveModule = tslib.__decorate([
14
+ core.Module({
15
+ providers: [
16
+ core.provide({
17
+ provide: tokensCommon.ENV_USED_TOKEN,
18
+ useValue: [
19
+ {
20
+ key: 'NODE_KEEPALIVE_TIMEOUT',
21
+ optional: true,
22
+ validator(keepAliveTimeout) {
23
+ const value = Number(keepAliveTimeout);
24
+ if (Number.isNaN(value)) {
25
+ return 'Env variable NODE_KEEPALIVE_TIMEOUT should be a number';
26
+ }
27
+ if (value < 0) {
28
+ return 'Env variable NODE_KEEPALIVE_TIMEOUT should be greater or equal than 0';
29
+ }
30
+ return true;
31
+ },
32
+ },
33
+ ],
34
+ }),
35
+ core.provide({
36
+ provide: core.commandLineListTokens.init,
37
+ useFactory: ({ server, envManager, }) => () => {
38
+ const externalKeepAliveTimeout = envManager.get('NODE_KEEPALIVE_TIMEOUT');
39
+ if (externalKeepAliveTimeout !== 'undefined') {
40
+ // eslint-disable-next-line no-param-reassign
41
+ server.keepAliveTimeout = Number(externalKeepAliveTimeout);
42
+ }
43
+ },
44
+ multi: true,
45
+ deps: { server: tokensServer.SERVER_TOKEN, envManager: moduleCommon.ENV_MANAGER_TOKEN },
46
+ }),
47
+ ],
48
+ })
49
+ ], exports.KeepAliveModule);
@@ -0,0 +1,80 @@
1
+ import { fastifyCookie } from '@fastify/cookie';
2
+ import fastifyFormBody from '@fastify/formbody';
3
+ import { getPapiParameters } from '@tramvai/papi';
4
+ import { RESPONSE_MANAGER_TOKEN } from '@tramvai/tokens-common';
5
+ import { FASTIFY_REQUEST, FASTIFY_RESPONSE, PAPI_EXECUTOR } from '@tramvai/tokens-server-private';
6
+ import { createChildContainer, Scope } from '@tinkoff/dippy';
7
+ import { HttpError } from '@tinkoff/errors';
8
+
9
+ function createApi(rootApp, papiList, { baseUrl, di, logger }) {
10
+ const paths = new Set();
11
+ const papiLog = logger('papi');
12
+ rootApp.register(async (app) => {
13
+ await app.register(fastifyCookie);
14
+ await app.register(fastifyFormBody, { bodyLimit: 2097152 }); // 2mb
15
+ for (const papi of papiList) {
16
+ const papiParams = getPapiParameters(papi);
17
+ if (!papiParams) {
18
+ throw new Error(`papi should be created using createPapiMethod from @tramvai/papi,
19
+ got: ${JSON.stringify(papi)}`);
20
+ }
21
+ const { method, path, options } = papiParams;
22
+ const { timeout, schema } = options;
23
+ if (!path) {
24
+ throw new Error(`No path in papi handler, got: ${JSON.stringify(papi)}`);
25
+ }
26
+ const key = `${method} ${path}`;
27
+ if (paths.has(key)) {
28
+ throw new Error(`papi: route '${key}' already registered`);
29
+ }
30
+ paths.add(key);
31
+ const childLog = papiLog.child(`${papiParams.method}_${papiParams.path}`);
32
+ app[method](path, {
33
+ schema,
34
+ errorHandler: async (error, req, res) => {
35
+ var _a;
36
+ res.status(error.validation ? 400 : 503);
37
+ childLog.error(error);
38
+ return {
39
+ resultCode: 'INTERNAL_ERROR',
40
+ errorMessage: (_a = error.message) !== null && _a !== void 0 ? _a : 'internal error',
41
+ };
42
+ },
43
+ }, async (req, res) => {
44
+ const childDi = createChildContainer(di, [
45
+ {
46
+ provide: FASTIFY_REQUEST,
47
+ scope: Scope.REQUEST,
48
+ useValue: req,
49
+ },
50
+ {
51
+ provide: FASTIFY_RESPONSE,
52
+ scope: Scope.REQUEST,
53
+ useValue: res,
54
+ },
55
+ ]);
56
+ const papiExecutor = childDi.get(PAPI_EXECUTOR);
57
+ // TODO: use abortSignal
58
+ const payload = await Promise.race([
59
+ papiExecutor(papi),
60
+ new Promise((resolve, reject) => setTimeout(() => reject(new HttpError({ httpStatus: 503, message: 'Execution timeout' })), timeout)),
61
+ ]);
62
+ const responseManager = childDi.get(RESPONSE_MANAGER_TOKEN);
63
+ res.headers(responseManager.getHeaders()).status(responseManager.getStatus());
64
+ if (res.sent) {
65
+ return;
66
+ }
67
+ if (!payload && responseManager.getBody()) {
68
+ res.send(responseManager.getBody());
69
+ return res;
70
+ }
71
+ return {
72
+ resultCode: 'OK',
73
+ payload,
74
+ };
75
+ });
76
+ }
77
+ }, { prefix: baseUrl });
78
+ }
79
+
80
+ export { createApi };
@@ -0,0 +1,88 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var cookie = require('@fastify/cookie');
6
+ var fastifyFormBody = require('@fastify/formbody');
7
+ var papi = require('@tramvai/papi');
8
+ var tokensCommon = require('@tramvai/tokens-common');
9
+ var tokensServerPrivate = require('@tramvai/tokens-server-private');
10
+ var dippy = require('@tinkoff/dippy');
11
+ var errors = require('@tinkoff/errors');
12
+
13
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
+
15
+ var fastifyFormBody__default = /*#__PURE__*/_interopDefaultLegacy(fastifyFormBody);
16
+
17
+ function createApi(rootApp, papiList, { baseUrl, di, logger }) {
18
+ const paths = new Set();
19
+ const papiLog = logger('papi');
20
+ rootApp.register(async (app) => {
21
+ await app.register(cookie.fastifyCookie);
22
+ await app.register(fastifyFormBody__default["default"], { bodyLimit: 2097152 }); // 2mb
23
+ for (const papi$1 of papiList) {
24
+ const papiParams = papi.getPapiParameters(papi$1);
25
+ if (!papiParams) {
26
+ throw new Error(`papi should be created using createPapiMethod from @tramvai/papi,
27
+ got: ${JSON.stringify(papi$1)}`);
28
+ }
29
+ const { method, path, options } = papiParams;
30
+ const { timeout, schema } = options;
31
+ if (!path) {
32
+ throw new Error(`No path in papi handler, got: ${JSON.stringify(papi$1)}`);
33
+ }
34
+ const key = `${method} ${path}`;
35
+ if (paths.has(key)) {
36
+ throw new Error(`papi: route '${key}' already registered`);
37
+ }
38
+ paths.add(key);
39
+ const childLog = papiLog.child(`${papiParams.method}_${papiParams.path}`);
40
+ app[method](path, {
41
+ schema,
42
+ errorHandler: async (error, req, res) => {
43
+ var _a;
44
+ res.status(error.validation ? 400 : 503);
45
+ childLog.error(error);
46
+ return {
47
+ resultCode: 'INTERNAL_ERROR',
48
+ errorMessage: (_a = error.message) !== null && _a !== void 0 ? _a : 'internal error',
49
+ };
50
+ },
51
+ }, async (req, res) => {
52
+ const childDi = dippy.createChildContainer(di, [
53
+ {
54
+ provide: tokensServerPrivate.FASTIFY_REQUEST,
55
+ scope: dippy.Scope.REQUEST,
56
+ useValue: req,
57
+ },
58
+ {
59
+ provide: tokensServerPrivate.FASTIFY_RESPONSE,
60
+ scope: dippy.Scope.REQUEST,
61
+ useValue: res,
62
+ },
63
+ ]);
64
+ const papiExecutor = childDi.get(tokensServerPrivate.PAPI_EXECUTOR);
65
+ // TODO: use abortSignal
66
+ const payload = await Promise.race([
67
+ papiExecutor(papi$1),
68
+ new Promise((resolve, reject) => setTimeout(() => reject(new errors.HttpError({ httpStatus: 503, message: 'Execution timeout' })), timeout)),
69
+ ]);
70
+ const responseManager = childDi.get(tokensCommon.RESPONSE_MANAGER_TOKEN);
71
+ res.headers(responseManager.getHeaders()).status(responseManager.getStatus());
72
+ if (res.sent) {
73
+ return;
74
+ }
75
+ if (!payload && responseManager.getBody()) {
76
+ res.send(responseManager.getBody());
77
+ return res;
78
+ }
79
+ return {
80
+ resultCode: 'OK',
81
+ payload,
82
+ };
83
+ });
84
+ }
85
+ }, { prefix: baseUrl });
86
+ }
87
+
88
+ exports.createApi = createApi;
@@ -0,0 +1,13 @@
1
+ import { __decorate } from 'tslib';
2
+ import { Module } from '@tramvai/core';
3
+ import { sharedProviders } from './shared.browser.js';
4
+
5
+ let BrowserPapiModule = class BrowserPapiModule {
6
+ };
7
+ BrowserPapiModule = __decorate([
8
+ Module({
9
+ providers: [...sharedProviders],
10
+ })
11
+ ], BrowserPapiModule);
12
+
13
+ export { BrowserPapiModule };
@@ -0,0 +1,13 @@
1
+ import { __decorate } from 'tslib';
2
+ import { Module } from '@tramvai/core';
3
+ import { sharedProviders } from './shared.es.js';
4
+
5
+ let BrowserPapiModule = class BrowserPapiModule {
6
+ };
7
+ BrowserPapiModule = __decorate([
8
+ Module({
9
+ providers: [...sharedProviders],
10
+ })
11
+ ], BrowserPapiModule);
12
+
13
+ export { BrowserPapiModule };
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var core = require('@tramvai/core');
7
+ var shared = require('./shared.js');
8
+
9
+ exports.BrowserPapiModule = class BrowserPapiModule {
10
+ };
11
+ exports.BrowserPapiModule = tslib.__decorate([
12
+ core.Module({
13
+ providers: [...shared.sharedProviders],
14
+ })
15
+ ], exports.BrowserPapiModule);
@@ -0,0 +1,111 @@
1
+ import { __decorate } from 'tslib';
2
+ import flatten from '@tinkoff/utils/array/flatten';
3
+ import { Module, provide, DI_TOKEN } from '@tramvai/core';
4
+ import toArray from '@tinkoff/utils/array/toArray';
5
+ import { LOGGER_TOKEN, ENV_MANAGER_TOKEN } from '@tramvai/tokens-common';
6
+ import { SERVER_MODULE_PAPI_PRIVATE_ROUTE, SERVER_MODULE_PAPI_PUBLIC_ROUTE, SERVER_MODULE_PAPI_PRIVATE_URL, SERVER_MODULE_PAPI_PUBLIC_URL } from '@tramvai/tokens-server';
7
+ import { WEB_FASTIFY_APP_BEFORE_INIT_TOKEN } from '@tramvai/tokens-server-private';
8
+ import { createPapiMethod, getPapiParameters } from '@tramvai/papi';
9
+ import { createApi } from './api/index.es.js';
10
+ import { fileApiProvider } from './server/fileApi.es.js';
11
+ import { sharedProviders } from './shared.es.js';
12
+ import { papiExecutorProvider } from './server/executor.es.js';
13
+
14
+ let ServerPapiModule = class ServerPapiModule {
15
+ };
16
+ ServerPapiModule = __decorate([
17
+ Module({
18
+ providers: [
19
+ papiExecutorProvider,
20
+ fileApiProvider,
21
+ ...sharedProviders,
22
+ provide({
23
+ provide: WEB_FASTIFY_APP_BEFORE_INIT_TOKEN,
24
+ useFactory: ({ di, logger, privateRoutes, publicRoutes, publicBaseUrl, privateBaseUrl }) => (app) => {
25
+ if (process.env.NODE_ENV === 'development') {
26
+ const papiListRoute = createPapiMethod({
27
+ method: 'get',
28
+ path: '/papi-list',
29
+ async handler() {
30
+ return [
31
+ ...flatten(privateRoutes).map((papi) => {
32
+ const params = getPapiParameters(papi);
33
+ return {
34
+ path: params.path,
35
+ method: params.method,
36
+ options: params.options,
37
+ type: 'private',
38
+ };
39
+ }),
40
+ ...flatten(publicRoutes).map((papi) => {
41
+ const params = getPapiParameters(papi);
42
+ return {
43
+ path: params.path,
44
+ method: params.method,
45
+ options: params.options,
46
+ type: 'public',
47
+ };
48
+ }),
49
+ ];
50
+ },
51
+ });
52
+ // eslint-disable-next-line no-param-reassign
53
+ privateRoutes = privateRoutes
54
+ ? [...toArray(privateRoutes), papiListRoute]
55
+ : [papiListRoute];
56
+ }
57
+ if (privateRoutes) {
58
+ createApi(app, flatten(privateRoutes), {
59
+ baseUrl: privateBaseUrl,
60
+ di,
61
+ logger,
62
+ });
63
+ }
64
+ if (publicRoutes) {
65
+ createApi(app, flatten(publicRoutes), {
66
+ baseUrl: publicBaseUrl,
67
+ di,
68
+ logger,
69
+ });
70
+ }
71
+ },
72
+ deps: {
73
+ di: DI_TOKEN,
74
+ logger: LOGGER_TOKEN,
75
+ privateRoutes: {
76
+ token: SERVER_MODULE_PAPI_PRIVATE_ROUTE,
77
+ optional: true,
78
+ multi: true,
79
+ },
80
+ publicRoutes: {
81
+ token: SERVER_MODULE_PAPI_PUBLIC_ROUTE,
82
+ optional: true,
83
+ multi: true,
84
+ },
85
+ privateBaseUrl: SERVER_MODULE_PAPI_PRIVATE_URL,
86
+ publicBaseUrl: SERVER_MODULE_PAPI_PUBLIC_URL,
87
+ },
88
+ multi: true,
89
+ }),
90
+ // необходимо для утилит. Подумать как можно убрать в будующем. Если убрать не будет ломающим изменением
91
+ provide({
92
+ provide: SERVER_MODULE_PAPI_PUBLIC_ROUTE,
93
+ multi: true,
94
+ useValue: createPapiMethod({
95
+ method: 'get',
96
+ path: '/version',
97
+ async handler() {
98
+ return {
99
+ version: this.deps.envManager.get('APP_VERSION'),
100
+ };
101
+ },
102
+ deps: {
103
+ envManager: ENV_MANAGER_TOKEN,
104
+ },
105
+ }),
106
+ }),
107
+ ],
108
+ })
109
+ ], ServerPapiModule);
110
+
111
+ export { ServerPapiModule };
@@ -0,0 +1,118 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var flatten = require('@tinkoff/utils/array/flatten');
7
+ var core = require('@tramvai/core');
8
+ var toArray = require('@tinkoff/utils/array/toArray');
9
+ var tokensCommon = require('@tramvai/tokens-common');
10
+ var tokensServer = require('@tramvai/tokens-server');
11
+ var tokensServerPrivate = require('@tramvai/tokens-server-private');
12
+ var papi = require('@tramvai/papi');
13
+ var index = require('./api/index.js');
14
+ var fileApi = require('./server/fileApi.js');
15
+ var shared = require('./shared.js');
16
+ var executor = require('./server/executor.js');
17
+
18
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
+
20
+ var flatten__default = /*#__PURE__*/_interopDefaultLegacy(flatten);
21
+ var toArray__default = /*#__PURE__*/_interopDefaultLegacy(toArray);
22
+
23
+ exports.ServerPapiModule = class ServerPapiModule {
24
+ };
25
+ exports.ServerPapiModule = tslib.__decorate([
26
+ core.Module({
27
+ providers: [
28
+ executor.papiExecutorProvider,
29
+ fileApi.fileApiProvider,
30
+ ...shared.sharedProviders,
31
+ core.provide({
32
+ provide: tokensServerPrivate.WEB_FASTIFY_APP_BEFORE_INIT_TOKEN,
33
+ useFactory: ({ di, logger, privateRoutes, publicRoutes, publicBaseUrl, privateBaseUrl }) => (app) => {
34
+ if (process.env.NODE_ENV === 'development') {
35
+ const papiListRoute = papi.createPapiMethod({
36
+ method: 'get',
37
+ path: '/papi-list',
38
+ async handler() {
39
+ return [
40
+ ...flatten__default["default"](privateRoutes).map((papi$1) => {
41
+ const params = papi.getPapiParameters(papi$1);
42
+ return {
43
+ path: params.path,
44
+ method: params.method,
45
+ options: params.options,
46
+ type: 'private',
47
+ };
48
+ }),
49
+ ...flatten__default["default"](publicRoutes).map((papi$1) => {
50
+ const params = papi.getPapiParameters(papi$1);
51
+ return {
52
+ path: params.path,
53
+ method: params.method,
54
+ options: params.options,
55
+ type: 'public',
56
+ };
57
+ }),
58
+ ];
59
+ },
60
+ });
61
+ // eslint-disable-next-line no-param-reassign
62
+ privateRoutes = privateRoutes
63
+ ? [...toArray__default["default"](privateRoutes), papiListRoute]
64
+ : [papiListRoute];
65
+ }
66
+ if (privateRoutes) {
67
+ index.createApi(app, flatten__default["default"](privateRoutes), {
68
+ baseUrl: privateBaseUrl,
69
+ di,
70
+ logger,
71
+ });
72
+ }
73
+ if (publicRoutes) {
74
+ index.createApi(app, flatten__default["default"](publicRoutes), {
75
+ baseUrl: publicBaseUrl,
76
+ di,
77
+ logger,
78
+ });
79
+ }
80
+ },
81
+ deps: {
82
+ di: core.DI_TOKEN,
83
+ logger: tokensCommon.LOGGER_TOKEN,
84
+ privateRoutes: {
85
+ token: tokensServer.SERVER_MODULE_PAPI_PRIVATE_ROUTE,
86
+ optional: true,
87
+ multi: true,
88
+ },
89
+ publicRoutes: {
90
+ token: tokensServer.SERVER_MODULE_PAPI_PUBLIC_ROUTE,
91
+ optional: true,
92
+ multi: true,
93
+ },
94
+ privateBaseUrl: tokensServer.SERVER_MODULE_PAPI_PRIVATE_URL,
95
+ publicBaseUrl: tokensServer.SERVER_MODULE_PAPI_PUBLIC_URL,
96
+ },
97
+ multi: true,
98
+ }),
99
+ // необходимо для утилит. Подумать как можно убрать в будующем. Если убрать не будет ломающим изменением
100
+ core.provide({
101
+ provide: tokensServer.SERVER_MODULE_PAPI_PUBLIC_ROUTE,
102
+ multi: true,
103
+ useValue: papi.createPapiMethod({
104
+ method: 'get',
105
+ path: '/version',
106
+ async handler() {
107
+ return {
108
+ version: this.deps.envManager.get('APP_VERSION'),
109
+ };
110
+ },
111
+ deps: {
112
+ envManager: tokensCommon.ENV_MANAGER_TOKEN,
113
+ },
114
+ }),
115
+ }),
116
+ ],
117
+ })
118
+ ], exports.ServerPapiModule);
@@ -0,0 +1,39 @@
1
+ import { provide, Scope, DI_TOKEN } from '@tinkoff/dippy';
2
+ import { getPapiParameters } from '@tramvai/papi';
3
+ import { LOGGER_TOKEN, REQUEST_MANAGER_TOKEN, RESPONSE_MANAGER_TOKEN } from '@tramvai/tokens-common';
4
+ import { PAPI_EXECUTOR, FASTIFY_REQUEST } from '@tramvai/tokens-server-private';
5
+
6
+ const papiExecutorProvider = provide({
7
+ provide: PAPI_EXECUTOR,
8
+ scope: Scope.REQUEST,
9
+ useFactory: ({ di, logger, fastifyRequest, requestManager, responseManager }) => {
10
+ var _a;
11
+ const papiLog = logger('papi');
12
+ const papiOptions = {
13
+ requestManager,
14
+ responseManager,
15
+ params: (_a = fastifyRequest.params) !== null && _a !== void 0 ? _a : {},
16
+ cookies: requestManager.getCookies(),
17
+ headers: requestManager.getHeaders(),
18
+ body: requestManager.getBody(),
19
+ parsedUrl: requestManager.getParsedUrl(),
20
+ };
21
+ return (papi) => {
22
+ const { handler, deps, method, path } = getPapiParameters(papi);
23
+ const papiContext = {
24
+ deps: di.getOfDeps(deps),
25
+ log: papiLog.child(`${method}_${path}`),
26
+ };
27
+ return handler.call(papiContext, papiOptions);
28
+ };
29
+ },
30
+ deps: {
31
+ di: DI_TOKEN,
32
+ logger: LOGGER_TOKEN,
33
+ fastifyRequest: FASTIFY_REQUEST,
34
+ requestManager: REQUEST_MANAGER_TOKEN,
35
+ responseManager: RESPONSE_MANAGER_TOKEN,
36
+ },
37
+ });
38
+
39
+ export { papiExecutorProvider };
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var dippy = require('@tinkoff/dippy');
6
+ var papi = require('@tramvai/papi');
7
+ var tokensCommon = require('@tramvai/tokens-common');
8
+ var tokensServerPrivate = require('@tramvai/tokens-server-private');
9
+
10
+ const papiExecutorProvider = dippy.provide({
11
+ provide: tokensServerPrivate.PAPI_EXECUTOR,
12
+ scope: dippy.Scope.REQUEST,
13
+ useFactory: ({ di, logger, fastifyRequest, requestManager, responseManager }) => {
14
+ var _a;
15
+ const papiLog = logger('papi');
16
+ const papiOptions = {
17
+ requestManager,
18
+ responseManager,
19
+ params: (_a = fastifyRequest.params) !== null && _a !== void 0 ? _a : {},
20
+ cookies: requestManager.getCookies(),
21
+ headers: requestManager.getHeaders(),
22
+ body: requestManager.getBody(),
23
+ parsedUrl: requestManager.getParsedUrl(),
24
+ };
25
+ return (papi$1) => {
26
+ const { handler, deps, method, path } = papi.getPapiParameters(papi$1);
27
+ const papiContext = {
28
+ deps: di.getOfDeps(deps),
29
+ log: papiLog.child(`${method}_${path}`),
30
+ };
31
+ return handler.call(papiContext, papiOptions);
32
+ };
33
+ },
34
+ deps: {
35
+ di: dippy.DI_TOKEN,
36
+ logger: tokensCommon.LOGGER_TOKEN,
37
+ fastifyRequest: tokensServerPrivate.FASTIFY_REQUEST,
38
+ requestManager: tokensCommon.REQUEST_MANAGER_TOKEN,
39
+ responseManager: tokensCommon.RESPONSE_MANAGER_TOKEN,
40
+ },
41
+ });
42
+
43
+ exports.papiExecutorProvider = papiExecutorProvider;
@@ -0,0 +1,44 @@
1
+ import eachObj from '@tinkoff/utils/object/each';
2
+ import { LOGGER_TOKEN } from '@tramvai/tokens-common';
3
+ import { SERVER_MODULE_PAPI_PUBLIC_ROUTE } from '@tramvai/tokens-server';
4
+ import { isPapiMethod, getPapiParameters, createPapiMethod } from '@tramvai/papi';
5
+
6
+ let papis;
7
+ try {
8
+ // eslint-disable-next-line import/no-extraneous-dependencies
9
+ papis = require('@tramvai/cli/lib/external/api').default; // eslint-disable-line import/no-unresolved
10
+ }
11
+ catch (e) { }
12
+ const getFileApi = ({ logger }) => {
13
+ const log = logger('papi:fileApi');
14
+ const result = [];
15
+ eachObj(({ default: entry }, path) => {
16
+ if (!isPapiMethod(entry)) {
17
+ log.error({
18
+ path,
19
+ entry,
20
+ message: `Cannot resolve a papi handler.
21
+ Check that you are using file based papi right way by docs https://tramvai.dev/docs/how-to/how-create-papi#automatic-handler-creation
22
+ In case you have not added any file papi handler, consider renaming directory ./src/api (by default) to the other name to resolve conflicts with papi, or
23
+ change settings serverApiDir in tramvai.json`,
24
+ });
25
+ throw new Error('Not a papi');
26
+ }
27
+ const papiParameters = getPapiParameters(entry);
28
+ result.push(createPapiMethod({
29
+ ...papiParameters,
30
+ path: `/${path}`,
31
+ }));
32
+ }, papis);
33
+ return result;
34
+ };
35
+ const fileApiProvider = {
36
+ provide: SERVER_MODULE_PAPI_PUBLIC_ROUTE,
37
+ multi: true,
38
+ useFactory: getFileApi,
39
+ deps: {
40
+ logger: LOGGER_TOKEN,
41
+ },
42
+ };
43
+
44
+ export { fileApiProvider };