You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.1 KiB

export interface APIResponse {
success: boolean,
message?: string,
data?: any,
error?: {
name: string,
message: string,
stack?: string[],
}
}
export function message(message: string): APIResponse {
return {
success: true,
message,
}
}
export function one(record: any): APIResponse {
return {
success: true,
data: record,
}
}
export function many(records: any[]): APIResponse {
return {
success: true,
data: {
records,
total: records.length,
},
}
}
export function error(error: string | Error): APIResponse {
if ( typeof error === 'string' ) {
return {
success: false,
message: error,
}
} else {
return {
success: false,
message: error.message,
error: {
name: error.name,
message: error.message,
stack: error.stack ? error.stack.split(/\s+at\s+/).slice(1) : [],
},
}
}
}