From 372cd25de7030228bd12ea62eeaff824c7f6526c Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Tue, 11 Feb 2020 17:48:15 +0200 Subject: [PATCH] feat: REST api handling incoming msg --- examples/rest-api/README.md | 8 ++++++++ examples/rest-api/index.ts | 18 ++++++++++++++---- examples/rest-api/tsconfig.json | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/examples/rest-api/README.md b/examples/rest-api/README.md index e9db6a632..60104e6b9 100644 --- a/examples/rest-api/README.md +++ b/examples/rest-api/README.md @@ -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' +``` diff --git a/examples/rest-api/index.ts b/examples/rest-api/index.ts index 386bef358..6c83de2cd 100644 --- a/examples/rest-api/index.ts +++ b/examples/rest-api/index.ts @@ -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)) @@ -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}`)) diff --git a/examples/rest-api/tsconfig.json b/examples/rest-api/tsconfig.json index 45481ece2..295aa4d48 100644 --- a/examples/rest-api/tsconfig.json +++ b/examples/rest-api/tsconfig.json @@ -4,7 +4,7 @@ "strict": true, "sourceMap": true, "target": "es5", - "rootDir": "./src", + "rootDir": "./", "outDir": "./build", "moduleResolution": "node", "esModuleInterop": true,