generated from tpluscode/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
21 lines (16 loc) · 843 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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)
}
}