API Reference
class DeclaredAPI
method invoke
Purpose
Calls the declared APIUsage
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
Name | Type | Description |
---|---|---|
params | Object | All parameters to the API |
requestOptions | varies | Options 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
Name | Type | Description |
---|---|---|
body | Varies | Body to be sent to the API. The data type is specified by the route. |
params | Object | All parameters to the API, excluding body |
requestOptions | varies | Options 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