Skip to content

Commit

Permalink
fix: better handling of Accept headers (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntn-x2 authored Dec 16, 2021
1 parent 873b8fa commit 5971e52
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"rules": {
"no-console": "off",
"import/prefer-default-export": "off",
"no-restricted-syntax": [
"error",
{
Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { Did, init, connect } = require('@kiltprotocol/sdk-js')

const { PORT, BLOCKCHAIN_NODE } = require('./config')
const { URI_DID } = require('./consts')
const { processAcceptHeaders } = require('./utils')

const driver = express()

Expand All @@ -25,7 +26,10 @@ async function start() {
console.log('--------------------')
console.info('\n→ Received headers:')
console.info(JSON.stringify(req.headers, null, 2))
const { exportType, defaultExport, responseContentType } = processAcceptHeaders(req.headers.accept)
const { did } = req.params
// Add queried DID to default export for deleted resolutions
defaultExport.id = did

let didResolutionResult
// Throws if the address is not a valid address
Expand All @@ -51,8 +55,8 @@ async function start() {
// which is represented by the sole `id` property.
// https://www.w3.org/TR/did-core/#did-document-properties
const didDocument = didResolutionResult.details ?
Did.exportToDidDocument(didResolutionResult.details, 'application/ld+json') :
{ id: did, '@context': ['https://www.w3.org/ns/did/v1'] }
Did.exportToDidDocument(didResolutionResult.details, exportType) :
defaultExport

const exportedDidDocument = {
didDocument,
Expand All @@ -62,6 +66,7 @@ async function start() {
console.trace('\n← Exported DID document:')
console.trace(JSON.stringify(exportedDidDocument, null, 2))

res.contentType(responseContentType)
res.send(exportedDidDocument)
} catch (error) {
console.error("\n🚨 Could not satisfy request because of the following error:")
Expand Down
29 changes: 29 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2018-2021 BOTLabs GmbH.
*
* This source code is licensed under the BSD 4-Clause "Original" license
* found in the LICENSE file in the root directory of this source tree.
*/

function processAcceptHeaders(headers) {
const acceptedTypes = new Set([...headers.split(',').map((c) => c.trim())])
let exportType
let defaultExport
let responseContentType
if (acceptedTypes.has('application/did+json')) {
exportType = 'application/json'
defaultExport = {}
responseContentType = 'application/did+json'
} else {
exportType = 'application/ld+json'
defaultExport = { '@context': ['https://www.w3.org/ns/did/v1'] }
responseContentType = 'application/did+ld+json'
}
return {
exportType,
defaultExport,
responseContentType
}
}

module.exports = { processAcceptHeaders }

0 comments on commit 5971e52

Please sign in to comment.