attributes
When composing a serializer, the @Attribute
decorator is used to signify which of the properties/methods on your serialize should be rendered. To do this, you can simply provide the Attribute decorator above each attribute you wish to be rendered:
export default class UserSerializer extends DreamSerializer {
@Attribute()
public email: string
}
openapi support
When composing an app, you may choose to lean into the built-in support that comes out of the box if you are using Psychic and Dream together. When composing controllers in Psychic, you can take advantage of the built-in @OpenAPI
decorator, which enables you to annotate your openapi responses in typescript and leverage the auto-generating mechanisms provided with the psychic cli.
class UsersController extends PsychicController {
@OpenAPI(User, {
many: true,
method: 'get',
})
public index() {
this.ok(await User.all())
}
}
When a Dream model is passed to the @OpenAPI decorator, the serializer corresponding to that Dream model is leveraged to generate the final OpenAPI shape. As with Dream attribute types, here, Dream leverages the database schema to keep the OpenAPI schema in sync based on any migrations that have been applied:
import { Attribute, DreamColumn, DreamSerializer } from '@rvoh/dream'
import User from '../../models/User'
export default class UserSerializer extends DreamSerializer {
@Attribute(User)
public email: DreamColumn<User, 'email'>
}
// {
// type: 'object',
// required: ['email'],
// properties: {
// email: {
// type: 'string',
// nullable: false,
// }
// }
// }
Many generics are made available to you, including decimal
, date
, date-time
, boolean
, as well as array variants of these, which can be used like so:
import { Attribute, DreamSerializer } from '@rvoh/dream'
export default class UserSerializer extends DreamSerializer {
@Attribute('boolean[]')
public likesChalupas: boolean[]
}
// {
// type: 'object',
// required: ['likesChalupas'],
// properties: {
// likesChalupas: {
// type: 'array',
// items: {
// type: 'boolean',
// nullable: false,
// },
// nullable: false,
// }
// }
// }
custom schema definitions
In addition to leveraging primitive types, you can explicitly spell out custom openapi shapes using our hybrid openapi shorthand. It supports all the usual things you can do in openapi:
import { Attribute, DreamSerializer } from '@rvoh/dream'
export default class UserSerializer extends DreamSerializer {
@Attribute({
type: 'object',
properties: {
first: {
type: 'string',
nullable: true,
},
last: {
type: 'string',
nullable: true,
},
title: {
type: 'string',
description: 'either Mr, Ms, Mrs, or Mizzzz',
nullable: true,
enum: ['Mr', 'Mrs', 'Ms', 'Mizzzz'],
},
}
})
public name() {
...
}
}