Skip to content

Commit

Permalink
refactor: apigw
Browse files Browse the repository at this point in the history
  • Loading branch information
vighnesh153 committed Nov 16, 2024
1 parent a059548 commit a58f596
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 73 deletions.
27 changes: 12 additions & 15 deletions tools-deno/apigw-vighnesh153/deno.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
{
"tasks": {
"prepare": "./prepare.sh",
"deploy:dev": "deployctl deploy --env=STAGE=dev --project=api-vighnesh153-dev",
"deploy:prod": "deployctl deploy --env=STAGE=prod --project=api-vighnesh153-prod"
},
"deploy": {
"include": [".", ".local"],
"entrypoint": "main.ts",
"prod": true
},
"imports": {
"@aws-sdk/client-lambda": "npm:@aws-sdk/[email protected]",
"@std/assert": "jsr:@std/assert@1",
"@vighnesh153/tools": "jsr:@vighnesh153/tools@^0.1.13"
}
"tasks": {
"deploy": "deployctl deploy --project=apigw-vighnesh153"
},
"deploy": {
"entrypoint": "main.ts",
"prod": true
},
"imports": {
"@aws-sdk/client-lambda": "npm:@aws-sdk/[email protected]",
"@std/assert": "jsr:@std/assert@1",
"@vighnesh153/tools": "jsr:@vighnesh153/[email protected]"
}
}
10 changes: 5 additions & 5 deletions tools-deno/apigw-vighnesh153/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 2 additions & 53 deletions tools-deno/apigw-vighnesh153/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ import {

import { HttpHeaderKeys, HttpHeaderValues, not } from "@vighnesh153/tools";
import {
constructHttpApiLambdaName,
DEFAULT_AWS_REGION,
isValidLambdaMethod,
isValidStageType,
LambdaFunctionConfig,
type LambdaFunctionName,
LambdaFunctionNames,
type LambdaMethodType,
type LambdaRequestPayload,
type LambdaResponsePayload,
type StageType,
} from "@vighnesh153/tools/vighnesh153";

const acceptableOriginsForOptions = [
Expand All @@ -35,8 +30,6 @@ const client = new LambdaClient({
region: DEFAULT_AWS_REGION,
});

const STAGE = Deno.env.get("STAGE") ?? "dev";

const MAX_CONTENT_LENGTH = 10_000; // 20 KB

function isJsonRequest(req: Request): boolean {
Expand Down Expand Up @@ -108,20 +101,6 @@ Deno.serve(async (req, _connInfo) => {
});
}

if (!isValidStageType(STAGE)) {
console.error(`Stage is not configured in the project.`);
return new Response(
JSON.stringify({ error: "Stage is not configured." }),
{
status: 500,
headers: addCorsHeaders({
[HttpHeaderKeys.contentType]:
HttpHeaderValues.contentType.applicationJson,
}, req),
},
);
}

const method = req.method;
const url = new URL(req.url);
const headers = convertHeaders(req);
Expand All @@ -140,25 +119,6 @@ Deno.serve(async (req, _connInfo) => {
functionName,
) => url.pathname === `/${functionName}`) ?? null;

if (!isValidLambdaMethod(method)) {
console.log(
`Received request with unsupported http method:`,
method,
` with headers:`,
headers,
);
return new Response(
JSON.stringify({ error: "Unsupported http method", method }),
{
status: 400,
headers: addCorsHeaders({
[HttpHeaderKeys.contentType]:
HttpHeaderValues.contentType.applicationJson,
}, req),
},
);
}

if (functionName === null) {
console.log(
"Received request for unrecognized function name:",
Expand Down Expand Up @@ -215,7 +175,6 @@ Deno.serve(async (req, _connInfo) => {
}

const payload: LambdaRequestPayload = {
method,
headers,
body,
filterParams: convertUrlSearchParams(url.searchParams),
Expand All @@ -242,8 +201,6 @@ Deno.serve(async (req, _connInfo) => {
console.log("Auth required for", functionName);
const userInfoResponse = await invokeLambdaFunction({
functionName: "getUser",
method: "get",
stage: STAGE,
payload: { headers },
});

Expand Down Expand Up @@ -301,8 +258,6 @@ Deno.serve(async (req, _connInfo) => {
// invoke the actual requested function
const lambdaResponse = await invokeLambdaFunction({
functionName,
method,
stage: STAGE,
payload,
});

Expand All @@ -313,21 +268,15 @@ Deno.serve(async (req, _connInfo) => {
});

async function invokeLambdaFunction<TReq>(
{ functionName, stage, payload, method }: {
{ functionName, payload }: {
functionName: LambdaFunctionName;
stage: StageType;
payload: TReq;
method: LambdaMethodType;
},
): Promise<{ headers: Headers; status: number; data: string | null }> {
const encodedBody = new TextEncoder().encode(JSON.stringify(payload));

const input: InvocationRequest = {
FunctionName: constructHttpApiLambdaName({
functionIdentifier: functionName,
method,
stage,
}),
FunctionName: functionName,
InvocationType: InvocationType.RequestResponse,
LogType: LogType.Tail,
Payload: encodedBody,
Expand Down

0 comments on commit a58f596

Please sign in to comment.