feat(util): return helper to hit the api,logging,socket.io returns
This commit is contained in:
48
backend/src/utils/returnHelper.utils.ts
Normal file
48
backend/src/utils/returnHelper.utils.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
interface Data {
|
||||
success: boolean;
|
||||
module: "system" | "ocp";
|
||||
subModule?: "db" | "labeling" | "printer";
|
||||
level: "info" | "error" | "debug" | "fatal";
|
||||
message: string;
|
||||
data?: unknown[];
|
||||
notify?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* This dose the return process and log at the same time, vs needing to log and return at the same time.
|
||||
* When to use.
|
||||
* APIs
|
||||
* actual returns and needing to stop.
|
||||
* Example Data
|
||||
* success:true
|
||||
* module: system | printers | etc
|
||||
* submodule: sql connection | printer test | etc
|
||||
* level "info" | "error" | "debug" | "fatal"
|
||||
* message: "short description of the return"
|
||||
* data: [] the data that will be passed back
|
||||
* notify: false by default this is to send a notification to a users email to alert them of an issue.
|
||||
*/
|
||||
export const returnFunc = (data: Data) => {
|
||||
const notify = data.notify ? data.notify : false;
|
||||
// handle the logging part
|
||||
switch (data.level) {
|
||||
case "info":
|
||||
console.log({ notify: notify }, data.message);
|
||||
break;
|
||||
case "error":
|
||||
console.log({ notify: notify }, data.message);
|
||||
break;
|
||||
case "debug":
|
||||
console.log({ notify: notify }, data.message);
|
||||
break;
|
||||
case "fatal":
|
||||
console.log({ notify: notify }, data.message);
|
||||
}
|
||||
|
||||
// api section to return
|
||||
return {
|
||||
success: data.success,
|
||||
message: data.message,
|
||||
data: data.data || [],
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user