Skip to content

Commit 0ede4a7

Browse files
chore: try to use vercel plugin route
1 parent 1800926 commit 0ede4a7

File tree

12 files changed

+835
-55
lines changed

12 files changed

+835
-55
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ coverage
1818
cypress.env.json
1919
tests/cypress/screenshots
2020

21-
# next.js
22-
.next/
23-
out/
24-
build
21+
# Vercel
22+
/apps/main/.vercel/
2523

2624
# misc
2725
.DS_Store

apps/main/_api/router.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { VercelRequest, VercelResponse } from '@vercel/node'
2+
import { buildServer } from 'router-api/src/server'
3+
4+
const server = buildServer()
5+
6+
export default async function handler(request: VercelRequest, response: VercelResponse) {
7+
await server.ready()
8+
server.server.emit('request', request, response)
9+
console.info(`${request.method} ${request.url}`)
10+
}

apps/main/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"@types/node": "24.3.0",
5656
"@types/react": "*",
5757
"@types/react-dom": "*",
58-
"@vitejs/plugin-react": "^5.0.1",
58+
"@vercel/node": "^5.3.25",
59+
"@vitejs/plugin-react": "^5.0.4",
5960
"babel-plugin-styled-components": "^2.1.4",
6061
"eslint": "*",
6162
"eslint-config-custom": "*",

apps/main/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export default defineConfig(({ command }) => ({
3535
destination: '/favicon.ico',
3636
},
3737
{
38-
source: '/(.*)',
38+
// match all files except api routes
39+
source: '/((?!api/).*)',
3940
destination: '/index.html',
4041
},
4142
],

apps/router-api/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"scripts": {
1010
"dev": "tsx watch src/dev.ts",
11-
"start": "node dist/index.js",
11+
"start": "node dist/api/index.js",
1212
"build": "tsc --project tsconfig.build.json",
1313
"lint": "eslint \"src/**/*.ts\"",
1414
"typecheck": "tsc --noEmit",
@@ -21,9 +21,9 @@
2121
},
2222
"devDependencies": {
2323
"@types/node": "24.5.2",
24+
"dotenv": "^17.2.3",
2425
"eslint": "*",
2526
"eslint-config-custom": "*",
26-
"tsx": "^4.20.6",
2727
"typescript": "*",
2828
"vitest": "^3.2.4"
2929
}

apps/router-api/src/dev.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import dotenv from 'dotenv'
12
import type { FastifyListenOptions } from 'fastify'
23
import { buildServer } from './server'
34

5+
dotenv.config() // Load environment variables from .env file
6+
47
const loadConfigFromEnv = ({ HOST, PORT = 3010 } = process.env): FastifyListenOptions => ({
58
port: Number(PORT),
69
host: HOST,

apps/router-api/src/routes/optimal-route.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function registerOptimalRoute(server: FastifyInstance): void {
2222
},
2323
async (request: FastifyRequest<{ Querystring: OptimalRouteQuery }>) => {
2424
const query = request.query
25-
return buildOptimalRouteResponse(await loadCurve(query.chainId), query)
25+
return buildOptimalRouteResponse(query)
2626
},
2727
)
2828
}
@@ -55,14 +55,16 @@ export async function routerGetToStoredRate(routes: IRoute, curve: CurveJS, toAd
5555
] as Decimal
5656
}
5757

58-
async function buildOptimalRouteResponse(curve: CurveJS, query: OptimalRouteQuery): Promise<RouteResponse[]> {
58+
export async function buildOptimalRouteResponse(query: OptimalRouteQuery): Promise<RouteResponse[]> {
5959
const {
6060
tokenOut: [toToken],
6161
tokenIn: [fromToken],
6262
chainId,
6363
amountIn: [amountIn] = [],
6464
amountOut,
6565
} = query
66+
67+
const curve = await loadCurve(query.chainId)
6668
const fromAmount = amountIn ?? ((await curve.router.required(fromToken, toToken, amountOut?.[0] ?? '0')) as Decimal)
6769
const { route: routes, output } = await curve.router.getBestRouteAndOutput(fromToken, toToken, fromAmount)
6870
if (!routes.length) return []
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4+
"module": "ESNext",
5+
"moduleResolution": "node",
46
"noEmit": false,
5-
"outDir": "dist",
7+
"outDir": "dist/api",
68
"rootDir": "src",
79
"sourceMap": true
810
},
9-
"include": ["src"]
11+
"include": ["src"],
12+
"exclude": ["src/dev.ts"]
1013
}

apps/router-api/vercel.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
2+
"functions": {
3+
"api/index.ts": {
4+
"runtime": "nodejs"
5+
}
6+
},
27
"rewrites": [
38
{
4-
"source": "/(.*)",
5-
"destination": "/api"
9+
"source": "/api/(.*)",
10+
"destination": "/api/index.ts"
611
}
712
]
813
}

packages/curve-ui-kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"@storybook/react-vite": "9.1.3",
4646
"@types/react": "*",
4747
"@types/react-dom": "*",
48-
"@vitejs/plugin-react": "^5.0.1",
48+
"@vitejs/plugin-react": "^5.0.4",
4949
"eslint": "*",
5050
"eslint-config-custom": "*",
5151
"storybook": "9.1.3",

0 commit comments

Comments
 (0)