Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
106 changes: 91 additions & 15 deletions packages/aws/src/api-gateway.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,102 @@
import { registerResource } from "@notation/core";
import { ApiGatewayHandler } from "./lambda";
import { AwsResourceGroup } from "./core";
import { ApiGatewayHandler, fn } from "./lambda";

export type ApiOptions = {
export type ApiConfig = {
name: string;
};

export const api = (apiOpts: ApiOptions) => {
const apiResource = registerResource("api", apiOpts);
return {
get: (path: string, handler: ApiGatewayHandler) => {
registerResource("api-route", {
api: apiResource.id,
service: "aws/api-gateway",
path,
method: "GET",
// @ts-ignore – property exists at runtime
handler: handler.id,
});
export const api = (config: ApiConfig) => {
const apiGroup = new AwsResourceGroup({ type: "api", config: config });

const apiGateway = apiGroup.createResource({
type: "api-gateway",
});

apiGroup.createResource({
type: "api-gateway/stage",
dependencies: {
routerId: apiGateway.id,
},
});

return apiGroup;
};

export const router = (apiGroup: ReturnType<typeof api>) => {
const createRouteCallback =
(method: string) => (path: string, handler: ApiGatewayHandler) => {
return route(apiGroup, method, path, handler);
};
return {
get: createRouteCallback("GET"),
post: createRouteCallback("POST"),
put: createRouteCallback("PUT"),
patch: createRouteCallback("PATCH"),
delete: createRouteCallback("DELETE"),
};
};

export const route = (
apiGroup: ReturnType<typeof api>,
method: string,
path: string,
handler: ApiGatewayHandler,
) => {
const apiGateway = apiGroup.findResourceByType("api-gateway")!;

// at compile time becomes infra module
const fnGroup = handler as any as ReturnType<typeof fn>;

const routeGroup = new AwsResourceGroup({
type: "route",
dependencies: {
router: apiGroup.id,
fn: fnGroup.id,
},
config: {
service: "aws/api-gateway",
path,
method,
},
});

let integration;

const lambda = fnGroup.findResourceByType("lambda")!;
const permission = fnGroup.findResourceByType("lambda/permission");
integration = fnGroup.findResourceByType("lambda/integration");

if (!integration) {
integration = fnGroup.createResource({
type: "lambda/integration",
dependencies: {
apiGatewayId: apiGateway.id,
lambdaId: lambda.id,
},
});
}

if (!permission) {
fnGroup.createResource({
type: "lambda/permission",
dependencies: {
apiGatewayId: apiGateway.id,
lambdaId: lambda.id,
},
});
}

routeGroup.createResource({
type: "api-gateway/route",
dependencies: {
apiGatewayId: apiGateway.id,
integrationId: integration.id,
},
});

return routeGroup;
};

export const json = (result: any) => ({
body: JSON.stringify(result),
statusCode: 200,
Expand Down
8 changes: 8 additions & 0 deletions packages/aws/src/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ResourceGroup, ResourceGroupOptions } from "@notation/core";

export class AwsResourceGroup<Config extends {}> extends ResourceGroup<Config> {
constructor(opts: ResourceGroupOptions<Config>) {
super(opts);
this.platform = "aws";
}
}
25 changes: 25 additions & 0 deletions packages/aws/src/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AwsResourceGroup } from "./core";
import type {
Context,
APIGatewayProxyEvent,
Expand All @@ -18,3 +19,27 @@ export type ApiGatewayHandler = (
export const handle = {
apiRequest: (handler: ApiGatewayHandler): ApiGatewayHandler => handler,
};

export const fn = (config: { handler: string }) => {
const functionGroup = new AwsResourceGroup({ type: "function", config });

const role = functionGroup.createResource({
type: "iam/role",
});

const policyAttachment = functionGroup.createResource({
type: "iam/policy-attachment",
dependencies: {
roleId: role.id,
},
});

functionGroup.createResource({
type: "lambda",
dependencies: {
policyId: policyAttachment.id,
},
});

return functionGroup;
};
Loading