API Reference
class DeclaredAPI
method implementWithExpressMiddleware
Availability
This functionality is available from papupata version 1.5.0 onwards.Purpose
Implements an API using express. This variant allows for inclusion of express middleware for the implementation.Usage
Instead of calling the methods on an express app or router yourself, you use this function to have papupata do it for you.Parameters
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
middleware | Function[] | An array of express middleware functions. | |||||||||
implementation | Function | A function that implements the route. Specified as followsParameters
ReturnsResponseType, ServerResponseType, or a promise of either
|
Returns
NothingCaveats
- Either application of router must be configured or the function throws
- You can implement an API multiple times, but it is unlikely to do you any good.
- There is at this time no way to cleanly implement an API that does not just return its value, and instead, say, streams it.
Examples
Older styles are still supported in later versions, just not preferred.
Declaration
import { APIDeclaration } from 'papupata'
const api = new APIDeclaration()
const myAPI = api.declarePostAPI('/do-stuff/:param')
.params({param: String})
.query({q: String})
.body<{key: string}>()
.response<string>()
Usage in implementation
myAPI.implementWithExpressMiddleware(
[(req, res, next) => { console.log(req.url); next() }],
(req, res) => {
const {q} = req.query,
{param} = req.params,
{key} = req.body
res.set('x-my-header', 'Hello')
return [param, q, key].join()
})
Usage in invocation
const response = await myAPI({param: 'abc', q: 'def', key: 'ghi'})
// Response in the example will be abc,def,ghi