This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shared Metadata): extracted API
- Loading branch information
Showing
5 changed files
with
125 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
const API = require('./api'); | ||
const S = {}; | ||
|
||
S.request = function (method, endpoint, data, authToken) { | ||
var url = API.API_ROOT_URL + 'metadata/' + endpoint; | ||
var options = { | ||
headers: { 'Content-Type': 'application/json' }, | ||
credentials: 'omit' | ||
}; | ||
if (authToken) { | ||
options.headers.Authorization = 'Bearer ' + authToken; | ||
} | ||
// encodeFormData :: Object -> url encoded params | ||
var encodeFormData = function (data) { | ||
if (!data) return ''; | ||
var encoded = Object.keys(data).map(function (k) { | ||
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]); | ||
}).join('&'); | ||
return encoded ? '?' + encoded : encoded; | ||
}; | ||
if (data && data !== {}) { | ||
if (method === 'GET') { | ||
url += encodeFormData(data); | ||
} else { | ||
options.body = JSON.stringify(data); | ||
} | ||
} | ||
options.method = method; | ||
var handleNetworkError = function (e) { | ||
return Promise.reject({ error: 'SHARED_METADATA_CONNECT_ERROR', message: e }); | ||
}; | ||
var checkStatus = function (response) { | ||
if (response.ok) { | ||
return response.json(); | ||
} else { | ||
return response.text().then(Promise.reject.bind(Promise)); | ||
} | ||
}; | ||
return fetch(url, options) | ||
.catch(handleNetworkError) | ||
.then(checkStatus); | ||
}; | ||
|
||
// authentication | ||
S.getAuth = () => S.request('GET', 'auth'); | ||
S.postAuth = (data) => S.request('POST', 'auth', data); | ||
|
||
// messages | ||
S.getMessages = (token, onlyNew) => S.request('GET', 'messages', onlyNew ? {new: true} : {}, token); | ||
S.getMessage = (token, uuid) => S.request('GET', 'message/' + uuid, null, token); | ||
S.sendMessage = (token, recipient, payload, signature, type) => | ||
S.request('POST', 'messages', {type, payload, signature, recipient}, token); | ||
S.processMessage = (token, uuid) => S.request('PUT', 'message/' + uuid + '/processed', null, token); | ||
|
||
// trusted contact list | ||
S.addTrusted = (token, mdid) => S.request('PUT', 'trusted/' + mdid, null, token); | ||
S.getTrusted = (token, mdid) => S.request('GET', 'trusted/' + mdid, null, token); | ||
S.deleteTrusted = (token, mdid) => S.request('DELETE', 'trusted/' + mdid, null, mdid); | ||
S.getTrustedList = (token) => S.request('GET', 'trusted', null, token); | ||
|
||
// invitation process | ||
S.createInvitation = (token) => S.request('POST', 'share', null, token); | ||
S.readInvitation = (token, uuid) => S.request('GET', 'share/' + uuid, null, token); | ||
S.acceptInvitation = (token, uuid) => S.request('POST', 'share/' + uuid, null, token); | ||
S.deleteInvitation = (token, uuid) => S.request('DELETE', 'share/' + uuid, null, token); | ||
|
||
module.exports = S; |