Source Configuration
Configure encoding options on the defineApi call. These apply to all endpoints in the source.
ts
import { defineApi } from "@noverax/courier/define"
const encodedApi = defineApi({
baseURL: "http://localhost:4000",
groups: { echo },
// Sends URL-encoded body instead of JSON
bodySerializer: (data) =>
new URLSearchParams(
Object.entries(data).map(([k, v]) => [k, String(v)])
),
// Encodes arrays as key[]=val1&key[]=val2
searchParamsSerializer: {
stringify: (params) => {
const sp = new URLSearchParams()
for (const [key, value] of Object.entries(params)) {
if (Array.isArray(value)) {
for (const item of value)
sp.append(`${key}[]`, String(item))
} else {
sp.append(key, String(value))
}
}
return sp.toString()
},
parse: (str) =>
Object.fromEntries(new URLSearchParams(str).entries()),
},
// Auto-merged into every POST/PUT/PATCH body
defaultBody: { apiVersion: "v2" },
// Auto-merged into every GET/DELETE query string
defaultQueryParams: { format: "json" },
})The source above configures custom body encoding, search params encoding, and default values. See the demos below for interactive examples.
Body Encoding
Compare JSON (default) vs URL-encoded body. Both send the same data to the echo endpoint.
ts
// Default (api): JSON body
await caller.echo.send({ name: "John", role: "admin" })
// Content-Type: application/json
// Body: { "name": "John", "role": "admin" }
// Custom (encodedApi): URL-encoded body + defaultBody merged
await encodedCaller.echo.send({ name: "John", role: "admin" })
// Content-Type: application/x-www-form-urlencoded
// Body: apiVersion=v2&name=John&role=adminecho.send — two body encodingsSANDBOX
// run the request to see the response// run the request to see the responseSearch Params Encoding
Custom encoder supports arrays. Arrays are encoded as key[]=val1&key[]=val2.
ts
// Custom searchParamsSerializer supports arrays
await encodedCaller.echo.retrieve({
tags: ["javascript", "react"],
page: 1,
})
// GET /echo?format=json&apiVersion=v2&tags[]=javascript&tags[]=react&page=1echo.retrieve — arrays in the querySANDBOX
// run the request to see the responseDefault Data & Query Params
defaultBody merges into POST body. defaultQueryParams merges into GET query string.
ts
// defaultBody auto-merges into POST body
await encodedCaller.echo.send({ name: "John" })
// Body includes: apiVersion=v2&name=John
// defaultQueryParams auto-merges into GET query
await encodedCaller.echo.retrieve({ page: 1 })
// Query includes: format=json&page=1echo — defaultBody · defaultQueryParamsSANDBOX
// run the request to see the response// run the request to see the response