generated from tpluscode/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
709 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"express-rdf-problem-details": major | ||
--- | ||
|
||
First release |
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,25 @@ | ||
# express-rdf-problem-details | ||
|
||
This package wraps [express-http-problem-details](https://github.com/PDMLab/express-http-problem-details) so that RDF/Linked Data express applications will return Problem documents ([RFC7807](https://tools.ietf.org/html/rfc7807)) as valid JSON-LD. | ||
|
||
This is done simply by adding a `Link` header pointing to a JSON-LD `@context`. By default, it's the [context provided by Hydra Community Group](http://www.hydra-cg.com/spec/latest/core/#example-31-rfc-7807-compatible-error-description) | ||
|
||
## Usage | ||
|
||
```typescript | ||
import express from 'express' | ||
import * as error from 'express-rdf-problem-details' | ||
import { NotFoundMapper } from './error-mappers' | ||
|
||
const app = express() | ||
|
||
// All params optional | ||
express.use(error.handler({ | ||
// change the @context URL | ||
context: 'http://example.com/error.jsonld', | ||
// additional mappers to customize error documents | ||
mappers: [new NotFoundMapper()] | ||
})) | ||
``` | ||
|
||
Implementing mappers is explained in [PDMLab/express-http-problem-details](https://github.com/PDMLab/express-http-problem-details#example) |
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 |
---|---|---|
@@ -1 +1,21 @@ | ||
export default 'foo' | ||
import type * as express from 'express' | ||
import { HttpProblemResponse } from 'express-http-problem-details' | ||
import { DefaultMappingStrategy, IErrorMapper, MapperRegistry } from 'http-problem-details-mapper' | ||
import setLink from 'set-link' | ||
|
||
interface Factory { | ||
(arg?: { mappers?: IErrorMapper[]; context?: string }): express.ErrorRequestHandler | ||
} | ||
|
||
export const handler: Factory = ({ mappers = [], context = 'https://www.w3.org/ns/hydra/error' } = {}) => { | ||
const registry = mappers.reduce((r, mapper) => r.registerMapper(mapper, true), new MapperRegistry()) | ||
|
||
const problemDetailsHandler = HttpProblemResponse({ strategy: new DefaultMappingStrategy(registry) }) | ||
|
||
return (err, req, res, next) => { | ||
setLink.attach(res) | ||
|
||
res.setLink(context, 'http://www.w3.org/ns/json-ld#context') | ||
problemDetailsHandler(err, req, res, next) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,62 @@ | ||
import { describe, it } from 'mocha' | ||
import express from 'express' | ||
import request from 'supertest' | ||
import { BadRequest } from 'http-errors' | ||
import { expect } from 'chai' | ||
import index from '..' | ||
import * as error from '..' | ||
|
||
describe('index', () => { | ||
it('exports foo', () => { | ||
expect(index).to.eq('foo') | ||
describe('express-rdf-problem-details', () => { | ||
it('sets context link', async () => { | ||
// given | ||
const app = express() | ||
app | ||
.use((req, res, next) => next(404)) | ||
.use(error.handler()) | ||
|
||
// when | ||
const response = request(app).get('/foo') | ||
|
||
await response.expect('link', /^<https:\/\/www\.w3\.org\/ns\/hydra\/error>/) | ||
}) | ||
|
||
it('allows customizing mappers', async () => { | ||
// given | ||
const app = express() | ||
app | ||
.use((req, res, next) => next(new BadRequest())) | ||
.use(error.handler({ | ||
mappers: [new (class { | ||
get error() { | ||
return 'BadRequestError' | ||
} | ||
|
||
mapError() { | ||
return { | ||
title: 'something wrong', | ||
status: 400, | ||
} | ||
} | ||
})()], | ||
})) | ||
|
||
// when | ||
const { body } = await request(app).get('/foo') | ||
|
||
expect(body).to.have.property('title', 'something wrong') | ||
}) | ||
|
||
it('allows changing context', async () => { | ||
// given | ||
const app = express() | ||
app | ||
.use((req, res, next) => next(404)) | ||
.use(error.handler({ | ||
context: 'https://example.com/error.jsonld', | ||
})) | ||
|
||
// when | ||
const response = request(app).get('/foo') | ||
|
||
await response.expect('link', /^<https:\/\/example\.com\/error\.jsonld>/) | ||
}) | ||
}) |
Oops, something went wrong.