Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add middleware for APIs and Commands #281

Merged
merged 7 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"internalConsoleOptions": "openOnSessionStart",
"cwd": "${workspaceFolder}/apps/test-app/",
"outFiles": ["${workspaceFolder}/**/*.js", "!**/node_modules/**"]
}
},
{
"type": "node",
"name": "synth apps/tests/aws-runtime-cdk",
Expand Down
8 changes: 1 addition & 7 deletions packages/@eventual/aws-runtime/src/handlers/api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,7 @@ export default async function (
let headers: Record<string, string>;
response.body;

if (typeof response.headers?.forEach === "function") {
headers = {};
// handle node fetch API
response.headers.forEach((value, key) => (headers[key] = value));
sam-goodwin marked this conversation as resolved.
Show resolved Hide resolved
} else {
headers = (response.headers as Record<string, string>) ?? {};
}
headers = (response.headers as Record<string, string>) ?? {};

let responseBody: Buffer;
if (typeof response.body === "string") {
Expand Down
2 changes: 1 addition & 1 deletion packages/@eventual/compiler/bin/eventual-infer.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node -r "ts-node/register"
#!/usr/bin/env node

import { infer } from "../lib/cjs/eventual-infer.js";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,10 @@ var CommandType;
CommandType2["StartWorkflow"] = "StartWorkflow";
})(CommandType || (CommandType = {}));

function isSourceLocation(a) {
return a && typeof a === "object" && typeof a.fileName === "string" && typeof a.exportName === "string";
}

var import_ulidx2 = __toESM(require_dist2(), 1);
var ExecutionStatus;
(function(ExecutionStatus2) {
Expand All @@ -626,41 +630,76 @@ function e({ base: t = "", routes: n = [] } = {}) {
}
var itty_router_min_default = { Router: e };

function command(...args) {
const [sourceLocation, name, options, handler] = parseCommandArgs(args);
const command2 = {
kind: "Command",
name,
handler,
sourceLocation,
...options
};
commands.push(command2);
return command2;
}
function parseCommandArgs(args) {
return [
args.find(isSourceLocation),
args.find((a) => typeof a === "string"),
args.find((a) => typeof a === "object" && !isSourceLocation(a)),
args.find((a) => typeof a === "function")
];
}

var router = itty_router_min_default.Router();
var api = new Proxy({}, {
get: (_, method) => {
if (method === "routes" || method === "handle") {
return router[method];
} else {
return (...args) => {
const [sourceLocation, path, routeProps, handler] = typeof args[0] === "object" ? typeof args[3] === "function" ? args : [
args[0],
args[1],
void 0,
args[2]
] : typeof args[2] === "function" ? [
void 0,
args[0],
args[1],
args[2]
] : [void 0, args[0], void 0, args[1]];
const command = {
kind: "Command",
handler,
memorySize: routeProps?.memorySize,
method: method.toUpperCase(),
name: path,
path: typeof args[0] === "string" ? args[0] : args[1],
sourceLocation,
timeout: routeProps?.timeout,
passThrough: true
var api = createRouter([]);
function createRouter(middlewares) {
return new Proxy({}, {
get: (_, method) => {
if (method === "routes" || method === "handle") {
return router[method];
} else if (method === "use") {
return (middleware) => createRouter([...middlewares ?? [], middleware]);
} else if (method === "command") {
return (...args) => {
const [sourceLocation, name, options, handler] = parseCommandArgs(args);
return command(sourceLocation, name, {
...options ?? {},
middlewares
}, handler);
};
commands.push(command);
return router[method](path, command.handler);
};
} else {
return (...args) => {
const [sourceLocation, path, routeProps, handler] = typeof args[0] === "object" ? typeof args[3] === "function" ? args : [
args[0],
args[1],
void 0,
args[2]
] : typeof args[2] === "function" ? [
void 0,
args[0],
args[1],
args[2]
] : [void 0, args[0], void 0, args[1]];
const command2 = {
kind: "Command",
handler,
memorySize: routeProps?.memorySize,
method: method.toUpperCase(),
name: path,
path: typeof args[0] === "string" ? args[0] : args[1],
sourceLocation,
timeout: routeProps?.timeout,
middlewares,
passThrough: true
};
commands.push(command2);
return router[method](path, command2.handler);
};
}
}
}
});
});
}

var LogLevel;
(function(LogLevel2) {
Expand Down
Loading