Papupata Documentation

API Reference

class PartiallyDeclaredAPI

type TypeMapping

Availability

This functionality is available from papupata version 2.0.0 onwards.

Purpose

Representing the types of values.

Usage

These objects are used to provide types for query and path parameters, and from there they are also exposed via apiUrlParameters field of a declared API.

TypeMapping is an object, that is essentially a collection of key-value pairs where the key stands for the name of the parameter and the value stands for the type, and when applicable, any other constraints put on the field.

For additional information you might want to look at the TypeMapping guide

const typeMapping = { fieldName: String}
Valid types and constraints
ValueImported fromDescription
StringNot applicableAny string, not constraints.
StringMatching
import { StringMatching } from 'papupata/queryTypes'

A string that must match a given regular expression. Do note that the regex is NOT checked with typescript, so from its point of view any string will do.

const typeMapping = { fieldName: StringMatching(/^hello/) }

It can be identified as being an object with a "type" field that has the value you can import as regexStringToken from papupata/queryTypes.

StringEnum
import { StringEnum } from 'papupata/queryTypes'

A string that must be one of the provided options. Do note that the as const seen in the example is required in order to have typescript check the values being entered.

const typeMapping = { fieldName: StringEnum(['alpha', 'beta', 'gamma'] as const) }

It can be identified as being an object with a "type" field that has the value you can import as stringEnumToken from papupata/queryTypes.

NumberNot applicableAny number, negative numbers, decimal numbers, NaN and Infinity are all allowed.
Integer
import { Integer } from 'papupata/queryTypes'

An integer number. Negative number are allowed, Infinity and NaN are not. Do note that these constraints are NOT checked with typescript, so it appears to just accept a number.

It can be identified as being an object with a "type" field that has the value you can import as integerToken from papupata/queryTypes.

BooleanNot applicableAccepts the strings true and false to convert to their boolean counterparts. Empty strings becomes false as well.
DateNot applicableThe value of the parameter is passed to the constructor of Date to produce a date. Invalid dates are rejected.
ArrayNot applicable

An array of one entry that is one of the types specified above indicates an array of that type. Not applicable to path parameters. An array parameter is never strictly required -- it being missing results in an empty array.

const typeMapping = { fieldName: [String]} // an array of strings

Examples

import { APIDeclaration } from 'papupata'
import { StringMatching } from 'papupata/queryTypes'

const api = new APIDeclaration()
const myAPI = api.declarePostAPI('/do-stuff/:id')
  .params({
    id: StringMatching(/^[0-9a-f]{24}$/)
  })
  .query({
    name: String,
    age: Number
  })
  .optionalQuery({
    wantsMarketingEmail: Boolean
  })
  .response<string>()