Papupata Documentation

API Reference

class DeclaredAPI

exposed type RequestType

Purpose

Represents the type of express request that has been modified to be typed according to the declaration. This makes it easier to have functions that take the request and do things with it.

Usage

The exposed types are used with the typeof operator. See the example below for details.

Although the types are presented as fields on the API, they have no runtime value.

Caveats

There is at this time no way to create partial typed requests objects, which should be helpful for the same purposes.

Examples

Older styles are still supported in later versions, just not preferred.
Basic usage
import { APIDeclaration } from 'papupata'
const api = new APIDeclaration()
const myAPI = api.declarePostAPI('/do-stuff')
  .query({collection: String})
  .response<string>()

type RequestType = typeof myAPI['RequestType']
// RequestType is now the type of a modified express request
Practical usage
myAPI.implement(req => {
  return getCollection(req)
})

function getCollection(req: typeof myAPI['RequestType']) {
  // This function has access to the typed request
  return collections.get(req.query.collection)
}