Skip to content

Commit

Permalink
Added version to gatekeeper endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
macterra committed Mar 19, 2024
1 parent 13de9ef commit 30dc2f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
20 changes: 11 additions & 9 deletions gatekeeper-sdk.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
}
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
21 changes: 12 additions & 9 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ 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) {
res.status(500).send(error.toString());
}
});

app.post('/did', async (req, res) => {
v1router.post('/did', async (req, res) => {
try {
const txn = req.body;
const did = await gatekeeper.createDID(txn);
Expand All @@ -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);
Expand All @@ -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 = '<html><body>';
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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, () => {
Expand Down

0 comments on commit 30dc2f7

Please sign in to comment.