From 30dc2f7d7a3dfa38b74ff3f7ccd9c3e415348cc4 Mon Sep 17 00:00:00 2001 From: David McFadzean Date: Tue, 19 Mar 2024 14:52:38 -0400 Subject: [PATCH] Added version to gatekeeper endpoints --- gatekeeper-sdk.js | 20 +++++++++++--------- server.js | 21 ++++++++++++--------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/gatekeeper-sdk.js b/gatekeeper-sdk.js index 7eac89e4..7cf6cf27 100644 --- a/gatekeeper-sdk.js +++ b/gatekeeper-sdk.js @@ -1,6 +1,8 @@ import axios from 'axios'; import config from './config.js'; +const URL = `${config.gatekeeperURL}/api/v1`; + function throwError(error) { if (error.response) { throw error.response.data; @@ -17,7 +19,7 @@ export async function stop() { export async function getVersion() { try { - const response = await axios.get(`${config.gatekeeperURL}/version/`); + const response = await axios.get(`${URL}/version/`); return response.data; } catch (error) { @@ -27,7 +29,7 @@ export async function getVersion() { export async function createDID(txn) { try { - const response = await axios.post(`${config.gatekeeperURL}/did/`, txn); + const response = await axios.post(`${URL}/did/`, txn); return response.data; } catch (error) { @@ -38,11 +40,11 @@ export async function createDID(txn) { export async function resolveDID(did, asof = null) { try { if (asof) { - const response = await axios.get(`${config.gatekeeperURL}/did/${did}?asof=${asof}`); + const response = await axios.get(`${URL}/did/${did}?asof=${asof}`); return response.data; } else { - const response = await axios.get(`${config.gatekeeperURL}/did/${did}`); + const response = await axios.get(`${URL}/did/${did}`); return response.data; } } @@ -53,7 +55,7 @@ export async function resolveDID(did, asof = null) { export async function updateDID(txn) { try { - const response = await axios.post(`${config.gatekeeperURL}/did/${txn.did}`, txn); + const response = await axios.post(`${URL}/did/${txn.did}`, txn); return response.data; } catch (error) { @@ -63,7 +65,7 @@ export async function updateDID(txn) { export async function deleteDID(txn) { try { - const response = await axios.delete(`${config.gatekeeperURL}/did/${txn.did}`, { data: txn }); + const response = await axios.delete(`${URL}/did/${txn.did}`, { data: txn }); return response.data; } catch (error) { @@ -73,7 +75,7 @@ export async function deleteDID(txn) { export async function exportDID(did) { try { - const response = await axios.get(`${config.gatekeeperURL}/export/${did}`); + const response = await axios.get(`${URL}/export/${did}`); return response.data; } catch (error) { @@ -83,7 +85,7 @@ export async function exportDID(did) { export async function importDID(txns) { try { - const response = await axios.post(`${config.gatekeeperURL}/import/`, txns); + const response = await axios.post(`${URL}/import/`, txns); return response.data; } catch (error) { @@ -93,7 +95,7 @@ export async function importDID(txns) { export async function mergeBatch(batch) { try { - const response = await axios.post(`${config.gatekeeperURL}/merge/`, batch); + const response = await axios.post(`${URL}/merge/`, batch); return response.data; } catch (error) { diff --git a/server.js b/server.js index 14f20160..214a6a21 100644 --- a/server.js +++ b/server.js @@ -8,11 +8,12 @@ EventEmitter.defaultMaxListeners = 100; gatekeeper.start(); const app = express(); +const v1router = express.Router(); app.use(morgan('dev')); app.use(express.json({ limit: '1mb' })); // Sets the JSON payload limit to 1MB -app.get('/version', async (req, res) => { +v1router.get('/version', async (req, res) => { try { res.json(1); } catch (error) { @@ -20,7 +21,7 @@ app.get('/version', async (req, res) => { } }); -app.post('/did', async (req, res) => { +v1router.post('/did', async (req, res) => { try { const txn = req.body; const did = await gatekeeper.createDID(txn); @@ -31,7 +32,7 @@ app.post('/did', async (req, res) => { } }); -app.get('/did/:did', async (req, res) => { +v1router.get('/did/:did', async (req, res) => { try { const doc = await gatekeeper.resolveDID(req.params.did, req.query.asof); res.json(doc); @@ -41,7 +42,7 @@ app.get('/did/:did', async (req, res) => { } }); -app.get('/explore/:did', async (req, res) => { +v1router.get('/explore/:did', async (req, res) => { try { const doc = await gatekeeper.resolveDID(req.params.did, req.query.asof); var hthead = ''; @@ -67,7 +68,7 @@ app.get('/explore/:did', async (req, res) => { } }); -app.post('/did/:did', async (req, res) => { +v1router.post('/did/:did', async (req, res) => { try { const txn = req.body; const ok = await gatekeeper.updateDID(txn); @@ -78,7 +79,7 @@ app.post('/did/:did', async (req, res) => { } }); -app.delete('/did/:did', async (req, res) => { +v1router.delete('/did/:did', async (req, res) => { try { const txn = req.body; const ok = await gatekeeper.deleteDID(txn); @@ -89,7 +90,7 @@ app.delete('/did/:did', async (req, res) => { } }); -app.get('/export/:did', async (req, res) => { +v1router.get('/export/:did', async (req, res) => { try { const txns = await gatekeeper.exportDID(req.params.did); res.json(txns); @@ -99,7 +100,7 @@ app.get('/export/:did', async (req, res) => { } }); -app.post('/import', async (req, res) => { +v1router.post('/import', async (req, res) => { try { const txns = req.body; const did = await gatekeeper.importDID(txns); @@ -110,7 +111,7 @@ app.post('/import', async (req, res) => { } }); -app.post('/merge', async (req, res) => { +v1router.post('/merge', async (req, res) => { try { const batch = req.body; const did = await gatekeeper.mergeBatch(batch); @@ -123,6 +124,8 @@ app.post('/merge', async (req, res) => { const port = 3000; +app.use('/api/v1', v1router); + gatekeeper.verifyDb().then((invalid) => { if (invalid === 0) { app.listen(port, () => {