Skip to content

Added ngrok, clean up minor things #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example → .env.local.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
BASE_API_PATH=http://localhost:9032
API_CLIENT_ID=
API_CLIENT_SECRET=
INTEGRATION_PUBLIC_KEY=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ node_modules/
# Optional npm cache directory
.npm

.nx

# Yarn Integrity file
.yarn-integrity

Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,29 @@ Populate `.env` with the provider values from agnoStack.
yarn watch
```

Post requests can then be made via `http://localhost:3000/dev/agnostack/orders/12345`
NOTE: this will run your local project via serverless offline AND also generate an ngrok URL that you can then use to access.

Requests can then be made via `https://<<generated>>.ngrok.app/dev/agnostack/<<xyz-api-route>>` or `http://localhost:3000/dev/agnostack/<<xyz-api-route>>`

## AWS Deployment

```bash
yarn deploy
```

## Postman Collection

agnoStack API sample postman requests available via: https://agnostack.dev/postman.json

## Making requests

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

- x-organization-id
- x-providerstack-id
- X-Organization-Id
- X-Providerstack-Id

```bash
curl --location --request POST 'http://localhost:3000/dev/agnostack/orders/12345' \
--header 'x-organization-id: YOUR_ORGANIZATION_ID' \
--header 'x-providerstack-id: YOUR_PROVIDERSTACK_ID'
--header 'X-Organization-Id: YOUR_ORGANIZATION_ID' \
--header 'X-Providerstack-Id: YOUR_PROVIDERSTACK_ID'
```
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
"node": ">=18.x"
},
"scripts": {
"deploy": "yarn serverless deploy",
"watch": "yarn serverless offline"
"nx": "nx",
"deploy": "serverless deploy",
"dev": "serverless offline",
"watch": "nx run dev-ngrok --port=4000"
},
"dependencies": {
"@agnostack/env": "latest",
"@agnostack/verifyd": "latest"
},
"devDependencies": {
"chalk": "4.1.2",
"nx": "latest",
"ngrok": "latest",
"serverless": "3.x",
"serverless-dotenv-plugin": "6.0.0",
"serverless-offline": "12.x"
Expand Down
14 changes: 14 additions & 0 deletions project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"targets": {
"dev-ngrok": {
"executor": "nx:run-commands",
"options": {
"commands": [
"nx run @agnostack/secure-api-proxy:dev --param=\"port={args.port}\" --output-style=stream --nx-bail=true",
"node scripts/ngrok --port={args.port}"
],
"color": true
}
}
}
}
28 changes: 28 additions & 0 deletions scripts/ngrok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable no-param-reassign, import/no-extraneous-dependencies */
const chalk = require('chalk')
const ngrok = require('ngrok')

const { parseEnvData } = require('@agnostack/env')

const { params } = parseEnvData()

const getNgrok = async (_addr = 8000) => {
const addr = params.addr || params.port || params.PORT || _addr // port or network address

const url = await ngrok.connect({
proto: 'http', // http|tcp|tls, defaults to http
addr,
...params,
})

return { url, addr }
}

module.exports = getNgrok

if (require.main === module) {
// NOTE: this can be run direct with node /scripts/ngrok.js --port=12345
getNgrok().then(({ url, addr }) => {
console.log(`${chalk.yellowBright('ngrok')} - ${chalk.cyan('info')} - [HTTP] server ready (${chalk.yellow(addr)}): ${chalk.yellow(url)} 🚀🚀`)
})
}
3 changes: 2 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins:
custom:
serverless-offline:
reloadHandler: true
httpPort: ${param:port}

provider:
name: aws
Expand All @@ -18,7 +19,7 @@ provider:

functions:
proxy:
handler: handlers.proxy
handler: src/handlers.proxy
events:
- http:
path: /agnostack/{route+}
Expand Down
18 changes: 8 additions & 10 deletions handlers.js → src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
API_CLIENT_ID,
API_CLIENT_SECRET,
INTEGRATION_PUBLIC_KEY,
INTEGRATION_DISABLE_RECRYPTION,
INTEGRATION_DISABLE_RECRYPTION, // NOTE: this will not work unless running your own local BASE_API_PATH
} = process.env

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

try {
const keysData = await getVerificationKeysData(INTEGRATION_PUBLIC_KEY)

const _prepareVerificationRequest = prepareVerificationRequest({ keysData, disableRecryption: INTEGRATION_DISABLE_RECRYPTION })
const _processVerificationResponse = processVerificationResponse({ keysData, disableRecryption: INTEGRATION_DISABLE_RECRYPTION })

const url = `${BASE_API_PATH}/${event?.pathParameters?.route}`
const options = {
const [
requestPath,
requestOptions,
derivedSecretKey
] = await _prepareVerificationRequest(`${BASE_API_PATH}/${event?.pathParameters?.route}`, {
method: 'POST',
body: JSON.parse(event?.body ?? '{}'),
headers: {
...filterHeaders(event?.headers),
'X-Client-Id': API_CLIENT_ID,
'X-Client-Secret': API_CLIENT_SECRET,
},
}

const [
requestPath,
requestOptions,
derivedSecretKey
] = await _prepareVerificationRequest(url, options) ?? []
}) ?? []

const encryptedResponse = await fetch(requestPath, requestOptions).then((_response) => (
_response.json()
Expand Down
Loading