@naturalcycles/backend-lib 4.0.0 → 4.1.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.
@@ -38,8 +38,22 @@ function createDefaultApp(cfg) {
38
38
  app.use((0, simpleRequestLoggerMiddleware_1.simpleRequestLoggerMiddleware)());
39
39
  }
40
40
  // app.use(safeJsonMiddleware()) // optional
41
- app.use(express.json({ limit: '1mb' }));
42
- app.use(express.urlencoded({ limit: '1mb', extended: true }));
41
+ // accepts application/json
42
+ app.use(express.json({
43
+ limit: '1mb',
44
+ ...cfg.bodyParserJsonOptions,
45
+ }));
46
+ app.use(express.urlencoded({
47
+ limit: '1mb',
48
+ extended: true,
49
+ ...cfg.bodyParserUrlEncodedOptions,
50
+ }));
51
+ // accepts application/octet-stream
52
+ app.use(express.raw({
53
+ // inflate: true, // default is `true`
54
+ limit: '1mb',
55
+ ...cfg.bodyParserRawOptions,
56
+ }));
43
57
  app.use(cookieParser());
44
58
  if (!isTest) {
45
59
  // leaks, load lazily
@@ -52,6 +66,7 @@ function createDefaultApp(cfg) {
52
66
  credentials: true,
53
67
  // methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', // default
54
68
  maxAge: 86400,
69
+ ...cfg.corsOptions,
55
70
  }));
56
71
  // app.use(clearBodyParserTimeout()) // removed by default
57
72
  // Static is now disabled by default due to performance
@@ -1,3 +1,5 @@
1
+ import { Options, OptionsJson, OptionsUrlencoded } from 'body-parser';
2
+ import { CorsOptions } from 'cors';
1
3
  import { SentrySharedService } from '../sentry/sentry.shared.service';
2
4
  import { BackendRequestHandler } from './server.model';
3
5
  /**
@@ -23,4 +25,8 @@ export interface DefaultAppCfg {
23
25
  resources?: BackendRequestHandlerCfg[];
24
26
  postHandlers?: BackendRequestHandlerCfg[];
25
27
  sentryService?: SentrySharedService;
28
+ bodyParserJsonOptions?: OptionsJson;
29
+ bodyParserUrlEncodedOptions?: OptionsUrlencoded;
30
+ bodyParserRawOptions?: Options;
31
+ corsOptions?: CorsOptions;
26
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install && patch-package",
6
6
  "serve": "APP_ENV=dev nodemon",
@@ -44,7 +44,7 @@
44
44
  "@sentry/node": "^6.5.1",
45
45
  "@types/ejs": "^3.0.0",
46
46
  "@types/js-yaml": "^4.0.0",
47
- "@types/node": "^16.4.1",
47
+ "@types/node": "^17.0.0",
48
48
  "esbuild-register": "^3.1.2",
49
49
  "fastify": "^3.20.1",
50
50
  "jest": "^27.0.1",
@@ -1,3 +1,5 @@
1
+ import { Options, OptionsJson, OptionsUrlencoded } from 'body-parser'
2
+ import { CorsOptions } from 'cors'
1
3
  import { SentrySharedService } from '../sentry/sentry.shared.service'
2
4
  import { BackendRequestHandler } from './server.model'
3
5
 
@@ -25,5 +27,12 @@ export interface DefaultAppCfg {
25
27
  handlers?: BackendRequestHandlerCfg[]
26
28
  resources?: BackendRequestHandlerCfg[]
27
29
  postHandlers?: BackendRequestHandlerCfg[]
30
+
28
31
  sentryService?: SentrySharedService
32
+
33
+ bodyParserJsonOptions?: OptionsJson
34
+ bodyParserUrlEncodedOptions?: OptionsUrlencoded
35
+ bodyParserRawOptions?: Options
36
+
37
+ corsOptions?: CorsOptions
29
38
  }
@@ -51,9 +51,34 @@ export function createDefaultApp(cfg: DefaultAppCfg): BackendApplication {
51
51
  }
52
52
 
53
53
  // app.use(safeJsonMiddleware()) // optional
54
- app.use(express.json({ limit: '1mb' }))
55
- app.use(express.urlencoded({ limit: '1mb', extended: true }))
54
+
55
+ // accepts application/json
56
+ app.use(
57
+ express.json({
58
+ limit: '1mb',
59
+ ...cfg.bodyParserJsonOptions,
60
+ }),
61
+ )
62
+
63
+ app.use(
64
+ express.urlencoded({
65
+ limit: '1mb',
66
+ extended: true,
67
+ ...cfg.bodyParserUrlEncodedOptions,
68
+ }),
69
+ )
70
+
71
+ // accepts application/octet-stream
72
+ app.use(
73
+ express.raw({
74
+ // inflate: true, // default is `true`
75
+ limit: '1mb',
76
+ ...cfg.bodyParserRawOptions,
77
+ }),
78
+ )
79
+
56
80
  app.use(cookieParser())
81
+
57
82
  if (!isTest) {
58
83
  // leaks, load lazily
59
84
  app.use(
@@ -69,6 +94,7 @@ export function createDefaultApp(cfg: DefaultAppCfg): BackendApplication {
69
94
  credentials: true,
70
95
  // methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', // default
71
96
  maxAge: 86400,
97
+ ...cfg.corsOptions,
72
98
  }),
73
99
  )
74
100