nestjs-date-format-interceptor
v1.0.0
Published
Easy formatting for output dates in NestJS
Downloads
64
Readme
nestjs-date-format-interceptor
Easy formatting for output dates in NestJS
This NPM provides an easy way to give a format to api response dates in NestJs
How to install.
npm install nestjs-date-format-interceptorBinding in controller
import { DateFormatInterceptor, DateFormat } from "utilities/lib/date";
// ...
@UseInterceptors(new DateFormatInterceptor({ maxDeep: 6, format: DateFormat.toISOString }))
export class CatsController {}Binding globally
import { DateFormatInterceptor, DateFormat } from "utilities/lib/date";
// ...
const app = await NestFactory.create(AppModule);
app.useGlobalInterceptors(new DateFormatInterceptor({ maxDeep: 6, format: DateFormat.toISOString }));Parameter options
{
maxDeep: number = 6;
format: DateFormat = DateFormat.toISOString;
fnFormat?: (value: Date) => string;
}
- maxDeep: Max deep of nested objects to check dates
- format: The format function to build the date-string
- fnFormat?: Optional, Custom format function.
format options
| Format | Result | | ------------ | ----------------------------------------------------------- | | toDateString | "Tue Aug 19 1975" | | toISOString | "1975-08-20T05:15:30.000Z" | | toJSON | "1975-08-20T05:15:30.000Z" | | toString | "Tue Aug 19 1975 23:15:30 GMT-0600 (Central Standard Time)" | | toTimeString | "23:15:30 GMT-0600 (Central Standard Time)" | | toUTCString | "Wed, 20 Aug 1975 05:15:30 GMT" |
fnFormat Custom format example
import { DateFormatInterceptor, DateFormat } from 'utilities/lib/date';
...
const app = await NestFactory.create(AppModule);
app.useGlobalInterceptors(
new DateFormatInterceptor({
maxDeep: 6,
format: DateFormat.toISOString,
fnFormat: date => `Date in Costa Rica is ${date.toLocaleString('es-CR')}`,
}),
);
Result:
Date in Costa Rica is 7/14/2020, 8:01:15 PMAuthor
- Luis Arias [email protected] - GitHub profile
