Skip to content

Commit bb35486

Browse files
kind of working
1 parent 3a12066 commit bb35486

37 files changed

+338
-172
lines changed

apps/cart-e2e/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"options": {
1010
"cypressConfig": "apps/cart-e2e/cypress.config.ts",
1111
"devServerTarget": "cart:serve",
12-
"testingType": "e2e"
12+
"testingType": "e2e",
13+
"baseUrl": "http://localhost:4200"
1314
},
1415
"configurations": {
1516
"production": {

apps/cart/.env.serve

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BASE_API_PATH=/.netlify/functions

apps/cart/functions/api/api.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Handler } from '@netlify/functions';
2+
import fastify, { FastifyInstance, FastifyRequest } from 'fastify';
3+
import awsLambdaFastify from '@fastify/aws-lambda';
4+
import sensible from '@fastify/sensible';
5+
import { products } from '@nx-example/shared/product/data';
6+
import cors from '@fastify/cors';
7+
import { randomUUID } from 'crypto';
8+
9+
async function routes(fastify: FastifyInstance) {
10+
fastify.get('/products', async () => {
11+
return products;
12+
});
13+
fastify.post(
14+
'/checkout',
15+
async (
16+
request: FastifyRequest<{
17+
Body: { productId: string; quanntity: number }[];
18+
}>
19+
) => {
20+
const items = request.body;
21+
console.log(request.body);
22+
const price = items.reduce((acc, item) => {
23+
const product = products.find((p) => p.id === item.productId);
24+
return acc + product.price * item.quanntity;
25+
}, 0);
26+
27+
// gotta think real hard
28+
await new Promise((resolve) => setTimeout(resolve, Math.random() * 1000));
29+
30+
return { success: true, orderId: randomUUID(), total: price };
31+
}
32+
);
33+
}
34+
35+
function init() {
36+
const app = fastify();
37+
app.register(sensible);
38+
app.register(cors);
39+
// set the prefix for the netlify functions url
40+
app.register(routes, {
41+
prefix: `${process.env.BASE_API_PATH || ''}/api`,
42+
});
43+
return app;
44+
}
45+
46+
// Note: Netlify deploys this function at the endpoint /.netlify/functions/api
47+
export const handler: Handler = awsLambdaFastify(init());

apps/cart/project.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"outputs": ["{options.outputPath}"],
7171
"defaultConfiguration": "production"
7272
},
73-
"serve": {
73+
"serve-app": {
7474
"executor": "@nx/webpack:dev-server",
7575
"options": {
7676
"buildTarget": "cart:build"
@@ -88,7 +88,10 @@
8888
"lint": {
8989
"executor": "@nx/linter:eslint",
9090
"options": {
91-
"lintFilePatterns": ["apps/cart/**/*.{ts,tsx,js,jsx}"]
91+
"lintFilePatterns": [
92+
"apps/cart/**/*.{ts,tsx,js,jsx}",
93+
"./functions/**/*.ts"
94+
]
9295
},
9396
"outputs": ["{options.outputFile}"]
9497
},
@@ -100,15 +103,18 @@
100103
},
101104
"outputs": ["{workspaceRoot}/coverage/apps/cart"]
102105
},
103-
"deploy": {
106+
"serve": {
104107
"executor": "nx:run-commands",
105108
"options": {
106109
"commands": [
107-
{
108-
"command": "npx ts-node --project tools/tsconfig.tools.json tools/scripts/deploy --siteName nrwl-nx-examples-cart --outputPath dist/apps/cart"
109-
}
110+
"BROWSER=false netlify dev --functions=apps/cart/functions",
111+
"nx serve-app cart"
110112
]
111113
}
114+
},
115+
"deploy": {
116+
"dependsOn": ["build"],
117+
"command": "netlify deploy --site=nx-examples-cart-test --dir dist/apps/cart --functions apps/cart/functions --prod"
112118
}
113119
},
114120
"tags": ["type:app", "scope:cart"],

apps/cart/public/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Netlify Functions</h1>
2+
<p>
3+
The sample function is available at
4+
<a href="/.netlify/functions/hello"><code>/.netlify/functions/hello</code></a
5+
>.
6+
</p>

apps/cart/src/_redirects

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/cart/* /index.html 200
2-
/ https://nrwl-nx-examples-products.netlify.com/ 301
3-
* https://nrwl-nx-examples-products.netlify.com/:splat 301
2+
/api/* https://nx-examples-cart-test.netlify.app/.netlify/functions/api/:splat 200
3+
/ https://nx-examples-products-test.netlify.com/cart 301
4+
* https://nx-examples-products-test.netlify.com/:splat 301

apps/cart/src/app/app.spec.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { MemoryRouter } from 'react-router-dom';
22

33
import { cleanup, render } from '@testing-library/react';
44

5+
jest.doMock('@nx-example/cart/cart-page');
6+
// eslint-disable-next-line
57
import App from './app';
68

79
describe('App', () => {

apps/cart/src/app/app.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import { Route, Routes } from 'react-router-dom';
33
import '@nx-example/shared/header';
44

55
import { CartCartPage } from '@nx-example/cart/cart-page';
6+
import { environment } from '../environments/environment';
67

78
export const App = () => {
89
return (
910
<>
1011
<nx-example-header />
1112
<Routes>
12-
<Route path="/cart" element={<CartCartPage />} />
13+
<Route
14+
path="/cart"
15+
element={<CartCartPage baseUrl={environment.baseApiPath} />}
16+
/>
1317
</Routes>
1418
</>
1519
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const environment = {
22
production: true,
3+
baseApiPath: '',
34
};

apps/cart/src/environments/environment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44
export const environment = {
55
production: false,
6+
baseApiPath: 'http://localhost:8888/.netlify/functions',
67
};

0 commit comments

Comments
 (0)