@mmstack/resource 19.3.2 → 19.4.1

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.
@@ -1,5 +1,5 @@
1
1
  import { type HttpResourceRequest } from '@angular/common/http';
2
- import { Signal, ValueEqualityFn } from '@angular/core';
2
+ import { type Signal, type ValueEqualityFn } from '@angular/core';
3
3
  import { type QueryResourceOptions, type QueryResourceRef } from './query-resource';
4
4
  /**
5
5
  * @internal
@@ -1,4 +1,4 @@
1
- import { HttpContext, HttpInterceptorFn } from '@angular/common/http';
1
+ import { HttpContext, type HttpInterceptorFn, type HttpRequest } from '@angular/common/http';
2
2
  /**
3
3
  * Disables request deduplication for a specific HTTP request.
4
4
  *
@@ -24,6 +24,9 @@ export declare function noDedupe(ctx?: HttpContext): HttpContext;
24
24
  *
25
25
  * @param allowed - An array of HTTP methods for which deduplication should be enabled.
26
26
  * Defaults to `['GET', 'DELETE', 'HEAD', 'OPTIONS']`.
27
+ * @param keyFn - Optional function to compute the dedupe key from a request.
28
+ * Defaults to `hashRequest`, which includes method, URL,
29
+ * response type, params, and body.
27
30
  *
28
31
  * @returns An `HttpInterceptorFn` that implements the request deduplication logic.
29
32
  *
@@ -47,4 +50,4 @@ export declare function noDedupe(ctx?: HttpContext): HttpContext;
47
50
  * ],
48
51
  * };
49
52
  */
50
- export declare function createDedupeRequestsInterceptor(allowed?: string[]): HttpInterceptorFn;
53
+ export declare function createDedupeRequestsInterceptor(allowed?: string[], keyFn?: (req: HttpRequest<unknown>) => string): HttpInterceptorFn;
@@ -0,0 +1,21 @@
1
+ import { type HttpRequest, type HttpResourceRequest } from '@angular/common/http';
2
+ type HashableRequest = {
3
+ method?: string;
4
+ url: string;
5
+ responseType?: string;
6
+ params?: HttpResourceRequest['params'] | HttpRequest<unknown>['params'];
7
+ body?: unknown;
8
+ };
9
+ /**
10
+ * Builds a stable cache/dedupe key from an HTTP request shape (accepts both
11
+ * `HttpRequest` and `HttpResourceRequest`).
12
+ *
13
+ * Key composition: `${method}:${url}:${responseType}[:${params}][:${body}]`
14
+ * - `method` defaults to `'GET'`, `responseType` to `'json'` (Angular defaults).
15
+ * - Query params are sorted alphabetically and URL-encoded for stability.
16
+ * - Body hashing handles `File`/`Blob`/`FormData`/`URLSearchParams`/`ArrayBuffer`
17
+ * and typed arrays explicitly; everything else flows through key-sorted
18
+ * `JSON.stringify` via `hash()`.
19
+ */
20
+ export declare function hashRequest(req: HashableRequest): string;
21
+ export {};
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Generates a stable, unique string hash from one or more arguments.
3
+ * Useful for creating cache keys or identifiers where object key order shouldn't matter.
4
+ *
5
+ * How it works:
6
+ * - Object-like values (plain objects, class instances, `Object.create(null)`) have
7
+ * their own enumerable keys sorted alphabetically before hashing. This ensures
8
+ * `{ a: 1, b: 2 }` and `{ b: 2, a: 1 }` produce the same hash.
9
+ * - `Map` and `Set` are serialized via stable, sorted markers (`__map__` / `__set__`).
10
+ * - Arrays preserve positional order; `Date` uses its ISO string via `toJSON`.
11
+ *
12
+ * @param {...unknown} args Values to include in the hash.
13
+ * @returns A stable string hash representing the input arguments.
14
+ * @example
15
+ * hash('posts', 10);
16
+ * // => '["posts",10]'
17
+ *
18
+ * hash({ a: 1, b: 2 }) === hash({ b: 2, a: 1 }); // true
19
+ *
20
+ * hash(new Map([['a', 1]])) === hash(new Map([['a', 1]])); // true
21
+ *
22
+ * // Be mindful of values JSON.stringify cannot handle (functions, undefined, Symbols)
23
+ * // hash('a', undefined, function() {}) => '["a",null,null]'
24
+ */
25
+ export declare function hash(...args: unknown[]): string;
@@ -4,9 +4,9 @@ export * from './circuit-breaker';
4
4
  export * from './dedupe-interceptor';
5
5
  export * from './equality';
6
6
  export * from './has-slow-connection';
7
+ export * from './hash-request';
7
8
  export * from './persist';
8
9
  export * from './refresh';
9
10
  export * from './retry-on-error';
10
11
  export * from './sensors';
11
12
  export * from './to-resource-object';
12
- export * from './url-with-params';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmstack/resource",
3
- "version": "19.3.2",
3
+ "version": "19.4.1",
4
4
  "keywords": [
5
5
  "angular",
6
6
  "signals",
@@ -1,2 +0,0 @@
1
- import { type HttpResourceRequest } from '@angular/common/http';
2
- export declare function urlWithParams(req: HttpResourceRequest): string;