@rvoh/psychic 1.10.3 → 1.10.5

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 (33) hide show
  1. package/CHANGELOG.md +320 -0
  2. package/dist/cjs/src/bin/helpers/enumsFileStr.js +1 -0
  3. package/dist/cjs/src/cli/helpers/PsychicLogos.js +38 -0
  4. package/dist/cjs/src/controller/helpers/httpMethodColor.js +34 -0
  5. package/dist/cjs/src/controller/helpers/logIfDevelopment.js +28 -0
  6. package/dist/cjs/src/controller/helpers/statusCodeColor.js +22 -0
  7. package/dist/cjs/src/controller/index.js +32 -13
  8. package/dist/cjs/src/openapi-renderer/endpoint.js +8 -2
  9. package/dist/cjs/src/openapi-renderer/helpers/dreamAttributeOpenapiShape.js +9 -8
  10. package/dist/cjs/src/router/index.js +10 -19
  11. package/dist/cjs/src/server/helpers/startPsychicServer.js +17 -4
  12. package/dist/cjs/src/server/index.js +20 -3
  13. package/dist/esm/src/bin/helpers/enumsFileStr.js +1 -0
  14. package/dist/esm/src/cli/helpers/PsychicLogos.js +32 -0
  15. package/dist/esm/src/controller/helpers/httpMethodColor.js +30 -0
  16. package/dist/esm/src/controller/helpers/logIfDevelopment.js +22 -0
  17. package/dist/esm/src/controller/helpers/statusCodeColor.js +18 -0
  18. package/dist/esm/src/controller/index.js +32 -13
  19. package/dist/esm/src/openapi-renderer/endpoint.js +8 -2
  20. package/dist/esm/src/openapi-renderer/helpers/dreamAttributeOpenapiShape.js +9 -8
  21. package/dist/esm/src/router/index.js +10 -19
  22. package/dist/esm/src/server/helpers/startPsychicServer.js +17 -4
  23. package/dist/esm/src/server/index.js +21 -4
  24. package/dist/types/src/cli/helpers/PsychicLogos.d.ts +3 -0
  25. package/dist/types/src/controller/helpers/httpMethodColor.d.ts +4 -0
  26. package/dist/types/src/controller/helpers/logIfDevelopment.d.ts +7 -0
  27. package/dist/types/src/controller/helpers/statusCodeColor.d.ts +3 -0
  28. package/dist/types/src/controller/index.d.ts +5 -1
  29. package/dist/types/src/helpers/EnvInternal.d.ts +2 -2
  30. package/dist/types/src/openapi-renderer/endpoint.d.ts +39 -1
  31. package/dist/types/src/server/index.d.ts +1 -1
  32. package/dist/types/src/server/params.d.ts +2 -1
  33. package/package.json +4 -3
