@oats-ts/openapi-parameter-serialization
TypeScript icon, indicating that this package has built-in type declarations

0.0.51 • Public • Published

@oats-ts/openapi-parameter-serialization

This package implements all possible options for parameter serialization/deserialization described by the OpenAPI spec

The package's main goal is to support oats, but it can be used on it's own, if you have an OpenAPI related project, that needs parameter serialization/deserialization.

Model

Since this is not 100% clear from the OpenAPI spec, you can serialize and deserialize the following structures (if the serialization method is defined) as a parmeter:

  • primitives (string, number, boolean)
  • arrays with primitive values (string[], number[], boolean[])
  • objects with primitive-valued fields ({ s: string, n: number, b: boolean })

Anything else is not defined behaviour, and this project does not handle it. In case you need to serialize something more complex, you should consider using the request body instead. Details in the OpenAPI repo.

With that in mind, let's consider you have the following structure, that you would like to express as query parameters:

export type AnimalType = 'canine' | 'feline' | 'marsupial'

export type AnimalDimension = {
  weight: number
  height: number
  length: number
}

// We want to put this in the query
export type AnimalQueryParams = {
  type?: AnimalType
  name: string
  isPredator?: boolean
  legs?: number
  aliases?: string[]
  dimensions: AnimalDimension
}

Dsl

After you have your model, you need a model description on how you want each of the fields to be serialized/deserialized. Notice that you can mix and match different parameter serialization methods (here I use form, pipeDelimited and deepObject), and you can also change the explode and required options for each of these, just like in an OpenAPI document:

import { dsl, QueryDslRoot } from '@oats-ts/openapi-parameter-serialization'

const animalQueryParamsDsl: QueryDslRoot<AnimalQueryParams> = {
  type: dsl.query.form.primitive(dsl.value.string(dsl.value.enum(['canine', 'feline', 'marsupial']))),
  name: dsl.query.form.primitive(dsl.value.string(), { required: true }),
  isPredator: dsl.query.form.primitive(dsl.value.boolean()),
  legs: dsl.query.form.primitive(dsl.value.number()),
  aliases: dsl.query.pipeDelimited.array(dsl.value.string(), { explode: false }),
  dimensions: dsl.query.deepObject.object(
    {
      weight: dsl.value.number(),
      height: dsl.value.number(),
      length: dsl.value.number(),
    },
    { required: true },
  ),
}

Notice, that there is no strict typing requirements between this dsl and the actual type itself.

Serializing

This DSL then can be used to serialize your model objects. The result of each serializer and deserializer will be a Try, which is a wrapper object either containing the expected data, or a list of issues detailing what went wrong:

import { createQuerySerializer } from '@oats-ts/openapi-parameter-serialization'
import { isSuccess } from '@oats-ts/try'

const animalQueryParamsSerializer = createQuerySerializer(animalQueryParamsDsl)

const queryString = animalQueryParamsSerializer({
  type: 'canine',
  name: 'fox',
  aliases: ['vixen', 'kit', 'reynard'],
  isPredator: true,
  legs: 4,
  dimensions: {
    height: 60,
    weight: 8,
    length: 120,
  },
})

if (isSuccess(queryString)) {
  console.log(queryString.data)
} else {
  console.log(queryString.issues)
}

Which will print:

'?type=canine&isPredator=true&legs=4&name=fox&aliases=vixen|kit|reynard&dimensions[height]=60&dimensions[weight]=8&dimensions[length]=120'

Deserializing

In case you need to deserialize the same structure, you can create a deserializer using the same DSL, which does the opposite:

import { createQueryDeserializer } from '@oats-ts/openapi-parameter-serialization'
import { isSuccess } from '@oats-ts/try'

const animalQueryParamsDeserializer = createQuerySerializer(animalQueryParamsDsl)

const queryString =
  '?type=canine&isPredator=true&legs=4&name=fox&aliases=vixen|kit|reynard&dimensions[height]=60&dimensions[weight]=8&dimensions[length]=120'

const fox = animalQueryParamsDeserializer(queryString)

if (isSuccess(fox)) {
  console.log(fox.data)
} else {
  console.log(fox.issues)
}

Which will print:

{
  type: 'canine',
  isPredator: true,
  legs: 4,
  name: 'fox',
  aliases: ['vixen', 'kit', 'reynard'],
  dimensions: { weight: 8, height: 60, length: 120 },
}

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.0.5187latest

Version History

VersionDownloads (Last 7 Days)Published
0.0.5187
0.0.501
0.0.492
0.0.481
0.0.471
0.0.461
0.0.451
0.0.441
0.0.431
0.0.421
0.0.411
0.0.401
0.0.391
0.0.381
0.0.371
0.0.361
0.0.351
0.0.341
0.0.331
0.0.321
0.0.311
0.0.301
0.0.291
0.0.281
0.0.271
0.0.261
0.0.251
0.0.241
0.0.231
0.0.221
0.0.211
0.0.201
0.0.191
0.0.181
0.0.171
0.0.161
0.0.152
0.0.141
0.0.131
0.0.121
0.0.111
0.0.101
0.0.91
0.0.81
0.0.71
0.0.61
0.0.51
0.0.41
0.0.31
0.0.21
0.0.01

Package Sidebar

Install

npm i @oats-ts/openapi-parameter-serialization

Weekly Downloads

136

Version

0.0.51

License

MIT

Unpacked Size

345 kB

Total Files

117

Last publish

Collaborators

  • bali182