0.0.51 • Published 5 months ago

@oats-ts/openapi-parameter-serialization v0.0.51

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@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 },
}
0.0.51

5 months ago

0.0.50

5 months ago

0.0.49

1 year ago

0.0.44

1 year ago

0.0.45

1 year ago

0.0.46

1 year ago

0.0.47

1 year ago

0.0.48

1 year ago

0.0.40

2 years ago

0.0.41

2 years ago

0.0.42

2 years ago

0.0.43

2 years ago

0.0.37

2 years ago

0.0.38

2 years ago

0.0.39

2 years ago

0.0.36

2 years ago

0.0.34

2 years ago

0.0.35

2 years ago

0.0.25

2 years ago

0.0.30

2 years ago

0.0.31

2 years ago

0.0.32

2 years ago

0.0.33

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

3 years ago

0.0.3

3 years ago

0.0.4

3 years ago

0.0.2

3 years ago

0.0.0

3 years ago