Utilities
Some functions used inside the library and that you can use for your code too.
Logs
The utilities exported are:
log
An instance of a logger from
utils-logger-av
that can log colors and icons and save your desired messages inside a specified file.
fullLogOk
A function that uses the log instance and print a succesful message in green on console and saves the message in the file specified in the envs
fullLogNok
Same as before but regarding errors.
i
utils-logger-av
icons for logs
c
Utility funtion for the logger library that helps you crate custom combo-color logs
errorCatcher
An utility used inside the library.
It helps returning a standardized response alongs some error and status configurations.
Importants
Those methods return the type expected from the RouterWrapper
api section.
apiOk
export const apiOk = (res:any, status:number = 200, contentType?:string):ExpressReturn => ({
result:res,
status,
contentType,
isOk:true
})
apiNok
export const apiNok = (err:any, status:number = 500):ExpressReturn => ({
result:err,
status,
isOk:false
})
Those methods return the type expected from the SocketWrapper
middleware.
socketOk
export const socketOk = <T extends Record<string, any> = any>(newMetadata?:T):SocketConnectionOk => ({ ok:true, newMetadata:newMetadata });
socketNok
export const socketNok = (message:string):SocketConnectionNok => ({ ok:false, message });
Those are some generics utilities exported by the package
sleep
export const sleep = (ms:number) => new Promise(resolve => setTimeout(resolve, ms));
getCompiledPath
This one let you fetch the right file both on production/dev environment with typescript
export const getCompiledPath = (__filename:string, __dirname:string, pathsWithoutExtension:string[]) =>
{
const isCompiled = __filename.endsWith('.js');
return pathsWithoutExtension.map(p => (path.join(__dirname, p + (isCompiled ? '.js' : '.ts'))).replaceAll('\\', '/'));
}
Last updated