Skip to content

Commit

Permalink
Add a demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
swansontec committed Aug 5, 2022
1 parent 94a54e6 commit 32b732a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

This server sends push notifications to Edge client apps. It contains an HTTP server that clients can use to register for notifications, and a background process that checks for price changes and actually sends the messages.

The docs folder has can find [an example of how to use the v2 API](./docs/demo.ts).

## Setup

This server requires a working copies of Node.js, Yarn, PM2, and CouchDB. We also recommend using Caddy to terminate SSL connections.
Expand Down
112 changes: 112 additions & 0 deletions docs/demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { asJSON, asMaybe, asObject, asString, uncleaner } from 'cleaners'
import fetch from 'node-fetch'
import { base64 } from 'rfc4648'

import {
asDeviceUpdatePayload,
asLoginUpdatePayload,
asPushRequestBody
} from '../src/types/pushApiTypes'

// We are going to use uncleaners to type-check our payloads:
const wasPushRequestBody = uncleaner(asPushRequestBody)
const wasDeviceUpdatePayload = uncleaner(asDeviceUpdatePayload)
const wasLoginUpdatePayload = uncleaner(asLoginUpdatePayload)

/**
* Failed requests usually return this as their body.
*/
const asErrorBody = asJSON(
asObject({
error: asString
})
)

const apiKey = 'demo-api-key'
const deviceId = 'example-device'
const loginId = base64.parse('EE+tBb5wM63qwCDVidzwUQThH9ekCSfpUuTQYujSmY8=')

/**
* All push server HTTP methods use "POST" with JSON.
*/
async function postJson(uri: string, body: unknown): Promise<unknown> {
console.log(JSON.stringify(body, null, 1))
const response = await fetch(uri, {
body: JSON.stringify(body),
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
method: 'POST'
})
if (!response.ok) {
const error = asMaybe(asErrorBody)(await response.text())
let message = `POST ${uri} returned ${response.status}`
if (error != null) message += `: ${error.error}`
throw new Error(message)
}
return await response.json()
}

async function main(): Promise<void> {
// Create a device:
await postJson(
'http://127.0.0.1:8001/v2/device/update/',
wasPushRequestBody({
apiKey,
deviceId,
data: wasDeviceUpdatePayload({ loginIds: [loginId] })
})
)
console.log(`Updated device "${deviceId}"`)

// Grab the device status:
console.log(
await postJson(
'http://127.0.0.1:8001/v2/device/',
wasPushRequestBody({ apiKey, deviceId })
)
)

// Subscribe the user to a price change:
await postJson(
'http://127.0.0.1:8001/v2/login/update/',
wasPushRequestBody({
apiKey,
deviceId,
loginId,
data: wasLoginUpdatePayload({
createEvents: [
{
eventId: 'demo-event',
pushMessage: {
title: 'Example title',
body: 'Example body',
data: { what: 'happened' }
},
recurring: false,
trigger: {
type: 'price-level',
currencyPair: 'BTC-USD',
aboveRate: 50000
}
}
]
})
})
)
console.log(`Updated login "${base64.stringify(loginId)}"`)

// Grab the login status:
console.log(
await postJson(
'http://127.0.0.1:8001/v2/login/',
wasPushRequestBody({ apiKey, deviceId, loginId })
)
)
}

main().catch(error => {
console.error(String(error))
process.exitCode = 1
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"scripts": {
"build": "sucrase -q -t typescript,imports -d ./lib ./src",
"clean": "rimraf lib",
"demo": "node -r sucrase/register docs/demo.ts",
"fix": "yarn-deduplicate && eslint . --fix",
"lint": "eslint .",
"precommit": "lint-staged && npm-run-all types prepare",
Expand Down

0 comments on commit 32b732a

Please sign in to comment.