CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/lib/api/framework.ts
Views: 687
1
/*
2
We import and export from here, so we can put some wrapping around these.
3
*/
4
5
export { z } from "zod";
6
import {
7
apiRoute as apiRoute0,
8
apiRouteOperation as apiRouteOperation0,
9
} from "next-rest-framework";
10
11
export function apiRoute(obj) {
12
if (process.env.NODE_ENV != "production" && process.env.COCALC_DISABLE_API_VALIDATION != 'yes') {
13
// this actually does all the clever validation, etc.
14
return apiRoute0(obj);
15
} else {
16
// this IGNORES all validation etc and just uses the original handler,
17
// thus completely skipping next-rest-framework.
18
// NOTE: We are assuming there is at most one handler defined per route!
19
// That is the case in the current codebase. I.e., our current handler
20
// function internally handles all of POST, GET, etc. in one function,
21
// and apiRoute is only called with one distinct handler.
22
for (const k in obj) {
23
return obj[k].handler;
24
}
25
}
26
}
27
28
export { apiRouteOperation0 as apiRouteOperation };
29
30
/*
31
// When we want to check validation in production and log
32
// warnings, we'll use something based on this.
33
34
export function apiRouteOperation(obj): ReturnType<typeof apiRouteOperation0> {
35
if (process.env.NODE_ENV != "production") {
36
return apiRouteOperation0(obj);
37
}
38
// In production mode we disable all validation, since
39
// we do not want to (1) slow things down, and
40
// (2) break anything.
41
// TODO: once things seem to work well in dev mode,
42
// check validation in production and log failures
43
// as WARNINGS to our database. Only when this is stable
44
// with zero errors for a while do we switch to actual
45
// runtime validation.
46
47
const x = apiRouteOperation0(obj);
48
return neuterApiRouteOperation(x);
49
}
50
51
// The output of apiRouteOperation0 has methods:
52
// input
53
// outputs
54
// middleware
55
// handler
56
// which get chained together, e.g.,
57
// x.input(...).outputs(...).middleware(...).handler(...)
58
// to define how the route is checked and handled.
59
// We have to fake that in such a way that input and outputs
60
// are ignored, but the rest work.
61
// The following takes
62
function neuterApiRouteOperation(x) {
63
return {
64
...x,
65
input: () => x,
66
outputs: () => x,
67
middleware: (...args) => {
68
const y = x.middleware(...args);
69
return neuterApiRouteOperation(y);
70
},
71
handler: (...args) => {
72
const y = x.handler(...args);
73
return neuterApiRouteOperation(y);
74
},
75
};
76
}
77
*/
78
79