@arsedizioni/ars-utils 22.0.82 → 22.0.84
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fesm2022/arsedizioni-ars-utils-core.mjs +210 -19
- package/fesm2022/arsedizioni-ars-utils-core.mjs.map +1 -1
- package/package.json +1 -1
- package/types/arsedizioni-ars-utils-clipper.ui.d.ts +1 -1
- package/types/arsedizioni-ars-utils-core.d.ts +20 -6
- package/types/arsedizioni-ars-utils-ui.application.d.ts +2 -2
- package/types/arsedizioni-ars-utils-ui.d.ts +1 -1
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { inject, Injectable, makeEnvironmentProviders, ElementRef, afterNextRender, Directive, input, DestroyRef, HostListener, output, forwardRef, effect, Pipe, EventEmitter, signal, computed, Service, PLATFORM_ID, RendererFactory2 } from '@angular/core';
|
|
2
|
+
import { InjectionToken, inject, Injectable, makeEnvironmentProviders, ElementRef, afterNextRender, Directive, input, DestroyRef, HostListener, output, forwardRef, effect, Pipe, EventEmitter, signal, computed, Service, PLATFORM_ID, RendererFactory2 } from '@angular/core';
|
|
3
3
|
import { DateAdapter, MAT_DATE_LOCALE, MAT_DATE_FORMATS } from '@angular/material/core';
|
|
4
4
|
import { TZDate } from '@date-fns/tz';
|
|
5
5
|
import { format, getYear, getMonth, getDate, getDay, getDaysInMonth, parseISO, parse, addYears, addMonths, addDays, isDate, isValid, addSeconds, endOfDay } from 'date-fns';
|
|
6
|
+
import { HTTP_INTERCEPTORS } from '@angular/common/http';
|
|
6
7
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
7
8
|
import { Subject, filter as filter$1, map as map$1, BehaviorSubject } from 'rxjs';
|
|
8
9
|
import { debounceTime, filter, map } from 'rxjs/operators';
|
|
@@ -12,6 +13,189 @@ import { DomSanitizer } from '@angular/platform-browser';
|
|
|
12
13
|
import { SelectionModel } from '@angular/cdk/collections';
|
|
13
14
|
import { isPlatformBrowser } from '@angular/common';
|
|
14
15
|
|
|
16
|
+
/** Default application timezone: all dates are serialised as Europe/Rome wall-clock values. */
|
|
17
|
+
const DEFAULT_TIME_ZONE = 'Europe/Rome';
|
|
18
|
+
/**
|
|
19
|
+
* IANA timezone used to serialise dates towards the backend.
|
|
20
|
+
* Override it in the application providers to run the same code in another zone:
|
|
21
|
+
* `{ provide: ARS_TIME_ZONE, useValue: 'Europe/Zurich' }`.
|
|
22
|
+
*/
|
|
23
|
+
const ARS_TIME_ZONE = new InjectionToken('ARS_TIME_ZONE', {
|
|
24
|
+
providedIn: 'root',
|
|
25
|
+
factory: () => DEFAULT_TIME_ZONE,
|
|
26
|
+
});
|
|
27
|
+
/** Naive local datetime pattern (no timezone designator): the server reads it as DateTimeKind.Unspecified. */
|
|
28
|
+
const NAIVE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
|
|
29
|
+
/**
|
|
30
|
+
* Returns `true` for payloads that must be forwarded untouched
|
|
31
|
+
* (binary or already-encoded bodies).
|
|
32
|
+
* @param body - The request body to test.
|
|
33
|
+
*/
|
|
34
|
+
function isOpaqueBody(body) {
|
|
35
|
+
return (body instanceof FormData ||
|
|
36
|
+
body instanceof Blob ||
|
|
37
|
+
body instanceof ArrayBuffer ||
|
|
38
|
+
ArrayBuffer.isView(body) ||
|
|
39
|
+
body instanceof URLSearchParams ||
|
|
40
|
+
typeof body === 'string');
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Recursively replaces every `Date` found in `value` with its naive local
|
|
44
|
+
* datetime string, returning a NEW structure: the original object graph — and
|
|
45
|
+
* therefore any bound form model — is never mutated.
|
|
46
|
+
*
|
|
47
|
+
* @param value - The value to normalise (object, array, Date or primitive).
|
|
48
|
+
* @param timeZone - IANA timezone used to compute the wall-clock value.
|
|
49
|
+
* @param seen - Guard against circular references.
|
|
50
|
+
* @returns The normalised value, safe to hand over to `JSON.stringify`.
|
|
51
|
+
*/
|
|
52
|
+
function normalizeDates(value, timeZone, seen) {
|
|
53
|
+
if (value instanceof Date) {
|
|
54
|
+
// Invalid dates would make format() throw a RangeError: send null instead.
|
|
55
|
+
if (isNaN(value.getTime()))
|
|
56
|
+
return null;
|
|
57
|
+
return format(new TZDate(value.getTime(), timeZone), NAIVE_PATTERN);
|
|
58
|
+
}
|
|
59
|
+
if (value === null || typeof value !== 'object')
|
|
60
|
+
return value;
|
|
61
|
+
if (isOpaqueBody(value))
|
|
62
|
+
return value;
|
|
63
|
+
// Circular reference: leave it as is, JSON.stringify will report it anyway.
|
|
64
|
+
if (seen.has(value))
|
|
65
|
+
return value;
|
|
66
|
+
seen.add(value);
|
|
67
|
+
if (Array.isArray(value)) {
|
|
68
|
+
return value.map(item => normalizeDates(item, timeZone, seen));
|
|
69
|
+
}
|
|
70
|
+
const source = value;
|
|
71
|
+
const result = {};
|
|
72
|
+
for (const key of Object.keys(source)) {
|
|
73
|
+
result[key] = normalizeDates(source[key], timeZone, seen);
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Rewrites the request body, if any, replacing Dates with naive local strings.
|
|
79
|
+
* Shared by the functional and the class-based interceptor.
|
|
80
|
+
* @param req - The outgoing request.
|
|
81
|
+
* @param timeZone - IANA timezone used to compute the wall-clock value.
|
|
82
|
+
* @returns The original request when there is nothing to convert, a clone otherwise.
|
|
83
|
+
*/
|
|
84
|
+
function localizeRequest(req, timeZone) {
|
|
85
|
+
const body = req.body;
|
|
86
|
+
if (body === null || body === undefined || isOpaqueBody(body))
|
|
87
|
+
return req;
|
|
88
|
+
return req.clone({ body: normalizeDates(body, timeZone, new WeakSet()) });
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Functional interceptor that serialises every `Date` in an outgoing request
|
|
92
|
+
* body as a naive local datetime string (`yyyy-MM-ddTHH:mm:ss`: no `Z`, no offset).
|
|
93
|
+
*
|
|
94
|
+
* WHY: `JSON.stringify` calls `Date.prototype.toJSON`, which emits a UTC instant.
|
|
95
|
+
* A date picked as 29/07/2026 00:00 in Rome (UTC+2) becomes
|
|
96
|
+
* `2026-07-28T22:00:00.000Z`, so the server stores the 28th — the classic
|
|
97
|
+
* "off by one day" bug. Sending the wall-clock value instead makes
|
|
98
|
+
* System.Text.Json produce a `DateTime` with `Kind = Unspecified` and the exact
|
|
99
|
+
* day/time the user selected.
|
|
100
|
+
*
|
|
101
|
+
* This is the single serialisation boundary of the application: no call site has
|
|
102
|
+
* to convert anything, and it also covers dates that lost a per-instance
|
|
103
|
+
* `toJSON` along the way (e.g. after `structuredClone`).
|
|
104
|
+
*
|
|
105
|
+
* Register it explicitly in `provideHttpClient(withInterceptors([...]))`.
|
|
106
|
+
* If you would rather have the library register it for you, use
|
|
107
|
+
* {@link provideArsLocalDates} instead.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* provideHttpClient(withInterceptors([
|
|
111
|
+
* arsLocalDateInterceptor,
|
|
112
|
+
* evolutionAuthInterceptor(...),
|
|
113
|
+
* ]));
|
|
114
|
+
*/
|
|
115
|
+
const arsLocalDateInterceptor = (req, next) => next(localizeRequest(req, inject(ARS_TIME_ZONE)));
|
|
116
|
+
/**
|
|
117
|
+
* Class-based twin of {@link arsLocalDateInterceptor}.
|
|
118
|
+
*
|
|
119
|
+
* Exists because a functional `HttpInterceptorFn` can ONLY be registered inside
|
|
120
|
+
* `provideHttpClient(withInterceptors([...]))`, while a class registered on the
|
|
121
|
+
* `HTTP_INTERCEPTORS` multi-token can be contributed by any `EnvironmentProviders` —
|
|
122
|
+
* which is what lets {@link provideArsLocalDates} (and `provideArsDateFns`) wire it up
|
|
123
|
+
* on their own.
|
|
124
|
+
*
|
|
125
|
+
* REQUIRES `withInterceptorsFromDi()` in the application's `provideHttpClient()`:
|
|
126
|
+
* without it Angular never reads `HTTP_INTERCEPTORS` and this interceptor is
|
|
127
|
+
* silently skipped.
|
|
128
|
+
*/
|
|
129
|
+
class ArsLocalDateInterceptor {
|
|
130
|
+
constructor() {
|
|
131
|
+
this.timeZone = inject(ARS_TIME_ZONE);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Normalises the request body before handing it to the next handler.
|
|
135
|
+
* @param req - The outgoing request.
|
|
136
|
+
* @param next - The next handler in the chain.
|
|
137
|
+
*/
|
|
138
|
+
intercept(req, next) {
|
|
139
|
+
return next.handle(localizeRequest(req, this.timeZone));
|
|
140
|
+
}
|
|
141
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: ArsLocalDateInterceptor, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
142
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: ArsLocalDateInterceptor }); }
|
|
143
|
+
}
|
|
144
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: ArsLocalDateInterceptor, decorators: [{
|
|
145
|
+
type: Injectable
|
|
146
|
+
}] });
|
|
147
|
+
/**
|
|
148
|
+
* Standalone providers for date serialisation towards the backend.
|
|
149
|
+
*
|
|
150
|
+
* Registers {@link ArsLocalDateInterceptor} so that every `Date` in a request
|
|
151
|
+
* body travels as a naive local datetime string instead of a UTC instant.
|
|
152
|
+
*
|
|
153
|
+
* IMPORTANT: the application MUST call `withInterceptorsFromDi()`, otherwise
|
|
154
|
+
* these providers have no effect at all (no error is raised).
|
|
155
|
+
*
|
|
156
|
+
* @param timeZone - IANA timezone name (default: `Europe/Rome`).
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* providers: [
|
|
160
|
+
* provideArsLocalDates(),
|
|
161
|
+
* provideHttpClient(withInterceptors([evolutionAuthInterceptor(...)]), withInterceptorsFromDi()),
|
|
162
|
+
* ]
|
|
163
|
+
*/
|
|
164
|
+
function provideArsLocalDates(timeZone = DEFAULT_TIME_ZONE) {
|
|
165
|
+
return makeEnvironmentProviders([
|
|
166
|
+
{ provide: ARS_TIME_ZONE, useValue: timeZone },
|
|
167
|
+
{ provide: HTTP_INTERCEPTORS, useClass: ArsLocalDateInterceptor, multi: true },
|
|
168
|
+
]);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Formats a date as a date-only ISO string (`yyyy-MM-dd`) using its wall-clock
|
|
172
|
+
* calendar day in the given timezone. Use it for query-string parameters and for
|
|
173
|
+
* DTO fields mapped to a .NET `DateOnly`, which the interceptor cannot detect.
|
|
174
|
+
*
|
|
175
|
+
* @param value - The date to serialise.
|
|
176
|
+
* @param timeZone - IANA timezone name (default: `Europe/Rome`).
|
|
177
|
+
* @returns The `yyyy-MM-dd` string, or `undefined` for empty/invalid input.
|
|
178
|
+
*/
|
|
179
|
+
function toLocalDateOnlyString(value, timeZone = DEFAULT_TIME_ZONE) {
|
|
180
|
+
if (!value || isNaN(value.getTime()))
|
|
181
|
+
return undefined;
|
|
182
|
+
return format(new TZDate(value.getTime(), timeZone), 'yyyy-MM-dd');
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Formats a date as a naive local datetime string (`yyyy-MM-ddTHH:mm:ss`), the
|
|
186
|
+
* same representation produced by the interceptor. Useful for query-string
|
|
187
|
+
* parameters, which never pass through the request body.
|
|
188
|
+
*
|
|
189
|
+
* @param value - The date to serialise.
|
|
190
|
+
* @param timeZone - IANA timezone name (default: `Europe/Rome`).
|
|
191
|
+
* @returns The naive datetime string, or `undefined` for empty/invalid input.
|
|
192
|
+
*/
|
|
193
|
+
function toLocalDateTimeString(value, timeZone = DEFAULT_TIME_ZONE) {
|
|
194
|
+
if (!value || isNaN(value.getTime()))
|
|
195
|
+
return undefined;
|
|
196
|
+
return format(new TZDate(value.getTime(), timeZone), NAIVE_PATTERN);
|
|
197
|
+
}
|
|
198
|
+
|
|
15
199
|
/**
|
|
16
200
|
* Creates an array of the given length, filling each slot with the result of `valueFunction`.
|
|
17
201
|
* @param length - Number of elements to create.
|
|
@@ -411,13 +595,12 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.8", ngImpor
|
|
|
411
595
|
* providers: [provideArsDateFns()]
|
|
412
596
|
* });
|
|
413
597
|
*/
|
|
414
|
-
function provideArsDateFns() {
|
|
598
|
+
function provideArsDateFns(options = {}) {
|
|
599
|
+
const { timeZone = 'Europe/Rome', httpSerialization = true } = options;
|
|
415
600
|
return makeEnvironmentProviders([
|
|
416
|
-
{
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
},
|
|
420
|
-
{ provide: MAT_DATE_FORMATS, useValue: MAT_DATE_FNS_FORMATS }
|
|
601
|
+
{ provide: DateAdapter, useClass: DateFnsAdapter },
|
|
602
|
+
{ provide: MAT_DATE_FORMATS, useValue: MAT_DATE_FNS_FORMATS },
|
|
603
|
+
...(httpSerialization ? [provideArsLocalDates(timeZone)] : []),
|
|
421
604
|
]);
|
|
422
605
|
}
|
|
423
606
|
|
|
@@ -1748,22 +1931,30 @@ class SystemUtils {
|
|
|
1748
1931
|
}
|
|
1749
1932
|
}
|
|
1750
1933
|
/**
|
|
1751
|
-
*
|
|
1752
|
-
*
|
|
1753
|
-
*
|
|
1934
|
+
* Converts a Date, timestamp or ISO string into a Europe/Rome Date whose JSON serialisation
|
|
1935
|
+
* emits a naive local datetime string ("yyyy-MM-dd'T'HH:mm:ss"), with no timezone designator,
|
|
1936
|
+
* so the wall-clock value round-trips to the server unchanged. Use this instead of `new Date(...)`
|
|
1937
|
+
* when reviving dates received from the API, to stay consistent with the DateFnsAdapter (values
|
|
1938
|
+
* entered through the datepicker already behave this way).
|
|
1939
|
+
* @param value : the source Date, Unix timestamp (ms) or ISO string
|
|
1940
|
+
* @returns : a Europe/Rome Date serialising as naive local time, or undefined for empty/invalid input
|
|
1754
1941
|
*/
|
|
1755
1942
|
static toLocalDate(value) {
|
|
1756
|
-
|
|
1757
|
-
if (!value)
|
|
1943
|
+
if (value == null || value === '')
|
|
1758
1944
|
return undefined;
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
if (!this.isValidDate(value))
|
|
1945
|
+
const ms = value instanceof Date ? value.getTime()
|
|
1946
|
+
: typeof value === 'number' ? value
|
|
1947
|
+
: new Date(value).getTime();
|
|
1948
|
+
if (isNaN(ms))
|
|
1764
1949
|
return undefined;
|
|
1765
|
-
|
|
1766
|
-
|
|
1950
|
+
const t = new TZDate(ms, 'Europe/Rome');
|
|
1951
|
+
Object.defineProperty(t, 'toJSON', {
|
|
1952
|
+
value: () => format(t, "yyyy-MM-dd'T'HH:mm:ss"),
|
|
1953
|
+
enumerable: false,
|
|
1954
|
+
configurable: true,
|
|
1955
|
+
writable: true,
|
|
1956
|
+
});
|
|
1957
|
+
return t;
|
|
1767
1958
|
}
|
|
1768
1959
|
/**
|
|
1769
1960
|
* Update a DateInterval object according to a string
|