Papupata Documentation

API Reference

class IncompleteAPIDeclaration

method response

Purpose

Declares the response type or types for an API and concludes the declaration.

Usage

The response type is declared as a type parameter.

In some cases you might be in a situation where the types returned from the implementation don't quite match what clients receive. An example of this would be with dates -- you could have a Date object on the server, but when it gets to the browser it's just a string. To deal with this another type parameter exists, allowing you to specify another type to be returned from the implementation.

Parameters

NameTypeDescription
<ResponseType>InterfaceThe response type as seen on the client side.
<ServerResponseType>InterfaceThe response type as seen on the server side. Default to the value of ResponseType.

Returns

DeclaredAPI

Caveats

  • There is no client side validation for the data types. Only typescript itself validates the response on server side
  • It's up to the developer to ensure that the ResponseType is really resulting from returning a ServerResponseType.

Examples

Example 1 Declaration
import { APIDeclaration } from 'papupata'
const api = new APIDeclaration()
const myAPI = api.declarePostAPI('/do-stuff')
  .response<string>()
Example 1 Usage in implementation
myAPI.implement(req => {
  return "Hello"
})
Example 1 Usage in invocation
await myAPI({}) // Returns a promise that resolves to "Hello"
Example 2, differing types; Declaration
import { APIDeclaration } from 'papupata'
const api = new APIDeclaration()
const myAPI = api.declarePostAPI('/do-stuff')
  .response<{date: string}, {date: Date}}>()
Example 2 Usage in implementation
myAPI.implement(req => {
  return { date: new Date('2019-01-01T12:12:12.000Z') }
})
Example 2 Usage in invocation
await myAPI({}) // Returns a promise that resolves to '2019-01-01T12:12:12.000Z'