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
Value | Imported from | Description |
---|---|---|
String | Not applicable | Any string, not constraints. |
StringMatching |
| 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.
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 |
| 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.
It can be identified as being an object with a "type" field that has the value you can import as stringEnumToken from papupata/queryTypes. |
Number | Not applicable | Any number, negative numbers, decimal numbers, NaN and Infinity are all allowed. |
Integer |
| 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. |
Boolean | Not applicable | Accepts the strings true and false to convert to their boolean counterparts. Empty strings becomes false as well. |
Date | Not applicable | The value of the parameter is passed to the constructor of Date to produce a date. Invalid dates are rejected. |
Array | Not 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.
|
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>()