Papupata Documentation

API Reference

class DeclaredAPI

method invoke

Purpose

Calls the declared API

Usage

In the simplest for mall query parameters (including body) are passed as a single object. The invocation mechanisms spreads them out to the declared containers automatically. There are overload for different uses, however, listed below.

Parameters: All parameters in one

NameTypeDescription
paramsObjectAll parameters to the API
requestOptionsvariesOptions passed to the request. These have no inherent meaning in papupata, but can be used by the requestAdapter function defined by the application. The type of this parameter is set by the RequestOptions type parameter of the API declaration.

Parameters: Separate body

NameTypeDescription
bodyVariesBody to be sent to the API. The data type is specified by the route.
paramsObjectAll parameters to the API, excluding body
requestOptionsvariesOptions passed to the request. These have no inherent meaning in papupata, but can be used by the requestAdapter function defined by the application. The type of this parameter is set by the RequestOptions type parameter of the API declaration.

Returns

Promise<ResponseType>

Caveats

  • Base URL and requestAdapter function must be configured or the function will throw.
  • There is no validation for the data returned by the server, it is assumed to be of the correct shape
  • Error handling is adapter-dependant.

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.implement(req => {
  const {q} = req.query,
    {param} = req.params,
    {key} = req.body
  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