Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/lib/api/framework.ts
Views: 687
/*1We import and export from here, so we can put some wrapping around these.2*/34export { z } from "zod";5import {6apiRoute as apiRoute0,7apiRouteOperation as apiRouteOperation0,8} from "next-rest-framework";910export function apiRoute(obj) {11if (process.env.NODE_ENV != "production" && process.env.COCALC_DISABLE_API_VALIDATION != 'yes') {12// this actually does all the clever validation, etc.13return apiRoute0(obj);14} else {15// this IGNORES all validation etc and just uses the original handler,16// thus completely skipping next-rest-framework.17// NOTE: We are assuming there is at most one handler defined per route!18// That is the case in the current codebase. I.e., our current handler19// function internally handles all of POST, GET, etc. in one function,20// and apiRoute is only called with one distinct handler.21for (const k in obj) {22return obj[k].handler;23}24}25}2627export { apiRouteOperation0 as apiRouteOperation };2829/*30// When we want to check validation in production and log31// warnings, we'll use something based on this.3233export function apiRouteOperation(obj): ReturnType<typeof apiRouteOperation0> {34if (process.env.NODE_ENV != "production") {35return apiRouteOperation0(obj);36}37// In production mode we disable all validation, since38// we do not want to (1) slow things down, and39// (2) break anything.40// TODO: once things seem to work well in dev mode,41// check validation in production and log failures42// as WARNINGS to our database. Only when this is stable43// with zero errors for a while do we switch to actual44// runtime validation.4546const x = apiRouteOperation0(obj);47return neuterApiRouteOperation(x);48}4950// The output of apiRouteOperation0 has methods:51// input52// outputs53// middleware54// handler55// which get chained together, e.g.,56// x.input(...).outputs(...).middleware(...).handler(...)57// to define how the route is checked and handled.58// We have to fake that in such a way that input and outputs59// are ignored, but the rest work.60// The following takes61function neuterApiRouteOperation(x) {62return {63...x,64input: () => x,65outputs: () => x,66middleware: (...args) => {67const y = x.middleware(...args);68return neuterApiRouteOperation(y);69},70handler: (...args) => {71const y = x.handler(...args);72return neuterApiRouteOperation(y);73},74};75}76*/777879