Skip to content

Commit 95de4d2

Browse files
committed
Add vercel functions starter server
1 parent 8bb21c6 commit 95de4d2

16 files changed

Lines changed: 750 additions & 0 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generated files
2+
node_modules
3+
dist
4+
jest.config.js
5+
jest.setup.js
6+
.lintstagedrc.js
7+
**.d.ts
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const baseConfig = require("../../.lintstagedrc.js");
2+
3+
module.exports = {
4+
...baseConfig,
5+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Generated files
2+
pnpm-lock.yaml
3+
node_modules
4+
dist

‎serverless/vercel-server/LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 NodeBoot
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.

‎serverless/vercel-server/README.md‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# NodeBoot Vercel Server
2+
3+
Vercel Serverless Functions server package for NodeBoot framework. Provides seamless integration with Vercel's
4+
Node.js runtime while maintaining all NodeBoot features including dependency injection, middleware, routing, and
5+
error handling.
6+
7+
## Features
8+
9+
- **Multi-route Serverless Functions**: Handle multiple HTTP routes in a single Vercel Serverless Function
10+
- **Full NodeBoot Integration**: Complete support for controllers, services, middleware, and dependency injection
11+
- **Native Node.js Runtime**: Works directly with Vercel's Node.js `VercelRequest`/`VercelResponse` objects
12+
- **Request/Response Handling**: Automatic parsing of JSON bodies, query parameters, headers, and cookies
13+
- **Error Handling**: Integrated error handling with proper HTTP status codes
14+
- **Authorization**: Built-in authorization support using NodeBoot's authorization system
15+
16+
## Installation
17+
18+
```bash
19+
npm install @nodeboot/vercel-server
20+
```
21+
22+
## Basic Usage
23+
24+
### 1. Create your Vercel application
25+
26+
```typescript
27+
import {VercelServer} from "@nodeboot/vercel-server";
28+
import {NodeBootApplication} from "@nodeboot/core";
29+
30+
@EnableDI(Container)
31+
@EnableValidations()
32+
@EnableComponentScan()
33+
@NodeBootApplication()
34+
export class VercelSampleApp implements NodeBootApp {
35+
start(): Promise<NodeBootAppView> {
36+
return NodeBoot.run(VercelServer);
37+
}
38+
}
39+
```
40+
41+
### 2. Create the Serverless Function entry point
42+
43+
Vercel automatically maps files under the `api/` directory to routes. Use a catch-all route so that a single
44+
Serverless Function can handle every path processed by NodeBoot's internal router:
45+
46+
```typescript
47+
// api/[...path].ts
48+
import {VercelHandler, VercelServer} from "@nodeboot/vercel-server";
49+
import {VercelSampleApp} from "../src/app";
50+
51+
// Reused across warm invocations of the same execution environment.
52+
// Only re-initialized when Vercel spins up a brand-new instance (cold start).
53+
let vercelHandler: VercelHandler | null = null;
54+
55+
export default async function handler(req: VercelRequest, res: VercelResponse) {
56+
if (!vercelHandler) {
57+
const app = await new VercelSampleApp().start();
58+
const vercelServer = app.server as VercelServer;
59+
vercelHandler = vercelServer.getHandler();
60+
}
61+
62+
return vercelHandler(req, res);
63+
}
64+
```
65+
66+
### 3. Create Controllers
67+
68+
```typescript
69+
import {Controller, Get, Post, Param, Body} from "@nodeboot/core";
70+
71+
@Controller("/api")
72+
export class UserController {
73+
@Get("/users/:id")
74+
getUser(@Param("id") id: string) {
75+
return {id, name: `User ${id}`};
76+
}
77+
78+
@Post("/users")
79+
createUser(@Body() userData: any) {
80+
return {success: true, user: userData};
81+
}
82+
}
83+
```
84+
85+
### 4. Deploy to Vercel
86+
87+
The exported `handler` function can be deployed directly as a Vercel Node.js Serverless Function - no additional
88+
configuration is required beyond routing every request to the catch-all `api/[...path].ts` entry point.
89+
90+
## Middleware Support
91+
92+
All NodeBoot middleware is supported:
93+
94+
```typescript
95+
import {Middleware, MiddlewareInterface, Action} from "@nodeboot/core";
96+
97+
@Middleware({type: "before"})
98+
export class LoggingMiddleware implements MiddlewareInterface {
99+
@Inject()
100+
private logger: Logger;
101+
102+
use(action: Action, payload: any): void {
103+
this.logger.info(`${action.request.method} ${action.request.url}`);
104+
}
105+
}
106+
```
107+
108+
## Error Handling
109+
110+
Custom error handlers work seamlessly:
111+
112+
```typescript
113+
import {ErrorHandler, ErrorHandlerInterface} from "@nodeboot/core";
114+
115+
@ErrorHandler()
116+
export class CustomErrorHandler implements ErrorHandlerInterface {
117+
onError(error: any, action: Action): void {
118+
// Custom error handling logic
119+
}
120+
}
121+
```
122+
123+
## Authorization
124+
125+
NodeBoot's authorization system is fully supported. Use decorators like `@Authorize()` in your controllers to protect
126+
routes.
127+
128+
```typescript
129+
import {Authorize, Controller, Get} from "@nodeboot/core";
130+
131+
@Controller("/secure")
132+
export class SecureController {
133+
@Get("/data")
134+
@Authorize("admin")
135+
getSecureData() {
136+
return {secret: "This is secure data"};
137+
}
138+
}
139+
```
140+
141+
## Conclusion
142+
143+
The NodeBoot Vercel Server package provides a powerful way to build serverless applications using the familiar
144+
NodeBoot framework. With full support for routing, middleware, error handling, and authorization, you can create
145+
robust APIs that run on Vercel's Node.js Serverless Functions with ease.
146+
147+
## License
148+
149+
This project is licensed under the MIT License.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
transform: {
3+
"^.+\\.(t|j)sx?$": "@swc/jest",
4+
},
5+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://json.schemastore.org/nodemon.json",
3+
"watch": ["./src/**", "./node_modules/@mme/**/dist/**"],
4+
"ignoreRoot": [],
5+
"ext": "ts,js",
6+
"exec": "pnpm tsc && pnpm build"
7+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "@nodeboot/vercel-server",
3+
"version": "1.0.0",
4+
"description": "Node-Boot Vercel serverless functions server package. It provides a simple way to create Vercel Node.js serverless function handlers using NodeBoot, with support for routing, middleware, and request handling.",
5+
"author": "Manuel Santos <ney.br.santos@gmail.com>",
6+
"license": "MIT",
7+
"keywords": [
8+
"nodeboot",
9+
"vercel",
10+
"serverless"
11+
],
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/nodejs-boot/node-boot.git"
15+
},
16+
"publishConfig": {
17+
"access": "public"
18+
},
19+
"main": "dist/index.js",
20+
"types": "dist/index.d.ts",
21+
"scripts": {
22+
"build": "tsc -p tsconfig.build.json",
23+
"clean:build": "rimraf ./dist",
24+
"lint": "eslint . --ext .js,.ts",
25+
"lint:fix": "pnpm lint --fix",
26+
"format": "prettier --check .",
27+
"format:fix": "prettier --write .",
28+
"pretest": "pnpm run clean:build && pnpm run build",
29+
"test": "node --test --test-reporter node-test-reporter --require ts-node/register test/**/*.{test,it.test}.ts",
30+
"test:coverage": "node --experimental-test-coverage --test --test-reporter node-test-reporter --require ts-node/register test/**/*.{test,it.test}.ts",
31+
"tsc": "tsc"
32+
},
33+
"dependencies": {
34+
"@nodeboot/context": "workspace:*",
35+
"@nodeboot/core": "workspace:*",
36+
"@nodeboot/engine": "workspace:*",
37+
"@nodeboot/error": "workspace:*",
38+
"find-my-way": "^9.3.0",
39+
"cookie": "^1.0.2"
40+
},
41+
"devDependencies": {
42+
"@types/node": "^24.0.1"
43+
},
44+
"files": [
45+
"dist/**/*",
46+
"README.md",
47+
"LICENSE"
48+
]
49+
}

0 commit comments

Comments
 (0)