A package of Ts.ED framework. See website: https://tsed.io/
Features
Transform data returned by a method to a formatted response based on the stored metadata.
Installation
npm install --save @tsed/di @tsed/platform-views @tsed/schema @tsed/json-schema @tsed/platform-response-filter
Usage
Define a class that return data:
import {Injectable} from "@tsed/di";
import {Returns} from "@tsed/schema";
import {MyModel} from "../models/MyModel.js";
@Injectable()
class MyService {
@Returns(200, MyModel)
async getData() {
return new MyModel({id: "id", test: "test"});
}
@Returns(200, MyModel)
@View("myview.ejs")
async getDataView() {
return new MyModel({id: "id", test: "test"});
}
}
Add a response filter for a specific content-type:
import {ResponseFilter, Context, ResponseFilterMethods} from "@tsed/common";
@ResponseFilter("application/json")
export class WrapperResponseFilter implements ResponseFilterMethods {
transform(data: any, ctx: Context) {
return {data, errors: [], links: {}};
}
}
Then call the service in you module:
import {Module, DIContext} from "@tsed/di";
import {ResponseFilter} from "@tsed/platform-response-filter";
import "./filters/WrapperResponseFilter";
@Module()
class MyModule {
@Inject()
injector: InjectorService;
@Inject()
responseFilter: ResponseFilter;
async onRequest(req: any, res: any) {
const context = new DIContext({
id: uuid.v4(),
injector: this.injector,
logger: this.injector.logger
});
context.request = {
accepts(...args: any[]) {
return req.accepts(...args);
},
get(key: string) {
return req.get(key);
}
};
context.response = {
contentType(contentType: string) {
res.contentType(contentType);
}
};
const service = this.injector.get<MyService>(MyService);
let data = await service.getData();
data = await this.responseFilter.serialize(data, context);
data = await this.responseFilter.transform(data, context);
if (isObject(data)) {
res.json(data);
} else {
res.send(data);
}
}
}
This example will call the getData() method, serialize the instance MyModel to a plain object then call the WrapperResponseFilter,
to produce the following response:
{
"data": {
"id": "id",
"test": "test"
},
"error": [],
"links": {}
}
Contributors
Please read contributing guidelines here.
Backers
Thank you to all our backers! 🙏 [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your
website. [Become a sponsor]
License
The MIT License (MIT)
Copyright (c) 2016 - 2022 Romain Lenzotti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.