2022-03-23 14:41:12 +00:00
|
|
|
import { Feature, Manifest } from "../types";
|
|
|
|
import { Express, NextFunction, Request, Response } from "express";
|
|
|
|
import { System } from "../system";
|
|
|
|
|
|
|
|
export type EndpointRequest = {
|
|
|
|
method: string;
|
|
|
|
path: string;
|
|
|
|
query: { [key: string]: string };
|
|
|
|
headers: { [key: string]: string };
|
|
|
|
body: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type EndpointResponse = {
|
|
|
|
status: number;
|
|
|
|
headers?: { [key: string]: string };
|
|
|
|
body: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type EndpointHook = {
|
2022-03-27 09:26:13 +00:00
|
|
|
http?: EndPointDef | EndPointDef[];
|
2022-03-23 14:41:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type EndPointDef = {
|
2022-03-27 09:26:13 +00:00
|
|
|
method?: "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "ANY";
|
2022-03-23 14:41:12 +00:00
|
|
|
path: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class EndpointFeature implements Feature<EndpointHook> {
|
|
|
|
private app: Express;
|
2022-03-27 07:55:29 +00:00
|
|
|
private prefix: string;
|
2022-03-23 14:41:12 +00:00
|
|
|
|
2022-03-27 07:55:29 +00:00
|
|
|
constructor(app: Express, prefix: string) {
|
2022-03-23 14:41:12 +00:00
|
|
|
this.app = app;
|
2022-03-27 07:55:29 +00:00
|
|
|
this.prefix = prefix;
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
apply(system: System<EndpointHook>): void {
|
|
|
|
this.app.use((req: Request, res: Response, next: NextFunction) => {
|
2022-03-27 07:55:29 +00:00
|
|
|
if (!req.path.startsWith(this.prefix)) {
|
2022-03-23 14:41:12 +00:00
|
|
|
return next();
|
|
|
|
}
|
2022-03-27 07:55:29 +00:00
|
|
|
console.log("Endpoint request", req.path);
|
2022-03-23 14:41:12 +00:00
|
|
|
Promise.resolve()
|
|
|
|
.then(async () => {
|
2022-03-27 09:26:13 +00:00
|
|
|
// Iterate over all loaded plugins
|
2022-03-23 14:41:12 +00:00
|
|
|
for (const [plugName, plug] of system.loadedPlugs.entries()) {
|
|
|
|
const manifest = plug.manifest;
|
|
|
|
if (!manifest) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-27 09:26:13 +00:00
|
|
|
const functions = manifest.functions;
|
|
|
|
console.log("Checking plug", plugName);
|
|
|
|
let prefix = `${this.prefix}/${plugName}`;
|
|
|
|
if (!req.path.startsWith(prefix)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const [name, functionDef] of Object.entries(functions)) {
|
|
|
|
if (!functionDef.http) {
|
2022-03-23 14:41:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-27 09:26:13 +00:00
|
|
|
let endpoints = Array.isArray(functionDef.http)
|
|
|
|
? functionDef.http
|
|
|
|
: [functionDef.http];
|
|
|
|
console.log(endpoints);
|
|
|
|
for (const { path, method } of endpoints) {
|
2022-03-23 14:41:12 +00:00
|
|
|
let prefixedPath = `${prefix}${path}`;
|
2022-03-27 09:26:13 +00:00
|
|
|
if (
|
|
|
|
prefixedPath === req.path &&
|
|
|
|
((method || "GET") === req.method || method === "ANY")
|
|
|
|
) {
|
2022-03-23 14:41:12 +00:00
|
|
|
try {
|
2022-03-27 09:26:13 +00:00
|
|
|
const response: EndpointResponse = await plug.invoke(name, [
|
|
|
|
{
|
|
|
|
path: req.path,
|
|
|
|
method: req.method,
|
|
|
|
body: req.body,
|
|
|
|
query: req.query,
|
|
|
|
headers: req.headers,
|
|
|
|
} as EndpointRequest,
|
|
|
|
]);
|
2022-03-23 14:41:12 +00:00
|
|
|
let resp = res.status(response.status);
|
|
|
|
if (response.headers) {
|
|
|
|
for (const [key, value] of Object.entries(
|
|
|
|
response.headers
|
|
|
|
)) {
|
|
|
|
resp = resp.header(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.send(response.body);
|
|
|
|
return;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error("Error executing function", e);
|
|
|
|
res.status(500).send(e.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
next(e);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
validateManifest(manifest: Manifest<EndpointHook>): string[] {
|
|
|
|
let errors = [];
|
2022-03-27 09:26:13 +00:00
|
|
|
for (const [name, functionDef] of Object.entries(manifest.functions)) {
|
|
|
|
if (!functionDef.http) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let endpoints = Array.isArray(functionDef.http)
|
|
|
|
? functionDef.http
|
|
|
|
: [functionDef.http];
|
|
|
|
for (let { path, method } of endpoints) {
|
2022-03-23 14:41:12 +00:00
|
|
|
if (!path) {
|
|
|
|
errors.push("Path not defined for endpoint");
|
|
|
|
}
|
2022-03-27 09:26:13 +00:00
|
|
|
if (
|
|
|
|
method &&
|
|
|
|
["GET", "POST", "PUT", "DELETE", "ANY"].indexOf(method) === -1
|
|
|
|
) {
|
2022-03-23 14:41:12 +00:00
|
|
|
errors.push(
|
|
|
|
`Invalid method ${method} for end point with with ${path}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
}
|