Skip to content

Commit 5eb098d

Browse files
pietercolpaertrubensworks
authored andcommitted
Add wellKnownMediaTypes configuration option
Closes #124
1 parent 46611ac commit 5eb098d

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Optionally, the following parameters can be set in the `JsonLdParser` constructo
181181
* `skipContextValidation`: If JSON-LD context validation should be skipped. This is useful when parsing large contexts that are known to be valid. _(Default: `false`)_
182182
* `rdfstar`: If embedded nodes and annotated objects should be parsed according to the [JSON-LD star specification](https://json-ld.github.io/json-ld-star/). _(Default: `true`)_
183183
* `rdfstarReverseInEmbedded`: If embedded nodes in JSON-LD star can have reverse properties. _(Default: `false`)_
184+
* `wellKnownMediaTypes`: an array of media types that can also be parsed as JSON-LD. _(Default: `['application/activity+json']`)_
184185

185186
```javascript
186187
new JsonLdParser({

lib/JsonLdParser.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ export class JsonLdParser extends Transform implements RDF.Sink<EventEmitter, RD
106106
public static fromHttpResponse(baseIRI: string, mediaType: string,
107107
headers?: Headers, options?: IJsonLdParserOptions): JsonLdParser {
108108
let context: JsonLdContext | undefined;
109-
// Special cases when receiving something else than the JSON-LD media type
110-
if (mediaType !== 'application/ld+json') {
109+
let wellKnownMediaTypes = ['application/activity+json'];
110+
if (options && options.wellKnownMediaTypes) {
111+
wellKnownMediaTypes = options.wellKnownMediaTypes;
112+
}
113+
// Special cases when receiving something else than the JSON-LD media type or the wellKnownMediaTypes
114+
if (mediaType !== 'application/ld+json' && !wellKnownMediaTypes.includes(mediaType)) {
111115
// Only accept JSON or JSON extension types
112116
if (mediaType !== 'application/json' && !mediaType.endsWith('+json')) {
113117
throw new ErrorCoded(`Unsupported JSON-LD media type ${mediaType}`,
@@ -672,4 +676,10 @@ export interface IJsonLdParserOptions {
672676
* Defaults to false.
673677
*/
674678
rdfstarReverseInEmbedded?: boolean;
679+
/**
680+
* If the APIs you interact with publish valid JSON-LD on media types that are not application/ld+json,
681+
* provide those content-types in this array.
682+
* Default to ['application/activity+json']
683+
*/
684+
wellKnownMediaTypes?: string[];
675685
}

test/JsonLdParser-test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ describe('JsonLdParser', () => {
105105
expect((<any> parser).options.baseIRI).toEqual('BASE');
106106
});
107107

108+
it('should handle an ActivityStreams JSON response in the same way as JSON-LD', () => {
109+
const parser = JsonLdParser.fromHttpResponse('BASE', 'application/activity+json');
110+
expect((<any> parser).options.baseIRI).toEqual('BASE');
111+
});
112+
108113
it('should handle a JSON-LD response and allow option overrides', () => {
109114
const parser = JsonLdParser.fromHttpResponse('BASE', 'application/ld+json', undefined, { baseIRI: 'base2' });
110115
expect((<any> parser).options.baseIRI).toEqual('base2');
@@ -116,6 +121,13 @@ describe('JsonLdParser', () => {
116121
ERROR_CODES.LOADING_DOCUMENT_FAILED))
117122
});
118123

124+
it('should error on an application/activity+json without link header if the wellknowntypes do not include it', () => {
125+
expect(() => JsonLdParser.fromHttpResponse('BASE', 'application/activity+json', undefined, {
126+
wellKnownMediaTypes: []
127+
})).toThrow(new ErrorCoded(`Missing context link header for media type application/activity+json on BASE`,
128+
ERROR_CODES.LOADING_DOCUMENT_FAILED))
129+
});
130+
119131
it('should error on a plain JSON response without link header', () => {
120132
expect(() => JsonLdParser.fromHttpResponse('BASE', 'application/json'))
121133
.toThrow(new ErrorCoded(`Missing context link header for media type application/json on BASE`,

0 commit comments

Comments
 (0)