@dads-framework/client 0.0.0-stage → 0.3.0
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/LICENSE.md +21 -0
- package/README.md +69 -2
- package/dist/client.tsbuildinfo +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/access-refusal.d.ts +8 -0
- package/dist/lib/access-refusal.d.ts.map +1 -0
- package/dist/lib/access-refusal.js +11 -0
- package/dist/lib/access-refusal.js.map +1 -0
- package/dist/lib/api-error.d.ts +86 -0
- package/dist/lib/api-error.d.ts.map +1 -0
- package/dist/lib/api-error.js +124 -0
- package/dist/lib/api-error.js.map +1 -0
- package/dist/lib/api-exchange-channel.d.ts +47 -0
- package/dist/lib/api-exchange-channel.d.ts.map +1 -0
- package/dist/lib/api-exchange-channel.js +43 -0
- package/dist/lib/api-exchange-channel.js.map +1 -0
- package/dist/lib/api-verbs.d.ts +115 -0
- package/dist/lib/api-verbs.d.ts.map +1 -0
- package/dist/lib/api-verbs.js +150 -0
- package/dist/lib/api-verbs.js.map +1 -0
- package/dist/lib/build-client-context.d.ts +44 -0
- package/dist/lib/build-client-context.d.ts.map +1 -0
- package/dist/lib/build-client-context.js +74 -0
- package/dist/lib/build-client-context.js.map +1 -0
- package/dist/lib/client-context.d.ts +32 -0
- package/dist/lib/client-context.d.ts.map +1 -0
- package/dist/lib/client-context.js +33 -0
- package/dist/lib/client-context.js.map +1 -0
- package/dist/lib/create-request-id.d.ts +14 -0
- package/dist/lib/create-request-id.d.ts.map +1 -0
- package/dist/lib/create-request-id.js +31 -0
- package/dist/lib/create-request-id.js.map +1 -0
- package/dist/lib/failure-presentation.d.ts +21 -0
- package/dist/lib/failure-presentation.d.ts.map +1 -0
- package/dist/lib/failure-presentation.js +12 -0
- package/dist/lib/failure-presentation.js.map +1 -0
- package/dist/lib/optimistic-cache.d.ts +84 -0
- package/dist/lib/optimistic-cache.d.ts.map +1 -0
- package/dist/lib/optimistic-cache.js +119 -0
- package/dist/lib/optimistic-cache.js.map +1 -0
- package/dist/lib/read-client-environment.d.ts +67 -0
- package/dist/lib/read-client-environment.d.ts.map +1 -0
- package/dist/lib/read-client-environment.js +85 -0
- package/dist/lib/read-client-environment.js.map +1 -0
- package/dist/lib/request-locale.d.ts +6 -0
- package/dist/lib/request-locale.d.ts.map +1 -0
- package/dist/lib/request-locale.js +31 -0
- package/dist/lib/request-locale.js.map +1 -0
- package/dist/lib/request-metadata.d.ts +22 -0
- package/dist/lib/request-metadata.d.ts.map +1 -0
- package/dist/lib/request-metadata.js +26 -0
- package/dist/lib/request-metadata.js.map +1 -0
- package/dist/lib/save-with-refresh.d.ts +13 -0
- package/dist/lib/save-with-refresh.d.ts.map +1 -0
- package/dist/lib/save-with-refresh.js +18 -0
- package/dist/lib/save-with-refresh.js.map +1 -0
- package/dist/lib/search-params.d.ts +11 -0
- package/dist/lib/search-params.d.ts.map +1 -0
- package/dist/lib/search-params.js +41 -0
- package/dist/lib/search-params.js.map +1 -0
- package/dist/lib/send-api-request.d.ts +24 -0
- package/dist/lib/send-api-request.d.ts.map +1 -0
- package/dist/lib/send-api-request.js +100 -0
- package/dist/lib/send-api-request.js.map +1 -0
- package/dist/lib/signed-url-upload.d.ts +18 -0
- package/dist/lib/signed-url-upload.d.ts.map +1 -0
- package/dist/lib/signed-url-upload.js +26 -0
- package/dist/lib/signed-url-upload.js.map +1 -0
- package/dist/lib/unauthorized-response-channel.d.ts +30 -0
- package/dist/lib/unauthorized-response-channel.d.ts.map +1 -0
- package/dist/lib/unauthorized-response-channel.js +38 -0
- package/dist/lib/unauthorized-response-channel.js.map +1 -0
- package/dist/telemetry/core/index.d.ts +3 -0
- package/dist/telemetry/core/index.d.ts.map +1 -0
- package/dist/telemetry/core/index.js +3 -0
- package/dist/telemetry/core/index.js.map +1 -0
- package/dist/telemetry/core/lib/client-telemetry-conformance.d.ts +13 -0
- package/dist/telemetry/core/lib/client-telemetry-conformance.d.ts.map +1 -0
- package/dist/telemetry/core/lib/client-telemetry-conformance.js +38 -0
- package/dist/telemetry/core/lib/client-telemetry-conformance.js.map +1 -0
- package/dist/telemetry/core/lib/client-telemetry.d.ts +48 -0
- package/dist/telemetry/core/lib/client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/core/lib/client-telemetry.js +26 -0
- package/dist/telemetry/core/lib/client-telemetry.js.map +1 -0
- package/dist/telemetry/core/lib/in-memory-client-telemetry.d.ts +20 -0
- package/dist/telemetry/core/lib/in-memory-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/core/lib/in-memory-client-telemetry.js +23 -0
- package/dist/telemetry/core/lib/in-memory-client-telemetry.js.map +1 -0
- package/dist/telemetry/core/lib/none-client-telemetry.d.ts +16 -0
- package/dist/telemetry/core/lib/none-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/core/lib/none-client-telemetry.js +22 -0
- package/dist/telemetry/core/lib/none-client-telemetry.js.map +1 -0
- package/dist/telemetry/index.d.ts +19 -0
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +19 -0
- package/dist/telemetry/index.js.map +1 -0
- package/dist/telemetry/lib/create-client-telemetry.d.ts +38 -0
- package/dist/telemetry/lib/create-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/lib/create-client-telemetry.js +65 -0
- package/dist/telemetry/lib/create-client-telemetry.js.map +1 -0
- package/dist/telemetry/lib/fan-out-client-telemetry.d.ts +24 -0
- package/dist/telemetry/lib/fan-out-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/lib/fan-out-client-telemetry.js +56 -0
- package/dist/telemetry/lib/fan-out-client-telemetry.js.map +1 -0
- package/dist/telemetry/lib/install-global-error-reporting.d.ts +27 -0
- package/dist/telemetry/lib/install-global-error-reporting.d.ts.map +1 -0
- package/dist/telemetry/lib/install-global-error-reporting.js +43 -0
- package/dist/telemetry/lib/install-global-error-reporting.js.map +1 -0
- package/dist/telemetry/lib/local-client-telemetry.d.ts +44 -0
- package/dist/telemetry/lib/local-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/lib/local-client-telemetry.js +122 -0
- package/dist/telemetry/lib/local-client-telemetry.js.map +1 -0
- package/dist/telemetry/lib/logic/describe-client-error.d.ts +24 -0
- package/dist/telemetry/lib/logic/describe-client-error.d.ts.map +1 -0
- package/dist/telemetry/lib/logic/describe-client-error.js +124 -0
- package/dist/telemetry/lib/logic/describe-client-error.js.map +1 -0
- package/dist/telemetry/lib/logic/is-chunk-load-failure.d.ts +16 -0
- package/dist/telemetry/lib/logic/is-chunk-load-failure.d.ts.map +1 -0
- package/dist/telemetry/lib/logic/is-chunk-load-failure.js +34 -0
- package/dist/telemetry/lib/logic/is-chunk-load-failure.js.map +1 -0
- package/dist/telemetry/lib/logic/refused-read.d.ts +24 -0
- package/dist/telemetry/lib/logic/refused-read.d.ts.map +1 -0
- package/dist/telemetry/lib/logic/refused-read.js +33 -0
- package/dist/telemetry/lib/logic/refused-read.js.map +1 -0
- package/dist/telemetry/lib/logic/sentry-configuration.d.ts +67 -0
- package/dist/telemetry/lib/logic/sentry-configuration.d.ts.map +1 -0
- package/dist/telemetry/lib/logic/sentry-configuration.js +74 -0
- package/dist/telemetry/lib/logic/sentry-configuration.js.map +1 -0
- package/dist/telemetry/lib/logic/should-report-failure.d.ts +19 -0
- package/dist/telemetry/lib/logic/should-report-failure.d.ts.map +1 -0
- package/dist/telemetry/lib/logic/should-report-failure.js +41 -0
- package/dist/telemetry/lib/logic/should-report-failure.js.map +1 -0
- package/dist/telemetry/lib/sentry-client-telemetry.d.ts +64 -0
- package/dist/telemetry/lib/sentry-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/lib/sentry-client-telemetry.js +187 -0
- package/dist/telemetry/lib/sentry-client-telemetry.js.map +1 -0
- package/dist/telemetry/testing.d.ts +3 -0
- package/dist/telemetry/testing.d.ts.map +1 -0
- package/dist/telemetry/testing.js +3 -0
- package/dist/telemetry/testing.js.map +1 -0
- package/package.json +58 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { getFromApi, getPageFromApi, getPageWithMetaFromApi, requestBinary, sendToApi, sendToApiWithoutResponse, type ApiArgument, type ApiPage, type ApiPageWithMeta, type ApiQueryArgument, type BinaryRequest, } from "./lib/api-verbs.js";
|
|
2
|
+
export { ApiError, NetworkError, UnauthorizedError, readClientFailure, type ClientFailure, } from "./lib/api-error.js";
|
|
3
|
+
export { onUnauthorizedResponse, type UnauthorizedResponseListener, } from "./lib/unauthorized-response-channel.js";
|
|
4
|
+
export { onApiExchange, type ApiExchange, type ApiExchangeListener, } from "./lib/api-exchange-channel.js";
|
|
5
|
+
export { currentRequestLocale, setRequestLocale, } from "./lib/request-locale.js";
|
|
6
|
+
export { putToSignedUrl, SignedUploadRefusedError, } from "./lib/signed-url-upload.js";
|
|
7
|
+
export { setClientContextProvider } from "./lib/client-context.js";
|
|
8
|
+
export { buildClientContext, type ClientEnvironmentSnapshot, } from "./lib/build-client-context.js";
|
|
9
|
+
export { markTabStarted, readClientEnvironment, setCurrentRouteId, type ClientEnvironmentSources, type ClientNavigatorLike, type ClientWindowLike, } from "./lib/read-client-environment.js";
|
|
10
|
+
export { registerRequestMetadata, type ApiResponseObserver, type RequestMetadataProvider, } from "./lib/request-metadata.js";
|
|
11
|
+
export type { HttpMethod, ApiRequestHeaders } from "./lib/send-api-request.js";
|
|
12
|
+
export { FAILURE_PRESENTED_INLINE, isFailurePresentedInline, type FailurePresentationMeta, } from "./lib/failure-presentation.js";
|
|
13
|
+
export { applyRecordEffect, optimisticRecordWrite, optimisticRecordWriteWithoutInput, type OptimisticQueryCache, type OptimisticRecordEffect, type OptimisticRecordWriteOptions, type OptimisticSnapshot, } from "./lib/optimistic-cache.js";
|
|
14
|
+
export { saveWithRefresh, type SaveWithRefreshResult, } from "./lib/save-with-refresh.js";
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,cAAc,EACd,sBAAsB,EACtB,aAAa,EACb,SAAS,EACT,wBAAwB,EACxB,KAAK,WAAW,EAChB,KAAK,OAAO,EACZ,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,aAAa,GACnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,QAAQ,EACR,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,KAAK,aAAa,GACnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,sBAAsB,EACtB,KAAK,4BAA4B,GAClC,MAAM,wCAAwC,CAAC;AAChD,OAAO,EACL,aAAa,EACb,KAAK,WAAW,EAChB,KAAK,mBAAmB,GACzB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,cAAc,EACd,wBAAwB,GACzB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,EACL,kBAAkB,EAClB,KAAK,yBAAyB,GAC/B,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,cAAc,EACd,qBAAqB,EACrB,iBAAiB,EACjB,KAAK,wBAAwB,EAC7B,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,GACtB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EACL,uBAAuB,EACvB,KAAK,mBAAmB,EACxB,KAAK,uBAAuB,GAC7B,MAAM,2BAA2B,CAAC;AACnC,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC/E,OAAO,EACL,wBAAwB,EACxB,wBAAwB,EACxB,KAAK,uBAAuB,GAC7B,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,iBAAiB,EACjB,qBAAqB,EACrB,iCAAiC,EACjC,KAAK,oBAAoB,EACzB,KAAK,sBAAsB,EAC3B,KAAK,4BAA4B,EACjC,KAAK,kBAAkB,GACxB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,eAAe,EACf,KAAK,qBAAqB,GAC3B,MAAM,4BAA4B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { getFromApi, getPageFromApi, getPageWithMetaFromApi, requestBinary, sendToApi, sendToApiWithoutResponse, } from "./lib/api-verbs.js";
|
|
2
|
+
export { ApiError, NetworkError, UnauthorizedError, readClientFailure, } from "./lib/api-error.js";
|
|
3
|
+
export { onUnauthorizedResponse, } from "./lib/unauthorized-response-channel.js";
|
|
4
|
+
export { onApiExchange, } from "./lib/api-exchange-channel.js";
|
|
5
|
+
export { currentRequestLocale, setRequestLocale, } from "./lib/request-locale.js";
|
|
6
|
+
export { putToSignedUrl, SignedUploadRefusedError, } from "./lib/signed-url-upload.js";
|
|
7
|
+
export { setClientContextProvider } from "./lib/client-context.js";
|
|
8
|
+
export { buildClientContext, } from "./lib/build-client-context.js";
|
|
9
|
+
export { markTabStarted, readClientEnvironment, setCurrentRouteId, } from "./lib/read-client-environment.js";
|
|
10
|
+
export { registerRequestMetadata, } from "./lib/request-metadata.js";
|
|
11
|
+
export { FAILURE_PRESENTED_INLINE, isFailurePresentedInline, } from "./lib/failure-presentation.js";
|
|
12
|
+
export { applyRecordEffect, optimisticRecordWrite, optimisticRecordWriteWithoutInput, } from "./lib/optimistic-cache.js";
|
|
13
|
+
export { saveWithRefresh, } from "./lib/save-with-refresh.js";
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,cAAc,EACd,sBAAsB,EACtB,aAAa,EACb,SAAS,EACT,wBAAwB,GAMzB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,QAAQ,EACR,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,GAElB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,sBAAsB,GAEvB,MAAM,wCAAwC,CAAC;AAChD,OAAO,EACL,aAAa,GAGd,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,cAAc,EACd,wBAAwB,GACzB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,EACL,kBAAkB,GAEnB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,cAAc,EACd,qBAAqB,EACrB,iBAAiB,GAIlB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EACL,uBAAuB,GAGxB,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EACL,wBAAwB,EACxB,wBAAwB,GAEzB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,iBAAiB,EACjB,qBAAqB,EACrB,iCAAiC,GAKlC,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,eAAe,GAEhB,MAAM,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether an answer is the API's access edge refusing the caller: a 404 the
|
|
3
|
+
* edge marked (`ACCESS_REFUSAL_HEADER`). Only the mark counts. An unmarked
|
|
4
|
+
* 404 is a record a handler did not find, an unmarked 403 is some other rule
|
|
5
|
+
* saying no, and a 401 is a session that ended.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isAccessRefusal(status: number, refusalHeader: string | null): boolean;
|
|
8
|
+
//# sourceMappingURL=access-refusal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access-refusal.d.ts","sourceRoot":"","sources":["../../src/lib/access-refusal.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,MAAM,EACd,aAAa,EAAE,MAAM,GAAG,IAAI,GAC3B,OAAO,CAET"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ACCESS_REFUSAL_VALUE } from "@dads-framework/core/contract";
|
|
2
|
+
/**
|
|
3
|
+
* Whether an answer is the API's access edge refusing the caller: a 404 the
|
|
4
|
+
* edge marked (`ACCESS_REFUSAL_HEADER`). Only the mark counts. An unmarked
|
|
5
|
+
* 404 is a record a handler did not find, an unmarked 403 is some other rule
|
|
6
|
+
* saying no, and a 401 is a session that ended.
|
|
7
|
+
*/
|
|
8
|
+
export function isAccessRefusal(status, refusalHeader) {
|
|
9
|
+
return status === 404 && refusalHeader === ACCESS_REFUSAL_VALUE;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=access-refusal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access-refusal.js","sourceRoot":"","sources":["../../src/lib/access-refusal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AAErE;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAC7B,MAAc,EACd,aAA4B;IAE5B,OAAO,MAAM,KAAK,GAAG,IAAI,aAAa,KAAK,oBAAoB,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed failure every transport verb throws.
|
|
3
|
+
*
|
|
4
|
+
* Callers branch on `code` (the stable SCREAMING_SNAKE identifier the error
|
|
5
|
+
* envelope carries) or on `status` — never on `message`, whose wording the
|
|
6
|
+
* API is free to change (docs/architecture/api-conventions.md).
|
|
7
|
+
*/
|
|
8
|
+
export declare class ApiError extends Error {
|
|
9
|
+
readonly status: number;
|
|
10
|
+
readonly code: string;
|
|
11
|
+
readonly details?: Record<string, unknown> | undefined;
|
|
12
|
+
readonly requestId?: string | undefined;
|
|
13
|
+
constructor(status: number, code: string, message: string, details?: Record<string, unknown> | undefined, requestId?: string | undefined);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A 401, given its own class.
|
|
17
|
+
*
|
|
18
|
+
* An expired or missing session is the one failure the whole app reacts to the
|
|
19
|
+
* same way, so it gets a type an `instanceof` can test instead of a status
|
|
20
|
+
* number every caller has to remember.
|
|
21
|
+
*/
|
|
22
|
+
export declare class UnauthorizedError extends ApiError {
|
|
23
|
+
constructor(code: string, message: string, details?: Record<string, unknown>, requestId?: string);
|
|
24
|
+
}
|
|
25
|
+
/** A request that never received an HTTP response from the API. */
|
|
26
|
+
export declare class NetworkError extends Error {
|
|
27
|
+
constructor(cause: unknown);
|
|
28
|
+
}
|
|
29
|
+
/** One thing a response said it could not be. Paths and codes, never text. */
|
|
30
|
+
export type ContractDriftIssue = {
|
|
31
|
+
/** Where in the response the shape was wrong, e.g. `data.lines.0.quantity`. */
|
|
32
|
+
readonly path: string;
|
|
33
|
+
/** Zod's machine code for the mismatch, e.g. `invalid_type`. */
|
|
34
|
+
readonly code: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Message-free failure data safe to hand to a localized renderer.
|
|
38
|
+
*
|
|
39
|
+
* The server and JavaScript messages remain on their original errors for logs
|
|
40
|
+
* and debugging. They deliberately do not cross this seam into presentation.
|
|
41
|
+
*
|
|
42
|
+
* `cause` is the exception to that rule and the reason it is spelled out
|
|
43
|
+
* below: it carries the ORIGINAL thrown value for telemetry, which previously
|
|
44
|
+
* had nowhere to go — `readClientFailure` classified a `TypeError` from a
|
|
45
|
+
* render, a chunk-load failure, and a contract mismatch identically as
|
|
46
|
+
* `unexpected` and dropped every one of them on the floor. It is for reporting
|
|
47
|
+
* and logging only and is never rendered; the message-free posture of the
|
|
48
|
+
* presentation path is unchanged.
|
|
49
|
+
*/
|
|
50
|
+
export type ClientFailure = {
|
|
51
|
+
readonly kind: "session-expired";
|
|
52
|
+
} | {
|
|
53
|
+
readonly kind: "network";
|
|
54
|
+
readonly cause?: unknown;
|
|
55
|
+
} | {
|
|
56
|
+
readonly kind: "api";
|
|
57
|
+
readonly status: number;
|
|
58
|
+
readonly code: string;
|
|
59
|
+
readonly details?: Readonly<Record<string, unknown>>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The response did not match the contract the client compiled against —
|
|
63
|
+
* the two ends have drifted. Distinguished from `unexpected` because it is
|
|
64
|
+
* the one client-side failure that always means a deployment problem rather
|
|
65
|
+
* than a bug in either end's logic, and because it is actionable: the paths
|
|
66
|
+
* name exactly which fields disagree.
|
|
67
|
+
*/
|
|
68
|
+
| {
|
|
69
|
+
readonly kind: "contract-drift";
|
|
70
|
+
readonly issues: readonly ContractDriftIssue[];
|
|
71
|
+
} | {
|
|
72
|
+
readonly kind: "unexpected";
|
|
73
|
+
readonly cause?: unknown;
|
|
74
|
+
};
|
|
75
|
+
/** Classify a thrown value without making its raw message renderable. */
|
|
76
|
+
export declare function readClientFailure(error: unknown): ClientFailure;
|
|
77
|
+
/**
|
|
78
|
+
* Turn a failed response into the typed error for its status.
|
|
79
|
+
*
|
|
80
|
+
* The body is parsed with the shared error-envelope contract when it matches
|
|
81
|
+
* and ignored when it does not — a proxy, a gateway, or a crash can answer
|
|
82
|
+
* with HTML, and the caller still deserves a typed failure rather than a
|
|
83
|
+
* parse error from the transport.
|
|
84
|
+
*/
|
|
85
|
+
export declare function toApiError(status: number, body: unknown, fallbackRequestId: string): ApiError;
|
|
86
|
+
//# sourceMappingURL=api-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-error.d.ts","sourceRoot":"","sources":["../../src/lib/api-error.ts"],"names":[],"mappings":"AAqBA;;;;;;GAMG;AACH,qBAAa,QAAS,SAAQ,KAAK;IAE/B,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM;IAErB,QAAQ,CAAC,OAAO,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AAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,MAAM,EAAE,SAAS,kBAAkB,EAAE,CAAC;CAChD,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAE9D,yEAAyE;AACzE,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAqB/D;AAmBD;;;;;;;GAOG;AACH,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,iBAAiB,EAAE,MAAM,GACxB,QAAQ,CA2BV"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { apiErrorSchema } from "@dads-framework/core/contract";
|
|
3
|
+
/**
|
|
4
|
+
* Re-points a just-constructed error at its own class.
|
|
5
|
+
*
|
|
6
|
+
* Every "is this an expected refusal?" decision in the app is an `instanceof`
|
|
7
|
+
* check on these classes. A script on the page (a browser extension, a link
|
|
8
|
+
* scanner) can replace the global `Error` with a constructor that returns an
|
|
9
|
+
* object of its own; `super()` then hands back that object, whose prototype is
|
|
10
|
+
* plain `Error`, and a 409 or a 401 stops being recognisable as one — it gets
|
|
11
|
+
* reported as a fault and shown as an unknown failure. Setting the prototype after `super()` keeps the class no
|
|
12
|
+
* matter what `Error` was when this module loaded.
|
|
13
|
+
*/
|
|
14
|
+
function keepOwnPrototype(error, ownClass) {
|
|
15
|
+
if (Object.getPrototypeOf(error) !== ownClass.prototype) {
|
|
16
|
+
Object.setPrototypeOf(error, ownClass.prototype);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The typed failure every transport verb throws.
|
|
21
|
+
*
|
|
22
|
+
* Callers branch on `code` (the stable SCREAMING_SNAKE identifier the error
|
|
23
|
+
* envelope carries) or on `status` — never on `message`, whose wording the
|
|
24
|
+
* API is free to change (docs/architecture/api-conventions.md).
|
|
25
|
+
*/
|
|
26
|
+
export class ApiError extends Error {
|
|
27
|
+
status;
|
|
28
|
+
code;
|
|
29
|
+
details;
|
|
30
|
+
requestId;
|
|
31
|
+
constructor(status, code, message, details, requestId) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.status = status;
|
|
34
|
+
this.code = code;
|
|
35
|
+
this.details = details;
|
|
36
|
+
this.requestId = requestId;
|
|
37
|
+
keepOwnPrototype(this, new.target);
|
|
38
|
+
this.name = "ApiError";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A 401, given its own class.
|
|
43
|
+
*
|
|
44
|
+
* An expired or missing session is the one failure the whole app reacts to the
|
|
45
|
+
* same way, so it gets a type an `instanceof` can test instead of a status
|
|
46
|
+
* number every caller has to remember.
|
|
47
|
+
*/
|
|
48
|
+
export class UnauthorizedError extends ApiError {
|
|
49
|
+
constructor(code, message, details, requestId) {
|
|
50
|
+
super(401, code, message, details, requestId);
|
|
51
|
+
this.name = "UnauthorizedError";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** A request that never received an HTTP response from the API. */
|
|
55
|
+
export class NetworkError extends Error {
|
|
56
|
+
constructor(cause) {
|
|
57
|
+
super("The API request did not receive a response", { cause });
|
|
58
|
+
keepOwnPrototype(this, new.target);
|
|
59
|
+
this.name = "NetworkError";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Classify a thrown value without making its raw message renderable. */
|
|
63
|
+
export function readClientFailure(error) {
|
|
64
|
+
if (error instanceof UnauthorizedError) {
|
|
65
|
+
return { kind: "session-expired" };
|
|
66
|
+
}
|
|
67
|
+
if (error instanceof NetworkError) {
|
|
68
|
+
return { kind: "network", cause: error.cause };
|
|
69
|
+
}
|
|
70
|
+
if (error instanceof ApiError) {
|
|
71
|
+
return error.details === undefined
|
|
72
|
+
? { kind: "api", status: error.status, code: error.code }
|
|
73
|
+
: {
|
|
74
|
+
kind: "api",
|
|
75
|
+
status: error.status,
|
|
76
|
+
code: error.code,
|
|
77
|
+
details: error.details,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
if (error instanceof z.ZodError) {
|
|
81
|
+
return { kind: "contract-drift", issues: driftIssues(error) };
|
|
82
|
+
}
|
|
83
|
+
return { kind: "unexpected", cause: error };
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Zod's issues, reduced to what is safe to send anywhere.
|
|
87
|
+
*
|
|
88
|
+
* Paths and codes only. `issue.message` is prose, and a custom refinement is
|
|
89
|
+
* free to compose it from the value it rejected — so a message can quote a
|
|
90
|
+
* customer's data, which must not ride a telemetry event out of the browser.
|
|
91
|
+
* A path names which field disagreed, which is the actionable half anyway.
|
|
92
|
+
*/
|
|
93
|
+
function driftIssues(error) {
|
|
94
|
+
return error.issues.map((issue) => ({
|
|
95
|
+
path: issue.path.join("."),
|
|
96
|
+
code: issue.code,
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
const UNAUTHORIZED_STATUS = 401;
|
|
100
|
+
/**
|
|
101
|
+
* Turn a failed response into the typed error for its status.
|
|
102
|
+
*
|
|
103
|
+
* The body is parsed with the shared error-envelope contract when it matches
|
|
104
|
+
* and ignored when it does not — a proxy, a gateway, or a crash can answer
|
|
105
|
+
* with HTML, and the caller still deserves a typed failure rather than a
|
|
106
|
+
* parse error from the transport.
|
|
107
|
+
*/
|
|
108
|
+
export function toApiError(status, body, fallbackRequestId) {
|
|
109
|
+
const envelope = apiErrorSchema.safeParse(body);
|
|
110
|
+
const failure = envelope.success
|
|
111
|
+
? envelope.data.error
|
|
112
|
+
: {
|
|
113
|
+
code: "HTTP_ERROR",
|
|
114
|
+
message: `Request failed with ${String(status)}`,
|
|
115
|
+
details: undefined,
|
|
116
|
+
requestId: undefined,
|
|
117
|
+
};
|
|
118
|
+
const requestId = failure.requestId ?? fallbackRequestId;
|
|
119
|
+
if (status === UNAUTHORIZED_STATUS) {
|
|
120
|
+
return new UnauthorizedError(failure.code, failure.message, failure.details, requestId);
|
|
121
|
+
}
|
|
122
|
+
return new ApiError(status, failure.code, failure.message, failure.details, requestId);
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=api-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-error.js","sourceRoot":"","sources":["../../src/lib/api-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAE/D;;;;;;;;;;GAUG;AACH,SAAS,gBAAgB,CAAC,KAAY,EAAE,QAA+B;IACrE,IAAI,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,KAAK,QAAQ,CAAC,SAAS,EAAE,CAAC;QACxD,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC;IACnD,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IAEtB,MAAM;IACN,IAAI;IAEJ,OAAO;IACP,SAAS;IALpB,YACW,MAAc,EACd,IAAY,EACrB,OAAe,EACN,OAAiC,EACjC,SAAkB;QAE3B,KAAK,CAAC,OAAO,CAAC,CAAC;sBANN,MAAM;oBACN,IAAI;uBAEJ,OAAO;yBACP,SAAS;QAGlB,gBAAgB,CAAC,IAAI,EAAE,IAAI,MAAM,CAAC,CAAC;QACnC,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;IACzB,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,OAAO,iBAAkB,SAAQ,QAAQ;IAC7C,YACE,IAAY,EACZ,OAAe,EACf,OAAiC,EACjC,SAAkB;QAElB,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AAED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AK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QAClC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;QAC1B,IAAI,EAAE,KAAK,CAAC,IAAI;KACjB,CAAC,CAAC,CAAC;AACN,CAAC;AAED,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAEhC;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CACxB,MAAc,EACd,IAAa,EACb,iBAAyB;IAEzB,MAAM,QAAQ,GAAG,cAAc,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IAChD,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO;QAC9B,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK;QACrB,CAAC,CAAC;YACE,IAAI,EAAE,YAAY;YAClB,OAAO,EAAE,uBAAuB,MAAM,CAAC,MAAM,CAAC,EAAE;YAChD,OAAO,EAAE,SAAS;YAClB,SAAS,EAAE,SAAS;SACrB,CAAC;IACN,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,iBAAiB,CAAC;IAEzD,IAAI,MAAM,KAAK,mBAAmB,EAAE,CAAC;QACnC,OAAO,IAAI,iBAAiB,CAC1B,OAAO,CAAC,IAAI,EACZ,OAAO,CAAC,OAAO,EACf,OAAO,CAAC,OAAO,EACf,SAAS,CACV,CAAC;IACJ,CAAC;IACD,OAAO,IAAI,QAAQ,CACjB,MAAM,EACN,OAAO,CAAC,IAAI,EACZ,OAAO,CAAC,OAAO,EACf,OAAO,CAAC,OAAO,EACf,SAAS,CACV,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every API call, announced to whoever is listening.
|
|
3
|
+
*
|
|
4
|
+
* Telemetry's problem is correlation: a browser fault is only half a story
|
|
5
|
+
* until you can reach the request it was about, and through that request's id
|
|
6
|
+
* the `request_log` row and the `audit_log` rows it produced. The transport
|
|
7
|
+
* mints that id, so the transport is the only place that can say it.
|
|
8
|
+
*
|
|
9
|
+
* A LIST of listeners, not a slot. `registerRequestMetadata` next door is
|
|
10
|
+
* deliberately singular — one owner may add headers and steer requests — and
|
|
11
|
+
* the feature-flag runtime holds it. Observation is a different kind of thing:
|
|
12
|
+
* several things may legitimately watch, and none of them may evict another.
|
|
13
|
+
* Modelled on `unauthorized-response-channel.ts` beside it for exactly that
|
|
14
|
+
* reason.
|
|
15
|
+
*
|
|
16
|
+
* Announcing is best-effort: a listener that throws is isolated, because a
|
|
17
|
+
* broken observer must not break the request it was observing.
|
|
18
|
+
*/
|
|
19
|
+
import type { HttpMethod } from "./send-api-request.js";
|
|
20
|
+
/** One completed API call, as an observer needs to see it. */
|
|
21
|
+
export type ApiExchange = {
|
|
22
|
+
readonly method: HttpMethod;
|
|
23
|
+
/** The request path, without its query string. */
|
|
24
|
+
readonly path: string;
|
|
25
|
+
/** The status the API answered, or null when no response arrived. */
|
|
26
|
+
readonly status: number | null;
|
|
27
|
+
/** The id this call carried, which is `request_log`'s primary key. */
|
|
28
|
+
readonly requestId: string;
|
|
29
|
+
readonly durationMs: number;
|
|
30
|
+
/**
|
|
31
|
+
* True when the API refused the call for access: a 403, or a 404 its
|
|
32
|
+
* access edge marked (`ACCESS_REFUSAL_HEADER`) rather than a record that
|
|
33
|
+
* is not there. A 401 is a signed-out session, not this.
|
|
34
|
+
*/
|
|
35
|
+
readonly refusedForAccess: boolean;
|
|
36
|
+
};
|
|
37
|
+
export type ApiExchangeListener = (exchange: ApiExchange) => void;
|
|
38
|
+
/** Watch every API call. Returns the function that stops watching. */
|
|
39
|
+
export declare function onApiExchange(listener: ApiExchangeListener): () => void;
|
|
40
|
+
/**
|
|
41
|
+
* Tell every listener about one call.
|
|
42
|
+
*
|
|
43
|
+
* Not exported from the barrel: announcing is the transport's own business,
|
|
44
|
+
* and a caller that could announce could forge correlation.
|
|
45
|
+
*/
|
|
46
|
+
export declare function announceApiExchange(exchange: ApiExchange): void;
|
|
47
|
+
//# sourceMappingURL=api-exchange-channel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-exchange-channel.d.ts","sourceRoot":"","sources":["../../src/lib/api-exchange-channel.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAExD,8DAA8D;AAC9D,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,kDAAkD;IAClD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,qEAAqE;IACrE,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,sEAAsE;IACtE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B;;;;OAIG;IACH,QAAQ,CAAC,gBAAgB,EAAE,OAAO,CAAC;CACpC,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG,CAAC,QAAQ,EAAE,WAAW,KAAK,IAAI,CAAC;AAIlE,sEAAsE;AACtE,wBAAgB,aAAa,CAAC,QAAQ,EAAE,mBAAmB,GAAG,MAAM,IAAI,CAKvE;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,WAAW,GAAG,IAAI,CAQ/D"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every API call, announced to whoever is listening.
|
|
3
|
+
*
|
|
4
|
+
* Telemetry's problem is correlation: a browser fault is only half a story
|
|
5
|
+
* until you can reach the request it was about, and through that request's id
|
|
6
|
+
* the `request_log` row and the `audit_log` rows it produced. The transport
|
|
7
|
+
* mints that id, so the transport is the only place that can say it.
|
|
8
|
+
*
|
|
9
|
+
* A LIST of listeners, not a slot. `registerRequestMetadata` next door is
|
|
10
|
+
* deliberately singular — one owner may add headers and steer requests — and
|
|
11
|
+
* the feature-flag runtime holds it. Observation is a different kind of thing:
|
|
12
|
+
* several things may legitimately watch, and none of them may evict another.
|
|
13
|
+
* Modelled on `unauthorized-response-channel.ts` beside it for exactly that
|
|
14
|
+
* reason.
|
|
15
|
+
*
|
|
16
|
+
* Announcing is best-effort: a listener that throws is isolated, because a
|
|
17
|
+
* broken observer must not break the request it was observing.
|
|
18
|
+
*/
|
|
19
|
+
const listeners = new Set();
|
|
20
|
+
/** Watch every API call. Returns the function that stops watching. */
|
|
21
|
+
export function onApiExchange(listener) {
|
|
22
|
+
listeners.add(listener);
|
|
23
|
+
return () => {
|
|
24
|
+
listeners.delete(listener);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Tell every listener about one call.
|
|
29
|
+
*
|
|
30
|
+
* Not exported from the barrel: announcing is the transport's own business,
|
|
31
|
+
* and a caller that could announce could forge correlation.
|
|
32
|
+
*/
|
|
33
|
+
export function announceApiExchange(exchange) {
|
|
34
|
+
for (const listener of listeners) {
|
|
35
|
+
try {
|
|
36
|
+
listener(exchange);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// A broken observer must not break the request it was observing.
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=api-exchange-channel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-exchange-channel.js","sourceRoot":"","sources":["../../src/lib/api-exchange-channel.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAwBH,MAAM,SAAS,GAAG,IAAI,GAAG,EAAuB,CAAC;AAEjD,sEAAsE;AACtE,MAAM,UAAU,aAAa,CAAC,QAA6B;IACzD,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACxB,OAAO,GAAG,EAAE;QACV,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC7B,CAAC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,QAAqB;IACvD,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,IAAI,CAAC;YACH,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACrB,CAAC;QAAC,MAAM,CAAC;YACP,iEAAiE;QACnE,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type BoundRoute, type HttpMethod, type PaginationMeta } from "@dads-framework/core/contract";
|
|
3
|
+
import { type ApiRequestHeaders } from "./send-api-request.js";
|
|
4
|
+
/**
|
|
5
|
+
* The three verbs every `web` API client calls.
|
|
6
|
+
*
|
|
7
|
+
* Each one takes the contract schema for what it sends and the contract schema
|
|
8
|
+
* for what it expects back, so both sides of a call are validated at the same
|
|
9
|
+
* seam and a caller never names a type by hand. That is the whole reason the
|
|
10
|
+
* schemas sit in the signature: a generic parameter the caller supplies (or an
|
|
11
|
+
* `as` on the result) would let the client's idea of a payload drift from the
|
|
12
|
+
* server's silently, which is the failure mode this lib exists to prevent.
|
|
13
|
+
*/
|
|
14
|
+
/** A value paired with the contract schema that proves it is well formed. */
|
|
15
|
+
export type ApiArgument<TSchema extends z.ZodType> = {
|
|
16
|
+
readonly schema: TSchema;
|
|
17
|
+
readonly value: z.input<TSchema>;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A query object paired with its schema.
|
|
21
|
+
*
|
|
22
|
+
* Narrower than {@link ApiArgument} because a query string can only be built
|
|
23
|
+
* from a record of named values.
|
|
24
|
+
*/
|
|
25
|
+
export type ApiQueryArgument<TQuery extends z.ZodType<Record<string, unknown>>> = {
|
|
26
|
+
readonly schema: TQuery;
|
|
27
|
+
readonly value: z.input<TQuery>;
|
|
28
|
+
};
|
|
29
|
+
/** One page of a collection endpoint, already unwrapped from its envelope. */
|
|
30
|
+
export type ApiPage<TItem> = {
|
|
31
|
+
readonly items: readonly TItem[];
|
|
32
|
+
readonly meta: PaginationMeta;
|
|
33
|
+
};
|
|
34
|
+
/** A page whose endpoint declares its own meta, extending the pagination meta. */
|
|
35
|
+
export type ApiPageWithMeta<TItem, TMeta extends PaginationMeta> = {
|
|
36
|
+
readonly items: readonly TItem[];
|
|
37
|
+
readonly meta: TMeta;
|
|
38
|
+
};
|
|
39
|
+
export type BinaryRequest<TBody extends z.ZodType, TQuery extends z.ZodType<Record<string, unknown>>> = {
|
|
40
|
+
readonly route: BoundRoute;
|
|
41
|
+
readonly body?: ApiArgument<TBody>;
|
|
42
|
+
readonly query?: ApiQueryArgument<TQuery>;
|
|
43
|
+
readonly signal?: AbortSignal;
|
|
44
|
+
readonly headers?: ApiRequestHeaders;
|
|
45
|
+
readonly accept?: string;
|
|
46
|
+
};
|
|
47
|
+
type GetRequest<TQuery extends z.ZodType<Record<string, unknown>>, TData extends z.ZodType> = {
|
|
48
|
+
readonly route: BoundRoute<"GET">;
|
|
49
|
+
readonly returns: TData;
|
|
50
|
+
readonly query?: ApiQueryArgument<TQuery>;
|
|
51
|
+
readonly signal?: AbortSignal;
|
|
52
|
+
readonly headers?: ApiRequestHeaders;
|
|
53
|
+
};
|
|
54
|
+
type SendRequest<TBody extends z.ZodType, TData extends z.ZodType, TQuery extends z.ZodType<Record<string, unknown>>> = {
|
|
55
|
+
readonly query?: ApiQueryArgument<TQuery>;
|
|
56
|
+
readonly returns: TData;
|
|
57
|
+
readonly body?: ApiArgument<TBody>;
|
|
58
|
+
readonly signal?: AbortSignal;
|
|
59
|
+
readonly headers?: ApiRequestHeaders;
|
|
60
|
+
} & MutationRequestLocation;
|
|
61
|
+
type NoContentRequest<TBody extends z.ZodType> = {
|
|
62
|
+
readonly body?: ApiArgument<TBody>;
|
|
63
|
+
readonly signal?: AbortSignal;
|
|
64
|
+
readonly headers?: ApiRequestHeaders;
|
|
65
|
+
} & MutationRequestLocation;
|
|
66
|
+
type MutationMethod = Exclude<HttpMethod, "GET">;
|
|
67
|
+
type MutationRequestLocation = {
|
|
68
|
+
readonly route: BoundRoute<MutationMethod>;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Read one resource: `GET path` unwrapped from its `{ data }` envelope.
|
|
72
|
+
*/
|
|
73
|
+
export declare function getFromApi<TQuery extends z.ZodType<Record<string, unknown>>, TData extends z.ZodType>(request: GetRequest<TQuery, TData>): Promise<z.output<TData>>;
|
|
74
|
+
/**
|
|
75
|
+
* Read one page of a collection: `GET path` unwrapped to `{ items, meta }`.
|
|
76
|
+
*
|
|
77
|
+
* Separate from {@link getFromApi} because a paginated endpoint's `meta` is
|
|
78
|
+
* part of the answer, not decoration — a caller that dropped it could not
|
|
79
|
+
* render the next page.
|
|
80
|
+
*/
|
|
81
|
+
export declare function getPageFromApi<TQuery extends z.ZodType<Record<string, unknown>>, TItem extends z.ZodType>(request: GetRequest<TQuery, TItem>): Promise<ApiPage<z.output<TItem>>>;
|
|
82
|
+
/**
|
|
83
|
+
* Read one page of a collection whose endpoint declares its own meta (a
|
|
84
|
+
* schema extending `paginationMetaSchema`), such as counts the page shows
|
|
85
|
+
* beside the rows: `GET path` unwrapped to `{ items, meta }`, the meta
|
|
86
|
+
* parsed by the endpoint's own meta schema.
|
|
87
|
+
*/
|
|
88
|
+
export declare function getPageWithMetaFromApi<TQuery extends z.ZodType<Record<string, unknown>>, TItem extends z.ZodType, TMeta extends z.ZodType<PaginationMeta>>(request: GetRequest<TQuery, TItem> & {
|
|
89
|
+
readonly meta: TMeta;
|
|
90
|
+
}): Promise<ApiPageWithMeta<z.output<TItem>, z.output<TMeta>>>;
|
|
91
|
+
/**
|
|
92
|
+
* Change something: any non-GET verb, with the body validated before it is
|
|
93
|
+
* sent and the response unwrapped from its `{ data }` envelope.
|
|
94
|
+
*
|
|
95
|
+
* Validating the request body here rather than at the call site means a
|
|
96
|
+
* malformed payload fails in the caller's own stack trace, against the
|
|
97
|
+
* server's own contract, instead of coming back as a 400 the UI has to
|
|
98
|
+
* interpret.
|
|
99
|
+
*/
|
|
100
|
+
export declare function sendToApi<TBody extends z.ZodType, TData extends z.ZodType, TQuery extends z.ZodType<Record<string, unknown>> = z.ZodType<Record<string, unknown>>>(request: SendRequest<TBody, TData, TQuery>): Promise<z.output<TData>>;
|
|
101
|
+
/**
|
|
102
|
+
* Change something whose answer is "done": a verb the API replies to with
|
|
103
|
+
* `204 No Content`.
|
|
104
|
+
*
|
|
105
|
+
* Separate from {@link sendToApi} because a 204 has no body at all, and
|
|
106
|
+
* reading one is not a degenerate case of reading a `{ data }` envelope — it
|
|
107
|
+
* throws. Every `deleteRoute` in this repo answers this way, and each still
|
|
108
|
+
* declares a request body (an audit reason), which is why the body argument
|
|
109
|
+
* survives while the response schema does not.
|
|
110
|
+
*/
|
|
111
|
+
export declare function sendToApiWithoutResponse<TBody extends z.ZodType>(request: NoContentRequest<TBody>): Promise<void>;
|
|
112
|
+
/** Binary endpoints keep the transport's credentials, metadata, and error mapping. */
|
|
113
|
+
export declare function requestBinary<TBody extends z.ZodType, TQuery extends z.ZodType<Record<string, unknown>>>(request: BinaryRequest<TBody, TQuery>): Promise<Uint8Array>;
|
|
114
|
+
export {};
|
|
115
|
+
//# sourceMappingURL=api-verbs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-verbs.d.ts","sourceRoot":"","sources":["../../src/lib/api-verbs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,cAAc,EAEpB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,EAAE,KAAK,iBAAiB,EAAkB,MAAM,uBAAuB,CAAC;AAE/E;;;;;;;;;GASG;AAEH,6EAA6E;AAC7E,MAAM,MAAM,WAAW,CAAC,OAAO,SAAS,CAAC,CAAC,OAAO,IAAI;IACnD,QAAQ,CAAC,MAAM,EAA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