API: decorators
Source:
http/decorators.ts
Usage
javascript
import { Controller, Get, Documentation } from "sdk/http"
import { HttpUtils } from "sdk/http/utils";
import { Options } from "sdk/db";
import { Injected, Inject } from "sdk/component";
import { CountryEntity } from "./CountryEntity";
import { CountryRepository } from "./CountryRepository";
@Controller
@Documentation("Sample Country Controller")
@Injected()
class CountryController {
@Inject('CountryRepository')
private readonly repository!: CountryRepository;
@Get("/")
@Documentation("Sample Get All Countries")
public getAll(_: CountryEntity, ctx: any): CountryEntity[] {
try {
const options: Options = {
limit: ctx.queryParameters["$limit"] ? parseInt(ctx.queryParameters["$limit"]) : 20,
offset: ctx.queryParameters["$offset"] ? parseInt(ctx.queryParameters["$offset"]) : 0
};
return this.repository.findAll(options);
} catch (error: any) {
this.handleError(error);
}
return [];
}
private handleError(error: any) {
if (error.name === "ForbiddenError") {
HttpUtils.sendForbiddenRequest(error.message);
} else if (error.name === "ValidationError") {
HttpUtils.sendResponseBadRequest(error.message);
} else {
HttpUtils.sendInternalServerError(error.message);
}
}
}