Skip to content

Commit

Permalink
Merge be8fdc9 into 46611ac
Browse files Browse the repository at this point in the history
  • Loading branch information
pietercolpaert committed Mar 6, 2024
2 parents 46611ac + be8fdc9 commit d49f118
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Optionally, the following parameters can be set in the `JsonLdParser` constructo
* `skipContextValidation`: If JSON-LD context validation should be skipped. This is useful when parsing large contexts that are known to be valid. _(Default: `false`)_
* `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`)_
* `rdfstarReverseInEmbedded`: If embedded nodes in JSON-LD star can have reverse properties. _(Default: `false`)_
* `wellKnownContentTypes`: an array of content-types that can also be parsed as JSON-LD. _(Default: `['application/activity+json']`)_

```javascript
new JsonLdParser({
Expand Down
13 changes: 11 additions & 2 deletions lib/JsonLdParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ export class JsonLdParser extends Transform implements RDF.Sink<EventEmitter, RD
public static fromHttpResponse(baseIRI: string, mediaType: string,
headers?: Headers, options?: IJsonLdParserOptions): JsonLdParser {
let context: JsonLdContext | undefined;
// Special cases when receiving something else than the JSON-LD media type
if (mediaType !== 'application/ld+json') {
let wellKnownContentTypes = ['application/activity+json'];
if (options && options.wellKnownContentTypes) {
wellKnownContentTypes = options.wellKnownContentTypes;
}
// Special cases when receiving something else than the JSON-LD media type or the wellKnownContentTypes
if (mediaType !== 'application/ld+json' && !wellKnownContentTypes.includes(mediaType)) {
// Only accept JSON or JSON extension types
if (mediaType !== 'application/json' && !mediaType.endsWith('+json')) {
throw new ErrorCoded(`Unsupported JSON-LD media type ${mediaType}`,
Expand Down Expand Up @@ -672,4 +676,9 @@ export interface IJsonLdParserOptions {
* Defaults to false.
*/
rdfstarReverseInEmbedded?: boolean;
/**
* If the APIs you interact with publish valid JSON-LD on Content-Types that are not application/ld+json, provide those content-types in this array.
* Default to ['application/activity+json']
*/
wellKnownContentTypes?: string[];
}
5 changes: 5 additions & 0 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ describe('JsonLdParser', () => {
expect((<any> parser).options.baseIRI).toEqual('BASE');
});

it('should handle an ActivityStreams JSON response in the same way as JSON-LD', () => {
const parser = JsonLdParser.fromHttpResponse('BASE', 'application/activity+json');
expect((<any> parser).options.baseIRI).toEqual('BASE');
});

it('should handle a JSON-LD response and allow option overrides', () => {
const parser = JsonLdParser.fromHttpResponse('BASE', 'application/ld+json', undefined, { baseIRI: 'base2' });
expect((<any> parser).options.baseIRI).toEqual('base2');
Expand Down

0 comments on commit d49f118

Please sign in to comment.