Skip to content

Commit

Permalink
Parse JSON internally when content-type=json is missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
gannan08 committed Dec 5, 2024
1 parent 7f65af5 commit 77a97cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# bedrock-jsonld-document-loader ChangeLog

## 5.2.0 - 2024-12-xx

### Changed
- Handle parsing of JSON internally when `content-type` does not include `json`.

## 5.1.0 - 2024-07-31

### Changed
Expand Down
17 changes: 16 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {agent} from '@bedrock/https-agent';
import {config} from '@bedrock/core';
import {httpClient} from '@digitalbazaar/http-client';
import {JsonLdDocumentLoader} from 'jsonld-document-loader';
import {logger} from './logger.js';

import './config.js';

Expand All @@ -31,10 +32,24 @@ export const httpClientHandler = {
throw new Error('NotFoundError');
}

return result.data;
return _getJsonData(result);
}
};

async function _getJsonData(result) {
if(result.data) {
return result.data;
}

try {
return await result.json();
} catch(e) {
const {url} = result;
logger.error(`DataError: could not parse JSON from: "${url}"`);
throw new Error('DataError');
}
}

export const jsonLdDocumentLoader = jdl;
export const documentLoader = jdl.documentLoader.bind(jdl);
export {JsonLdDocumentLoader};
7 changes: 7 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*!
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
import {loggers} from '@bedrock/core';

const pkgName = 'bedrock-jsonld-document-loader';
export const logger = loggers.get('app').child(pkgName);

0 comments on commit 77a97cd

Please sign in to comment.