Skip to content

Commit

Permalink
feat: REST api handling incoming msg
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Feb 11, 2020
1 parent 385a0eb commit 372cd25
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
8 changes: 8 additions & 0 deletions examples/rest-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ curl --location --request POST 'http://localhost:8080/handle-action' \
}
}'
```

### Handle new message

```
curl --location --request POST 'http://localhost:8080/handle-message' \
--header 'Content-Type: application/javascript' \
--data-raw 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1ODE0MzM2NDEsInN1YiI6ImRpZDp3ZWI6dXBvcnQubWUiLCJ2YyI6eyJAY29udGV4dCI6WyJodHRwczovL3d3dy53My5vcmcvMjAxOC9jcmVkZW50aWFscy92MSJdLCJ0eXBlIjpbIlZlcmlmaWFibGVDcmVkZW50aWFsIl0sImNyZWRlbnRpYWxTdWJqZWN0Ijp7InlvdSI6IlJvY2sifX0sImlzcyI6ImRpZDpldGhyOnJpbmtlYnk6MHg3ZGQyODMyOGM4YjA1YTg1MjgzOTI4NGMzMDRiNGJkNGI2MjhlMzU3In0.xJ6LY91PPIhFojWAfmBaSAqBNIDPShJRvH2ZyZlenCEHATclUD_f2vHFSQrFXGSP88JfdNQWzK6PqXgGWoqECgE'
```
18 changes: 14 additions & 4 deletions examples/rest-api/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import express from 'express'
const app = express()
const port = process.env.PORT || 8080
import { Message } from 'daf-core'

import { core } from './setup'

app.use(express.json())

app.get('/identities', async (req, res) => {
const identities = await core.identityManager.getIdentities()
res.json(identities.map(identity => identity.did))
Expand All @@ -16,14 +15,25 @@ app.get('/providers', async (req, res) => {
res.json(providers)
})

app.post('/create-identity', async (req, res) => {
app.post('/create-identity', express.json(), async (req, res) => {
const identity = await core.identityManager.createIdentity(req.body.type)
res.json({ did: identity.did })
})

app.post('/handle-action', async (req, res) => {
app.post('/handle-action', express.json(), async (req, res) => {
const result = await core.handleAction(req.body)
res.json({ result })
})

app.post('/handle-message', express.text({ type: '*/*' }), async (req, res) => {
try {
const result = await core.validateMessage(
new Message({ raw: req.body, meta: { type: 'serviceEndpoint', id: '/handle-message' } }),
)
res.json({ id: result.id })
} catch (e) {
res.send(e.message)
}
})

app.listen(port, () => console.log(`Server running at http://localhost:${port}`))
2 changes: 1 addition & 1 deletion examples/rest-api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"strict": true,
"sourceMap": true,
"target": "es5",
"rootDir": "./src",
"rootDir": "./",
"outDir": "./build",
"moduleResolution": "node",
"esModuleInterop": true,
Expand Down

0 comments on commit 372cd25

Please sign in to comment.