@rvoh/psychic 2.3.9 → 3.0.0-alpha.1
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/controller/helpers/logIfDevelopment.js +5 -5
- package/dist/cjs/src/controller/index.js +115 -40
- package/dist/cjs/src/devtools/helpers/launchDevServer.js +0 -1
- package/dist/cjs/src/error/router/cannot-commit-routes-without-koa-app.js +12 -0
- package/dist/cjs/src/helpers/toJson.js +2 -8
- package/dist/cjs/src/helpers/validateOpenApiSchema.js +1 -1
- package/dist/cjs/src/openapi-renderer/helpers/OpenapiPayloadValidator.js +75 -9
- package/dist/cjs/src/openapi-renderer/helpers/stringify-cache.js +55 -0
- package/dist/cjs/src/openapi-renderer/helpers/validator-cache.js +52 -0
- package/dist/cjs/src/psychic-app/helpers/import/importControllers.js +1 -1
- package/dist/cjs/src/psychic-app/index.js +3 -10
- package/dist/cjs/src/router/index.js +31 -25
- package/dist/cjs/src/server/helpers/startPsychicServer.js +6 -2
- package/dist/cjs/src/server/index.js +32 -35
- package/dist/cjs/src/session/index.js +9 -12
- package/dist/esm/src/controller/helpers/logIfDevelopment.js +5 -5
- package/dist/esm/src/controller/index.js +115 -40
- package/dist/esm/src/devtools/helpers/launchDevServer.js +0 -1
- package/dist/esm/src/error/router/cannot-commit-routes-without-koa-app.js +12 -0
- package/dist/esm/src/helpers/toJson.js +2 -8
- package/dist/esm/src/helpers/validateOpenApiSchema.js +1 -1
- package/dist/esm/src/openapi-renderer/helpers/OpenapiPayloadValidator.js +75 -9
- package/dist/esm/src/openapi-renderer/helpers/stringify-cache.js +55 -0
- package/dist/esm/src/openapi-renderer/helpers/validator-cache.js +52 -0
- package/dist/esm/src/psychic-app/helpers/import/importControllers.js +1 -1
- package/dist/esm/src/psychic-app/index.js +3 -10
- package/dist/esm/src/router/index.js +31 -25
- package/dist/esm/src/server/helpers/startPsychicServer.js +6 -2
- package/dist/esm/src/server/index.js +32 -35
- package/dist/esm/src/session/index.js +9 -12
- package/dist/types/src/controller/helpers/logIfDevelopment.d.ts +3 -4
- package/dist/types/src/controller/index.d.ts +18 -7
- package/dist/types/src/error/router/cannot-commit-routes-without-koa-app.d.ts +3 -0
- package/dist/types/src/helpers/cookieMaxAgeFromCookieOpts.d.ts +1 -1
- package/dist/types/src/helpers/toJson.d.ts +1 -1
- package/dist/types/src/helpers/validateOpenApiSchema.d.ts +5 -1
- package/dist/types/src/openapi-renderer/helpers/OpenapiPayloadValidator.d.ts +41 -0
- package/dist/types/src/openapi-renderer/helpers/stringify-cache.d.ts +34 -0
- package/dist/types/src/openapi-renderer/helpers/validator-cache.d.ts +35 -0
- package/dist/types/src/psychic-app/index.d.ts +11 -14
- package/dist/types/src/router/index.d.ts +17 -17
- package/dist/types/src/router/route-manager.d.ts +4 -3
- package/dist/types/src/server/helpers/startPsychicServer.d.ts +3 -3
- package/dist/types/src/server/index.d.ts +3 -3
- package/dist/types/src/session/index.d.ts +13 -5
- package/package.json +29 -18
- package/dist/cjs/src/error/router/cannot-commit-routes-without-express-app.js +0 -12
- package/dist/esm/src/error/router/cannot-commit-routes-without-express-app.js +0 -12
- package/dist/types/src/error/router/cannot-commit-routes-without-express-app.d.ts +0 -3
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { DataIncompatibleWithDatabaseField, RecordNotFound, ValidationError } from '@rvoh/dream/errors';
|
|
2
2
|
import { camelize } from '@rvoh/dream/utils';
|
|
3
|
-
import
|
|
3
|
+
import KoaRouter from '@koa/router';
|
|
4
4
|
import util, { debuglog } from 'node:util';
|
|
5
5
|
import pluralize from 'pluralize-esm';
|
|
6
6
|
import ParamValidationError from '../error/controller/ParamValidationError.js';
|
|
7
7
|
import ParamValidationErrors from '../error/controller/ParamValidationErrors.js';
|
|
8
8
|
import OpenapiRequestValidationFailure from '../error/openapi/OpenapiRequestValidationFailure.js';
|
|
9
|
-
import
|
|
9
|
+
import CannotCommitRoutesWithoutKoaApp from '../error/router/cannot-commit-routes-without-koa-app.js';
|
|
10
10
|
import EnvInternal from '../helpers/EnvInternal.js';
|
|
11
11
|
import errorIsRescuableHttpError from '../helpers/error/errorIsRescuableHttpError.js';
|
|
12
12
|
import PsychicApp from '../psychic-app/index.js';
|
|
@@ -31,19 +31,25 @@ export default class PsychicRouter {
|
|
|
31
31
|
commit() {
|
|
32
32
|
const app = this.app;
|
|
33
33
|
if (!app)
|
|
34
|
-
throw new
|
|
34
|
+
throw new CannotCommitRoutesWithoutKoaApp();
|
|
35
|
+
const router = new KoaRouter();
|
|
35
36
|
this.routes.forEach(route => {
|
|
36
37
|
if (route.middleware) {
|
|
37
38
|
const routeConf = route;
|
|
38
|
-
|
|
39
|
+
const middlewares = Array.isArray(routeConf.middleware)
|
|
40
|
+
? routeConf.middleware
|
|
41
|
+
: [routeConf.middleware];
|
|
42
|
+
router[routeConf.httpMethod](routePath(routeConf.path), ...middlewares);
|
|
39
43
|
}
|
|
40
44
|
else {
|
|
41
45
|
const routeConf = route;
|
|
42
|
-
|
|
43
|
-
this.handle(routeConf.controller, routeConf.action, {
|
|
46
|
+
router[routeConf.httpMethod](routePath(routeConf.path), async (ctx) => {
|
|
47
|
+
await this.handle(routeConf.controller, routeConf.action, { ctx });
|
|
44
48
|
});
|
|
45
49
|
}
|
|
46
50
|
});
|
|
51
|
+
app.use(router.routes());
|
|
52
|
+
app.use(router.allowedMethods());
|
|
47
53
|
}
|
|
48
54
|
get(path, controller, action) {
|
|
49
55
|
this.crud('get', path, controller, action);
|
|
@@ -72,7 +78,7 @@ export default class PsychicRouter {
|
|
|
72
78
|
this.checkPathForInvalidChars(path);
|
|
73
79
|
const isMiddleware = (typeof controllerOrMiddleware === 'function' || Array.isArray(controllerOrMiddleware)) &&
|
|
74
80
|
!controllerOrMiddleware?.isPsychicController;
|
|
75
|
-
// devs can provide custom
|
|
81
|
+
// devs can provide custom Koa middleware which bypasses
|
|
76
82
|
// the normal Controller#action paradigm.
|
|
77
83
|
if (isMiddleware) {
|
|
78
84
|
this.routeManager.addMiddleware({
|
|
@@ -98,7 +104,7 @@ export default class PsychicRouter {
|
|
|
98
104
|
if (path.includes('{'))
|
|
99
105
|
throw new Error(`
|
|
100
106
|
The provided route "${path}" contains characters that are not supported.
|
|
101
|
-
If you are trying to write a uri param, you will need to use
|
|
107
|
+
If you are trying to write a uri param, you will need to use the
|
|
102
108
|
param syntax, which is a prefixing colon, rather than using brackets
|
|
103
109
|
to surround the param.
|
|
104
110
|
|
|
@@ -236,10 +242,10 @@ suggested fix: "${convertRouteParams(path)}"
|
|
|
236
242
|
* By default, do not provide an attacker with any visibility into which layer
|
|
237
243
|
* of the application rejected their request.
|
|
238
244
|
*/
|
|
239
|
-
async handle(controller, action, {
|
|
240
|
-
const controllerInstance = this._initializeController(controller,
|
|
245
|
+
async handle(controller, action, { ctx, }) {
|
|
246
|
+
const controllerInstance = this._initializeController(controller, ctx, action);
|
|
241
247
|
if (typeof controllerInstance[action] !== 'function') {
|
|
242
|
-
controllerInstance['
|
|
248
|
+
controllerInstance['koaSendStatus'](404);
|
|
243
249
|
return;
|
|
244
250
|
}
|
|
245
251
|
try {
|
|
@@ -250,20 +256,20 @@ suggested fix: "${convertRouteParams(path)}"
|
|
|
250
256
|
if (errorIsRescuableHttpError(err)) {
|
|
251
257
|
const httpErr = err;
|
|
252
258
|
if (httpErr.data) {
|
|
253
|
-
controllerInstance['
|
|
259
|
+
controllerInstance['koaSendJson'](httpErr.data, httpErr.status);
|
|
254
260
|
}
|
|
255
261
|
else {
|
|
256
|
-
controllerInstance['
|
|
262
|
+
controllerInstance['koaSendStatus'](httpErr.status);
|
|
257
263
|
}
|
|
258
264
|
}
|
|
259
265
|
else if (err instanceof RecordNotFound) {
|
|
260
|
-
controllerInstance['
|
|
266
|
+
controllerInstance['koaSendStatus'](404);
|
|
261
267
|
}
|
|
262
268
|
else if (err instanceof DataIncompatibleWithDatabaseField) {
|
|
263
269
|
/**
|
|
264
270
|
* See comment at top of this method for philosophy of 400
|
|
265
271
|
*/
|
|
266
|
-
controllerInstance['
|
|
272
|
+
controllerInstance['koaSendStatus'](400);
|
|
267
273
|
}
|
|
268
274
|
else if (err instanceof ValidationError) {
|
|
269
275
|
if (this.validationErrorLoggingEnabled) {
|
|
@@ -275,7 +281,7 @@ suggested fix: "${convertRouteParams(path)}"
|
|
|
275
281
|
/**
|
|
276
282
|
* See comment at top of this method for philosophy of 400
|
|
277
283
|
*/
|
|
278
|
-
controllerInstance['
|
|
284
|
+
controllerInstance['koaSendStatus'](400);
|
|
279
285
|
}
|
|
280
286
|
else if (err instanceof OpenapiRequestValidationFailure) {
|
|
281
287
|
if (this.validationErrorLoggingEnabled) {
|
|
@@ -288,7 +294,7 @@ suggested fix: "${convertRouteParams(path)}"
|
|
|
288
294
|
/**
|
|
289
295
|
* See comment at top of this method for philosophy of 400
|
|
290
296
|
*/
|
|
291
|
-
controllerInstance['
|
|
297
|
+
controllerInstance['koaSendStatus'](400);
|
|
292
298
|
}
|
|
293
299
|
else if (err instanceof ParamValidationError) {
|
|
294
300
|
if (this.validationErrorLoggingEnabled) {
|
|
@@ -302,7 +308,7 @@ suggested fix: "${convertRouteParams(path)}"
|
|
|
302
308
|
/**
|
|
303
309
|
* See comment at top of this method for philosophy of 400
|
|
304
310
|
*/
|
|
305
|
-
controllerInstance['
|
|
311
|
+
controllerInstance['koaSendStatus'](400);
|
|
306
312
|
}
|
|
307
313
|
else if (err instanceof ParamValidationErrors) {
|
|
308
314
|
if (this.validationErrorLoggingEnabled) {
|
|
@@ -314,14 +320,14 @@ suggested fix: "${convertRouteParams(path)}"
|
|
|
314
320
|
/**
|
|
315
321
|
* See comment at top of this method for philosophy of 400
|
|
316
322
|
*/
|
|
317
|
-
controllerInstance['
|
|
323
|
+
controllerInstance['koaSendStatus'](400);
|
|
318
324
|
}
|
|
319
325
|
else {
|
|
320
326
|
PsychicApp.logWithLevel('error', util.inspect(err, { depth: ERROR_LOGGING_DEPTH }));
|
|
321
327
|
if (PsychicApp.getOrFail().specialHooks.serverError.length) {
|
|
322
328
|
try {
|
|
323
329
|
for (const hook of PsychicApp.getOrFail().specialHooks.serverError) {
|
|
324
|
-
await hook(err,
|
|
330
|
+
await hook(err, ctx);
|
|
325
331
|
}
|
|
326
332
|
}
|
|
327
333
|
catch (error) {
|
|
@@ -348,17 +354,17 @@ suggested fix: "${convertRouteParams(path)}"
|
|
|
348
354
|
}
|
|
349
355
|
}
|
|
350
356
|
}
|
|
351
|
-
_initializeController(ControllerClass,
|
|
352
|
-
return new ControllerClass(
|
|
357
|
+
_initializeController(ControllerClass, ctx, action) {
|
|
358
|
+
return new ControllerClass(ctx, {
|
|
353
359
|
action,
|
|
354
360
|
});
|
|
355
361
|
}
|
|
356
362
|
}
|
|
357
363
|
export class PsychicNestedRouter extends PsychicRouter {
|
|
358
364
|
router;
|
|
359
|
-
constructor(
|
|
360
|
-
super(
|
|
361
|
-
this.router =
|
|
365
|
+
constructor(koaApp, routeManager, { namespaces = [], } = {}) {
|
|
366
|
+
super(koaApp);
|
|
367
|
+
this.router = new KoaRouter();
|
|
362
368
|
this.currentNamespaces = namespaces;
|
|
363
369
|
this.routeManager = routeManager;
|
|
364
370
|
}
|
|
@@ -17,6 +17,7 @@ export default async function startPsychicServer({ app, port, sslCredentials, })
|
|
|
17
17
|
}
|
|
18
18
|
export function createPsychicHttpInstance(app, sslCredentials) {
|
|
19
19
|
const psychicApp = PsychicApp.getOrFail();
|
|
20
|
+
const callback = app.callback();
|
|
20
21
|
if (sslCredentials?.key && sslCredentials?.cert) {
|
|
21
22
|
return https.createServer({
|
|
22
23
|
key: fs.readFileSync(sslCredentials.key),
|
|
@@ -24,10 +25,13 @@ export function createPsychicHttpInstance(app, sslCredentials) {
|
|
|
24
25
|
ca: sslCredentials.ca?.map(filePath => fs.readFileSync(filePath)),
|
|
25
26
|
rejectUnauthorized: sslCredentials?.rejectUnauthorized,
|
|
26
27
|
...psychicApp.httpServerOptions,
|
|
27
|
-
},
|
|
28
|
+
},
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
30
|
+
callback);
|
|
28
31
|
}
|
|
29
32
|
else {
|
|
30
|
-
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
34
|
+
return http.createServer(psychicApp.httpServerOptions, callback);
|
|
31
35
|
}
|
|
32
36
|
}
|
|
33
37
|
function welcomeMessage({ port }) {
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { closeAllDbConnections } from '@rvoh/dream/db';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
2
|
+
import cors from '@koa/cors';
|
|
3
|
+
import Koa from 'koa';
|
|
4
|
+
import koaBodyparser from 'koa-bodyparser';
|
|
5
|
+
import conditional from 'koa-conditional-get';
|
|
6
|
+
import etag from 'koa-etag';
|
|
5
7
|
import logIfDevelopment from '../controller/helpers/logIfDevelopment.js';
|
|
6
8
|
import EnvInternal from '../helpers/EnvInternal.js';
|
|
7
9
|
import PsychicApp from '../psychic-app/index.js';
|
|
@@ -15,14 +17,14 @@ export default class PsychicServer {
|
|
|
15
17
|
static createPsychicHttpInstance(app, sslCredentials) {
|
|
16
18
|
return createPsychicHttpInstance(app, sslCredentials);
|
|
17
19
|
}
|
|
18
|
-
|
|
20
|
+
koaApp;
|
|
19
21
|
httpServer;
|
|
20
22
|
booted = false;
|
|
21
23
|
constructor() {
|
|
22
24
|
this.buildApp();
|
|
23
25
|
}
|
|
24
26
|
async routes() {
|
|
25
|
-
const r = new PsychicRouter(this.
|
|
27
|
+
const r = new PsychicRouter(this.koaApp);
|
|
26
28
|
await PsychicApp.getOrFail().routesCb(r);
|
|
27
29
|
return r.routes;
|
|
28
30
|
}
|
|
@@ -31,16 +33,19 @@ export default class PsychicServer {
|
|
|
31
33
|
return;
|
|
32
34
|
const psychicApp = PsychicApp.getOrFail();
|
|
33
35
|
this.setSecureDefaultHeaders();
|
|
34
|
-
this.
|
|
36
|
+
this.koaApp.use(async (ctx, next) => {
|
|
35
37
|
Object.keys(psychicApp.defaultResponseHeaders).forEach(key => {
|
|
36
|
-
|
|
38
|
+
ctx.set(key, psychicApp.defaultResponseHeaders[key]);
|
|
37
39
|
});
|
|
38
|
-
next();
|
|
40
|
+
await next();
|
|
39
41
|
});
|
|
40
42
|
for (const serverInitBeforeMiddlewareHook of PsychicApp.getOrFail().specialHooks
|
|
41
43
|
.serverInitBeforeMiddleware) {
|
|
42
44
|
await serverInitBeforeMiddlewareHook(this);
|
|
43
45
|
}
|
|
46
|
+
// ETag support (Express has this built-in, Koa needs middleware)
|
|
47
|
+
this.koaApp.use(conditional());
|
|
48
|
+
this.koaApp.use(etag());
|
|
44
49
|
this.initializeCors();
|
|
45
50
|
this.initializeJSON();
|
|
46
51
|
try {
|
|
@@ -69,29 +74,23 @@ export default class PsychicServer {
|
|
|
69
74
|
applyNotFoundMiddleware() {
|
|
70
75
|
if (!EnvInternal.isDevelopment)
|
|
71
76
|
return;
|
|
72
|
-
this.
|
|
73
|
-
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
// pick up the correct status code.
|
|
80
|
-
if (res.statusCode === 200)
|
|
81
|
-
res.status(404);
|
|
82
|
-
logIfDevelopment({ req, res, startTime: Date.now(), fallbackStatusCode: 404 });
|
|
83
|
-
// call next to let express handle sending the 404
|
|
84
|
-
next();
|
|
77
|
+
this.koaApp.use(async (ctx, next) => {
|
|
78
|
+
await next();
|
|
79
|
+
// Koa defaults to 404 for unmatched routes. If nothing set the body,
|
|
80
|
+
// log the 404 in development.
|
|
81
|
+
if (ctx.status === 404 && !ctx.body) {
|
|
82
|
+
logIfDevelopment({ ctx, startTime: Date.now(), fallbackStatusCode: 404 });
|
|
83
|
+
}
|
|
85
84
|
});
|
|
86
85
|
}
|
|
87
86
|
setSecureDefaultHeaders() {
|
|
88
|
-
|
|
89
|
-
this.
|
|
90
|
-
|
|
87
|
+
// Koa doesn't send x-powered-by by default, no need to disable it.
|
|
88
|
+
this.koaApp.use(async (ctx, next) => {
|
|
89
|
+
ctx.set('X-Content-Type-Options', 'nosniff');
|
|
91
90
|
if (EnvInternal.isProduction) {
|
|
92
|
-
|
|
91
|
+
ctx.set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
|
|
93
92
|
}
|
|
94
|
-
next();
|
|
93
|
+
await next();
|
|
95
94
|
});
|
|
96
95
|
}
|
|
97
96
|
// TODO: use config helper for fetching default port
|
|
@@ -104,7 +103,7 @@ export default class PsychicServer {
|
|
|
104
103
|
}
|
|
105
104
|
else {
|
|
106
105
|
const httpServer = await startPsychicServer({
|
|
107
|
-
app: this.
|
|
106
|
+
app: this.koaApp,
|
|
108
107
|
port: port || psychicApp.port,
|
|
109
108
|
sslCredentials: PsychicApp.getOrFail().sslCredentials,
|
|
110
109
|
});
|
|
@@ -146,26 +145,24 @@ export default class PsychicServer {
|
|
|
146
145
|
await this.boot();
|
|
147
146
|
let server;
|
|
148
147
|
await new Promise(accept => {
|
|
149
|
-
server = this.
|
|
148
|
+
server = this.koaApp.listen(port, () => accept({}));
|
|
150
149
|
});
|
|
151
150
|
await block();
|
|
152
151
|
server.close();
|
|
153
152
|
return true;
|
|
154
153
|
}
|
|
155
154
|
buildApp() {
|
|
156
|
-
this.
|
|
157
|
-
this.expressApp.use(cookieParser.default());
|
|
155
|
+
this.koaApp = new Koa();
|
|
158
156
|
}
|
|
159
157
|
initializeCors() {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
cors.default(PsychicApp.getOrFail().corsOptions));
|
|
158
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
|
|
159
|
+
this.koaApp.use(cors(PsychicApp.getOrFail().corsOptions));
|
|
163
160
|
}
|
|
164
161
|
initializeJSON() {
|
|
165
|
-
this.
|
|
162
|
+
this.koaApp.use(koaBodyparser(PsychicApp.getOrFail().jsonOptions));
|
|
166
163
|
}
|
|
167
164
|
async buildRoutes() {
|
|
168
|
-
const r = new PsychicRouter(this.
|
|
165
|
+
const r = new PsychicRouter(this.koaApp);
|
|
169
166
|
await PsychicApp.getOrFail().routesCb(r);
|
|
170
167
|
r.commit();
|
|
171
168
|
}
|
|
@@ -3,31 +3,28 @@ import cookieMaxAgeFromCookieOpts from '../helpers/cookieMaxAgeFromCookieOpts.js
|
|
|
3
3
|
import EnvInternal from '../helpers/EnvInternal.js';
|
|
4
4
|
import PsychicApp from '../psychic-app/index.js';
|
|
5
5
|
export default class Session {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
this.req = req;
|
|
10
|
-
this.res = res;
|
|
6
|
+
ctx;
|
|
7
|
+
constructor(ctx) {
|
|
8
|
+
this.ctx = ctx;
|
|
11
9
|
}
|
|
12
10
|
getCookie(name) {
|
|
13
|
-
const
|
|
14
|
-
const value = cookies[name];
|
|
11
|
+
const value = this.ctx.cookies.get(name);
|
|
15
12
|
if (value)
|
|
16
13
|
return InternalEncrypt.decryptCookie(value);
|
|
17
14
|
return null;
|
|
18
15
|
}
|
|
19
16
|
setCookie(name, data, opts = {}) {
|
|
20
|
-
this.
|
|
21
|
-
secure: EnvInternal.isProduction,
|
|
22
|
-
httpOnly: true,
|
|
17
|
+
this.ctx.cookies.set(name, InternalEncrypt.encryptCookie(data), {
|
|
23
18
|
...opts,
|
|
19
|
+
secure: opts.secure ?? EnvInternal.isProduction,
|
|
20
|
+
httpOnly: opts.httpOnly ?? true,
|
|
24
21
|
maxAge: opts.maxAge
|
|
25
22
|
? cookieMaxAgeFromCookieOpts(opts.maxAge)
|
|
26
|
-
: PsychicApp.getOrFail().cookieOptions?.maxAge,
|
|
23
|
+
: (PsychicApp.getOrFail().cookieOptions?.maxAge ?? cookieMaxAgeFromCookieOpts()),
|
|
27
24
|
});
|
|
28
25
|
}
|
|
29
26
|
clearCookie(name) {
|
|
30
|
-
this.
|
|
27
|
+
this.ctx.cookies.set(name, '', { maxAge: 0 });
|
|
31
28
|
}
|
|
32
29
|
daysToMilliseconds(numDays) {
|
|
33
30
|
return numDays * 60 * 60 * 24 * 1000;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
export default function logIfDevelopment({
|
|
3
|
-
|
|
4
|
-
res: Response;
|
|
1
|
+
import Koa from 'koa';
|
|
2
|
+
export default function logIfDevelopment({ ctx, startTime, fallbackStatusCode, }: {
|
|
3
|
+
ctx: Koa.Context;
|
|
5
4
|
startTime: number;
|
|
6
5
|
fallbackStatusCode?: number;
|
|
7
6
|
}): void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Dream } from '@rvoh/dream';
|
|
2
2
|
import { OpenapiSchemaBody } from '@rvoh/dream/openapi';
|
|
3
3
|
import { DreamParamSafeAttributes, DreamParamSafeColumnNames, SerializerRendererOpts, StrictInterface } from '@rvoh/dream/types';
|
|
4
|
-
import
|
|
4
|
+
import Koa from 'koa';
|
|
5
5
|
import { ControllerHook } from '../controller/hooks.js';
|
|
6
6
|
import { HttpStatusCodeInt, HttpStatusSymbol } from '../error/http/status-codes.js';
|
|
7
7
|
import OpenapiEndpointRenderer from '../openapi-renderer/endpoint.js';
|
|
@@ -101,13 +101,13 @@ export default class PsychicController {
|
|
|
101
101
|
* and non-controllers
|
|
102
102
|
*/
|
|
103
103
|
get isPsychicControllerInstance(): boolean;
|
|
104
|
-
|
|
105
|
-
res: Response;
|
|
104
|
+
ctx: Koa.Context;
|
|
106
105
|
session: Session;
|
|
107
106
|
action: string;
|
|
108
107
|
renderOpts: SerializerRendererOpts;
|
|
109
108
|
private startTime;
|
|
110
|
-
|
|
109
|
+
private _responseSent;
|
|
110
|
+
constructor(ctx: Koa.Context, { action, }: {
|
|
111
111
|
action: string;
|
|
112
112
|
});
|
|
113
113
|
/**
|
|
@@ -384,9 +384,20 @@ export default class PsychicController {
|
|
|
384
384
|
* @param data - the data to validate and render
|
|
385
385
|
*/
|
|
386
386
|
private validateAndRenderJsonResponse;
|
|
387
|
-
private
|
|
388
|
-
|
|
389
|
-
|
|
387
|
+
private koaSendJson;
|
|
388
|
+
/**
|
|
389
|
+
* @internal
|
|
390
|
+
*
|
|
391
|
+
* Attempts to retrieve or create a cached fast-json-stringify function
|
|
392
|
+
* for the current endpoint and status code. Returns undefined if no
|
|
393
|
+
* OpenAPI schema exists or if the controller's globalName is not set.
|
|
394
|
+
*
|
|
395
|
+
* @param statusCode - the HTTP status code
|
|
396
|
+
* @returns A stringify function, or undefined if no schema exists
|
|
397
|
+
*/
|
|
398
|
+
private getFastJsonStringifyFunction;
|
|
399
|
+
private koaSendStatus;
|
|
400
|
+
private koaRedirect;
|
|
390
401
|
private logIfDevelopment;
|
|
391
402
|
protected defaultSerializerPassthrough: SerializerResult;
|
|
392
403
|
/**
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { CustomCookieMaxAgeOptions } from '../psychic-app/index.js';
|
|
2
|
-
export default function cookieMaxAgeFromCookieOpts(cookieOpts
|
|
2
|
+
export default function cookieMaxAgeFromCookieOpts(cookieOpts?: CustomCookieMaxAgeOptions): number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default function toJson<T>(data: T
|
|
1
|
+
export default function toJson<T>(data: T): string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Ajv, type JSONSchemaType, type ValidateFunction } from 'ajv';
|
|
1
|
+
import { Ajv, type ErrorObject, type JSONSchemaType, type ValidateFunction } from 'ajv';
|
|
2
2
|
/**
|
|
3
3
|
* @internal
|
|
4
4
|
*
|
|
@@ -56,6 +56,10 @@ export declare function validateObject<T = unknown>(data: unknown, schema: JSONS
|
|
|
56
56
|
* @returns A validator function that can be reused for multiple validations
|
|
57
57
|
*/
|
|
58
58
|
export declare function createValidator<T = unknown>(schema: JSONSchemaType<T> | object, options?: ValidateOpenapiSchemaOptions): ValidateFunction<T>;
|
|
59
|
+
/**
|
|
60
|
+
* Formats AJV errors into a more readable format
|
|
61
|
+
*/
|
|
62
|
+
export declare function formatAjvErrors(ajvErrors: ErrorObject[]): ValidationError[];
|
|
59
63
|
export interface ValidationResult<T = unknown> {
|
|
60
64
|
isValid: boolean;
|
|
61
65
|
data?: T;
|
|
@@ -98,6 +98,27 @@ export default class OpenapiPayloadValidator {
|
|
|
98
98
|
* @param statusCode - the status code used to render
|
|
99
99
|
*/
|
|
100
100
|
validateOpenapiResponseBody(data: any, statusCode: number): void;
|
|
101
|
+
/**
|
|
102
|
+
* @internal
|
|
103
|
+
*
|
|
104
|
+
* Retrieves the OpenAPI response schema for a given status code.
|
|
105
|
+
* Returns undefined if no schema is defined for this endpoint/status code.
|
|
106
|
+
*
|
|
107
|
+
* @param statusCode - the HTTP status code
|
|
108
|
+
* @returns The response schema, or undefined if not found
|
|
109
|
+
*/
|
|
110
|
+
getResponseSchema(statusCode: number): object | undefined;
|
|
111
|
+
/**
|
|
112
|
+
* @internal
|
|
113
|
+
*
|
|
114
|
+
* Retrieves the OpenAPI response schema for a given status code with
|
|
115
|
+
* all components merged in. This is the schema format needed by
|
|
116
|
+
* fast-json-stringify and AJV validators.
|
|
117
|
+
*
|
|
118
|
+
* @param statusCode - the HTTP status code
|
|
119
|
+
* @returns The response schema with components, or undefined if not found
|
|
120
|
+
*/
|
|
121
|
+
getResponseSchemaWithComponents(statusCode: number): object | undefined;
|
|
101
122
|
/**
|
|
102
123
|
* @internal
|
|
103
124
|
*
|
|
@@ -137,6 +158,26 @@ export default class OpenapiPayloadValidator {
|
|
|
137
158
|
* @param headers - the request headers
|
|
138
159
|
*/
|
|
139
160
|
private validateOrFail;
|
|
161
|
+
/**
|
|
162
|
+
* @internal
|
|
163
|
+
*
|
|
164
|
+
* Generates a cache key for the validator based on controller, action, openapiName, and target.
|
|
165
|
+
*
|
|
166
|
+
* @param target - the validation target (one of: 'requestBody', 'query', 'headers', 'responseBody')
|
|
167
|
+
* @returns cache key string
|
|
168
|
+
*/
|
|
169
|
+
private getCacheKey;
|
|
170
|
+
/**
|
|
171
|
+
* @internal
|
|
172
|
+
*
|
|
173
|
+
* Compiles a validator using AJV and caches it for future use.
|
|
174
|
+
*
|
|
175
|
+
* @param cacheKey - the cache key for this validator
|
|
176
|
+
* @param schema - the schema to compile
|
|
177
|
+
* @param options - AJV options
|
|
178
|
+
* @returns compiled validator function
|
|
179
|
+
*/
|
|
180
|
+
private compileAndCacheValidator;
|
|
140
181
|
/**
|
|
141
182
|
* @internal
|
|
142
183
|
*
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*
|
|
4
|
+
* Retrieves a cached stringify function if it exists.
|
|
5
|
+
*
|
|
6
|
+
* @param cacheKey - The cache key identifying the stringify function
|
|
7
|
+
* @returns The cached stringify function, or undefined if not found
|
|
8
|
+
*/
|
|
9
|
+
export declare function getCachedStringify(cacheKey: string): ((data: any) => string) | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* @internal
|
|
12
|
+
*
|
|
13
|
+
* Stores a compiled stringify function in the cache.
|
|
14
|
+
*
|
|
15
|
+
* @param cacheKey - The cache key identifying the stringify function
|
|
16
|
+
* @param stringifyFn - The compiled fast-json-stringify function to cache
|
|
17
|
+
*/
|
|
18
|
+
export declare function cacheStringify(cacheKey: string, stringifyFn: (data: any) => string): void;
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*
|
|
22
|
+
* Clears a specific stringify function from the cache.
|
|
23
|
+
* Used in test environments to ensure test isolation.
|
|
24
|
+
*
|
|
25
|
+
* @param cacheKey - The cache key identifying the stringify function to clear
|
|
26
|
+
*/
|
|
27
|
+
export declare function _testOnlyClearStringify(cacheKey: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* @internal
|
|
30
|
+
*
|
|
31
|
+
* Clears all stringify functions from the cache.
|
|
32
|
+
* Used in test environments to ensure test isolation.
|
|
33
|
+
*/
|
|
34
|
+
export declare function _testOnlyClearStringifyCache(): void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ValidateFunction } from 'ajv';
|
|
2
|
+
/**
|
|
3
|
+
* @internal
|
|
4
|
+
*
|
|
5
|
+
* Retrieves a cached validator function if it exists.
|
|
6
|
+
*
|
|
7
|
+
* @param cacheKey - The cache key identifying the validator
|
|
8
|
+
* @returns The cached validator function, or undefined if not found
|
|
9
|
+
*/
|
|
10
|
+
export declare function getCachedValidator(cacheKey: string): ValidateFunction | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*
|
|
14
|
+
* Stores a compiled validator function in the cache.
|
|
15
|
+
*
|
|
16
|
+
* @param cacheKey - The cache key identifying the validator
|
|
17
|
+
* @param validator - The compiled AJV validator function to cache
|
|
18
|
+
*/
|
|
19
|
+
export declare function cacheValidator(cacheKey: string, validator: ValidateFunction): void;
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*
|
|
23
|
+
* Clears a specific validator from the cache.
|
|
24
|
+
* Used in test environments to ensure test isolation.
|
|
25
|
+
*
|
|
26
|
+
* @param cacheKey - The cache key identifying the validator to clear
|
|
27
|
+
*/
|
|
28
|
+
export declare function _testOnlyClearValidator(cacheKey: string): void;
|
|
29
|
+
/**
|
|
30
|
+
* @internal
|
|
31
|
+
*
|
|
32
|
+
* Clears all validators from the cache.
|
|
33
|
+
* Used in test environments to ensure test isolation.
|
|
34
|
+
*/
|
|
35
|
+
export declare function _testOnlyClearValidatorCache(): void;
|