Add all repos
This commit is contained in:
parent
faa12c60bc
commit
8a91c9b89b
369 changed files with 29047 additions and 28 deletions
59
metamigo-common/helpers/response.ts
Normal file
59
metamigo-common/helpers/response.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import * as Boom from "@hapi/boom";
|
||||
import * as Hapi from "@hapi/hapi";
|
||||
|
||||
interface IResponseMeta {
|
||||
operation?: string;
|
||||
method?: string;
|
||||
paging?: string | null;
|
||||
}
|
||||
|
||||
interface IResponseError {
|
||||
code?: string | number;
|
||||
message?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
interface IResponse<T> {
|
||||
meta: IResponseMeta;
|
||||
data: T[];
|
||||
errors: IResponseError[];
|
||||
}
|
||||
|
||||
interface IResponseOptions<T> {
|
||||
value?: T | null | undefined;
|
||||
boom?: Boom.Boom<any> | null | undefined;
|
||||
}
|
||||
|
||||
export function createResponse<T>(
|
||||
request: Hapi.Request,
|
||||
{ value = undefined, boom = undefined }: IResponseOptions<T>
|
||||
): IResponse<T> {
|
||||
const errors: IResponseError[] = [];
|
||||
const data: any = [];
|
||||
|
||||
if (boom) {
|
||||
errors.push({
|
||||
code: boom.output.payload.statusCode,
|
||||
error: boom.output.payload.error,
|
||||
message: boom.output.payload.message,
|
||||
});
|
||||
}
|
||||
|
||||
if (value && data) {
|
||||
if (Array.isArray(value)) {
|
||||
data.push(...value);
|
||||
} else {
|
||||
data.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
meta: {
|
||||
method: request.method.toUpperCase(),
|
||||
operation: request.url.pathname,
|
||||
},
|
||||
data,
|
||||
errors,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue