Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handler chains #179

Merged
merged 7 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions packages/core/lib/Kopflos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class Impl implements Kopflos {
return this.graph.has(this.env.ns.rdf.type, this.env.ns.kopflos.Api)
}

async getResponse(req: KopflosRequest<Dataset>): Promise<KopflosResponse> {
async getResponse(req: KopflosRequest<Dataset>): Promise<KopflosResponse | undefined | null> {
const resourceShapeMatch = await this.findResourceShape(req.iri)
if (isResponse(resourceShapeMatch)) {
return resourceShapeMatch
Expand Down Expand Up @@ -139,22 +139,31 @@ export default class Impl implements Kopflos {
args.property = resourceShapeMatch.property
args.object = resourceGraph.node(resourceShapeMatch.object)
}
const [handler, ...rest] = handlerChain
let response = this.asEnvelope(await handler(args, undefined))
for (const handler of rest) {
response = this.asEnvelope(await handler(args, response))

let handler = handlerChain.shift()
let response: ResultEnvelope | undefined
while (handler) {
tpluscode marked this conversation as resolved.
Show resolved Hide resolved
const rawResult = await handler(args, response)
if (!rawResult) {
return rawResult
}

response = this.asEnvelope(rawResult)
if (response.end) {
break
}

handler = handlerChain.shift()
}

return response
}

async handleRequest(req: KopflosRequest<Dataset>): Promise<ResultEnvelope> {
log.info(`${req.method} ${req.iri.value}`)
log.debug('Request headers', req.headers)

let result: KopflosResponse
let result: KopflosResponse | undefined | null
try {
result = await this.getResponse(req)
} catch (cause: Error | unknown) {
Expand All @@ -171,7 +180,7 @@ export default class Impl implements Kopflos {
}

if (!result) {
log.error('Undefined result returned from handler')
log.error('Falsy result returned from handler')
return {
status: 500,
body: new Error('Handler did not return a result'),
Expand Down
36 changes: 36 additions & 0 deletions packages/core/test/lib/Kopflos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,42 @@ describe('lib/Kopflos', () => {
// then
expect(response.body).to.eq('C')
})

it('guards against falsy handler result', async function () {
// given
const chainedHandler = (letter: string): Handler => () => {
return {
status: 200,
body: letter,
}
}
const kopflos = new Kopflos(config, {
dataset: this.rdf.dataset,
resourceShapeLookup: async () => [{
api: ex.api,
resourceShape: ex.FooShape,
subject: ex.foo,
}],
handlerLookup: () => [
chainedHandler('A'),
() => undefined as unknown as KopflosResponse,
chainedHandler('C'),
],
resourceLoaderLookup: async () => () => rdf.dataset().toStream(),
})

// when
const response = await kopflos.handleRequest({
iri: ex.foo,
method: 'GET',
headers: {},
body: {} as Body,
query: {},
})

// then
expect(response.status).to.eq(500)
})
})

it('guards against falsy handler result', async function () {
Expand Down