@cedvict/http-guardian 0.0.1-next.8 → 0.0.1-next.9
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/README.md +66 -0
- package/dist/guards/bearerRefreshGuard.d.ts +45 -0
- package/dist/guards/bearerRefreshGuard.d.ts.map +1 -0
- package/dist/guards/correlationIdGuard.d.ts +18 -0
- package/dist/guards/correlationIdGuard.d.ts.map +1 -0
- package/dist/guards/csrfGuard.d.ts +25 -0
- package/dist/guards/csrfGuard.d.ts.map +1 -0
- package/dist/guards/idempotencyGuard.d.ts +25 -0
- package/dist/guards/idempotencyGuard.d.ts.map +1 -0
- package/dist/index.cjs +325 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +321 -5
- package/dist/index.js.map +1 -1
- package/dist/internal/context.d.ts +26 -0
- package/dist/internal/context.d.ts.map +1 -1
- package/dist/parsing/apiParserStructured.d.ts +94 -0
- package/dist/parsing/apiParserStructured.d.ts.map +1 -0
- package/package.json +1 -1
|
@@ -2,6 +2,27 @@ import type { HttpMethod } from "../client/types.js";
|
|
|
2
2
|
import type { AuthConfig } from "../auth/authConfig.js";
|
|
3
3
|
import type { Notifier } from "../notify/types.js";
|
|
4
4
|
import type { HeadersPolyfill } from "./headersPolyfill.js";
|
|
5
|
+
/**
|
|
6
|
+
* Meta side-channel attached to a RequestContext. Populated by guards (e.g.
|
|
7
|
+
* correlationIdGuard, idempotencyGuard) and parsers (envelope) so downstream
|
|
8
|
+
* plugins (logger, sentry, otel) can pick the values up without having to
|
|
9
|
+
* re-read headers.
|
|
10
|
+
*
|
|
11
|
+
* All fields are optional — a context without a `meta` object is valid and
|
|
12
|
+
* keeps backwards compatibility.
|
|
13
|
+
*/
|
|
14
|
+
export type RequestContextMeta = {
|
|
15
|
+
/** ID corrélation propagé en header (cf. correlationIdGuard). */
|
|
16
|
+
correlationId?: string;
|
|
17
|
+
/** Clé d'idempotence (cf. idempotencyGuard / RequestOptions.idempotencyKey). */
|
|
18
|
+
idempotencyKey?: string;
|
|
19
|
+
/** Trace ID lu depuis l'enveloppe de réponse (parser-dependent). */
|
|
20
|
+
traceId?: string;
|
|
21
|
+
/** Span ID lu depuis l'enveloppe de réponse (parser-dependent). */
|
|
22
|
+
spanId?: string;
|
|
23
|
+
/** Enveloppe brute exposée par le parser (cf. createApiParserStructured). */
|
|
24
|
+
envelope?: unknown;
|
|
25
|
+
};
|
|
5
26
|
export type RequestContext = {
|
|
6
27
|
/**
|
|
7
28
|
* Base URL configured on the client (always set by createHttpClient).
|
|
@@ -26,6 +47,11 @@ export type RequestContext = {
|
|
|
26
47
|
noAuth?: boolean;
|
|
27
48
|
notifier?: Notifier;
|
|
28
49
|
idempotencyKey?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Meta side-channel for guards/parsers/plugins. Always undefined unless a
|
|
52
|
+
* guard or parser populates it; consumers must check `ctx.meta != null`.
|
|
53
|
+
*/
|
|
54
|
+
meta?: RequestContextMeta;
|
|
29
55
|
_fetch: typeof fetch;
|
|
30
56
|
};
|
|
31
57
|
//# sourceMappingURL=context.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/internal/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,MAAM,MAAM,cAAc,GAAG;IAC3B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;OAGG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,UAAU,CAAC;IACnB,GAAG,EAAE,M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AC;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,OAAO,GAAG,eAAe,CAAC;IACnC,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC,QAAQ,CAAC,EAAE,eAAe,CAAC;IAC3B,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,UAAU,CAAC;IACjB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,IAAI,CAAC,EAAE,kBAAkB,CAAC;IAC1B,MAAM,EAAE,OAAO,KAAK,CAAC;CACtB,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { ShapeParser } from "./shapeParser.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parser pour une enveloppe HTTP "structurée" : succès / erreurs typées avec
|
|
4
|
+
* `issues[]`, `i18n`, et `meta { traceId, spanId, timestamp }`.
|
|
5
|
+
*
|
|
6
|
+
* Cette forme est typique des backends qui exposent un protocole d'erreur
|
|
7
|
+
* uniforme (FastAPI, NestJS custom, etc.) avec :
|
|
8
|
+
* - une distinction `success: boolean`
|
|
9
|
+
* - une liste d'issues structurées (code, message, category, severity,
|
|
10
|
+
* retryable, i18n)
|
|
11
|
+
* - un bloc meta de tracing (traceId, spanId)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Succès
|
|
15
|
+
* { "success": true, "data": { ... }, "meta": { "traceId": "...", "timestamp": "..." } }
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Erreur
|
|
19
|
+
* {
|
|
20
|
+
* "success": false,
|
|
21
|
+
* "code": "AUTH_OTP_INVALID",
|
|
22
|
+
* "message": "Auth otp invalid",
|
|
23
|
+
* "issues": [
|
|
24
|
+
* {
|
|
25
|
+
* "code": "AUTH_OTP_INVALID",
|
|
26
|
+
* "message": "...",
|
|
27
|
+
* "category": "auth",
|
|
28
|
+
* "severity": "error",
|
|
29
|
+
* "retryable": false,
|
|
30
|
+
* "i18n": { "key": "auth.otp_invalid", "params": {}, "locale": null }
|
|
31
|
+
* }
|
|
32
|
+
* ],
|
|
33
|
+
* "i18n": { "key": "...", "params": {}, "locale": null },
|
|
34
|
+
* "meta": { "traceId": "...", "spanId": "...", "timestamp": "..." }
|
|
35
|
+
* }
|
|
36
|
+
*/
|
|
37
|
+
export type StructuredI18n = {
|
|
38
|
+
key?: string;
|
|
39
|
+
params?: Record<string, unknown>;
|
|
40
|
+
locale?: string | null;
|
|
41
|
+
};
|
|
42
|
+
export type StructuredIssue = {
|
|
43
|
+
code?: string;
|
|
44
|
+
message?: string;
|
|
45
|
+
category?: string;
|
|
46
|
+
severity?: string;
|
|
47
|
+
retryable?: boolean;
|
|
48
|
+
i18n?: StructuredI18n;
|
|
49
|
+
field?: string;
|
|
50
|
+
};
|
|
51
|
+
export type StructuredEnvelopeMeta = {
|
|
52
|
+
traceId?: string;
|
|
53
|
+
spanId?: string;
|
|
54
|
+
timestamp?: string;
|
|
55
|
+
[k: string]: unknown;
|
|
56
|
+
};
|
|
57
|
+
export type StructuredSuccessEnvelope<T = unknown> = {
|
|
58
|
+
success: true;
|
|
59
|
+
data: T;
|
|
60
|
+
meta?: StructuredEnvelopeMeta;
|
|
61
|
+
};
|
|
62
|
+
export type StructuredErrorEnvelope = {
|
|
63
|
+
success: false;
|
|
64
|
+
code?: string;
|
|
65
|
+
message?: string;
|
|
66
|
+
issues?: StructuredIssue[];
|
|
67
|
+
i18n?: StructuredI18n;
|
|
68
|
+
meta?: StructuredEnvelopeMeta;
|
|
69
|
+
};
|
|
70
|
+
export type StructuredEnvelope<T = unknown> = StructuredSuccessEnvelope<T> | StructuredErrorEnvelope;
|
|
71
|
+
export type ApiParserStructuredOptions = {
|
|
72
|
+
/** Garde l'enveloppe `meta` accessible via `AppError.details.envelope`. */
|
|
73
|
+
exposeMeta?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Pour les erreurs avec `retryable: false`, marquer la requête comme
|
|
76
|
+
* non-retryable (via `details.retryable`). Le retry plugin peut consulter
|
|
77
|
+
* ce flag via un `isRetryableRequest` custom — voir docs.
|
|
78
|
+
*/
|
|
79
|
+
honorRetryableHint?: boolean;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Parser pour une enveloppe structurée à `issues[]` + `i18n` + `meta` de tracing.
|
|
83
|
+
*
|
|
84
|
+
* - Succès : `data = envelope.data`
|
|
85
|
+
* - Erreur : chaque `issue` est mappée en `AppError`, avec en `details` les
|
|
86
|
+
* informations riches (code, category, severity, retryable, i18n, traceId,
|
|
87
|
+
* spanId, timestamp, enveloppe brute si `exposeMeta=true`).
|
|
88
|
+
*
|
|
89
|
+
* Le `traceId` / `spanId` de l'enveloppe se retrouvent dans :
|
|
90
|
+
* - `errors[0].details.traceId` / `.spanId` (côté erreur)
|
|
91
|
+
* - `errors[0].details.envelope.meta` (enveloppe brute, si `exposeMeta`)
|
|
92
|
+
*/
|
|
93
|
+
export declare function createApiParserStructured(opts?: ApiParserStructuredOptions): ShapeParser;
|
|
94
|
+
//# sourceMappingURL=apiParserStructured.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apiParserStructured.d.ts","sourceRoot":"","sources":["../../src/parsing/apiParserStructured.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAGpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,MAAM,CAAC,EAAE,MAAM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;GAWG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,GAAE,0BAA+B,GAAG,WAAW,CAqE5F"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cedvict/http-guardian",
|
|
3
|
-
"version": "0.0.1-next.
|
|
3
|
+
"version": "0.0.1-next.9",
|
|
4
4
|
"description": "Configurable HTTP client for TypeScript: auth (bearer/cookie), guards, cache+dedupe, dynamic envelope parsing presets, notifications, redirect handling.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|