-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a12066
commit bb35486
Showing
37 changed files
with
338 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BASE_API_PATH=/.netlify/functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Handler } from '@netlify/functions'; | ||
import fastify, { FastifyInstance, FastifyRequest } from 'fastify'; | ||
import awsLambdaFastify from '@fastify/aws-lambda'; | ||
import sensible from '@fastify/sensible'; | ||
import { products } from '@nx-example/shared/product/data'; | ||
import cors from '@fastify/cors'; | ||
import { randomUUID } from 'crypto'; | ||
|
||
async function routes(fastify: FastifyInstance) { | ||
fastify.get('/products', async () => { | ||
return products; | ||
}); | ||
fastify.post( | ||
'/checkout', | ||
async ( | ||
request: FastifyRequest<{ | ||
Body: { productId: string; quanntity: number }[]; | ||
}> | ||
) => { | ||
const items = request.body; | ||
console.log(request.body); | ||
const price = items.reduce((acc, item) => { | ||
const product = products.find((p) => p.id === item.productId); | ||
return acc + product.price * item.quanntity; | ||
}, 0); | ||
|
||
// gotta think real hard | ||
await new Promise((resolve) => setTimeout(resolve, Math.random() * 1000)); | ||
|
||
return { success: true, orderId: randomUUID(), total: price }; | ||
} | ||
); | ||
} | ||
|
||
function init() { | ||
const app = fastify(); | ||
app.register(sensible); | ||
app.register(cors); | ||
// set the prefix for the netlify functions url | ||
app.register(routes, { | ||
prefix: `${process.env.BASE_API_PATH || ''}/api`, | ||
}); | ||
return app; | ||
} | ||
|
||
// Note: Netlify deploys this function at the endpoint /.netlify/functions/api | ||
export const handler: Handler = awsLambdaFastify(init()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Netlify Functions</h1> | ||
<p> | ||
The sample function is available at | ||
<a href="/.netlify/functions/hello"><code>/.netlify/functions/hello</code></a | ||
>. | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/cart/* /index.html 200 | ||
/ https://nrwl-nx-examples-products.netlify.com/ 301 | ||
* https://nrwl-nx-examples-products.netlify.com/:splat 301 | ||
/api/* https://nx-examples-cart-test.netlify.app/.netlify/functions/api/:splat 200 | ||
/ https://nx-examples-products-test.netlify.com/cart 301 | ||
* https://nx-examples-products-test.netlify.com/:splat 301 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const environment = { | ||
production: true, | ||
baseApiPath: '', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
|
||
export const environment = { | ||
production: false, | ||
baseApiPath: 'http://localhost:8888/.netlify/functions', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BASE_API_PATH=/.netlify/functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/cart https://nrwl-nx-examples-cart.netlify.com/cart 301 | ||
/* /index.html 200 | ||
/api/* https://nx-examples-products-test.netlify.app/.netlify/functions/api/:splat 200 | ||
/cart https://nx-examples-cart-test.netlify.com/cart 301 | ||
/* /index.html 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const environment = { | ||
production: true, | ||
baseApiPath: '', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.