Skip to content

Commit d69b057

Browse files
author
Hidetaka Okamoto
committed
feat: init project
1 parent 4e22829 commit d69b057

File tree

6 files changed

+10032
-26
lines changed

6 files changed

+10032
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ coverage/
66
dist/
77
examples/
88
.rpt2_cache/
9+
yarn-error.log

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
# TypeScript Package template
2-
3-
GitHub Template repository to create a npm package by using TypeScript.
1+
# Serverless AWS Lambda Nestjs Adapter
42

53
## Badges
64

7-
[![NPM](https://nodei.co/npm/MY_REPO_NAME.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/MY_REPO_NAME/)
8-
[![npm version](https://badge.fury.io/js/MY_REPO_NAME.svg)](https://badge.fury.io/js/MY_REPO_NAME)
5+
[![NPM](https://nodei.co/npm/serverless-lambda-nestjs.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/serverless-lambda-nestjs/)
6+
[![npm version](https://badge.fury.io/js/serverless-lambda-nestjs.svg)](https://badge.fury.io/js/serverless-lambda-nestjs)
97
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
10-
[![Maintainability](https://api.codeclimate.com/v1/badges/c17851759423ce151b9e/maintainability)](https://codeclimate.com/github/hideokamoto/MY_REPO_NAME/maintainability)
11-
[![Test Coverage](https://api.codeclimate.com/v1/badges/c17851759423ce151b9e/test_coverage)](https://codeclimate.com/github/hideokamoto/MY_REPO_NAME/test_coverage)
12-
[![Build Status](https://travis-ci.org/hideokamoto/MY_REPO_NAME.svg?branch=master)](https://travis-ci.org/hideokamoto/MY_REPO_NAME)
8+
[![Maintainability](https://api.codeclimate.com/v1/badges/c17851759423ce151b9e/maintainability)](https://codeclimate.com/github/hideokamoto/serverless-lambda-nestjs/maintainability)
9+
[![Test Coverage](https://api.codeclimate.com/v1/badges/c17851759423ce151b9e/test_coverage)](https://codeclimate.com/github/hideokamoto/serverless-lambda-nestjs/test_coverage)
10+
[![Build Status](https://travis-ci.org/hideokamoto/serverless-lambda-nestjs.svg?branch=master)](https://travis-ci.org/hideokamoto/serverless-lambda-nestjs)
1311

1412

1513
## OGP
@@ -68,8 +66,8 @@ $ git commit -m "<type>[optional scope]: <description>
6866

6967
```bash
7068
// clone
71-
$ git clone [email protected]:hideokamoto/MY_REPO_NAME.git
72-
$ cd MY_REPO_NAME
69+
$ git clone [email protected]:hideokamoto/serverless-lambda-nestjs.git
70+
$ cd serverless-lambda-nestjs
7371

7472
// setup
7573
$ yarn

libs/index.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,68 @@
1-
export default (name: string): string => `Hello ${name} !`
1+
import { NestApplicationOptions, INestApplication } from '@nestjs/common'
2+
import { NestFactory } from '@nestjs/core'
3+
import { Server } from 'http'
4+
import { ExpressAdapter } from '@nestjs/platform-express'
5+
import * as serverless from 'aws-serverless-express'
6+
import express from 'express'
7+
import { APIGatewayProxyEvent, Context, APIGatewayEvent } from 'aws-lambda'
8+
9+
let cachedServer: Server
10+
11+
export type NestAplicationCallback = (
12+
app: INestApplication,
13+
) => INestApplication | Promise<INestApplication>;
14+
const defaultCallback: NestAplicationCallback = app => app
15+
16+
export class ServerlessNestjsApplicationFactory<T = any> {
17+
private readonly AppModule: T;
18+
private options: NestApplicationOptions;
19+
private callback: NestAplicationCallback;
20+
constructor (
21+
AppModule: T,
22+
options: NestApplicationOptions = {},
23+
callback: NestAplicationCallback = defaultCallback
24+
) {
25+
this.AppModule = AppModule
26+
this.options = options
27+
this.callback = callback
28+
}
29+
30+
public updateOptions (options: NestApplicationOptions) {
31+
this.options = options
32+
return this
33+
}
34+
35+
public updateCallback (callback: NestAplicationCallback) {
36+
this.callback = callback
37+
return this
38+
}
39+
40+
public async createApplication () {
41+
const expressApp = express()
42+
const adapter = new ExpressAdapter(expressApp)
43+
const application = await NestFactory.create(
44+
this.AppModule,
45+
adapter,
46+
this.options
47+
)
48+
const app = await this.callback(application)
49+
app.init()
50+
return serverless.createServer(expressApp)
51+
}
52+
53+
public async create (
54+
event: APIGatewayProxyEvent | APIGatewayEvent,
55+
context: Context
56+
) {
57+
if (!cachedServer) {
58+
cachedServer = await this.createApplication()
59+
}
60+
const result = await serverless.proxy(
61+
cachedServer,
62+
event,
63+
context,
64+
'PROMISE'
65+
).promise
66+
return result
67+
}
68+
}

0 commit comments

Comments
 (0)