package/CHANGELOG.md ADDED
@@ -0,0 +1,320 @@
1
+ ## 1.10.5
2
+
3
+ - add "combining" option to requestBody for OpenAPI decorator, enabling you to combine additional openapi fields to the request body, while still leveraging the powerful automatically-generated request body.
4
+ - syncing client enums now sync types along with values
5
+ - better dev logging
6
+
7
+ ## 1.10.4
8
+
9
+ Fix issue with rendering incorrect enum descriptions when suppressResponseEnums is set to true and enums are explicitly overridden in the openapi option.
10
+
11
+ ## 1.10.3
12
+
13
+ - respect `required: false` when generating OpenAPI spec
14
+
15
+ ## 1.10.2
16
+
17
+ - return 400 instead of throwing error and 500 when there is a column overflow at the database level (let database validation suffice for enforcing data length validation rather than requiring model level validation)
18
+
19
+ ## 1.10.1
20
+
21
+ - OpenAPI and castParam validation errors are logged only when `NODE_DEBUG=psychic`
22
+
23
+ ## 1.10.0
24
+
25
+ - remove OpenAPI and Dream validation error response configuration and do not respond with errors (don't introduce such a difference between development and production environments)
26
+ - log validation errors in test and dev (not prod to avoid DOS)
27
+ - remove distinction between 400 and 422 to block ability of attacker to get feedback on how far into the system their request made it
28
+
29
+ ## 1.9.0
30
+
31
+ 1. Validate params against OpenAPI at the latest possible of:
32
+ a. when the params are accessed
33
+ b. when about to render the action
34
+ This ensures that we return the proper 401/403 response instead of 400 for authenticated endpoints that fail authentication and prevents unauthenticated requests from gaining information about the API
35
+
36
+ 2. Ability to configure whether or not OpenAPI validation errors include detailed information
37
+
38
+ ## 1.8.6
39
+
40
+ remove dead env variable, now that we are open sourced
41
+
42
+ ## 1.8.5
43
+
44
+ Do not hard crash when initializing a psychic application when one of the openapi routes is not found for an openapi-decorated controller endpoint. We will continue to raise this exception when building openapi specs, but not when booting up the psychic application, since one can define routes that are i.e. not available in certain environments, and we don't want this to cause hard crashes when our app boots in those environments.
45
+
46
+ ## 1.8.4
47
+
48
+ - OpenAPI decorator with default 204 status does not throw an exception when passed a Dream model without a `serializers` getter
49
+ - OpenAPI decorator that defines an explicit OpenAPI shape for the default status code does not throw an exception when passed a Dream model without a `serializers` getter
50
+
51
+ ## 1.8.3
52
+
53
+ - don't build openapi when `bypassModelIntegrityCheck: true`
54
+
55
+ ## 1.8.2
56
+
57
+ - openapi validation properly coerces non-array query params to arrays when validating, since both express and ajv fail to do this under the hood properly. This solves issues where sending up array params with only a single item in them are not treated as arrays.
58
+
59
+ ## 1.8.1
60
+
61
+ - do not coerce types in ajv when processing request or response bodies during validation. Type coercion will still happen for headers and query params, since they will need to respect the schema type specified in the openapi docuement.
62
+
63
+ ## 1.8.0
64
+
65
+ - remove unused `clientRoot` config
66
+
67
+ ## 1.7.2
68
+
69
+ - generate admin routes in routes.admin.ts (requires `routes.admin.ts` next to `routes.ts`)
70
+
71
+ ## 1.7.1
72
+
73
+ - compute openapi doc during intiialization, rather than problematically reading from a file cache
74
+
75
+ ## 1.7.0
76
+
77
+ - `sanitizeResponseJson` config to automatically escape `<`, `>`, `&`, `/`, `\`, `'`, and `"` unicode representations when rendering json to satisfy security reviews (e.g., a pentest report recently called this out on one of our applications). For all practical purposes, this doesn't protect against anything (now that we have the `nosniff` header) since `JSON.parse` on the other end restores the original, dangerous string. Modern front end web frameworks already handle safely displaying arbitrary content, so further sanitization generally isn't needed. This version does provide the `sanitizeString` function that could be used to sanitize individual strings, replacing the above characters with string representations of the unicode characters that will survive Psychic converting to json and then parsing that json (i.e.: `<` will end up as the string "\u003c")
78
+
79
+ - Fix openapi serializer fallback issue introduced in 1.6.3, where we mistakenly double render data that has already been serialized.
80
+
81
+ ## 1.6.4
82
+
83
+ Raise an exception if attempting to import an openapi file during PsychicApp.init when in production. We will still swallow the exception in non-prod environments so that one can create a new openapi configuration and run sync without getting an error.
84
+
85
+ ## 1.6.3
86
+
87
+ - castParam accepts raw openapi shapes as type arguments, correctly casting the result to an interface representing the provided openapi shape.
88
+
89
+ ```ts
90
+ class MyController extends ApplicationController {
91
+ public index() {
92
+ const myParam = this.castParam('myParam', {
93
+ type: 'array',
94
+ items: {
95
+ anyOf: [{ type: 'string' }, { type: 'number' }],
96
+ },
97
+ })
98
+ myParam[0] // string | number
99
+ }
100
+ }
101
+ ```
102
+
103
+ - simplify the needlessly-robust new psychic router patterns by making expressApp optional, essentially reverting us back to the same psychic router we had prior to the recent openapi validation changes.
104
+
105
+ - fallback to serializer specified in openapi decorator before falling back to dream serializer when rendering dreams
106
+
107
+ ## 1.6.2
108
+
109
+ fix OpenAPI spec generation by DRYing up generation of request and response body
110
+
111
+ ## 1.6.1
112
+
113
+ fix issue preventing validation fallbacks from properly overriding on OpenAPI decorator calls when explicitly opting out of validation
114
+
115
+ ## 1.6.0
116
+
117
+ enables validation to be added to both openapi configurations, as well as to `OpenAPI` decorator calls, enabling the developer to granularly control validation logic for their endpoints.
118
+
119
+ To leverage global config:
120
+
121
+ ```ts
122
+ // conf/app.ts
123
+ export default async (psy: PsychicApp) => {
124
+ ...
125
+
126
+ psy.set('openapi', {
127
+ // ...
128
+ validate: {
129
+ headers: true,
130
+ requestBody: true,
131
+ query: true,
132
+ responseBody: AppEnv.isTest,
133
+ },
134
+ })
135
+ }
136
+ ```
137
+
138
+ To leverage endpoint config:
139
+
140
+ ```ts
141
+ // controllers/PetsController
142
+ export default class PetsController {
143
+ @OpenAPI(Pet, {
144
+ ...
145
+ validate: {
146
+ headers: true,
147
+ requestBody: true,
148
+ query: true,
149
+ responseBody: AppEnv.isTest,
150
+ }
151
+ })
152
+ public async index() {
153
+ ...
154
+ }
155
+ }
156
+ ```
157
+
158
+ This PR additionally formally introduces a new possible error type for 400 status codes, and to help distinguish, it also introduces a `type` field, which can be either `openapi` or `dream` to aid the developer in easily handling the various cases.
159
+
160
+ We have made a conscious decision to render openapi errors in the exact format that ajv returns, since it empowers the developer to utilize tools which can already respond to ajv errors.
161
+
162
+ For added flexibility, this PR includes the ability to provide configuration overrides for the ajv instance, as well as the ability to provide an initialization function to override ajv behavior, since much of the configuration for ajv is driven by method calls, rather than simple config.
163
+
164
+ ```ts
165
+ // controllers/PetsController
166
+ export default class PetsController {
167
+ @OpenAPI(Pet, {
168
+ ...
169
+ validate: {
170
+ ajvOptions: {
171
+ // this is off by default, but you will
172
+ // always want to keep this off in prod
173
+ // to avoid DoS vulnerabilities
174
+ allErrors: AppEnv.isTest,
175
+
176
+ // provide a custom init function to further
177
+ // configure your ajv instance before validating
178
+ init: ajv => {
179
+ ajv.addFormat('myFormat', {
180
+ type: 'string',
181
+ validate: data => MY_FORMAT_REGEX.test(data),
182
+ })
183
+ }
184
+ }
185
+ }
186
+ })
187
+ public async index() {
188
+ ...
189
+ }
190
+ }
191
+ ```
192
+
193
+ ## 1.5.5
194
+
195
+ - ensure that openapi-typescript and typescript are not required dependencies when running migrations with --skip-sync flag
196
+
197
+ ## 1.5.4
198
+
199
+ - fix issue when providing the `including` argument exclusively to an OpenAPI decorator's `requestBody`
200
+
201
+ ## 1.5.3
202
+
203
+ - add missing peer dependency for openapi-typescript, allow BIGINT type when generating openapi-typescript bigints
204
+
205
+ ## 1.5.2
206
+
207
+ - ensure that bigints are converted to number | string when generating openapi-typescript type files
208
+
209
+ ## 1.5.1
210
+
211
+ - fix issue with enum syncing related to multi-db engine support regression
212
+
213
+ ## 1.5.0
214
+
215
+ - add support for multiple database engines in dream
216
+
217
+ ## 1.2.3
218
+
219
+ - add support for the connectionName argument when generating a resource
220
+
221
+ ## 1.2.2
222
+
223
+ - bump supertest and express-session to close dependabot issues [53](https://github.com/rvohealth/psychic/security/dependabot/53), [56](https://github.com/rvohealth/psychic/security/dependabot/56), and [57](https://github.com/rvohealth/psychic/security/dependabot/57)
224
+
225
+ ## 1.2.1
226
+
227
+ - add ability to set custom import extension, which will be used when generating new files for your application
228
+
229
+ ## 1.2.0
230
+
231
+ - update for Dream 1.4.0
232
+
233
+ ## 1.1.11
234
+
235
+ - 400 is more appropriate than 422 for `DataTypeColumnTypeMismatch`
236
+
237
+ ## 1.1.10
238
+
239
+ - Don't include deletedAt in generated create/update actions in resource specs since deletedAt is for deleting
240
+
241
+ - return 422 if Dream throws `NotNullViolation` or `CheckConstraintViolation`
242
+
243
+ ## 1.1.9
244
+
245
+ - return 422 if dream throws `DataTypeColumnTypeMismatch`, which happens when a dream is saved to the database with data that cannot be inserted into the respective columns, usually because of a type mismatch.
246
+
247
+ - castParam will now encase params in an array when being explicitly casted as an array type, bypassing a known bug in express from causing arrays with single items in them to be treated as non-arrays.
248
+
249
+ ## 1.1.8
250
+
251
+ - Tap into CliFileWriter provided by dream to tap into file reversion for sync files, since the auto-sync function in psychic can fail and leave your file tree in a bad state.
252
+
253
+ ## 1.1.7
254
+
255
+ - Add support for middleware arrays, enabling express plugins like passport
256
+
257
+ ## 1.1.6
258
+
259
+ - fix regression caused by missing --schema-only option in psychic cli
260
+
261
+ ## 1.1.5
262
+
263
+ - pass packageManager through to dream, now that it accepts a packageManager setting.
264
+ - update dream shadowing within psychic application initialization to take place after initializers and plugins are processed, so that those initializers and plugins have an opportunity to adjust the settings.
265
+
266
+ ## 1.1.4
267
+
268
+ - fix regressions to redux bindings caused by default openapi path location changes
269
+ - resource generator can handle prefixing slashes
270
+
271
+ ## 1.1.3
272
+
273
+ - fix more minor issues with redux openapi bindings
274
+
275
+ ## 1.1.2
276
+
277
+ - Fix various issues with openapi redux bindings
278
+ - raise hard exception if accidentally using openapi route params in an expressjs route path
279
+
280
+ ## 1.1.1
281
+
282
+ Fix route printing regression causing route printouts to show the path instead of the action
283
+
284
+ ## v1.1.0
285
+
286
+ Provides easier access to express middleware by exposing `PsychicApp#use`, which enables a developer to provide express middleware directly through the psychcic application, without tapping into any hooks.
287
+
288
+ ```ts
289
+ psy.use((_, res) => {
290
+ res.send(
291
+ 'this will be run after psychic middleware (i.e. cors and bodyParser) are processed, but before routes are processed',
292
+ )
293
+ })
294
+ ```
295
+
296
+ Some middleware needs to be run before other middleware, so we expose an optional first argument which can be provided so explicitly send your middleware into express at various stages of the psychic configuration process. For example, to inject your middleware before cors and bodyParser are configured, provide `before-middleware` as the first argument. To initialize your middleware after the psychic default middleware, but before your routes have been processed, provide `after-middleware` as the first argument (or simply provide a callback function directly, since this is the default). To run after routes have been processed, provide `after-routes` as the first argument.
297
+
298
+ ```ts
299
+ psy.use('before-middleware', (_, res) => {
300
+ res.send('this will be run before psychic has configured any default middleware')
301
+ })
302
+
303
+ psy.use('after-middleware', (_, res) => {
304
+ res.send('this will be run after psychic has configured default middleware')
305
+ })
306
+
307
+ psy.use('after-routes', (_, res) => {
308
+ res.send('this will be run after psychic has processed all the routes in your conf/routes.ts file')
309
+ })
310
+ ```
311
+
312
+ Additionally, a new overload has been added to all CRUD methods on the PsychicRouter class, enabling you to provide RequestHandler middleware directly to psychic, like so:
313
+
314
+ ```ts
315
+ // conf/routes.ts
316
+
317
+ r.get('helloworld', (req, res, next) => {
318
+ res.json({ hello: 'world' })
319
+ })
320
+ ```
@@ -20,6 +20,7 @@ export const ${exportedTypeName} = [
20
20
  .map(val => `'${val}'`)
21
21
  .join(',\n ')}
22
22
  ] as const
23
+ export type ${exportedTypeName.replace(/Values^/, '')} = (typeof ${exportedTypeName})[number]
23
24
 
24
25
  `;
25
26
  });
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const colorize_js_1 = __importDefault(require("./colorize.js"));
7
+ class PsychicLogos {
8
+ static babyAster() {
9
+ const g = (str) => (0, colorize_js_1.default)(str, { color: 'green' });
10
+ return `
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
+ `.replace(/▓/g, g('▓'));
36
+ }
37
+ }
38
+ exports.default = PsychicLogos;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = httpMethodColor;
4
+ exports.httpMethodBgColor = httpMethodBgColor;
5
+ function httpMethodColor(httpMethod) {
6
+ switch (httpMethod) {
7
+ case 'delete':
8
+ return 'red';
9
+ case 'options':
10
+ return 'gray';
11
+ case 'patch':
12
+ case 'put':
13
+ return 'magentaBright';
14
+ case 'post':
15
+ return 'greenBright';
16
+ default:
17
+ return 'blue';
18
+ }
19
+ }
20
+ function httpMethodBgColor(httpMethod) {
21
+ switch (httpMethod) {
22
+ case 'delete':
23
+ return 'bgRed';
24
+ case 'options':
25
+ return 'bgWhite';
26
+ case 'patch':
27
+ case 'put':
28
+ return 'bgMagentaBright';
29
+ case 'post':
30
+ return 'bgGreen';
31
+ default:
32
+ return 'bgBlue';
33
+ }
34
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.default = logIfDevelopment;
7
+ const dream_1 = require("@rvoh/dream");
8
+ const colorize_js_1 = __importDefault(require("../../cli/helpers/colorize.js"));
9
+ const EnvInternal_js_1 = __importDefault(require("../../helpers/EnvInternal.js"));
10
+ const httpMethodColor_js_1 = require("./httpMethodColor.js");
11
+ const statusCodeColor_js_1 = require("./statusCodeColor.js");
12
+ function logIfDevelopment({ req, res, startTime, fallbackStatusCode = 200, }) {
13
+ if (!EnvInternal_js_1.default.isDevelopment)
14
+ return;
15
+ const method = (0, colorize_js_1.default)(` ${req.method.toUpperCase()} `, {
16
+ color: 'black',
17
+ bgColor: (0, httpMethodColor_js_1.httpMethodBgColor)(req.method.toLowerCase()),
18
+ });
19
+ const computedStatus = res.statusCode || fallbackStatusCode;
20
+ const statusBgColor = (0, statusCodeColor_js_1.statusCodeBgColor)(computedStatus);
21
+ const status = (0, colorize_js_1.default)(` ${computedStatus} `, {
22
+ color: 'black',
23
+ bgColor: statusBgColor,
24
+ });
25
+ const url = (0, colorize_js_1.default)(req.url, { color: 'green' });
26
+ const benchmark = (0, colorize_js_1.default)(`${Date.now() - startTime}ms`, { color: 'gray' });
27
+ dream_1.DreamCLI.logger.log(`${method} ${url} ${status} ${benchmark}`, { logPrefix: '' });
28
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = statusCodeColor;
4
+ exports.statusCodeBgColor = statusCodeBgColor;
5
+ function statusCodeColor(statusCode) {
6
+ if (statusCode >= 200 && statusCode < 300)
7
+ return 'green';
8
+ if (statusCode >= 300 && statusCode < 400)
9
+ return 'yellow';
10
+ if (statusCode >= 400 && statusCode < 500)
11
+ return 'red';
12
+ return 'redBright';
13
+ }
14
+ function statusCodeBgColor(statusCode) {
15
+ if (statusCode >= 200 && statusCode < 300)
16
+ return 'bgGreen';
17
+ if (statusCode >= 300 && statusCode < 400)
18
+ return 'bgYellow';
19
+ if (statusCode >= 400 && statusCode < 500)
20
+ return 'bgRed';
21
+ return 'bgRedBright';
22
+ }
@@ -43,7 +43,9 @@ const index_js_1 = __importDefault(require("../psychic-app/index.js"));
43
43
  const params_js_1 = __importDefault(require("../server/params.js"));
44
44
  const index_js_2 = __importDefault(require("../session/index.js"));
45
45
  const isPaginatedResult_js_1 = __importDefault(require("./helpers/isPaginatedResult.js"));
46
+ const logIfDevelopment_js_1 = __importDefault(require("./helpers/logIfDevelopment.js"));
46
47
  const renderDreamOrViewModel_js_1 = __importDefault(require("./helpers/renderDreamOrViewModel.js"));
48
+ const EnvInternal_js_1 = __importDefault(require("../helpers/EnvInternal.js"));
47
49
  exports.PsychicParamsPrimitiveLiterals = [
48
50
  'bigint',
49
51
  'bigint[]',
@@ -177,7 +179,9 @@ class PsychicController {
177
179
  session;
178
180
  action;
179
181
  renderOpts;
182
+ startTime;
180
183
  constructor(req, res, { action, }) {
184
+ this.startTime = Date.now();
181
185
  this.req = req;
182
186
  this.res = res;
183
187
  this.session = new index_js_2.default(req, res);
@@ -428,7 +432,26 @@ class PsychicController {
428
432
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
429
433
  data) {
430
434
  this.validateOpenapiResponseBody(data);
431
- this.res.type('json').send((0, toJson_js_1.default)(data, index_js_1.default.getOrFail().sanitizeResponseJson));
435
+ this.expressSendJson(data);
436
+ }
437
+ expressSendJson(
438
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
439
+ data, statusCode = this.res.statusCode) {
440
+ this.res.type('json').status(statusCode).send((0, toJson_js_1.default)(data, index_js_1.default.getOrFail().sanitizeResponseJson));
441
+ this.logIfDevelopment();
442
+ }
443
+ expressSendStatus(statusCode) {
444
+ this.res.sendStatus(statusCode);
445
+ this.logIfDevelopment();
446
+ }
447
+ expressRedirect(statusCode, newLocation) {
448
+ this.res.redirect(statusCode, newLocation);
449
+ this.logIfDevelopment();
450
+ }
451
+ logIfDevelopment() {
452
+ if (!EnvInternal_js_1.default.isDevelopment)
453
+ return;
454
+ (0, logIfDevelopment_js_1.default)({ req: this.req, res: this.res, startTime: this.startTime });
432
455
  }
433
456
  defaultSerializerPassthrough = {};
434
457
  serializerPassthrough(passthrough) {
@@ -475,15 +498,15 @@ class PsychicController {
475
498
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
476
499
  nonAuthoritativeInformation(message = undefined) {
477
500
  if (message) {
478
- this.res.status(203).json(message);
501
+ this.expressSendJson(message, 203);
479
502
  }
480
503
  else {
481
- this.res.sendStatus(203);
504
+ this.expressSendStatus(203);
482
505
  }
483
506
  }
484
507
  // 204
485
508
  noContent() {
486
- this.res.sendStatus(204);
509
+ this.expressSendStatus(204);
487
510
  }
488
511
  // 205
489
512
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -499,27 +522,23 @@ class PsychicController {
499
522
  }
500
523
  // 301
501
524
  movedPermanently(newLocation) {
502
- this.res.redirect(301, newLocation);
525
+ this.expressRedirect(301, newLocation);
503
526
  }
504
527
  // 302
505
528
  found(newLocation) {
506
- this.res.redirect(302, newLocation);
529
+ this.expressRedirect(302, newLocation);
507
530
  }
508
531
  // 303
509
532
  seeOther(newLocation) {
510
- this.res.redirect(303, newLocation);
511
- }
512
- // 304
513
- notModified(newLocation) {
514
- this.res.redirect(304, newLocation);
533
+ this.expressRedirect(303, newLocation);
515
534
  }
516
535
  // 307
517
536
  temporaryRedirect(newLocation) {
518
- this.res.redirect(307, newLocation);
537
+ this.expressRedirect(307, newLocation);
519
538
  }
520
539
  // 308
521
540
  permanentRedirect(newLocation) {
522
- this.res.redirect(308, newLocation);
541
+ this.expressRedirect(308, newLocation);
523
542
  }
524
543
  // 400
525
544
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -474,6 +474,8 @@ class OpenapiEndpointRenderer {
474
474
  return true;
475
475
  if (body.including)
476
476
  return true;
477
+ if (body.combining)
478
+ return true;
477
479
  if (body.for)
478
480
  return true;
479
481
  if (body.required && body.type !== 'object')
@@ -494,12 +496,16 @@ class OpenapiEndpointRenderer {
494
496
  const dreamClass = forDreamClass || this.getSingleDreamModelClass();
495
497
  if (!dreamClass)
496
498
  return this.defaultRequestBody();
497
- const { only, including } = (this.requestBody || {});
499
+ const { only, including, combining } = (this.requestBody || {});
498
500
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
499
501
  const paramSafeColumns = (0, paramNamesForDreamClass_js_1.default)(dreamClass, { only, including });
500
502
  const paramsShape = {
501
503
  type: 'object',
502
- properties: {},
504
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
505
+ properties: {
506
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
507
+ ...(combining || {}),
508
+ },
503
509
  };
504
510
  const required = this.requestBody?.required;
505
511
  if (required) {
@@ -54,7 +54,7 @@ function dreamColumnOpenapiShape(dreamClass, column, openapi = undefined, { supp
54
54
  }
55
55
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
56
56
  const openapiObject = (0, openapiShorthandToOpenapi_js_1.default)((openapi ?? {}));
57
- const singleType = singularAttributeOpenapiShape(dreamColumnInfo, suppressResponseEnums);
57
+ const singleType = singularAttributeOpenapiShape(dreamColumnInfo, suppressResponseEnums, openapiObject);
58
58
  if (dreamColumnInfo.isArray) {
59
59
  return {
60
60
  type: dreamColumnInfo.allowNull ? ['array', 'null'] : 'array',
@@ -66,31 +66,32 @@ function dreamColumnOpenapiShape(dreamClass, column, openapi = undefined, { supp
66
66
  const existingType = dreamColumnInfo.allowNull
67
67
  ? [singleType.type, 'null']
68
68
  : singleType.type;
69
- return {
69
+ const returnObj = {
70
70
  ...singleType,
71
71
  type: existingType,
72
72
  ...openapiObject,
73
73
  };
74
+ if (suppressResponseEnums)
75
+ delete returnObj['enum'];
76
+ return returnObj;
74
77
  }
75
78
  }
76
79
  function baseDbType(dreamColumnInfo) {
77
80
  return dreamColumnInfo.dbType.replace('[]', '');
78
81
  }
79
- function singularAttributeOpenapiShape(dreamColumnInfo, suppressResponseEnums) {
82
+ function singularAttributeOpenapiShape(dreamColumnInfo, suppressResponseEnums, openapiSchema) {
80
83
  if (dreamColumnInfo.enumValues) {
84
+ const enumOverrides = openapiSchema.enum || dreamColumnInfo.enumValues;
81
85
  if (suppressResponseEnums) {
82
86
  return {
83
87
  type: 'string',
84
- description: `The following values will be allowed:\n ${dreamColumnInfo.enumValues.join(',\n ')}`,
88
+ description: `The following values will be allowed:\n ${enumOverrides.join(',\n ')}`,
85
89
  };
86
90
  }
87
91
  else {
88
92
  return {
89
93
  type: 'string',
90
- enum: [
91
- ...dreamColumnInfo.enumValues,
92
- ...(dreamColumnInfo.allowNull && !dreamColumnInfo.isArray ? [null] : []),
93
- ],
94
+ enum: [...enumOverrides, ...(dreamColumnInfo.allowNull && !dreamColumnInfo.isArray ? [null] : [])],
94
95
  };
95
96
  }
96
97
  }