@lafken/main 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENCE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Aníbal Emilio Jorquera Cornejo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # @lafken/main
2
+
3
+ This is the core entry point of an Lafken application. Internally, it initializes the @cdktf/provider-aws, allowing you to register resolvers, modules, and global configuration. It orchestrates the creation of all the infrastructure required for a fully serverless application.
4
+
5
+ ## Installation
6
+
7
+
8
+ ```bash
9
+ npm install @lafken/main
10
+ ```
11
+
12
+
13
+ ## Features
14
+
15
+
16
+ ### createApp
17
+
18
+
19
+ This is the main entry point of the application. It allows you to register the resolvers required for processing your infrastructure, as well as define global configuration.
20
+
21
+ Creating an application is straightforward.
22
+
23
+ ```ts
24
+ createApp({
25
+ name: 'awesome',
26
+ modules: [],
27
+ resolvers: [],
28
+ globalConfig: {
29
+ tags: {
30
+ 'global-tag': 'awesome-tag',
31
+ },
32
+ },
33
+ extend() {
34
+ // extend your application
35
+ },
36
+ });
37
+ ```
38
+
39
+ Just give your application a name, import your resolvers—such as `ApiResolver` or `BucketResolver`—add your modules, and you’re ready to go.
40
+
41
+ ### createModule
42
+
43
+ Each module contains one or more resources. With a module, you can import your classes and define the resources that will be processed by a resolver.
44
+ Additionally, you can apply global configuration to all resources managed by the module.
45
+
46
+ ```ts
47
+ createModule({
48
+ name: 'awesome-module',
49
+ resources: [/** API, QUEUE, SCHEDULE */],
50
+ globalConfig: {
51
+ lambda: {
52
+ services: ['cloudwatch', 's3', 'sqs', 'dynamodb'],
53
+ },
54
+ },
55
+ });
56
+ ```
57
+
58
+ This allows you to group related functionality, organize your infrastructure, and apply shared settings across multiple resources.
@@ -0,0 +1,16 @@
1
+ import { App, TerraformStack } from 'cdktf';
2
+ import type { CreateAppProps } from './app.types';
3
+ export declare class AppStack extends TerraformStack {
4
+ id: string;
5
+ private props;
6
+ constructor(scope: App, id: string, props: CreateAppProps);
7
+ init(): Promise<void>;
8
+ private triggerHook;
9
+ private resolveModuleResources;
10
+ private createRole;
11
+ private addAspectProperties;
12
+ }
13
+ export declare const createApp: (props: CreateAppProps) => Promise<{
14
+ app: App;
15
+ appStack: AppStack;
16
+ }>;
package/lib/app/app.js ADDED
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createApp = exports.AppStack = void 0;
7
+ const provider_1 = require("@cdktf/provider-aws/lib/provider");
8
+ const common_1 = require("@lafken/common");
9
+ const resolver_1 = require("@lafken/resolver");
10
+ const cdktf_1 = require("cdktf");
11
+ const pretty_error_1 = __importDefault(require("pretty-error"));
12
+ const aspect_1 = require("../aspect/aspect");
13
+ const context_1 = require("../context/context");
14
+ (0, common_1.enableBuildEnvVariable)();
15
+ new pretty_error_1.default().start();
16
+ class AppStack extends cdktf_1.TerraformStack {
17
+ id;
18
+ props;
19
+ constructor(scope, id, props) {
20
+ super(scope, id);
21
+ this.id = id;
22
+ this.props = props;
23
+ new context_1.AppContext(this, {
24
+ contextName: resolver_1.ContextName.app,
25
+ globalConfig: props.globalConfig?.lambda,
26
+ contextCreator: props.name,
27
+ });
28
+ new provider_1.AwsProvider(this, 'AWS', props.awsProviderConfig);
29
+ this.createRole();
30
+ }
31
+ async init() {
32
+ const { resolvers, extend } = this.props;
33
+ await this.triggerHook(resolvers, 'beforeCreate');
34
+ await this.resolveModuleResources();
35
+ await this.triggerHook(resolvers, 'afterCreate');
36
+ this.addAspectProperties();
37
+ await resolver_1.lafkenResource.callDependentCallbacks();
38
+ await resolver_1.lambdaAssets.createAssets();
39
+ if (extend) {
40
+ await extend(this);
41
+ }
42
+ }
43
+ async triggerHook(resolvers, trigger) {
44
+ for (const resolver of resolvers) {
45
+ if (resolver[trigger] !== undefined) {
46
+ await resolver[trigger](this);
47
+ }
48
+ }
49
+ }
50
+ async resolveModuleResources() {
51
+ const { modules, resolvers } = this.props;
52
+ const resolversByType = resolvers.reduce((acc, resolver) => {
53
+ acc[resolver.type] = resolver;
54
+ return acc;
55
+ }, {});
56
+ await Promise.all(modules.map((module) => module(this, resolversByType)));
57
+ }
58
+ createRole() {
59
+ const roleName = `${this.props.name}-global-role`;
60
+ const lambdaRole = new resolver_1.Role(this, roleName, {
61
+ name: roleName,
62
+ services: this.props.globalConfig?.lambda?.services || [
63
+ 'dynamodb',
64
+ 's3',
65
+ 'lambda',
66
+ 'cloudwatch',
67
+ 'sqs',
68
+ 'state_machine',
69
+ 'kms',
70
+ 'ssm',
71
+ 'event',
72
+ ],
73
+ });
74
+ lambdaRole.isGlobal('app', roleName);
75
+ }
76
+ addAspectProperties() {
77
+ cdktf_1.Aspects.of(this).add(new aspect_1.AppAspect({
78
+ tags: {
79
+ ...(this.props.globalConfig?.tags || {}),
80
+ 'lafken:app': this.id,
81
+ },
82
+ }));
83
+ }
84
+ }
85
+ exports.AppStack = AppStack;
86
+ const createApp = async (props) => {
87
+ const app = new cdktf_1.App({
88
+ skipValidation: true,
89
+ });
90
+ const appStack = new AppStack(app, props.name, props);
91
+ await appStack.init();
92
+ app.synth();
93
+ return {
94
+ app,
95
+ appStack,
96
+ };
97
+ };
98
+ exports.createApp = createApp;
@@ -0,0 +1,62 @@
1
+ import type { AwsProviderConfig } from '@cdktf/provider-aws/lib/provider';
2
+ import type { LambdaGlobalConfig, ResolverType } from '@lafken/resolver';
3
+ import type { StackModule } from '../module';
4
+ import type { ModuleResolverType } from '../module/module.types';
5
+ import type { AppStack } from './app';
6
+ export interface GlobalConfig {
7
+ lambda?: LambdaGlobalConfig;
8
+ /**
9
+ * Global resource tags.
10
+ *
11
+ * Specifies a set of tags that will be applied to all resources
12
+ * unless a resource explicitly defines its own tags. In that case,
13
+ * the resource-specific tags will override the global values.
14
+ */
15
+ tags?: Record<string, string>;
16
+ }
17
+ export interface CreateAppProps {
18
+ /**
19
+ * Application name.
20
+ *
21
+ * Specifies the name of the application, which is used within
22
+ * the AWS stack as an identifier for resources.
23
+ *
24
+ * @example
25
+ * name: "my-awesome-app"
26
+ */
27
+ name: string;
28
+ /**
29
+ * Application modules.
30
+ *
31
+ * Defines the set of modules to be created within the application.
32
+ */
33
+ modules: ((scope: AppStack, resources: Record<string, ModuleResolverType>) => Promise<StackModule>)[];
34
+ /**
35
+ * Resource resolvers.
36
+ *
37
+ * Defines the list of resolvers responsible for creating and configuring
38
+ * resources loaded by the stacks. Each resolver can receive detailed
39
+ * configuration options depending on the type of resource it manages.
40
+ *
41
+ * For example, an `ApiResolver` can include REST API settings,
42
+ * deployment options, and authorization configurations.
43
+ */
44
+ resolvers: ResolverType[];
45
+ /**
46
+ * Global configuration for the application.
47
+ *
48
+ * Provides settings that are applied across all resources, stacks,
49
+ * and Lambda functions unless overridden at a lower level.
50
+ * This includes global Lambda properties, environment configuration,
51
+ * and resource tags.
52
+ */
53
+ globalConfig?: GlobalConfig;
54
+ /**
55
+ *
56
+ */
57
+ awsProviderConfig?: AwsProviderConfig;
58
+ /**
59
+ *
60
+ */
61
+ extend?: (scope: AppStack) => Promise<void>;
62
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export * from './app';
2
+ export * from './app.types';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./app"), exports);
18
+ __exportStar(require("./app.types"), exports);
@@ -0,0 +1,12 @@
1
+ import type { IAspect } from 'cdktf';
2
+ import type { IConstruct } from 'constructs';
3
+ interface AppAspectProps {
4
+ tags: Record<string, string>;
5
+ }
6
+ export declare class AppAspect implements IAspect {
7
+ private props;
8
+ constructor(props: AppAspectProps);
9
+ visit(node: IConstruct): void;
10
+ private isTaggableResource;
11
+ }
12
+ export {};
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AppAspect = void 0;
4
+ class AppAspect {
5
+ props;
6
+ constructor(props) {
7
+ this.props = props;
8
+ }
9
+ visit(node) {
10
+ if (this.isTaggableResource(node)) {
11
+ const currentTags = node.tagsInput || {};
12
+ node.tags = { ...this.props.tags, ...currentTags };
13
+ }
14
+ }
15
+ isTaggableResource(resource) {
16
+ return 'tags' in resource && 'tagsInput' in resource;
17
+ }
18
+ }
19
+ exports.AppAspect = AppAspect;
@@ -0,0 +1,5 @@
1
+ import type { Construct } from 'constructs';
2
+ import type { ContextProps } from './context.types';
3
+ export declare class AppContext {
4
+ constructor(scope: Construct, props: ContextProps);
5
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AppContext = void 0;
4
+ class AppContext {
5
+ constructor(scope, props) {
6
+ const { contextName, globalConfig = {} } = props;
7
+ const { services: _services, ...contextData } = globalConfig || {};
8
+ scope.node.setContext(contextName, {
9
+ ...contextData,
10
+ contextCreator: props.contextCreator,
11
+ });
12
+ }
13
+ }
14
+ exports.AppContext = AppContext;
@@ -0,0 +1,6 @@
1
+ import type { LambdaGlobalConfig } from '@lafken/resolver';
2
+ export interface ContextProps {
3
+ globalConfig?: LambdaGlobalConfig;
4
+ contextCreator: string;
5
+ contextName: string;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './app';
2
+ export * from './module';
package/lib/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./app"), exports);
18
+ __exportStar(require("./module"), exports);
@@ -0,0 +1,2 @@
1
+ export * from './module';
2
+ export * from './module.types';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./module"), exports);
18
+ __exportStar(require("./module.types"), exports);
@@ -0,0 +1,11 @@
1
+ import { Construct } from 'constructs';
2
+ import type { CreateModuleProps, ModuleConstruct, ModuleProps, ModuleResolverType } from './module.types';
3
+ export declare class StackModule extends Construct {
4
+ id: string;
5
+ private props;
6
+ constructor(scope: Construct, id: string, props: ModuleProps);
7
+ generateResources(): Promise<void>;
8
+ private createRole;
9
+ private addAspectProperties;
10
+ }
11
+ export declare const createModule: (props: CreateModuleProps) => (scope: ModuleConstruct, resolvers: Record<string, ModuleResolverType>) => Promise<StackModule>;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createModule = exports.StackModule = void 0;
4
+ const common_1 = require("@lafken/common");
5
+ const resolver_1 = require("@lafken/resolver");
6
+ const cdktf_1 = require("cdktf");
7
+ const constructs_1 = require("constructs");
8
+ const aspect_1 = require("../aspect/aspect");
9
+ const context_1 = require("../context/context");
10
+ class StackModule extends constructs_1.Construct {
11
+ id;
12
+ props;
13
+ constructor(scope, id, props) {
14
+ super(scope, id);
15
+ this.id = id;
16
+ this.props = props;
17
+ new context_1.AppContext(this, {
18
+ contextName: resolver_1.ContextName.module,
19
+ globalConfig: props.globalConfig?.lambda,
20
+ contextCreator: props.name,
21
+ });
22
+ this.createRole();
23
+ }
24
+ async generateResources() {
25
+ const { resources } = this.props;
26
+ for (const resource of resources) {
27
+ const metadata = (0, common_1.getResourceMetadata)(resource);
28
+ const resolver = this.props.resolvers[metadata.type];
29
+ if (!resolver) {
30
+ throw new Error(`There is no resolver for the resource ${metadata.type}`);
31
+ }
32
+ await resolver.create(this, resource);
33
+ }
34
+ this.addAspectProperties();
35
+ }
36
+ createRole() {
37
+ if (!this.props.globalConfig?.lambda?.services?.length) {
38
+ return;
39
+ }
40
+ const roleName = `${this.props.name}-module-role`;
41
+ const lambdaRole = new resolver_1.Role(this, roleName, {
42
+ name: roleName,
43
+ services: this.props.globalConfig?.lambda?.services || [],
44
+ });
45
+ lambdaRole.isGlobal('module', roleName);
46
+ }
47
+ addAspectProperties() {
48
+ cdktf_1.Aspects.of(this).add(new aspect_1.AppAspect({
49
+ tags: {
50
+ ...(this.props.globalConfig?.tags || {}),
51
+ 'lafken:module': this.props.name,
52
+ },
53
+ }));
54
+ }
55
+ }
56
+ exports.StackModule = StackModule;
57
+ const createModule = (props) => async (scope, resolvers) => {
58
+ const module = new StackModule(scope, props.name, {
59
+ ...props,
60
+ resolvers,
61
+ });
62
+ await module.generateResources();
63
+ return module;
64
+ };
65
+ exports.createModule = createModule;
@@ -0,0 +1,42 @@
1
+ import type { ClassResource, ResourceMetadata } from '@lafken/common';
2
+ import type { ResolverType } from '@lafken/resolver';
3
+ import type { Construct } from 'constructs';
4
+ import type { GlobalConfig } from '../app/app.types';
5
+ import type { StackModule } from './module';
6
+ export interface CreateModuleProps {
7
+ /**
8
+ * Module name.
9
+ *
10
+ * Specifies the name of the module, which will be used as an identifier
11
+ * for all resources created within this stack.
12
+ */
13
+ name: string;
14
+ /**
15
+ * Module resources.
16
+ *
17
+ * Defines the list of resources to be created within this stack.
18
+ * Each item represents a resource such as Api, Queue, StateMachine, etc.
19
+ */
20
+ resources: ClassResource[];
21
+ /**
22
+ * Module-level global configuration.
23
+ *
24
+ * Provides settings that are applied to all resources and Lambda functions
25
+ * within this specific module. This configuration behaves similarly to
26
+ * the application-wide `GlobalConfig`, but excludes environment settings (`env`),
27
+ * which are managed at the application level.
28
+ */
29
+ globalConfig?: Omit<GlobalConfig, 'env'>;
30
+ }
31
+ export interface ModuleProps extends CreateModuleProps {
32
+ resolvers: Record<string, ResolverType>;
33
+ }
34
+ export interface ModuleResource {
35
+ module: StackModule;
36
+ metadata: ResourceMetadata;
37
+ Resource: ClassResource;
38
+ }
39
+ export interface ModuleResolverType extends ResolverType {
40
+ }
41
+ export interface ModuleConstruct extends Construct {
42
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@lafken/main",
3
+ "version": "0.6.0",
4
+ "private": false,
5
+ "license": "MIT",
6
+ "main": "lib/index.js",
7
+ "types": "lib/index.d.ts",
8
+ "files": [
9
+ "lib"
10
+ ],
11
+ "dependencies": {
12
+ "@cdktf/provider-aws": "21.22.0",
13
+ "cdktf": "0.21.0",
14
+ "constructs": "10.4.4",
15
+ "pretty-error": "4.0.0",
16
+ "reflect-metadata": "0.2.2",
17
+ "@lafken/common": "0.6.0",
18
+ "@lafken/resolver": "0.6.0"
19
+ },
20
+ "devDependencies": {
21
+ "@types/jest": "30.0.0",
22
+ "jest": "30.2.0",
23
+ "ts-jest": "29.4.6",
24
+ "ts-node": "10.9.2"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "scripts": {
30
+ "build": "pnpm clean && tsc -p ./tsconfig.build.json",
31
+ "clean": "rm -rf ./lib",
32
+ "dev": "tsc -w",
33
+ "test": "jest",
34
+ "test:coverage": "jest --coverage"
35
+ }
36
+ }