Skip to content

Commit 1853cd6

Browse files
authored
Merge pull request #3 from agnostack/feat/ngrok
Added ngrok, clean up minor things
2 parents 2d2e909 + d4f0b77 commit 1853cd6

File tree

9 files changed

+530
-40
lines changed

9 files changed

+530
-40
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
BASE_API_PATH=http://localhost:9032
12
API_CLIENT_ID=
23
API_CLIENT_SECRET=
34
INTEGRATION_PUBLIC_KEY=

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ node_modules/
1111
# Optional npm cache directory
1212
.npm
1313

14+
.nx
15+
1416
# Yarn Integrity file
1517
.yarn-integrity
1618

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,29 @@ Populate `.env` with the provider values from agnoStack.
3838
yarn watch
3939
```
4040

41-
Post requests can then be made via `http://localhost:3000/dev/agnostack/orders/12345`
41+
NOTE: this will run your local project via serverless offline AND also generate an ngrok URL that you can then use to access.
42+
43+
Requests can then be made via `https://<<generated>>.ngrok.app/dev/agnostack/<<xyz-api-route>>` or `http://localhost:3000/dev/agnostack/<<xyz-api-route>>`
4244

4345
## AWS Deployment
4446

4547
```bash
4648
yarn deploy
4749
```
4850

51+
## Postman Collection
52+
53+
agnoStack API sample postman requests available via: https://agnostack.dev/postman.json
54+
4955
## Making requests
5056

5157
All requests must contain the following request headers, provided from agnoStack.
5258

53-
- x-organization-id
54-
- x-providerstack-id
59+
- X-Organization-Id
60+
- X-Providerstack-Id
5561

5662
```bash
5763
curl --location --request POST 'http://localhost:3000/dev/agnostack/orders/12345' \
58-
--header 'x-organization-id: YOUR_ORGANIZATION_ID' \
59-
--header 'x-providerstack-id: YOUR_PROVIDERSTACK_ID'
64+
--header 'X-Organization-Id: YOUR_ORGANIZATION_ID' \
65+
--header 'X-Providerstack-Id: YOUR_PROVIDERSTACK_ID'
6066
```

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212
"node": ">=18.x"
1313
},
1414
"scripts": {
15-
"deploy": "yarn serverless deploy",
16-
"watch": "yarn serverless offline"
15+
"nx": "nx",
16+
"deploy": "serverless deploy",
17+
"dev": "serverless offline",
18+
"watch": "nx run dev-ngrok --port=4000"
1719
},
1820
"dependencies": {
21+
"@agnostack/env": "latest",
1922
"@agnostack/verifyd": "latest"
2023
},
2124
"devDependencies": {
25+
"chalk": "4.1.2",
26+
"nx": "latest",
27+
"ngrok": "latest",
2228
"serverless": "3.x",
2329
"serverless-dotenv-plugin": "6.0.0",
2430
"serverless-offline": "12.x"

project.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"targets": {
3+
"dev-ngrok": {
4+
"executor": "nx:run-commands",
5+
"options": {
6+
"commands": [
7+
"nx run @agnostack/secure-api-proxy:dev --param=\"port={args.port}\" --output-style=stream --nx-bail=true",
8+
"node scripts/ngrok --port={args.port}"
9+
],
10+
"color": true
11+
}
12+
}
13+
}
14+
}

scripts/ngrok.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-disable no-param-reassign, import/no-extraneous-dependencies */
2+
const chalk = require('chalk')
3+
const ngrok = require('ngrok')
4+
5+
const { parseEnvData } = require('@agnostack/env')
6+
7+
const { params } = parseEnvData()
8+
9+
const getNgrok = async (_addr = 8000) => {
10+
const addr = params.addr || params.port || params.PORT || _addr // port or network address
11+
12+
const url = await ngrok.connect({
13+
proto: 'http', // http|tcp|tls, defaults to http
14+
addr,
15+
...params,
16+
})
17+
18+
return { url, addr }
19+
}
20+
21+
module.exports = getNgrok
22+
23+
if (require.main === module) {
24+
// NOTE: this can be run direct with node /scripts/ngrok.js --port=12345
25+
getNgrok().then(({ url, addr }) => {
26+
console.log(`${chalk.yellowBright('ngrok')} - ${chalk.cyan('info')} - [HTTP] server ready (${chalk.yellow(addr)}): ${chalk.yellow(url)} 🚀🚀`)
27+
})
28+
}

serverless.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ plugins:
88
custom:
99
serverless-offline:
1010
reloadHandler: true
11+
httpPort: ${param:port}
1112

1213
provider:
1314
name: aws
@@ -18,7 +19,7 @@ provider:
1819

1920
functions:
2021
proxy:
21-
handler: handlers.proxy
22+
handler: src/handlers.proxy
2223
events:
2324
- http:
2425
path: /agnostack/{route+}

handlers.js renamed to src/handlers.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99
API_CLIENT_ID,
1010
API_CLIENT_SECRET,
1111
INTEGRATION_PUBLIC_KEY,
12-
INTEGRATION_DISABLE_RECRYPTION,
12+
INTEGRATION_DISABLE_RECRYPTION, // NOTE: this will not work unless running your own local BASE_API_PATH
1313
} = process.env
1414

1515
const BASE_HEADERS = {
@@ -30,25 +30,23 @@ const proxy = async (event) => {
3030

3131
try {
3232
const keysData = await getVerificationKeysData(INTEGRATION_PUBLIC_KEY)
33+
3334
const _prepareVerificationRequest = prepareVerificationRequest({ keysData, disableRecryption: INTEGRATION_DISABLE_RECRYPTION })
3435
const _processVerificationResponse = processVerificationResponse({ keysData, disableRecryption: INTEGRATION_DISABLE_RECRYPTION })
3536

36-
const url = `${BASE_API_PATH}/${event?.pathParameters?.route}`
37-
const options = {
37+
const [
38+
requestPath,
39+
requestOptions,
40+
derivedSecretKey
41+
] = await _prepareVerificationRequest(`${BASE_API_PATH}/${event?.pathParameters?.route}`, {
3842
method: 'POST',
3943
body: JSON.parse(event?.body ?? '{}'),
4044
headers: {
4145
...filterHeaders(event?.headers),
4246
'X-Client-Id': API_CLIENT_ID,
4347
'X-Client-Secret': API_CLIENT_SECRET,
4448
},
45-
}
46-
47-
const [
48-
requestPath,
49-
requestOptions,
50-
derivedSecretKey
51-
] = await _prepareVerificationRequest(url, options) ?? []
49+
}) ?? []
5250

5351
const encryptedResponse = await fetch(requestPath, requestOptions).then((_response) => (
5452
_response.json()

0 commit comments

Comments
 (0)