Created
December 19, 2021 13:10
-
-
Save leegilmorecode/1f73f423627b3caa604774d873b6da6d to your computer and use it in GitHub Desktop.
Example for blog: https://github.com/leegilmorecode/serverless-private-apis-part-2
This file contains hidden or 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
| import axios from "axios"; | |
| type Stock = { | |
| stockId: number; | |
| description: string; | |
| }; | |
| type StockResponse = { | |
| stock: Stock[]; | |
| }; | |
| async function handler(): Promise<{ body: string; statusCode: number }> { | |
| console.log("create-order.handler - started"); | |
| const domain = process.env.STOCK_DOMAIN; | |
| console.log(`create-order.handler - calling: https://${domain}/prod/stock`); // e.g. stock.yourdomain.co.uk | |
| const result = await axios.get( | |
| `https://${domain}/prod/stock`, // this is the private api dns entry | |
| { | |
| headers: { | |
| "x-api-key": "super-secret-api-key", // this is the api key for our private api | |
| }, | |
| } | |
| ); | |
| const data: StockResponse = result.data; | |
| console.log(`create-order.handler - private call is successful: ${data}`); | |
| return { | |
| body: JSON.stringify(data), | |
| statusCode: 200, | |
| }; | |
| } | |
| module.exports = { handler }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment