2022-11-18 13:20:19 +01:00
|
|
|
import { BaseResponse } from "./response/base.response";
|
2019-03-16 03:33:19 +01:00
|
|
|
|
|
|
|
export class Response {
|
2019-10-21 19:51:48 +02:00
|
|
|
static error(error: any, data?: any): Response {
|
2019-03-16 03:33:19 +01:00
|
|
|
const res = new Response();
|
|
|
|
res.success = false;
|
|
|
|
if (typeof error === "string") {
|
|
|
|
res.message = error;
|
|
|
|
} else {
|
2021-03-09 17:58:17 +01:00
|
|
|
res.message =
|
|
|
|
error.message != null
|
|
|
|
? error.message
|
|
|
|
: error.toString() === "[object Object]"
|
|
|
|
? JSON.stringify(error)
|
|
|
|
: error.toString();
|
2019-03-16 03:33:19 +01:00
|
|
|
}
|
2019-10-21 19:51:48 +02:00
|
|
|
res.data = data;
|
2019-03-16 03:33:19 +01:00
|
|
|
return res;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2019-03-16 03:33:19 +01:00
|
|
|
|
|
|
|
static notFound(): Response {
|
|
|
|
return Response.error("Not found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
static badRequest(message: string): Response {
|
|
|
|
return Response.error(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static multipleResults(ids: string[]): Response {
|
|
|
|
let msg =
|
|
|
|
"More than one result was found. Try getting a specific object by `id` instead. " +
|
|
|
|
"The following objects were found:";
|
2021-02-04 16:49:23 +01:00
|
|
|
ids.forEach((id) => {
|
2019-03-16 03:33:19 +01:00
|
|
|
msg += "\n" + id;
|
|
|
|
});
|
2019-10-21 19:51:48 +02:00
|
|
|
return Response.error(msg, ids);
|
2019-03-16 03:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static success(data?: BaseResponse): Response {
|
|
|
|
const res = new Response();
|
|
|
|
res.success = true;
|
|
|
|
res.data = data;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
success: boolean;
|
|
|
|
message: string;
|
|
|
|
errorCode: number;
|
|
|
|
data: BaseResponse;
|
|
|
|
}
|