@rvoh/psychic 1.8.2 → 1.8.4
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.
- package/dist/cjs/src/openapi-renderer/endpoint.js +14 -4
- package/dist/cjs/src/psychic-app/index.js +6 -4
- package/dist/esm/src/openapi-renderer/endpoint.js +14 -4
- package/dist/esm/src/psychic-app/index.js +6 -4
- package/dist/types/src/openapi-renderer/endpoint.d.ts +2 -1
- package/package.json +2 -2
- package/CHANGELOG.md +0 -266
|
@@ -215,7 +215,8 @@ class OpenapiEndpointRenderer {
|
|
|
215
215
|
* while parsing the responses and provided callback function.
|
|
216
216
|
*/
|
|
217
217
|
toSchemaObject({ openapiName, renderOpts, alreadyExtractedDescendantSerializers, renderedSchemasOpenapi, serializersAppearingInHandWrittenOpenapi, }) {
|
|
218
|
-
const serializers = this.
|
|
218
|
+
const serializers = this.extractSerializerFromSerializableArgument() ??
|
|
219
|
+
[];
|
|
219
220
|
serializersToSchemaObjects(this.controllerClass, this.action, [...serializers, ...serializersAppearingInHandWrittenOpenapi], {
|
|
220
221
|
openapiName,
|
|
221
222
|
renderOpts,
|
|
@@ -617,7 +618,7 @@ class OpenapiEndpointRenderer {
|
|
|
617
618
|
* if it was not passed.
|
|
618
619
|
*/
|
|
619
620
|
get defaultStatus() {
|
|
620
|
-
if (!this.
|
|
621
|
+
if (!this.dreamsOrSerializers)
|
|
621
622
|
return 204;
|
|
622
623
|
return 200;
|
|
623
624
|
}
|
|
@@ -628,7 +629,7 @@ class OpenapiEndpointRenderer {
|
|
|
628
629
|
* Openapi decorator.
|
|
629
630
|
*/
|
|
630
631
|
parseSerializerResponseShape({ renderOpts, }) {
|
|
631
|
-
const serializerClasses = this.
|
|
632
|
+
const serializerClasses = this.extractSerializerFromSerializableArgument();
|
|
632
633
|
if (!serializerClasses)
|
|
633
634
|
return {
|
|
634
635
|
referencedSerializers: [],
|
|
@@ -765,7 +766,11 @@ class OpenapiEndpointRenderer {
|
|
|
765
766
|
* attached dream or view model to identify a serializer
|
|
766
767
|
* match.
|
|
767
768
|
*/
|
|
768
|
-
|
|
769
|
+
extractSerializerFromSerializableArgument() {
|
|
770
|
+
if (this.status === 204)
|
|
771
|
+
return null;
|
|
772
|
+
if (this.openapiResponseProvidedForStatus(this.status))
|
|
773
|
+
return null;
|
|
769
774
|
if (!this.dreamsOrSerializers)
|
|
770
775
|
return null;
|
|
771
776
|
const serializers = (0, dream_1.compact)([this.dreamsOrSerializers]
|
|
@@ -778,6 +783,11 @@ class OpenapiEndpointRenderer {
|
|
|
778
783
|
});
|
|
779
784
|
return serializers;
|
|
780
785
|
}
|
|
786
|
+
openapiResponseProvidedForStatus(status) {
|
|
787
|
+
if (status === undefined)
|
|
788
|
+
return false;
|
|
789
|
+
return !!this.responses?.[status];
|
|
790
|
+
}
|
|
781
791
|
/**
|
|
782
792
|
* @internal
|
|
783
793
|
*
|
|
@@ -97,10 +97,12 @@ class PsychicApp {
|
|
|
97
97
|
dreamApp.set('logger', psychicApp.logger);
|
|
98
98
|
dreamApp.set('packageManager', psychicApp.packageManager);
|
|
99
99
|
(0, cache_js_1.cachePsychicApp)(psychicApp);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
100
|
+
if (!opts.bypassModelIntegrityCheck) {
|
|
101
|
+
// routes _must_ be built before openapi
|
|
102
|
+
// cache can be processed
|
|
103
|
+
await psychicApp.buildRoutesCache();
|
|
104
|
+
psychicApp.buildOpenapiCache();
|
|
105
|
+
}
|
|
104
106
|
});
|
|
105
107
|
return psychicApp;
|
|
106
108
|
}
|
|
@@ -209,7 +209,8 @@ export default class OpenapiEndpointRenderer {
|
|
|
209
209
|
* while parsing the responses and provided callback function.
|
|
210
210
|
*/
|
|
211
211
|
toSchemaObject({ openapiName, renderOpts, alreadyExtractedDescendantSerializers, renderedSchemasOpenapi, serializersAppearingInHandWrittenOpenapi, }) {
|
|
212
|
-
const serializers = this.
|
|
212
|
+
const serializers = this.extractSerializerFromSerializableArgument() ??
|
|
213
|
+
[];
|
|
213
214
|
serializersToSchemaObjects(this.controllerClass, this.action, [...serializers, ...serializersAppearingInHandWrittenOpenapi], {
|
|
214
215
|
openapiName,
|
|
215
216
|
renderOpts,
|
|
@@ -611,7 +612,7 @@ export default class OpenapiEndpointRenderer {
|
|
|
611
612
|
* if it was not passed.
|
|
612
613
|
*/
|
|
613
614
|
get defaultStatus() {
|
|
614
|
-
if (!this.
|
|
615
|
+
if (!this.dreamsOrSerializers)
|
|
615
616
|
return 204;
|
|
616
617
|
return 200;
|
|
617
618
|
}
|
|
@@ -622,7 +623,7 @@ export default class OpenapiEndpointRenderer {
|
|
|
622
623
|
* Openapi decorator.
|
|
623
624
|
*/
|
|
624
625
|
parseSerializerResponseShape({ renderOpts, }) {
|
|
625
|
-
const serializerClasses = this.
|
|
626
|
+
const serializerClasses = this.extractSerializerFromSerializableArgument();
|
|
626
627
|
if (!serializerClasses)
|
|
627
628
|
return {
|
|
628
629
|
referencedSerializers: [],
|
|
@@ -759,7 +760,11 @@ export default class OpenapiEndpointRenderer {
|
|
|
759
760
|
* attached dream or view model to identify a serializer
|
|
760
761
|
* match.
|
|
761
762
|
*/
|
|
762
|
-
|
|
763
|
+
extractSerializerFromSerializableArgument() {
|
|
764
|
+
if (this.status === 204)
|
|
765
|
+
return null;
|
|
766
|
+
if (this.openapiResponseProvidedForStatus(this.status))
|
|
767
|
+
return null;
|
|
763
768
|
if (!this.dreamsOrSerializers)
|
|
764
769
|
return null;
|
|
765
770
|
const serializers = compact([this.dreamsOrSerializers]
|
|
@@ -772,6 +777,11 @@ export default class OpenapiEndpointRenderer {
|
|
|
772
777
|
});
|
|
773
778
|
return serializers;
|
|
774
779
|
}
|
|
780
|
+
openapiResponseProvidedForStatus(status) {
|
|
781
|
+
if (status === undefined)
|
|
782
|
+
return false;
|
|
783
|
+
return !!this.responses?.[status];
|
|
784
|
+
}
|
|
775
785
|
/**
|
|
776
786
|
* @internal
|
|
777
787
|
*
|
|
@@ -68,10 +68,12 @@ export default class PsychicApp {
|
|
|
68
68
|
dreamApp.set('logger', psychicApp.logger);
|
|
69
69
|
dreamApp.set('packageManager', psychicApp.packageManager);
|
|
70
70
|
cachePsychicApp(psychicApp);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
if (!opts.bypassModelIntegrityCheck) {
|
|
72
|
+
// routes _must_ be built before openapi
|
|
73
|
+
// cache can be processed
|
|
74
|
+
await psychicApp.buildRoutesCache();
|
|
75
|
+
psychicApp.buildOpenapiCache();
|
|
76
|
+
}
|
|
75
77
|
});
|
|
76
78
|
return psychicApp;
|
|
77
79
|
}
|
|
@@ -287,7 +287,8 @@ export default class OpenapiEndpointRenderer<DreamsOrSerializersOrViewModels ext
|
|
|
287
287
|
* attached dream or view model to identify a serializer
|
|
288
288
|
* match.
|
|
289
289
|
*/
|
|
290
|
-
private
|
|
290
|
+
private extractSerializerFromSerializableArgument;
|
|
291
|
+
private openapiResponseProvidedForStatus;
|
|
291
292
|
/**
|
|
292
293
|
* @internal
|
|
293
294
|
*
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@rvoh/psychic",
|
|
4
4
|
"description": "Typescript web framework",
|
|
5
|
-
"version": "1.8.
|
|
5
|
+
"version": "1.8.4",
|
|
6
6
|
"author": "RVOHealth",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
@@ -96,4 +96,4 @@
|
|
|
96
96
|
"winston": "^3.14.2"
|
|
97
97
|
},
|
|
98
98
|
"packageManager": "yarn@4.7.0"
|
|
99
|
-
}
|
|
99
|
+
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
## 1.8.2
|
|
2
|
-
|
|
3
|
-
- 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.
|
|
4
|
-
|
|
5
|
-
## 1.8.1
|
|
6
|
-
|
|
7
|
-
- 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.
|
|
8
|
-
|
|
9
|
-
## 1.8.0
|
|
10
|
-
|
|
11
|
-
- remove unused `clientRoot` config
|
|
12
|
-
|
|
13
|
-
## 1.7.2
|
|
14
|
-
|
|
15
|
-
- generate admin routes in routes.admin.ts (requires `routes.admin.ts` next to `routes.ts`)
|
|
16
|
-
|
|
17
|
-
## 1.7.1
|
|
18
|
-
|
|
19
|
-
- compute openapi doc during intiialization, rather than problematically reading from a file cache
|
|
20
|
-
|
|
21
|
-
## 1.7.0
|
|
22
|
-
|
|
23
|
-
- `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")
|
|
24
|
-
|
|
25
|
-
- Fix openapi serializer fallback issue introduced in 1.6.3, where we mistakenly double render data that has already been serialized.
|
|
26
|
-
|
|
27
|
-
## 1.6.4
|
|
28
|
-
|
|
29
|
-
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.
|
|
30
|
-
|
|
31
|
-
## 1.6.3
|
|
32
|
-
|
|
33
|
-
- castParam accepts raw openapi shapes as type arguments, correctly casting the result to an interface representing the provided openapi shape.
|
|
34
|
-
|
|
35
|
-
```ts
|
|
36
|
-
class MyController extends ApplicationController {
|
|
37
|
-
public index() {
|
|
38
|
-
const myParam = this.castParam('myParam', {
|
|
39
|
-
type: 'array',
|
|
40
|
-
items: {
|
|
41
|
-
anyOf: [{ type: 'string' }, { type: 'number' }],
|
|
42
|
-
},
|
|
43
|
-
})
|
|
44
|
-
myParam[0] // string | number
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
- 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.
|
|
50
|
-
|
|
51
|
-
- fallback to serializer specified in openapi decorator before falling back to dream serializer when rendering dreams
|
|
52
|
-
|
|
53
|
-
## 1.6.2
|
|
54
|
-
|
|
55
|
-
fix OpenAPI spec generation by DRYing up generation of request and response body
|
|
56
|
-
|
|
57
|
-
## 1.6.1
|
|
58
|
-
|
|
59
|
-
fix issue preventing validation fallbacks from properly overriding on OpenAPI decorator calls when explicitly opting out of validation
|
|
60
|
-
|
|
61
|
-
## 1.6.0
|
|
62
|
-
|
|
63
|
-
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.
|
|
64
|
-
|
|
65
|
-
To leverage global config:
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
// conf/app.ts
|
|
69
|
-
export default async (psy: PsychicApp) => {
|
|
70
|
-
...
|
|
71
|
-
|
|
72
|
-
psy.set('openapi', {
|
|
73
|
-
// ...
|
|
74
|
-
validate: {
|
|
75
|
-
headers: true,
|
|
76
|
-
requestBody: true,
|
|
77
|
-
query: true,
|
|
78
|
-
responseBody: AppEnv.isTest,
|
|
79
|
-
},
|
|
80
|
-
})
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
To leverage endpoint config:
|
|
85
|
-
|
|
86
|
-
```ts
|
|
87
|
-
// controllers/PetsController
|
|
88
|
-
export default class PetsController {
|
|
89
|
-
@OpenAPI(Pet, {
|
|
90
|
-
...
|
|
91
|
-
validate: {
|
|
92
|
-
headers: true,
|
|
93
|
-
requestBody: true,
|
|
94
|
-
query: true,
|
|
95
|
-
responseBody: AppEnv.isTest,
|
|
96
|
-
}
|
|
97
|
-
})
|
|
98
|
-
public async index() {
|
|
99
|
-
...
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
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.
|
|
105
|
-
|
|
106
|
-
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.
|
|
107
|
-
|
|
108
|
-
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.
|
|
109
|
-
|
|
110
|
-
```ts
|
|
111
|
-
// controllers/PetsController
|
|
112
|
-
export default class PetsController {
|
|
113
|
-
@OpenAPI(Pet, {
|
|
114
|
-
...
|
|
115
|
-
validate: {
|
|
116
|
-
ajvOptions: {
|
|
117
|
-
// this is off by default, but you will
|
|
118
|
-
// always want to keep this off in prod
|
|
119
|
-
// to avoid DoS vulnerabilities
|
|
120
|
-
allErrors: AppEnv.isTest,
|
|
121
|
-
|
|
122
|
-
// provide a custom init function to further
|
|
123
|
-
// configure your ajv instance before validating
|
|
124
|
-
init: ajv => {
|
|
125
|
-
ajv.addFormat('myFormat', {
|
|
126
|
-
type: 'string',
|
|
127
|
-
validate: data => MY_FORMAT_REGEX.test(data),
|
|
128
|
-
})
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
})
|
|
133
|
-
public async index() {
|
|
134
|
-
...
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
## 1.5.5
|
|
140
|
-
|
|
141
|
-
- ensure that openapi-typescript and typescript are not required dependencies when running migrations with --skip-sync flag
|
|
142
|
-
|
|
143
|
-
## 1.5.4
|
|
144
|
-
|
|
145
|
-
- fix issue when providing the `including` argument exclusively to an OpenAPI decorator's `requestBody`
|
|
146
|
-
|
|
147
|
-
## 1.5.3
|
|
148
|
-
|
|
149
|
-
- add missing peer dependency for openapi-typescript, allow BIGINT type when generating openapi-typescript bigints
|
|
150
|
-
|
|
151
|
-
## 1.5.2
|
|
152
|
-
|
|
153
|
-
- ensure that bigints are converted to number | string when generating openapi-typescript type files
|
|
154
|
-
|
|
155
|
-
## 1.5.1
|
|
156
|
-
|
|
157
|
-
- fix issue with enum syncing related to multi-db engine support regression
|
|
158
|
-
|
|
159
|
-
## 1.5.0
|
|
160
|
-
|
|
161
|
-
- add support for multiple database engines in dream
|
|
162
|
-
|
|
163
|
-
## 1.2.3
|
|
164
|
-
|
|
165
|
-
- add support for the connectionName argument when generating a resource
|
|
166
|
-
|
|
167
|
-
## 1.2.2
|
|
168
|
-
|
|
169
|
-
- 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)
|
|
170
|
-
|
|
171
|
-
## 1.2.1
|
|
172
|
-
|
|
173
|
-
- add ability to set custom import extension, which will be used when generating new files for your application
|
|
174
|
-
|
|
175
|
-
## 1.2.0
|
|
176
|
-
|
|
177
|
-
- update for Dream 1.4.0
|
|
178
|
-
|
|
179
|
-
## 1.1.11
|
|
180
|
-
|
|
181
|
-
- 400 is more appropriate than 422 for `DataTypeColumnTypeMismatch`
|
|
182
|
-
|
|
183
|
-
## 1.1.10
|
|
184
|
-
|
|
185
|
-
- Don't include deletedAt in generated create/update actions in resource specs since deletedAt is for deleting
|
|
186
|
-
|
|
187
|
-
- return 422 if Dream throws `NotNullViolation` or `CheckConstraintViolation`
|
|
188
|
-
|
|
189
|
-
## 1.1.9
|
|
190
|
-
|
|
191
|
-
- 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.
|
|
192
|
-
|
|
193
|
-
- 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.
|
|
194
|
-
|
|
195
|
-
## 1.1.8
|
|
196
|
-
|
|
197
|
-
- 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.
|
|
198
|
-
|
|
199
|
-
## 1.1.7
|
|
200
|
-
|
|
201
|
-
- Add support for middleware arrays, enabling express plugins like passport
|
|
202
|
-
|
|
203
|
-
## 1.1.6
|
|
204
|
-
|
|
205
|
-
- fix regression caused by missing --schema-only option in psychic cli
|
|
206
|
-
|
|
207
|
-
## 1.1.5
|
|
208
|
-
|
|
209
|
-
- pass packageManager through to dream, now that it accepts a packageManager setting.
|
|
210
|
-
- 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.
|
|
211
|
-
|
|
212
|
-
## 1.1.4
|
|
213
|
-
|
|
214
|
-
- fix regressions to redux bindings caused by default openapi path location changes
|
|
215
|
-
- resource generator can handle prefixing slashes
|
|
216
|
-
|
|
217
|
-
## 1.1.3
|
|
218
|
-
|
|
219
|
-
- fix more minor issues with redux openapi bindings
|
|
220
|
-
|
|
221
|
-
## 1.1.2
|
|
222
|
-
|
|
223
|
-
- Fix various issues with openapi redux bindings
|
|
224
|
-
- raise hard exception if accidentally using openapi route params in an expressjs route path
|
|
225
|
-
|
|
226
|
-
## 1.1.1
|
|
227
|
-
|
|
228
|
-
Fix route printing regression causing route printouts to show the path instead of the action
|
|
229
|
-
|
|
230
|
-
## v1.1.0
|
|
231
|
-
|
|
232
|
-
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.
|
|
233
|
-
|
|
234
|
-
```ts
|
|
235
|
-
psy.use((_, res) => {
|
|
236
|
-
res.send(
|
|
237
|
-
'this will be run after psychic middleware (i.e. cors and bodyParser) are processed, but before routes are processed',
|
|
238
|
-
)
|
|
239
|
-
})
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
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.
|
|
243
|
-
|
|
244
|
-
```ts
|
|
245
|
-
psy.use('before-middleware', (_, res) => {
|
|
246
|
-
res.send('this will be run before psychic has configured any default middleware')
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
psy.use('after-middleware', (_, res) => {
|
|
250
|
-
res.send('this will be run after psychic has configured default middleware')
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
psy.use('after-routes', (_, res) => {
|
|
254
|
-
res.send('this will be run after psychic has processed all the routes in your conf/routes.ts file')
|
|
255
|
-
})
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
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:
|
|
259
|
-
|
|
260
|
-
```ts
|
|
261
|
-
// conf/routes.ts
|
|
262
|
-
|
|
263
|
-
r.get('helloworld', (req, res, next) => {
|
|
264
|
-
res.json({ hello: 'world' })
|
|
265
|
-
})
|
|
266
|
-
```
|