@appliance.sh/api-server 1.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/README.md ADDED
@@ -0,0 +1,98 @@
1
+ <p align="center">
2
+ <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
3
+ </p>
4
+
5
+ [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6
+ [circleci-url]: https://circleci.com/gh/nestjs/nest
7
+
8
+ <p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
9
+ <p align="center">
10
+ <a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
11
+ <a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
12
+ <a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
13
+ <a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
14
+ <a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
15
+ <a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
16
+ <a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
17
+ <a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg" alt="Donate us"/></a>
18
+ <a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
19
+ <a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow" alt="Follow us on Twitter"></a>
20
+ </p>
21
+ <!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
22
+ [![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
23
+
24
+ ## Description
25
+
26
+ [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
27
+
28
+ ## Project setup
29
+
30
+ ```bash
31
+ $ npm install
32
+ ```
33
+
34
+ ## Compile and run the project
35
+
36
+ ```bash
37
+ # development
38
+ $ npm run start
39
+
40
+ # watch mode
41
+ $ npm run start:dev
42
+
43
+ # production mode
44
+ $ npm run start:prod
45
+ ```
46
+
47
+ ## Run tests
48
+
49
+ ```bash
50
+ # unit tests
51
+ $ npm run test
52
+
53
+ # e2e tests
54
+ $ npm run test:e2e
55
+
56
+ # test coverage
57
+ $ npm run test:cov
58
+ ```
59
+
60
+ ## Deployment
61
+
62
+ When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the [deployment documentation](https://docs.nestjs.com/deployment) for more information.
63
+
64
+ If you are looking for a cloud-based platform to deploy your NestJS application, check out [Mau](https://mau.nestjs.com), our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:
65
+
66
+ ```bash
67
+ $ npm install -g @nestjs/mau
68
+ $ mau deploy
69
+ ```
70
+
71
+ With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
72
+
73
+ ## Resources
74
+
75
+ Check out a few resources that may come in handy when working with NestJS:
76
+
77
+ - Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework.
78
+ - For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy).
79
+ - To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/).
80
+ - Deploy your application to AWS with the help of [NestJS Mau](https://mau.nestjs.com) in just a few clicks.
81
+ - Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com).
82
+ - Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com).
83
+ - To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs).
84
+ - Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com).
85
+
86
+ ## Support
87
+
88
+ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
89
+
90
+ ## Stay in touch
91
+
92
+ - Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec)
93
+ - Website - [https://nestjs.com](https://nestjs.com/)
94
+ - Twitter - [@nestframework](https://twitter.com/nestframework)
95
+
96
+ ## License
97
+
98
+ Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).
package/nest-cli.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/nest-cli",
3
+ "collection": "@nestjs/schematics",
4
+ "sourceRoot": "src",
5
+ "compilerOptions": {
6
+ "deleteOutDir": true
7
+ }
8
+ }
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@appliance.sh/api-server",
3
+ "version": "1.6.0",
4
+ "description": "",
5
+ "author": "Eliot Lim",
6
+ "repository": "https://github.com/appliance-sh/appliance.sh",
7
+ "license": "MIT",
8
+ "scripts": {
9
+ "build": "nest build",
10
+ "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
11
+ "start": "nest start",
12
+ "start:dev": "nest start --watch",
13
+ "start:debug": "nest start --debug --watch",
14
+ "start:prod": "node dist/main",
15
+ "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
16
+ "test": "jest",
17
+ "test:watch": "jest --watch",
18
+ "test:cov": "jest --coverage",
19
+ "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
20
+ "test:e2e": "jest --config ./test/jest-e2e.json"
21
+ },
22
+ "dependencies": {
23
+ "@nestjs/common": "^11.0.1",
24
+ "@nestjs/core": "^11.0.1",
25
+ "@nestjs/platform-express": "^11.0.1",
26
+ "@aws-sdk/client-cloudformation": "^3.679.0",
27
+ "aws-cdk-lib": "^2.230.0",
28
+ "constructs": "^10.4.3",
29
+ "reflect-metadata": "^0.2.2",
30
+ "rxjs": "^7.8.1"
31
+ },
32
+ "devDependencies": {
33
+ "@eslint/eslintrc": "^3.2.0",
34
+ "@eslint/js": "^9.18.0",
35
+ "@nestjs/cli": "^11.0.0",
36
+ "@nestjs/schematics": "^11.0.0",
37
+ "@nestjs/testing": "^11.0.1",
38
+ "@types/express": "^5.0.0",
39
+ "@types/jest": "^30.0.0",
40
+ "@types/node": "^22.10.7",
41
+ "@types/supertest": "^6.0.2",
42
+ "eslint": "^9.18.0",
43
+ "eslint-config-prettier": "^10.0.1",
44
+ "eslint-plugin-prettier": "^5.2.2",
45
+ "globals": "^16.0.0",
46
+ "jest": "^30.0.0",
47
+ "prettier": "^3.4.2",
48
+ "source-map-support": "^0.5.21",
49
+ "supertest": "^7.0.0",
50
+ "ts-jest": "^29.2.5",
51
+ "ts-loader": "^9.5.2",
52
+ "ts-node": "^10.9.2",
53
+ "tsconfig-paths": "^4.2.0",
54
+ "typescript": "^5.7.3",
55
+ "typescript-eslint": "^8.20.0"
56
+ },
57
+ "jest": {
58
+ "moduleFileExtensions": [
59
+ "js",
60
+ "json",
61
+ "ts"
62
+ ],
63
+ "rootDir": "src",
64
+ "testRegex": ".*\\.spec\\.ts$",
65
+ "transform": {
66
+ "^.+\\.(t|j)s$": "ts-jest"
67
+ },
68
+ "collectCoverageFrom": [
69
+ "**/*.(t|j)s"
70
+ ],
71
+ "coverageDirectory": "../coverage",
72
+ "testEnvironment": "node"
73
+ }
74
+ }
@@ -0,0 +1,22 @@
1
+ import { Test, TestingModule } from '@nestjs/testing';
2
+ import { AppController } from './app.controller';
3
+ import { AppService } from './app.service';
4
+
5
+ describe('AppController', () => {
6
+ let appController: AppController;
7
+
8
+ beforeEach(async () => {
9
+ const app: TestingModule = await Test.createTestingModule({
10
+ controllers: [AppController],
11
+ providers: [AppService],
12
+ }).compile();
13
+
14
+ appController = app.get<AppController>(AppController);
15
+ });
16
+
17
+ describe('root', () => {
18
+ it('should return "Hello World!"', () => {
19
+ expect(appController.getHello()).toBe('Hello World!');
20
+ });
21
+ });
22
+ });
@@ -0,0 +1,12 @@
1
+ import { Controller, Get } from '@nestjs/common';
2
+ import { AppService } from './app.service';
3
+
4
+ @Controller()
5
+ export class AppController {
6
+ constructor(private readonly appService: AppService) {}
7
+
8
+ @Get()
9
+ getHello(): string {
10
+ return this.appService.getHello();
11
+ }
12
+ }
@@ -0,0 +1,11 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { AppController } from './app.controller';
3
+ import { AppService } from './app.service';
4
+ import { ApplianceStackModule } from './cdk/appliance-stack.module';
5
+
6
+ @Module({
7
+ imports: [ApplianceStackModule],
8
+ controllers: [AppController],
9
+ providers: [AppService],
10
+ })
11
+ export class AppModule {}
@@ -0,0 +1,8 @@
1
+ import { Injectable } from '@nestjs/common';
2
+
3
+ @Injectable()
4
+ export class AppService {
5
+ getHello(): string {
6
+ return 'Hello World!';
7
+ }
8
+ }
@@ -0,0 +1,131 @@
1
+ import { Injectable, Logger } from '@nestjs/common';
2
+ import * as cdk from 'aws-cdk-lib';
3
+ import { ApplianceStack } from './appliance-stack';
4
+ import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
5
+ import { tmpdir } from 'node:os';
6
+ import * as path from 'node:path';
7
+ import {
8
+ CloudFormationClient,
9
+ CreateStackCommand,
10
+ DeleteStackCommand,
11
+ DescribeStacksCommand,
12
+ UpdateStackCommand,
13
+ } from '@aws-sdk/client-cloudformation';
14
+ import {
15
+ waitUntilStackCreateComplete,
16
+ waitUntilStackUpdateComplete,
17
+ waitUntilStackDeleteComplete,
18
+ } from '@aws-sdk/client-cloudformation';
19
+
20
+ export type CdkAction = 'deploy' | 'destroy';
21
+
22
+ export interface CdkResult {
23
+ action: CdkAction;
24
+ ok: boolean;
25
+ idempotentNoop: boolean;
26
+ message: string;
27
+ stackName: string;
28
+ }
29
+
30
+ @Injectable()
31
+ export class ApplianceStackAwsCdkService {
32
+ private readonly logger = new Logger(ApplianceStackAwsCdkService.name);
33
+ private readonly region = process.env.AWS_REGION || 'us-east-1';
34
+
35
+ private client(): CloudFormationClient {
36
+ return new CloudFormationClient({ region: this.region });
37
+ }
38
+
39
+ private synthTemplate(stackName: string): { templateBody: string; outdir: string } {
40
+ const outdir = mkdtempSync(path.join(tmpdir(), 'appliance-cdk-'));
41
+ const app = new cdk.App({ outdir });
42
+ new ApplianceStack(app, stackName, {});
43
+ const assembly = app.synth();
44
+ const artifact = assembly.getStackByName(stackName);
45
+ const templatePath = path.join(assembly.directory, artifact.templateFile);
46
+ const templateBody = readFileSync(templatePath, 'utf-8');
47
+ return { templateBody, outdir: assembly.directory };
48
+ }
49
+
50
+ async deploy(stackName = 'appliance-api-managed'): Promise<CdkResult> {
51
+ const { templateBody, outdir } = this.synthTemplate(stackName);
52
+ try {
53
+ // Does stack exist?
54
+ const cf = this.client();
55
+ const exists = await this.stackExists(cf, stackName);
56
+ if (!exists) {
57
+ await cf.send(
58
+ new CreateStackCommand({
59
+ StackName: stackName,
60
+ TemplateBody: templateBody,
61
+ Capabilities: ['CAPABILITY_NAMED_IAM', 'CAPABILITY_AUTO_EXPAND'],
62
+ })
63
+ );
64
+ await waitUntilStackCreateComplete(
65
+ { client: cf, maxWaitTime: 30 * 60, minDelay: 5, maxDelay: 20 },
66
+ { StackName: stackName }
67
+ );
68
+ return { action: 'deploy', ok: true, idempotentNoop: false, message: 'Stack created', stackName };
69
+ }
70
+
71
+ try {
72
+ await cf.send(
73
+ new UpdateStackCommand({
74
+ StackName: stackName,
75
+ TemplateBody: templateBody,
76
+ Capabilities: ['CAPABILITY_NAMED_IAM', 'CAPABILITY_AUTO_EXPAND'],
77
+ })
78
+ );
79
+ await waitUntilStackUpdateComplete(
80
+ { client: cf, maxWaitTime: 30 * 60, minDelay: 5, maxDelay: 20 },
81
+ { StackName: stackName }
82
+ );
83
+ return { action: 'deploy', ok: true, idempotentNoop: false, message: 'Stack updated', stackName };
84
+ } catch (e) {
85
+ if (
86
+ e instanceof Error &&
87
+ typeof e?.message === 'string' &&
88
+ e.message.includes('No updates are to be performed')
89
+ ) {
90
+ return { action: 'deploy', ok: true, idempotentNoop: true, message: 'No changes (idempotent)', stackName };
91
+ }
92
+ throw e;
93
+ }
94
+ } finally {
95
+ // cleanup synth output
96
+ try {
97
+ rmSync(outdir, { recursive: true, force: true });
98
+ } catch {
99
+ // ignore
100
+ }
101
+ }
102
+ }
103
+
104
+ async destroy(stackName = 'appliance-api-managed'): Promise<CdkResult> {
105
+ const cf = this.client();
106
+ const exists = await this.stackExists(cf, stackName);
107
+ if (!exists) {
108
+ return { action: 'destroy', ok: true, idempotentNoop: true, message: 'Stack not found (idempotent)', stackName };
109
+ }
110
+ await cf.send(new DeleteStackCommand({ StackName: stackName }));
111
+ await waitUntilStackDeleteComplete(
112
+ { client: cf, maxWaitTime: 30 * 60, minDelay: 5, maxDelay: 20 },
113
+ { StackName: stackName }
114
+ );
115
+ return { action: 'destroy', ok: true, idempotentNoop: false, message: 'Stack deleted', stackName };
116
+ }
117
+
118
+ private async stackExists(cf: CloudFormationClient, stackName: string): Promise<boolean> {
119
+ try {
120
+ const res = await cf.send(new DescribeStacksCommand({ StackName: stackName }));
121
+ return !!res.Stacks && res.Stacks.length > 0;
122
+ } catch (e) {
123
+ if (!(e instanceof Error)) throw e;
124
+ const code = e?.name;
125
+ const message = e?.message || '';
126
+ if (code === 'ValidationError' && message.includes('does not exist')) return false;
127
+ if (message.includes('does not exist')) return false;
128
+ throw e;
129
+ }
130
+ }
131
+ }
@@ -0,0 +1,19 @@
1
+ import { Controller, HttpCode, Post } from '@nestjs/common';
2
+ import { ApplianceStackAwsCdkService } from './appliance-stack-aws-cdk.service';
3
+
4
+ @Controller('infra')
5
+ export class ApplianceStackController {
6
+ constructor(private readonly cdk: ApplianceStackAwsCdkService) {}
7
+
8
+ @Post('deploy')
9
+ @HttpCode(200)
10
+ async deploy() {
11
+ return await this.cdk.deploy();
12
+ }
13
+
14
+ @Post('destroy')
15
+ @HttpCode(200)
16
+ async destroy() {
17
+ return await this.cdk.destroy();
18
+ }
19
+ }
@@ -0,0 +1,9 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { ApplianceStackAwsCdkService } from './appliance-stack-aws-cdk.service';
3
+ import { ApplianceStackController } from './appliance-stack.controller';
4
+
5
+ @Module({
6
+ providers: [ApplianceStackAwsCdkService],
7
+ controllers: [ApplianceStackController],
8
+ })
9
+ export class ApplianceStackModule {}
@@ -0,0 +1,35 @@
1
+ import * as cdk from 'aws-cdk-lib';
2
+ import { Construct } from 'constructs';
3
+
4
+ export class ApplianceStack extends cdk.Stack {
5
+ constructor(scope: Construct, id: string, props?: cdk.StackProps) {
6
+ super(scope, id, props);
7
+
8
+ // Simple dummy resource: S3 bucket with a stable logical ID
9
+ const bucket = new cdk.aws_s3.Bucket(this, 'DummyBucket', {
10
+ versioned: true,
11
+ removalPolicy: cdk.RemovalPolicy.DESTROY,
12
+ // autoDeleteObjects: true,
13
+ });
14
+
15
+ const lambdaUrl = new cdk.aws_lambda.Function(this, 'DummyLambda', {
16
+ runtime: cdk.aws_lambda.Runtime.NODEJS_24_X,
17
+ handler: 'index.handler',
18
+ code: cdk.aws_lambda.Code.fromInline('exports.handler = async () => "Hello World!";'),
19
+ });
20
+
21
+ const fnUrl = lambdaUrl.addFunctionUrl({
22
+ authType: cdk.aws_lambda.FunctionUrlAuthType.AWS_IAM,
23
+ });
24
+
25
+ const fnDistribution = new cdk.aws_cloudfront.Distribution(this, 'DummyDistribution', {
26
+ defaultBehavior: {
27
+ origin: cdk.aws_cloudfront_origins.FunctionUrlOrigin.withOriginAccessControl(fnUrl),
28
+ },
29
+ });
30
+
31
+ new cdk.CfnOutput(this, 'BucketName', { value: bucket.bucketName });
32
+ new cdk.CfnOutput(this, 'FunctionUrl', { value: fnUrl.url });
33
+ new cdk.CfnOutput(this, 'Distribution', { value: fnDistribution.distributionDomainName });
34
+ }
35
+ }
package/src/main.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { NestFactory } from '@nestjs/core';
2
+ import { AppModule } from './app.module';
3
+
4
+ async function bootstrap() {
5
+ const app = await NestFactory.create(AppModule);
6
+ await app.listen(process.env.PORT ?? 3000);
7
+ }
8
+ bootstrap();
@@ -0,0 +1,22 @@
1
+ import { Test, TestingModule } from '@nestjs/testing';
2
+ import { INestApplication } from '@nestjs/common';
3
+ import request from 'supertest';
4
+ import { App } from 'supertest/types';
5
+ import { AppModule } from '../src/app.module';
6
+
7
+ describe('AppController (e2e)', () => {
8
+ let app: INestApplication<App>;
9
+
10
+ beforeEach(async () => {
11
+ const moduleFixture: TestingModule = await Test.createTestingModule({
12
+ imports: [AppModule],
13
+ }).compile();
14
+
15
+ app = moduleFixture.createNestApplication();
16
+ await app.init();
17
+ });
18
+
19
+ it('/ (GET)', () => {
20
+ return request(app.getHttpServer()).get('/').expect(200).expect('Hello World!');
21
+ });
22
+ });
@@ -0,0 +1,9 @@
1
+ {
2
+ "moduleFileExtensions": ["js", "json", "ts"],
3
+ "rootDir": ".",
4
+ "testEnvironment": "node",
5
+ "testRegex": ".e2e-spec.ts$",
6
+ "transform": {
7
+ "^.+\\.(t|j)s$": "ts-jest"
8
+ }
9
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "module": "nodenext",
5
+ "moduleResolution": "nodenext",
6
+ "resolvePackageJsonExports": true,
7
+ "esModuleInterop": true,
8
+ "isolatedModules": true,
9
+ "declaration": true,
10
+ "removeComments": true,
11
+ "emitDecoratorMetadata": true,
12
+ "experimentalDecorators": true,
13
+ "allowSyntheticDefaultImports": true,
14
+ "target": "ES2023",
15
+ "sourceMap": true,
16
+ "outDir": "./dist",
17
+ "baseUrl": "./",
18
+ "incremental": true,
19
+ "skipLibCheck": true,
20
+ "strictNullChecks": true,
21
+ "forceConsistentCasingInFileNames": true,
22
+ "noImplicitAny": false,
23
+ "strictBindCallApply": false,
24
+ "noFallthroughCasesInSwitch": false
25
+ }
26
+ }