Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone. ⭐️ Star to support our work!
MIT License
Published by Romakita about 3 years ago
With OpenAPI 3, it's possible to describe and use a deepObject style
as Query params.
It means, a consumer can call your endpoint with the following url:
/users?id[role]=admin&id[firstName]=Alex
Ts.ED will determine automatically the appropriate style
parameter based on the given User
model.
Here is an example with a DeepQueryObject model:
class DeepQueryObject {
@Property()
path: string;
@Property()
condition: string;
@Property()
value: string;
}
@Path("/test")
class TestDeepObjectCtrl {
@OperationPath("GET", "/")
async get(@QueryParams("s") q: DeepQueryObject) {
}
}
The url to be called will be:
/test?s[path]=title&s[condition]=eq&s[value]=tsed
And the generated swagger will be:
{
"components": {
"schemas": {
"DeepQueryObject": {
"properties": {
"condition": {
"type": "string"
},
"path": {
"type": "string"
},
"value": {
"type": "string"
}
},
"type": "object"
}
}
},
"paths": {
"/test": {
"get": {
"operationId": "testDeepObjectCtrlGet",
"parameters": [
{
"in": "query",
"name": "s",
"required": false,
"style": "deepObject",
"schema": {
"$ref": "#/components/schemas/DeepQueryObject"
}
}
]
}
}
}
}
Ts.ED support also Generics Deep object style!
class FindQuery {
@Property()
tableColumnNameA?: number;
@Property()
tableColumnNameB?: number;
}
@Generics("T")
class PaginationQuery<T> {
@Minimum(0)
@Default(0)
offset?: number;
@Minimum(1)
@Maximum(1000)
@Default(50)
limit?: number;
@Property("T")
where?: T;
}
@Path("/test")
class TestDeepObjectCtrl {
@OperationPath("GET", "/")
async get(@QueryParams() @GenericOf(FindQuery) q: PaginationQuery<FindQuery>) {}
}
/!\ Important: This feature is only available for OpenAPI 3.