|
10 | 10 | - [@ngx-toolset/date-interceptors](#ngx-toolsetdate-interceptors) |
11 | 11 | - [Features](#features) |
12 | 12 | - [Installation](#installation) |
| 13 | + - [NPM](#npm) |
13 | 14 | - [Usage](#usage) |
| 15 | + - [Module Import](#module-import) |
14 | 16 | - [Provide Injection Tokens](#provide-injection-tokens) |
15 | 17 | - [Injection Tokens](#injection-tokens) |
16 | 18 | - [API_DATE_FORMAT](#api_date_format) |
|
26 | 28 |
|
27 | 29 | ## Installation |
28 | 30 |
|
29 | | -``` |
30 | | -ng add @ngx-toolset/date-interceptors |
31 | | -``` |
| 31 | +### NPM |
| 32 | + |
| 33 | +`npm install @ngx-toolset/date-interceptors --save` |
| 34 | + |
| 35 | +Choose the version corresponding to your Angular version: |
| 36 | + |
| 37 | +| Angular | @ngx-toolset/date-interceptors | |
| 38 | +|---------|--------------------------------| |
| 39 | +| 14.x.x | >=0.0.1 <=1.0.0-rc.11 | |
| 40 | +| 15.x.x | 1.0.0-rc.12 | |
| 41 | +| 16.x.x | >=1.0.0-rc.13 <=2.0.0 | |
32 | 42 |
|
33 | 43 | ## Usage |
34 | 44 |
|
| 45 | +### Provide HTTP client with Interceptors |
| 46 | + |
| 47 | +Provide the HTTP client with the `requestBodyDateFormatInterceptor` and `responseBodyDateParseInterceptor` in your `main.ts`: |
| 48 | + |
| 49 | +```ts |
| 50 | +import { bootstrapApplication } from '@angular/platform-browser'; |
| 51 | +import { AppComponent } from './app/app.component'; |
| 52 | +import { |
| 53 | + requestBodyDateFormatInterceptor, |
| 54 | + responseBodyDateParseInterceptor |
| 55 | +} from '@ngx-toolset/date-interceptors'; |
| 56 | + |
| 57 | +bootstrapApplication(AppComponent, { |
| 58 | + providers: [ |
| 59 | + provideHttpClient( |
| 60 | + withInterceptors([ |
| 61 | + requestBodyDateFormatInterceptor, |
| 62 | + responseBodyDateParseInterceptor |
| 63 | + ]) |
| 64 | + ) |
| 65 | + ] |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
35 | 69 | ### Provide Injection Tokens |
36 | 70 |
|
37 | | -Provide proper values for automatically added `API_DATE_FORMAT`, `API_URL_REGEX` and `DATE_STRING_REGEX` |
38 | | -injection tokens in your `AppModule`. |
| 71 | +Provide `API_DATE_FORMAT`, `API_URL_REGEX` and `DATE_STRING_REGEX` in your `main.ts`: |
| 72 | + |
| 73 | +```ts |
| 74 | +import { bootstrapApplication } from '@angular/platform-browser'; |
| 75 | +import { AppComponent } from './app/app.component'; |
| 76 | +import { |
| 77 | + requestBodyDateFormatInterceptor, |
| 78 | + responseBodyDateParseInterceptor, |
| 79 | + API_URL_REGEX, |
| 80 | + DATE_STRING_REGEX, |
| 81 | + API_DATE_FORMAT |
| 82 | +} from '@ngx-toolset/date-interceptors'; |
| 83 | + |
| 84 | +bootstrapApplication(AppComponent, { |
| 85 | + providers: [ |
| 86 | + provideHttpClient( |
| 87 | + withInterceptors([ |
| 88 | + requestBodyDateFormatInterceptor, |
| 89 | + responseBodyDateParseInterceptor |
| 90 | + ]) |
| 91 | + ), |
| 92 | + { |
| 93 | + provide: API_URL_REGEX, |
| 94 | + useValue: /^https:\/\/test-url.com/ |
| 95 | + }, |
| 96 | + { |
| 97 | + provide: DATE_STRING_REGEX, |
| 98 | + useValue: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ |
| 99 | + }, |
| 100 | + { |
| 101 | + provide: API_DATE_FORMAT, |
| 102 | + useValue: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" |
| 103 | + } |
| 104 | + ] |
| 105 | +}); |
| 106 | +``` |
| 107 | + |
| 108 | +> Hint: The list of options to provide `API_DATE_FORMAT` value could be found here: [date-fns documentation](https://date-fns.org/v2.29.1/docs/parse). |
39 | 109 |
|
40 | 110 | ## Injection Tokens |
41 | 111 |
|
|
0 commit comments