@ahoo-wang/wow-client 10.0.0-alpha.7 → 10.0.0-beta.2
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 +8 -5
- package/README.zh-CN.md +4 -3
- package/dist/commands.cjs +1 -1
- package/dist/commands.cjs.map +1 -1
- package/dist/commands.d.cts +9 -13
- package/dist/commands.d.ts +9 -13
- package/dist/commands.es.js +60 -54
- package/dist/commands.es.js.map +1 -1
- package/dist/dsl/aggregation/admit.cjs +1 -1
- package/dist/dsl/aggregation/admit.cjs.map +1 -1
- package/dist/dsl/aggregation/admit.es.js +73 -69
- package/dist/dsl/aggregation/admit.es.js.map +1 -1
- package/dist/dsl/aggregation/aliases.cjs +2 -0
- package/dist/dsl/aggregation/aliases.cjs.map +1 -0
- package/dist/dsl/aggregation/aliases.d.cts +2 -0
- package/dist/dsl/aggregation/aliases.d.ts +2 -0
- package/dist/dsl/aggregation/aliases.es.js +13 -0
- package/dist/dsl/aggregation/aliases.es.js.map +1 -0
- package/dist/dsl/aggregation/builders.cjs +1 -1
- package/dist/dsl/aggregation/builders.cjs.map +1 -1
- package/dist/dsl/aggregation/builders.d.cts +17 -243
- package/dist/dsl/aggregation/builders.d.ts +17 -243
- package/dist/dsl/aggregation/builders.es.js +36 -171
- package/dist/dsl/aggregation/builders.es.js.map +1 -1
- package/dist/dsl/aggregation/derived.cjs +1 -1
- package/dist/dsl/aggregation/derived.cjs.map +1 -1
- package/dist/dsl/aggregation/derived.d.cts +123 -1
- package/dist/dsl/aggregation/derived.d.ts +123 -1
- package/dist/dsl/aggregation/derived.es.js +43 -28
- package/dist/dsl/aggregation/derived.es.js.map +1 -1
- package/dist/dsl/aggregation/groups.cjs +2 -0
- package/dist/dsl/aggregation/groups.cjs.map +1 -0
- package/dist/dsl/aggregation/groups.d.cts +325 -0
- package/dist/dsl/aggregation/groups.d.ts +325 -0
- package/dist/dsl/aggregation/groups.es.js +80 -0
- package/dist/dsl/aggregation/groups.es.js.map +1 -0
- package/dist/dsl/aggregation/having.cjs +1 -1
- package/dist/dsl/aggregation/having.cjs.map +1 -1
- package/dist/dsl/aggregation/having.d.cts +63 -1
- package/dist/dsl/aggregation/having.d.ts +63 -1
- package/dist/dsl/aggregation/having.es.js +33 -30
- package/dist/dsl/aggregation/having.es.js.map +1 -1
- package/dist/dsl/aggregation/index.d.cts +4 -2
- package/dist/dsl/aggregation/index.d.ts +4 -2
- package/dist/dsl/aggregation/metrics.cjs +2 -0
- package/dist/dsl/aggregation/metrics.cjs.map +1 -0
- package/dist/dsl/aggregation/metrics.d.cts +279 -0
- package/dist/dsl/aggregation/metrics.d.ts +279 -0
- package/dist/dsl/aggregation/metrics.es.js +75 -0
- package/dist/dsl/aggregation/metrics.es.js.map +1 -0
- package/dist/dsl/aggregation/types.cjs +1 -1
- package/dist/dsl/aggregation/types.cjs.map +1 -1
- package/dist/dsl/aggregation/types.d.cts +4 -524
- package/dist/dsl/aggregation/types.d.ts +4 -524
- package/dist/dsl/aggregation/types.es.js +5 -25
- package/dist/dsl/aggregation/types.es.js.map +1 -1
- package/dist/dsl/cursorQuery.cjs +1 -1
- package/dist/dsl/cursorQuery.cjs.map +1 -1
- package/dist/dsl/cursorQuery.d.cts +18 -7
- package/dist/dsl/cursorQuery.d.ts +18 -7
- package/dist/dsl/cursorQuery.es.js +13 -12
- package/dist/dsl/cursorQuery.es.js.map +1 -1
- package/dist/dsl/descriptor.cjs +1 -1
- package/dist/dsl/descriptor.cjs.map +1 -1
- package/dist/dsl/descriptor.d.cts +24 -379
- package/dist/dsl/descriptor.d.ts +24 -379
- package/dist/dsl/descriptor.es.js +6 -32
- package/dist/dsl/descriptor.es.js.map +1 -1
- package/dist/dsl/fieldDescriptor.cjs +2 -0
- package/dist/dsl/fieldDescriptor.cjs.map +1 -0
- package/dist/dsl/fieldDescriptor.d.cts +246 -0
- package/dist/dsl/fieldDescriptor.d.ts +246 -0
- package/dist/dsl/fieldDescriptor.es.js +24 -0
- package/dist/dsl/fieldDescriptor.es.js.map +1 -0
- package/dist/dsl/filter/builders.cjs +1 -1
- package/dist/dsl/filter/builders.cjs.map +1 -1
- package/dist/dsl/filter/builders.d.cts +7 -949
- package/dist/dsl/filter/builders.d.ts +7 -949
- package/dist/dsl/filter/builders.es.js +14 -285
- package/dist/dsl/filter/builders.es.js.map +1 -1
- package/dist/dsl/filter/expressionFilter.cjs +2 -0
- package/dist/dsl/filter/expressionFilter.cjs.map +1 -0
- package/dist/dsl/filter/expressionFilter.d.cts +35 -0
- package/dist/dsl/filter/expressionFilter.d.ts +35 -0
- package/dist/dsl/filter/expressionFilter.es.js +18 -0
- package/dist/dsl/filter/expressionFilter.es.js.map +1 -0
- package/dist/dsl/filter/fieldFilters.cjs +2 -0
- package/dist/dsl/filter/fieldFilters.cjs.map +1 -0
- package/dist/dsl/filter/fieldFilters.d.cts +297 -0
- package/dist/dsl/filter/fieldFilters.d.ts +297 -0
- package/dist/dsl/filter/fieldFilters.es.js +110 -0
- package/dist/dsl/filter/fieldFilters.es.js.map +1 -0
- package/dist/dsl/filter/index.d.cts +2 -1
- package/dist/dsl/filter/index.d.ts +2 -1
- package/dist/dsl/filter/logicalFilters.cjs +2 -0
- package/dist/dsl/filter/logicalFilters.cjs.map +1 -0
- package/dist/dsl/filter/logicalFilters.d.cts +109 -0
- package/dist/dsl/filter/logicalFilters.d.ts +109 -0
- package/dist/dsl/filter/logicalFilters.es.js +36 -0
- package/dist/dsl/filter/logicalFilters.es.js.map +1 -0
- package/dist/dsl/filter/metadataFilters.cjs +2 -0
- package/dist/dsl/filter/metadataFilters.cjs.map +1 -0
- package/dist/dsl/filter/metadataFilters.d.cts +132 -0
- package/dist/dsl/filter/metadataFilters.d.ts +132 -0
- package/dist/dsl/filter/metadataFilters.es.js +56 -0
- package/dist/dsl/filter/metadataFilters.es.js.map +1 -0
- package/dist/dsl/filter/operator.cjs +1 -1
- package/dist/dsl/filter/operator.cjs.map +1 -1
- package/dist/dsl/filter/operator.d.cts +11 -9
- package/dist/dsl/filter/operator.d.ts +11 -9
- package/dist/dsl/filter/operator.es.js +1 -1
- package/dist/dsl/filter/operator.es.js.map +1 -1
- package/dist/dsl/filter/searchFilter.cjs +2 -0
- package/dist/dsl/filter/searchFilter.cjs.map +1 -0
- package/dist/dsl/filter/searchFilter.d.cts +39 -0
- package/dist/dsl/filter/searchFilter.d.ts +39 -0
- package/dist/dsl/filter/searchFilter.es.js +19 -0
- package/dist/dsl/filter/searchFilter.es.js.map +1 -0
- package/dist/dsl/filter/timeFilters.cjs +2 -0
- package/dist/dsl/filter/timeFilters.cjs.map +1 -0
- package/dist/dsl/filter/timeFilters.d.cts +290 -0
- package/dist/dsl/filter/timeFilters.d.ts +290 -0
- package/dist/dsl/filter/timeFilters.es.js +89 -0
- package/dist/dsl/filter/timeFilters.es.js.map +1 -0
- package/dist/dsl/filter/types.d.cts +4 -14
- package/dist/dsl/filter/types.d.ts +4 -14
- package/dist/dsl/filter/validate.cjs +1 -1
- package/dist/dsl/filter/validate.cjs.map +1 -1
- package/dist/dsl/filter/validate.d.cts +2 -4
- package/dist/dsl/filter/validate.d.ts +2 -4
- package/dist/dsl/filter/validate.es.js +25 -31
- package/dist/dsl/filter/validate.es.js.map +1 -1
- package/dist/dsl/queryErrorCodes.cjs +1 -1
- package/dist/dsl/queryErrorCodes.cjs.map +1 -1
- package/dist/dsl/queryErrorCodes.d.cts +0 -2
- package/dist/dsl/queryErrorCodes.d.ts +0 -2
- package/dist/dsl/queryErrorCodes.es.js +0 -1
- package/dist/dsl/queryErrorCodes.es.js.map +1 -1
- package/dist/dsl/queryFilter.cjs +2 -0
- package/dist/dsl/queryFilter.cjs.map +1 -0
- package/dist/dsl/queryFilter.d.cts +3 -0
- package/dist/dsl/queryFilter.d.ts +3 -0
- package/dist/dsl/queryFilter.es.js +9 -0
- package/dist/dsl/queryFilter.es.js.map +1 -0
- package/dist/dsl/queryable.cjs +1 -1
- package/dist/dsl/queryable.cjs.map +1 -1
- package/dist/dsl/queryable.es.js +12 -16
- package/dist/dsl/queryable.es.js.map +1 -1
- package/dist/dsl/semanticTypes.cjs +2 -0
- package/dist/dsl/semanticTypes.cjs.map +1 -0
- package/dist/dsl/semanticTypes.d.cts +128 -0
- package/dist/dsl/semanticTypes.d.ts +128 -0
- package/dist/dsl/semanticTypes.es.js +13 -0
- package/dist/dsl/semanticTypes.es.js.map +1 -0
- package/dist/dsl.cjs +1 -1
- package/dist/dsl.d.cts +2 -0
- package/dist/dsl.d.ts +2 -0
- package/dist/dsl.es.js +14 -8
- package/dist/exactJson.cjs +2 -2
- package/dist/exactJson.cjs.map +1 -1
- package/dist/exactJson.d.cts +41 -16
- package/dist/exactJson.d.ts +41 -16
- package/dist/exactJson.es.js +104 -102
- package/dist/exactJson.es.js.map +1 -1
- package/dist/frameworkErrors.cjs.map +1 -1
- package/dist/frameworkErrors.d.cts +5 -5
- package/dist/frameworkErrors.d.ts +5 -5
- package/dist/frameworkErrors.es.js.map +1 -1
- package/dist/http.cjs +1 -1
- package/dist/http.cjs.map +1 -1
- package/dist/http.d.cts +29 -9
- package/dist/http.d.ts +29 -9
- package/dist/http.es.js +24 -19
- package/dist/http.es.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.es.js +12 -12
- package/dist/manifest.cjs +1 -1
- package/dist/manifest.cjs.map +1 -1
- package/dist/manifest.d.cts +30 -2
- package/dist/manifest.d.ts +30 -2
- package/dist/manifest.es.js +34 -6
- package/dist/manifest.es.js.map +1 -1
- package/dist/manifestTypes.d.cts +21 -4
- package/dist/manifestTypes.d.ts +21 -4
- package/dist/queries.cjs +1 -1
- package/dist/queries.cjs.map +1 -1
- package/dist/queries.d.cts +18 -8
- package/dist/queries.d.ts +18 -8
- package/dist/queries.es.js +46 -35
- package/dist/queries.es.js.map +1 -1
- package/dist/routes.cjs +1 -1
- package/dist/routes.cjs.map +1 -1
- package/dist/routes.d.cts +16 -3
- package/dist/routes.d.ts +16 -3
- package/dist/routes.es.js +24 -25
- package/dist/routes.es.js.map +1 -1
- package/dist/state.cjs +1 -1
- package/dist/state.cjs.map +1 -1
- package/dist/state.d.cts +21 -19
- package/dist/state.d.ts +21 -19
- package/dist/state.es.js +27 -22
- package/dist/state.es.js.map +1 -1
- package/dist/streams.cjs +1 -1
- package/dist/streams.cjs.map +1 -1
- package/dist/streams.d.cts +14 -7
- package/dist/streams.d.ts +14 -7
- package/dist/streams.es.js +65 -50
- package/dist/streams.es.js.map +1 -1
- package/dist/testing/wire.cjs +1 -1
- package/dist/testing/wire.cjs.map +1 -1
- package/dist/testing/wire.es.js +1 -1
- package/dist/testing/wire.es.js.map +1 -1
- package/dist/wait.cjs +1 -1
- package/dist/wait.cjs.map +1 -1
- package/dist/wait.d.cts +18 -5
- package/dist/wait.d.ts +18 -5
- package/dist/wait.es.js +36 -34
- package/dist/wait.es.js.map +1 -1
- package/dist/wire.cjs +1 -1
- package/dist/wire.cjs.map +1 -1
- package/dist/wire.d.cts +76 -31
- package/dist/wire.d.ts +76 -31
- package/dist/wire.es.js +1 -3
- package/dist/wire.es.js.map +1 -1
- package/package.json +5 -5
- package/dist/dsl/filter/datePattern.cjs +0 -2
- package/dist/dsl/filter/datePattern.cjs.map +0 -1
- package/dist/dsl/filter/datePattern.d.cts +0 -8
- package/dist/dsl/filter/datePattern.d.ts +0 -8
- package/dist/dsl/filter/datePattern.es.js +0 -96
- package/dist/dsl/filter/datePattern.es.js.map +0 -1
package/dist/routes.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TextNumbers } from './exactJson.js';
|
|
1
2
|
import { RequestOptions, WowClientOptions } from './http.js';
|
|
2
3
|
import { RoutingManifest } from './manifest.js';
|
|
3
4
|
/** What a call of a service route sends, besides its route. */
|
|
@@ -11,6 +12,14 @@ export interface ServiceRouteCall {
|
|
|
11
12
|
/** The request's id (`Wow-Request-Id`); the service makes one without it. */
|
|
12
13
|
readonly requestId?: string;
|
|
13
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* The exact numbers ({@link TextNumbers}) of a service route: of the body it
|
|
17
|
+
* takes and of the JSON it answers.
|
|
18
|
+
*/
|
|
19
|
+
export interface ServiceRouteNumbers {
|
|
20
|
+
readonly request?: TextNumbers;
|
|
21
|
+
readonly response?: TextNumbers;
|
|
22
|
+
}
|
|
14
23
|
/** Options of a {@link ServiceRoutes}. */
|
|
15
24
|
export interface ServiceRoutesOptions {
|
|
16
25
|
/**
|
|
@@ -18,6 +27,11 @@ export interface ServiceRoutesOptions {
|
|
|
18
27
|
* it the client reads the service's (`GET /wow/manifest`) once.
|
|
19
28
|
*/
|
|
20
29
|
readonly manifest?: RoutingManifest;
|
|
30
|
+
/**
|
|
31
|
+
* The exact numbers of the routes that have any, by `context.route`: a
|
|
32
|
+
* generated client passes those of its Manifest.
|
|
33
|
+
*/
|
|
34
|
+
readonly textNumbers?: Readonly<Record<string, ServiceRouteNumbers>>;
|
|
21
35
|
}
|
|
22
36
|
/**
|
|
23
37
|
* Calls a service's routes by the names its Manifest gives them. The answer
|
|
@@ -27,7 +41,8 @@ export interface ServiceRoutesOptions {
|
|
|
27
41
|
*/
|
|
28
42
|
export declare class ServiceRoutes {
|
|
29
43
|
readonly client: WowClientOptions;
|
|
30
|
-
private manifest;
|
|
44
|
+
private readonly manifest;
|
|
45
|
+
private readonly numbers;
|
|
31
46
|
/**
|
|
32
47
|
* @param client - How the service is reached
|
|
33
48
|
* @param options - The service's Manifest
|
|
@@ -44,6 +59,4 @@ export declare class ServiceRoutes {
|
|
|
44
59
|
* variable is missing
|
|
45
60
|
*/
|
|
46
61
|
call<T = unknown>(context: string, name: string, call?: ServiceRouteCall, options?: RequestOptions): Promise<T | undefined>;
|
|
47
|
-
/** The Manifest given, or the service's, fetched once (again after a failure). */
|
|
48
|
-
private manifestOf;
|
|
49
62
|
}
|
package/dist/routes.es.js
CHANGED
|
@@ -1,33 +1,32 @@
|
|
|
1
|
-
import { WowHeaders as e,
|
|
2
|
-
import {
|
|
1
|
+
import { WowHeaders as e, send as t } from "./http.es.js";
|
|
2
|
+
import { ManifestSource as n } from "./manifest.es.js";
|
|
3
3
|
// src/routes.ts
|
|
4
|
-
var
|
|
4
|
+
var r = /\{([A-Za-z][A-Za-z0-9]*)}/g, i = class {
|
|
5
5
|
constructor(e = {}, t = {}) {
|
|
6
|
-
this.client = e, this.manifest = t.manifest
|
|
6
|
+
this.client = e, this.manifest = new n(e, t.manifest), this.numbers = t.textNumbers ?? {};
|
|
7
7
|
}
|
|
8
|
-
async call(
|
|
9
|
-
let
|
|
10
|
-
if (
|
|
11
|
-
let
|
|
12
|
-
let
|
|
13
|
-
if (
|
|
14
|
-
return encodeURIComponent(String(
|
|
15
|
-
}),
|
|
16
|
-
method:
|
|
17
|
-
path:
|
|
18
|
-
...
|
|
19
|
-
...
|
|
20
|
-
...
|
|
21
|
-
|
|
8
|
+
async call(n, i, a = {}, o = {}) {
|
|
9
|
+
let s = (await this.manifest.read(o.signal)).contexts.find((e) => e.name === n)?.routes?.find((e) => e.name === i)?.route;
|
|
10
|
+
if (s === void 0) throw TypeError(`The Manifest lists no service route ${i} of ${n}.`);
|
|
11
|
+
let c = s.path.replace(r, (e, t) => {
|
|
12
|
+
let r = a.path?.[t];
|
|
13
|
+
if (r === void 0) throw TypeError(`The service route ${n}.${i} needs its path variable ${t}.`);
|
|
14
|
+
return encodeURIComponent(String(r));
|
|
15
|
+
}), l = a.query ? Object.fromEntries(Object.entries(a.query).map(([e, t]) => [e, t === void 0 ? void 0 : String(t)])) : void 0, u = this.numbers[`${n}.${i}`], d = await t(this.client, {
|
|
16
|
+
method: s.method,
|
|
17
|
+
path: c,
|
|
18
|
+
...l === void 0 ? {} : { query: l },
|
|
19
|
+
...a.body === void 0 ? {} : { body: a.body },
|
|
20
|
+
...a.requestId === void 0 ? {} : { headers: { [e.REQUEST_ID]: a.requestId } },
|
|
21
|
+
numbers: {
|
|
22
|
+
body: u?.request,
|
|
23
|
+
answer: u?.response
|
|
24
|
+
},
|
|
25
|
+
options: o
|
|
22
26
|
});
|
|
23
|
-
return d.status === 204 ? void 0 :
|
|
24
|
-
}
|
|
25
|
-
manifestOf() {
|
|
26
|
-
return this.manifest ??= i(this.client).catch((e) => {
|
|
27
|
-
throw this.manifest = void 0, e;
|
|
28
|
-
}), this.manifest;
|
|
27
|
+
return d.response.status === 204 ? void 0 : d.json();
|
|
29
28
|
}
|
|
30
29
|
};
|
|
31
|
-
export {
|
|
30
|
+
export { i as ServiceRoutes };
|
|
32
31
|
|
|
33
32
|
//# sourceMappingURL=routes.es.js.map
|
package/dist/routes.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes.es.js","names":["VARIABLE","ServiceRoutes","client","
|
|
1
|
+
{"version":3,"file":"routes.es.js","names":["VARIABLE","ServiceRoutes","client","ManifestSource","options","route","item","context","name","path","value","call","variable","query","key","numbers","answer","send","WowHeaders"],"sources":["../src/routes.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * The client of a service's own routes: routes whose handler is the service's\n * code, neither a command nor a query, listed in its Manifest with their\n * parameters and schemas. A generated client (`routes.ts`) calls each by its\n * name with typed parameters; a hand-written one calls {@link ServiceRoutes.call}.\n *\n * @packageDocumentation\n */\n\nimport type { TextNumbers } from './exactJson.js';\nimport {\n type Call,\n type RequestOptions,\n send,\n WowHeaders,\n type WowClientOptions,\n} from './http.js';\nimport { ManifestSource, type RoutingManifest } from './manifest.js';\n\n/** What a call of a service route sends, besides its route. */\nexport interface ServiceRouteCall {\n /** The path's variables, by name: each of the route's `{name}`. */\n readonly path?: Readonly<Record<string, string | number | boolean>>;\n /** The query parameters, by name; one left undefined is not sent. */\n readonly query?: Readonly<\n Record<string, string | number | boolean | undefined>\n >;\n /** The body, for a route that takes one. */\n readonly body?: unknown;\n /** The request's id (`Wow-Request-Id`); the service makes one without it. */\n readonly requestId?: string;\n}\n\n/**\n * The exact numbers ({@link TextNumbers}) of a service route: of the body it\n * takes and of the JSON it answers.\n */\nexport interface ServiceRouteNumbers {\n readonly request?: TextNumbers;\n readonly response?: TextNumbers;\n}\n\n/** Options of a {@link ServiceRoutes}. */\nexport interface ServiceRoutesOptions {\n /**\n * The service's Manifest, or the part of it that lists the routes; without\n * it the client reads the service's (`GET /wow/manifest`) once.\n */\n readonly manifest?: RoutingManifest;\n /**\n * The exact numbers of the routes that have any, by `context.route`: a\n * generated client passes those of its Manifest.\n */\n readonly textNumbers?: Readonly<Record<string, ServiceRouteNumbers>>;\n}\n\nconst VARIABLE = /\\{([A-Za-z][A-Za-z0-9]*)}/g;\n\n/**\n * Calls a service's routes by the names its Manifest gives them. The answer\n * is the route's JSON, `undefined` for one that answers `204 No Content`; a\n * refusal rejects with the {@link WowProblemError} of its code, a parameter\n * that does not read with `wow.route.malformed`.\n */\nexport class ServiceRoutes {\n private readonly manifest: ManifestSource;\n private readonly numbers: Readonly<Record<string, ServiceRouteNumbers>>;\n\n /**\n * @param client - How the service is reached\n * @param options - The service's Manifest\n */\n constructor(\n readonly client: WowClientOptions = {},\n options: ServiceRoutesOptions = {},\n ) {\n this.manifest = new ManifestSource(client, options.manifest);\n this.numbers = options.textNumbers ?? {};\n }\n\n /**\n * Calls the route `name` of `context` and answers what it answers.\n *\n * @param context - The bounded context whose route it is\n * @param name - The route's name in the Manifest\n * @param call - Its path variables, query parameters and body\n * @param options - The scope or space to act in, headers and cancellation\n * @throws TypeError when the Manifest lists no such route, or a path\n * variable is missing\n */\n async call<T = unknown>(\n context: string,\n name: string,\n call: ServiceRouteCall = {},\n options: RequestOptions = {},\n ): Promise<T | undefined> {\n const route = (await this.manifest.read(options.signal)).contexts\n .find(item => item.name === context)\n ?.routes?.find(item => item.name === name)?.route;\n if (route === undefined) {\n throw new TypeError(\n `The Manifest lists no service route ${name} of ${context}.`,\n );\n }\n const path = route.path.replace(VARIABLE, (_, variable: string) => {\n const value = call.path?.[variable];\n if (value === undefined) {\n throw new TypeError(\n `The service route ${context}.${name} needs its path variable ${variable}.`,\n );\n }\n return encodeURIComponent(String(value));\n });\n const query = call.query\n ? Object.fromEntries(\n Object.entries(call.query).map(([key, value]) => [\n key,\n value === undefined ? undefined : String(value),\n ]),\n )\n : undefined;\n const numbers = this.numbers[`${context}.${name}`];\n const answer = await send(this.client, {\n method: route.method as Call['method'],\n path,\n ...(query === undefined ? {} : { query }),\n ...(call.body === undefined ? {} : { body: call.body }),\n ...(call.requestId === undefined\n ? {}\n : { headers: { [WowHeaders.REQUEST_ID]: call.requestId } }),\n numbers: { body: numbers?.request, answer: numbers?.response },\n options,\n });\n return answer.response.status === 204 ? undefined : answer.json<T>();\n }\n}\n"],"mappings":";;;AAqEA,IAAMA,IAAW,8BAQJC,IAAb,MAA2B;CAQzB,YACE,IAAoC,CAAC,GACrC,IAAgC,CAAC,GACjC;EAEA,AAJS,KAAA,SAAAC,GAGT,KAAK,WAAW,IAAIC,EAAeD,GAAQE,EAAQ,QAAQ,GAC3D,KAAK,UAAUA,EAAQ,eAAe,CAAC;CACzC;CAYA,MAAM,KACJ,GACA,GACA,IAAyB,CAAC,GAC1B,IAA0B,CAAC,GACH;EACxB,IAAMC,KAAS,MAAM,KAAK,SAAS,KAAKD,EAAQ,MAAM,EAAA,CAAG,SACtD,MAAK,MAAQE,EAAK,SAASC,CAAO,CAAC,EAClC,QAAQ,MAAK,MAAQD,EAAK,SAASE,CAAI,CAAC,EAAE;EAC9C,IAAIH,MAAU,KAAA,GACZ,MAAU,UACR,uCAAuCG,EAAK,MAAMD,EAAQ,EAC5D;EAEF,IAAME,IAAOJ,EAAM,KAAK,QAAQL,IAAW,GAAG,MAAqB;GACjE,IAAMU,IAAQC,EAAK,OAAOC;GAC1B,IAAIF,MAAU,KAAA,GACZ,MAAU,UACR,qBAAqBH,EAAQ,GAAGC,EAAK,2BAA2BI,EAAS,EAC3E;GAEF,OAAO,mBAAmB,OAAOF,CAAK,CAAC;EACzC,CAAC,GACKG,IAAQF,EAAK,QACf,OAAO,YACL,OAAO,QAAQA,EAAK,KAAK,CAAC,CAAC,KAAK,CAACG,GAAKJ,OAAW,CAC/CI,GACAJ,MAAU,KAAA,IAAY,KAAA,IAAY,OAAOA,CAAK,CAChD,CAAC,CACH,IACA,KAAA,GACEK,IAAU,KAAK,QAAQ,GAAGR,EAAQ,GAAGC,MACrCQ,IAAS,MAAMC,EAAK,KAAK,QAAQ;GACrC,QAAQZ,EAAM;GACd;GACA,GAAIQ,MAAU,KAAA,IAAY,CAAC,IAAI,EAAE,SAAM;GACvC,GAAIF,EAAK,SAAS,KAAA,IAAY,CAAC,IAAI,EAAE,MAAMA,EAAK,KAAK;GACrD,GAAIA,EAAK,cAAc,KAAA,IACnB,CAAC,IACD,EAAE,SAAS,GAAGO,EAAW,aAAaP,EAAK,UAAU,EAAE;GAC3D,SAAS;IAAE,MAAMI,GAAS;IAAS,QAAQA,GAAS;GAAS;GAC7D;EACF,CAAC;EACD,OAAOC,EAAO,SAAS,WAAW,MAAM,KAAA,IAAYA,EAAO,KAAQ;CACrE;AACF"}
|
package/dist/state.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./exactJson.cjs"),t=require("./frameworkErrors.cjs"),n=require("./problem.cjs"),r=require("./http.cjs");var i=class{constructor(e,t={}){this.type=e,this.client=t}read(e,t={}){return this.readAt(e,t)}get(e,t={}){return this.getAt(e,t)}find(e,t={}){return this.findAt(e,t)}trace(e,t={}){return this.traceAt(e,t)}async readAt(e,t){let n=t.at instanceof Date?t.at.toISOString():t.at,i=await r.send(this.client,{method:`GET`,path:this.statePath(e),query:{version:t.version,at:n,minVersion:t.minVersion},headers:{[r.WowHeaders.IF_NONE_MATCH]:t.ifNoneMatch===void 0?void 0:`"${t.ifNoneMatch}"`},options:t,notModified:!0});if(
|
|
1
|
+
const e=require("./exactJson.cjs"),t=require("./frameworkErrors.cjs"),n=require("./problem.cjs"),r=require("./http.cjs");var i=class{constructor(e,t={}){this.type=e,this.client=t}read(e,t={}){return this.readAt(e,t)}get(e,t={}){return this.getAt(e,t)}find(e,t={}){return this.findAt(e,t)}trace(e,t={}){return this.traceAt(e,t)}async readAt(e,t){let n=t.at instanceof Date?t.at.toISOString():t.at,i=await r.send(this.client,{method:`GET`,path:this.statePath(e),query:{version:t.version,at:n,minVersion:t.minVersion},headers:{[r.WowHeaders.IF_NONE_MATCH]:t.ifNoneMatch===void 0?void 0:`"${t.ifNoneMatch}"`},numbers:{answer:this.stateNumbers},options:t,notModified:!0}),a=i.response;if(a.status===304){let e=r.entityTag(a)??t.ifNoneMatch??``;return{notModified:!0,version:Number.parseInt(e,10),etag:e}}return{notModified:!1,snapshot:await i.json(),etag:r.entityTag(a)}}async getAt(e,t){let n=await this.readAt(e,t);if(n.notModified)throw Error(`A read without If-None-Match was answered 304.`);return n.snapshot}async findAt(e,r){try{return await this.getAt(e,r)}catch(e){if(e instanceof n.WowProblemError&&e.status===404&&e.code===t.FrameworkErrorCodes.AGGREGATE_NOT_FOUND)return;throw e}}async traceAt(e,t){return(await r.send(this.client,{method:`GET`,path:`${this.statePath(e)}/trace`,query:{from:t.from,to:t.to,limit:t.limit},numbers:{answer:this.traceNumbers},options:t})).json()}get stateNumbers(){return e.recordNumbers({state:this.type.textNumbers?.state,access:this.type.textNumbers?.access})}get traceNumbers(){let t=e.eventNumbers(this.type),n=e.recordNumbers({state:this.type.textNumbers?.state,events:t===void 0?void 0:{"*":t},access:this.type.textNumbers?.access});return n===void 0?void 0:{commits:{"*":n}}}statePath(e){let t=`/${this.type.context}/${this.type.name}`;return e===void 0?`${t}/state`:`${t}/${encodeURIComponent(e)}/state`}},a=class extends i{readOwn(e={}){return this.readAt(void 0,e)}getOwn(e={}){return this.getAt(void 0,e)}findOwn(e={}){return this.findAt(void 0,e)}traceOwn(e={}){return this.traceAt(void 0,e)}};exports.OwnStateClient=a,exports.StateClient=i;
|
|
2
2
|
//# sourceMappingURL=state.cjs.map
|
package/dist/state.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.cjs","names":["send","WowHeaders","entityTag","json","WowProblemError","FrameworkErrorCodes","eventNumbers","require_exactJson","require_frameworkErrors","require_problem","require_http","StateClient","type","client","id","options","at","response","etag","result","error","state","events","base","OwnStateClient"],"sources":["../src/state.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n entityTag,\n json,\n type RequestOptions,\n send,\n type WowClientOptions,\n WowHeaders,\n} from './http.js';\nimport { eventNumbers, type TextNumbers } from './exactJson.js';\nimport { FrameworkErrorCodes, WowProblemError } from './problem.js';\nimport type {\n AggregateType,\n CommittedEvent,\n StateTrace,\n Snapshot,\n} from './wire.js';\n\n/** Which state a read asks for: the latest, at a version or as of a time. */\nexport interface StateReadOptions extends RequestOptions {\n /** The committed state at this version. */\n readonly version?: number;\n /** The committed state as of this instant (ISO-8601, or a `Date`). */\n readonly at?: string | Date;\n /**\n * The latest state at this version or later (review R4-Q, Q-13: read your\n * writes), such as the `version` of a command's result: a lagging snapshot is\n * caught up with the log. Not with `version` or `at`. One not reached within\n * the server's wait rejects with 409 `wow.query.min-version-not-reached`,\n * whose `version` is the current one.\n */\n readonly minVersion?: number;\n /**\n * The entity tag of the state already held, as a read answered it\n * (`etag`): a 304 then answers `notModified`. The tag is the version and a\n * digest of the body (`3-<digest>`), so it changes whenever what is read\n * does, not only with the version.\n */\n readonly ifNoneMatch?: string;\n}\n\n/**\n * A state read: the snapshot and its entity tag, or `notModified` when the\n * state held is current, with its tag and the version the tag names.\n */\nexport type StateReadResult<S> =\n | {\n readonly notModified: false;\n readonly snapshot: Snapshot<S>;\n readonly etag?: string;\n }\n | {\n readonly notModified: true;\n readonly version: number;\n readonly etag: string;\n };\n\n/** Which commits a trace reads. */\nexport interface StateTraceOptions extends RequestOptions {\n /** The first version; 1 when absent. */\n readonly from?: number;\n /** The last version; the latest when absent. */\n readonly to?: number;\n /** The most commits, at most 1000; 100 when absent. */\n readonly limit?: number;\n}\n\n/**\n * Reads an aggregate type's states:\n * `GET /{context}/{aggregate}/{id}/state` with `?version=`, `?at=` or `?minVersion=`, and\n * its trace, `…/state/trace`; {@link OwnStateClient} reads the caller's own\n * of a type whose id is its owner's. A state that does not exist rejects with 404\n * `wow.aggregate.not-found`, except through {@link StateClient.find}, which\n * answers `undefined`.\n *\n * @typeParam S - The state's type.\n * @typeParam E - The events' type of a trace.\n */\nexport class StateClient<\n S = unknown,\n E extends CommittedEvent = CommittedEvent,\n> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n readonly client: WowClientOptions = {},\n ) {}\n\n /** The state the options ask for. */\n read(\n id: string,\n options: StateReadOptions = {},\n ): Promise<StateReadResult<S>> {\n return this.readAt(id, options);\n }\n\n /** The latest state, or the one the options ask for, without a cache. */\n get(\n id: string,\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S>> {\n return this.getAt(id, options);\n }\n\n /**\n * The latest state, or the one the options ask for, or `undefined` when the\n * aggregate does not exist (404 `wow.aggregate.not-found`), as Kotlin's\n * `SnapshotQueries.state` answers null. Any other 404, such as a route that\n * does not exist, rejects.\n */\n find(\n id: string,\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S> | undefined> {\n return this.findAt(id, options);\n }\n\n /** The commits from `from` to `to`, each with its events and the state after it. */\n trace(\n id: string,\n options: StateTraceOptions = {},\n ): Promise<StateTrace<S, E>> {\n return this.traceAt(id, options);\n }\n\n /**\n * {@link StateClient.read} of the aggregate `id`, or by no id of the\n * caller's own ({@link OwnStateClient}).\n */\n protected async readAt(\n id: string | undefined,\n options: StateReadOptions,\n ): Promise<StateReadResult<S>> {\n const at =\n options.at instanceof Date ? options.at.toISOString() : options.at;\n const response = await send(this.client, {\n method: 'GET',\n path: this.statePath(id),\n query: { version: options.version, at, minVersion: options.minVersion },\n headers: {\n [WowHeaders.IF_NONE_MATCH]:\n options.ifNoneMatch === undefined\n ? undefined\n : `\"${options.ifNoneMatch}\"`,\n },\n options,\n notModified: true,\n });\n if (response.status === 304) {\n const etag = entityTag(response) ?? options.ifNoneMatch ?? '';\n return {\n notModified: true,\n version: Number.parseInt(etag, 10),\n etag,\n };\n }\n return {\n notModified: false,\n snapshot: await json<Snapshot<S>>(response, this.stateNumbers),\n etag: entityTag(response),\n };\n }\n\n /** {@link StateClient.get} of the aggregate `id`, or of the caller's own. */\n protected async getAt(\n id: string | undefined,\n options: Omit<StateReadOptions, 'ifNoneMatch'>,\n ): Promise<Snapshot<S>> {\n const result = await this.readAt(id, options);\n if (result.notModified) {\n throw new Error('A read without If-None-Match was answered 304.');\n }\n return result.snapshot;\n }\n\n /** {@link StateClient.find} of the aggregate `id`, or of the caller's own. */\n protected async findAt(\n id: string | undefined,\n options: Omit<StateReadOptions, 'ifNoneMatch'>,\n ): Promise<Snapshot<S> | undefined> {\n try {\n return await this.getAt(id, options);\n } catch (error) {\n if (\n error instanceof WowProblemError &&\n error.status === 404 &&\n error.code === FrameworkErrorCodes.AGGREGATE_NOT_FOUND\n ) {\n return undefined;\n }\n throw error;\n }\n }\n\n /** {@link StateClient.trace} of the aggregate `id`, or of the caller's own. */\n protected async traceAt(\n id: string | undefined,\n options: StateTraceOptions,\n ): Promise<StateTrace<S, E>> {\n return json<StateTrace<S, E>>(\n await send(this.client, {\n method: 'GET',\n path: `${this.statePath(id)}/trace`,\n query: { from: options.from, to: options.to, limit: options.limit },\n options,\n }),\n this.traceNumbers,\n );\n }\n\n /** The exact numbers of a state read: its state's. */\n private get stateNumbers(): TextNumbers | undefined {\n const state = this.type.textNumbers?.state;\n return state === undefined ? undefined : { state };\n }\n\n /** The exact numbers of a trace: each commit's state and events. */\n private get traceNumbers(): TextNumbers | undefined {\n const state = this.type.textNumbers?.state;\n const events = eventNumbers(this.type);\n if (state === undefined && events === undefined) return undefined;\n return {\n commits: {\n '*': {\n ...(state === undefined ? {} : { state }),\n ...(events === undefined ? {} : { events: { '*': events } }),\n },\n },\n };\n }\n\n private statePath(id: string | undefined): string {\n const base = `/${this.type.context}/${this.type.name}`;\n return id === undefined\n ? `${base}/state`\n : `${base}/${encodeURIComponent(id)}/state`;\n }\n}\n\n/**\n * Reads the states of an aggregate type whose id is its owner's\n * (`@AggregateRoot(idSource = OWNER)`, the Manifest's `idSource: \"owner\"`):\n * by id as {@link StateClient} does, for a caller with no owner, and the\n * caller's own by no id, `GET /{context}/{aggregate}/state` and\n * `…/state/trace`, its id the owner the caller reads as. An unauthenticated\n * caller is refused with 401 `wow.auth.unauthenticated`, one with no owner\n * with 400 `wow.query.malformed`.\n *\n * @typeParam S - The state's type.\n * @typeParam E - The events' type of a trace.\n */\nexport class OwnStateClient<\n S = unknown,\n E extends CommittedEvent = CommittedEvent,\n> extends StateClient<S, E> {\n /** {@link StateClient.read} of the caller's own aggregate. */\n readOwn(options: StateReadOptions = {}): Promise<StateReadResult<S>> {\n return this.readAt(undefined, options);\n }\n\n /** {@link StateClient.get} of the caller's own aggregate. */\n getOwn(\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S>> {\n return this.getAt(undefined, options);\n }\n\n /**\n * {@link StateClient.find} of the caller's own aggregate: `undefined` until\n * the caller's first command creates it.\n */\n findOwn(\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S> | undefined> {\n return this.findAt(undefined, options);\n }\n\n /** {@link StateClient.trace} of the caller's own aggregate. */\n traceOwn(options: StateTraceOptions = {}): Promise<StateTrace<S, E>> {\n return this.traceAt(undefined, options);\n }\n}\n"],"mappings":"A,yHA0FA,IAAaW,EAAb,KAGE,CAKA,YACE,EACA,EAAoC,CAAC,EACrC,CAFS,KAAA,KAAAC,EACA,KAAA,OAAAC,CACR,CAGH,KACE,EACA,EAA4B,CAAC,EACA,CAC7B,OAAO,KAAK,OAAOC,EAAIC,CAAO,CAChC,CAGA,IACE,EACA,EAAiD,CAAC,EAC5B,CACtB,OAAO,KAAK,MAAMD,EAAIC,CAAO,CAC/B,CAQA,KACE,EACA,EAAiD,CAAC,EAChB,CAClC,OAAO,KAAK,OAAOD,EAAIC,CAAO,CAChC,CAGA,MACE,EACA,EAA6B,CAAC,EACH,CAC3B,OAAO,KAAK,QAAQD,EAAIC,CAAO,CACjC,CAMA,MAAgB,OACd,EACA,EAC6B,CAC7B,IAAMC,EACJD,EAAQ,cAAc,KAAOA,EAAQ,GAAG,YAAY,EAAIA,EAAQ,GAC5DE,EAAW,MAAMjB,EAAAA,KAAK,KAAK,OAAQ,CACvC,OAAQ,MACR,KAAM,KAAK,UAAUc,CAAE,EACvB,MAAO,CAAE,QAASC,EAAQ,QAAS,KAAI,WAAYA,EAAQ,UAAW,EACtE,QAAS,EACNd,EAAAA,WAAW,eACVc,EAAQ,cAAgB,IAAA,GACpB,IAAA,GACA,IAAIA,EAAQ,YAAY,EAChC,EACA,UACA,YAAa,EACf,CAAC,EACD,GAAIE,EAAS,SAAW,IAAK,CAC3B,IAAMC,EAAOhB,EAAAA,UAAUe,CA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CAAC,EAAI,CAAE,OAAQ,CAAE,IAAKA,CAAO,CAAE,CAC5D,CACF,CACF,CACF,CAEA,UAAkB,EAAgC,CAChD,IAAMC,EAAO,IAAI,KAAK,KAAK,QAAQ,GAAG,KAAK,KAAK,OAChD,OAAOT,IAAO,IAAA,GACV,GAAGS,EAAK,QACR,GAAGA,EAAK,GAAG,mBAAmBT,CAAE,EAAE,OACxC,CACF,EAcaU,EAAb,cAGUb,CAAkB,CAE1B,QAAQ,EAA4B,CAAC,EAAgC,CACnE,OAAO,KAAK,OAAO,IAAA,GAAWI,CAAO,CACvC,CAGA,OACE,EAAiD,CAAC,EAC5B,CACtB,OAAO,KAAK,MAAM,IAAA,GAAWA,CAAO,CACtC,CAMA,QACE,EAAiD,CAAC,EAChB,CAClC,OAAO,KAAK,OAAO,IAAA,GAAWA,CAAO,CACvC,CAGA,SAAS,EAA6B,CAAC,EAA8B,CACnE,OAAO,KAAK,QAAQ,IAAA,GAAWA,CAAO,CACxC,CACF,E"}
|
|
1
|
+
{"version":3,"file":"state.cjs","names":["send","WowHeaders","entityTag","WowProblemError","FrameworkErrorCodes","recordNumbers","eventNumbers","require_exactJson","require_frameworkErrors","require_problem","require_http","StateClient","type","client","id","options","at","answer","response","etag","result","error","events","commit","base","OwnStateClient"],"sources":["../src/state.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n entityTag,\n type RequestOptions,\n send,\n type WowClientOptions,\n WowHeaders,\n} from './http.js';\nimport { eventNumbers, recordNumbers, type TextNumbers } from './exactJson.js';\nimport { FrameworkErrorCodes, WowProblemError } from './problem.js';\nimport type {\n AccessValues,\n AggregateType,\n CommittedEvent,\n StateTrace,\n Snapshot,\n} from './wire.js';\n\n/** Which state a read asks for: the latest, at a version or as of a time. */\nexport interface StateReadOptions extends RequestOptions {\n /** The committed state at this version. */\n readonly version?: number;\n /** The committed state as of this instant (ISO-8601, or a `Date`). */\n readonly at?: string | Date;\n /**\n * The latest state at this version or later (review R4-Q, Q-13: read your\n * writes), such as the `version` of a command's result: a lagging snapshot is\n * caught up with the log. Not with `version` or `at`. One not reached within\n * the server's wait rejects with 409 `wow.query.min-version-not-reached`,\n * whose `version` is the current one.\n */\n readonly minVersion?: number;\n /**\n * The entity tag of the state already held, as a read answered it\n * (`etag`): a 304 then answers `notModified`. The tag is the version and a\n * digest of the body (`3-<digest>`), so it changes whenever what is read\n * does, not only with the version.\n */\n readonly ifNoneMatch?: string;\n}\n\n/**\n * A state read: the snapshot and its entity tag, or `notModified` when the\n * state held is current, with its tag and the version the tag names.\n */\nexport type StateReadResult<S, A = AccessValues> =\n | {\n readonly notModified: false;\n readonly snapshot: Snapshot<S, A>;\n readonly etag?: string;\n }\n | {\n readonly notModified: true;\n readonly version: number;\n readonly etag: string;\n };\n\n/** Which commits a trace reads. */\nexport interface StateTraceOptions extends RequestOptions {\n /** The first version; 1 when absent. */\n readonly from?: number;\n /** The last version; the latest when absent. */\n readonly to?: number;\n /** The most commits, at most 1000; 100 when absent. */\n readonly limit?: number;\n}\n\n/**\n * Reads an aggregate type's states:\n * `GET /{context}/{aggregate}/{id}/state` with `?version=`, `?at=` or `?minVersion=`, and\n * its trace, `…/state/trace`; {@link OwnStateClient} reads the caller's own\n * of a type whose id is its owner's. A state that does not exist rejects with 404\n * `wow.aggregate.not-found`, except through {@link StateClient.find}, which\n * answers `undefined`.\n *\n * @typeParam S - The state's type.\n * @typeParam E - The events' type of a trace.\n * @typeParam A - The aggregate's access attributes.\n */\nexport class StateClient<\n S = unknown,\n E extends CommittedEvent = CommittedEvent,\n A = AccessValues,\n> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n readonly client: WowClientOptions = {},\n ) {}\n\n /** The state the options ask for. */\n read(\n id: string,\n options: StateReadOptions = {},\n ): Promise<StateReadResult<S, A>> {\n return this.readAt(id, options);\n }\n\n /** The latest state, or the one the options ask for, without a cache. */\n get(\n id: string,\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S, A>> {\n return this.getAt(id, options);\n }\n\n /**\n * The latest state, or the one the options ask for, or `undefined` when the\n * aggregate does not exist (404 `wow.aggregate.not-found`), as Kotlin's\n * `SnapshotQueries.state` answers null. Any other 404, such as a route that\n * does not exist, rejects.\n */\n find(\n id: string,\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S, A> | undefined> {\n return this.findAt(id, options);\n }\n\n /** The commits from `from` to `to`, each with its events and the state after it. */\n trace(\n id: string,\n options: StateTraceOptions = {},\n ): Promise<StateTrace<S, E, A>> {\n return this.traceAt(id, options);\n }\n\n /**\n * {@link StateClient.read} of the aggregate `id`, or by no id of the\n * caller's own ({@link OwnStateClient}).\n */\n protected async readAt(\n id: string | undefined,\n options: StateReadOptions,\n ): Promise<StateReadResult<S, A>> {\n const at =\n options.at instanceof Date ? options.at.toISOString() : options.at;\n const answer = await send(this.client, {\n method: 'GET',\n path: this.statePath(id),\n query: { version: options.version, at, minVersion: options.minVersion },\n headers: {\n [WowHeaders.IF_NONE_MATCH]:\n options.ifNoneMatch === undefined\n ? undefined\n : `\"${options.ifNoneMatch}\"`,\n },\n numbers: { answer: this.stateNumbers },\n options,\n notModified: true,\n });\n const response = answer.response;\n if (response.status === 304) {\n const etag = entityTag(response) ?? options.ifNoneMatch ?? '';\n return {\n notModified: true,\n version: Number.parseInt(etag, 10),\n etag,\n };\n }\n return {\n notModified: false,\n snapshot: await answer.json<Snapshot<S, A>>(),\n etag: entityTag(response),\n };\n }\n\n /** {@link StateClient.get} of the aggregate `id`, or of the caller's own. */\n protected async getAt(\n id: string | undefined,\n options: Omit<StateReadOptions, 'ifNoneMatch'>,\n ): Promise<Snapshot<S, A>> {\n const result = await this.readAt(id, options);\n if (result.notModified) {\n throw new Error('A read without If-None-Match was answered 304.');\n }\n return result.snapshot;\n }\n\n /** {@link StateClient.find} of the aggregate `id`, or of the caller's own. */\n protected async findAt(\n id: string | undefined,\n options: Omit<StateReadOptions, 'ifNoneMatch'>,\n ): Promise<Snapshot<S, A> | undefined> {\n try {\n return await this.getAt(id, options);\n } catch (error) {\n if (\n error instanceof WowProblemError &&\n error.status === 404 &&\n error.code === FrameworkErrorCodes.AGGREGATE_NOT_FOUND\n ) {\n return undefined;\n }\n throw error;\n }\n }\n\n /** {@link StateClient.trace} of the aggregate `id`, or of the caller's own. */\n protected async traceAt(\n id: string | undefined,\n options: StateTraceOptions,\n ): Promise<StateTrace<S, E, A>> {\n const answer = await send(this.client, {\n method: 'GET',\n path: `${this.statePath(id)}/trace`,\n query: { from: options.from, to: options.to, limit: options.limit },\n numbers: { answer: this.traceNumbers },\n options,\n });\n return answer.json<StateTrace<S, E, A>>();\n }\n\n /** The exact numbers of a state read: its state's and its access attributes'. */\n private get stateNumbers(): TextNumbers | undefined {\n return recordNumbers({\n state: this.type.textNumbers?.state,\n access: this.type.textNumbers?.access,\n });\n }\n\n /** The exact numbers of a trace: each commit's state, events and access attributes. */\n private get traceNumbers(): TextNumbers | undefined {\n const events = eventNumbers(this.type);\n const commit = recordNumbers({\n state: this.type.textNumbers?.state,\n events: events === undefined ? undefined : { '*': events },\n access: this.type.textNumbers?.access,\n });\n return commit === undefined ? undefined : { commits: { '*': commit } };\n }\n\n private statePath(id: string | undefined): string {\n const base = `/${this.type.context}/${this.type.name}`;\n return id === undefined\n ? `${base}/state`\n : `${base}/${encodeURIComponent(id)}/state`;\n }\n}\n\n/**\n * Reads the states of an aggregate type whose id is its owner's\n * (`@AggregateRoot(idSource = OWNER)`, the Manifest's `idSource: \"owner\"`):\n * by id as {@link StateClient} does, for a caller with no owner, and the\n * caller's own by no id, `GET /{context}/{aggregate}/state` and\n * `…/state/trace`, its id the owner the caller reads as. An unauthenticated\n * caller is refused with 401 `wow.auth.unauthenticated`, one with no owner\n * with 400 `wow.query.malformed`.\n *\n * @typeParam S - The state's type.\n * @typeParam E - The events' type of a trace.\n * @typeParam A - The aggregate's access attributes.\n */\nexport class OwnStateClient<\n S = unknown,\n E extends CommittedEvent = CommittedEvent,\n A = AccessValues,\n> extends StateClient<S, E, A> {\n /** {@link StateClient.read} of the caller's own aggregate. */\n readOwn(options: StateReadOptions = {}): Promise<StateReadResult<S, A>> {\n return this.readAt(undefined, options);\n }\n\n /** {@link StateClient.get} of the caller's own aggregate. */\n getOwn(\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S, A>> {\n return this.getAt(undefined, options);\n }\n\n /**\n * {@link StateClient.find} of the caller's own aggregate: `undefined` until\n * the caller's first command creates it.\n */\n findOwn(\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S, A> | undefined> {\n return this.findAt(undefined, options);\n }\n\n /** {@link StateClient.trace} of the caller's own aggregate. */\n traceOwn(options: StateTraceOptions = {}): Promise<StateTrace<S, E, A>> {\n return this.traceAt(undefined, options);\n }\n}\n"],"mappings":"A,yHA2FA,IAAaW,EAAb,KAIE,CAKA,YACE,EACA,EAAoC,CAAC,EACrC,CAFS,KAAA,KAAAC,EACA,KAAA,OAAAC,CACR,CAGH,KACE,EACA,EAA4B,CAAC,EACG,CAChC,OAAO,KAAK,OAAOC,EAAIC,CAAO,CAChC,CAGA,IACE,EACA,EAAiD,CAAC,EACzB,CACzB,OAAO,KAAK,MAAMD,EAAIC,CAAO,CAC/B,CAQA,KACE,EACA,EAAiD,CAAC,EACb,CACrC,OAAO,KAAK,OAAOD,EAAIC,CAAO,CAChC,CAGA,MACE,EACA,EAA6B,CAA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,5 @@
|
|
|
1
1
|
import { RequestOptions, WowClientOptions } from './http.cjs';
|
|
2
|
-
import { AggregateType, CommittedEvent, StateTrace, Snapshot } from './wire.cjs';
|
|
2
|
+
import { AccessValues, AggregateType, CommittedEvent, StateTrace, Snapshot } from './wire.cjs';
|
|
3
3
|
/** Which state a read asks for: the latest, at a version or as of a time. */
|
|
4
4
|
export interface StateReadOptions extends RequestOptions {
|
|
5
5
|
/** The committed state at this version. */
|
|
@@ -26,9 +26,9 @@ export interface StateReadOptions extends RequestOptions {
|
|
|
26
26
|
* A state read: the snapshot and its entity tag, or `notModified` when the
|
|
27
27
|
* state held is current, with its tag and the version the tag names.
|
|
28
28
|
*/
|
|
29
|
-
export type StateReadResult<S> = {
|
|
29
|
+
export type StateReadResult<S, A = AccessValues> = {
|
|
30
30
|
readonly notModified: false;
|
|
31
|
-
readonly snapshot: Snapshot<S>;
|
|
31
|
+
readonly snapshot: Snapshot<S, A>;
|
|
32
32
|
readonly etag?: string;
|
|
33
33
|
} | {
|
|
34
34
|
readonly notModified: true;
|
|
@@ -54,8 +54,9 @@ export interface StateTraceOptions extends RequestOptions {
|
|
|
54
54
|
*
|
|
55
55
|
* @typeParam S - The state's type.
|
|
56
56
|
* @typeParam E - The events' type of a trace.
|
|
57
|
+
* @typeParam A - The aggregate's access attributes.
|
|
57
58
|
*/
|
|
58
|
-
export declare class StateClient<S = unknown, E extends CommittedEvent = CommittedEvent> {
|
|
59
|
+
export declare class StateClient<S = unknown, E extends CommittedEvent = CommittedEvent, A = AccessValues> {
|
|
59
60
|
readonly type: AggregateType;
|
|
60
61
|
readonly client: WowClientOptions;
|
|
61
62
|
/**
|
|
@@ -64,32 +65,32 @@ export declare class StateClient<S = unknown, E extends CommittedEvent = Committ
|
|
|
64
65
|
*/
|
|
65
66
|
constructor(type: AggregateType, client?: WowClientOptions);
|
|
66
67
|
/** The state the options ask for. */
|
|
67
|
-
read(id: string, options?: StateReadOptions): Promise<StateReadResult<S>>;
|
|
68
|
+
read(id: string, options?: StateReadOptions): Promise<StateReadResult<S, A>>;
|
|
68
69
|
/** The latest state, or the one the options ask for, without a cache. */
|
|
69
|
-
get(id: string, options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S>>;
|
|
70
|
+
get(id: string, options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A>>;
|
|
70
71
|
/**
|
|
71
72
|
* The latest state, or the one the options ask for, or `undefined` when the
|
|
72
73
|
* aggregate does not exist (404 `wow.aggregate.not-found`), as Kotlin's
|
|
73
74
|
* `SnapshotQueries.state` answers null. Any other 404, such as a route that
|
|
74
75
|
* does not exist, rejects.
|
|
75
76
|
*/
|
|
76
|
-
find(id: string, options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S> | undefined>;
|
|
77
|
+
find(id: string, options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A> | undefined>;
|
|
77
78
|
/** The commits from `from` to `to`, each with its events and the state after it. */
|
|
78
|
-
trace(id: string, options?: StateTraceOptions): Promise<StateTrace<S, E>>;
|
|
79
|
+
trace(id: string, options?: StateTraceOptions): Promise<StateTrace<S, E, A>>;
|
|
79
80
|
/**
|
|
80
81
|
* {@link StateClient.read} of the aggregate `id`, or by no id of the
|
|
81
82
|
* caller's own ({@link OwnStateClient}).
|
|
82
83
|
*/
|
|
83
|
-
protected readAt(id: string | undefined, options: StateReadOptions): Promise<StateReadResult<S>>;
|
|
84
|
+
protected readAt(id: string | undefined, options: StateReadOptions): Promise<StateReadResult<S, A>>;
|
|
84
85
|
/** {@link StateClient.get} of the aggregate `id`, or of the caller's own. */
|
|
85
|
-
protected getAt(id: string | undefined, options: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S>>;
|
|
86
|
+
protected getAt(id: string | undefined, options: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A>>;
|
|
86
87
|
/** {@link StateClient.find} of the aggregate `id`, or of the caller's own. */
|
|
87
|
-
protected findAt(id: string | undefined, options: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S> | undefined>;
|
|
88
|
+
protected findAt(id: string | undefined, options: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A> | undefined>;
|
|
88
89
|
/** {@link StateClient.trace} of the aggregate `id`, or of the caller's own. */
|
|
89
|
-
protected traceAt(id: string | undefined, options: StateTraceOptions): Promise<StateTrace<S, E>>;
|
|
90
|
-
/** The exact numbers of a state read: its state's. */
|
|
90
|
+
protected traceAt(id: string | undefined, options: StateTraceOptions): Promise<StateTrace<S, E, A>>;
|
|
91
|
+
/** The exact numbers of a state read: its state's and its access attributes'. */
|
|
91
92
|
private get stateNumbers();
|
|
92
|
-
/** The exact numbers of a trace: each commit's state and
|
|
93
|
+
/** The exact numbers of a trace: each commit's state, events and access attributes. */
|
|
93
94
|
private get traceNumbers();
|
|
94
95
|
private statePath;
|
|
95
96
|
}
|
|
@@ -104,17 +105,18 @@ export declare class StateClient<S = unknown, E extends CommittedEvent = Committ
|
|
|
104
105
|
*
|
|
105
106
|
* @typeParam S - The state's type.
|
|
106
107
|
* @typeParam E - The events' type of a trace.
|
|
108
|
+
* @typeParam A - The aggregate's access attributes.
|
|
107
109
|
*/
|
|
108
|
-
export declare class OwnStateClient<S = unknown, E extends CommittedEvent = CommittedEvent> extends StateClient<S, E> {
|
|
110
|
+
export declare class OwnStateClient<S = unknown, E extends CommittedEvent = CommittedEvent, A = AccessValues> extends StateClient<S, E, A> {
|
|
109
111
|
/** {@link StateClient.read} of the caller's own aggregate. */
|
|
110
|
-
readOwn(options?: StateReadOptions): Promise<StateReadResult<S>>;
|
|
112
|
+
readOwn(options?: StateReadOptions): Promise<StateReadResult<S, A>>;
|
|
111
113
|
/** {@link StateClient.get} of the caller's own aggregate. */
|
|
112
|
-
getOwn(options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S>>;
|
|
114
|
+
getOwn(options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A>>;
|
|
113
115
|
/**
|
|
114
116
|
* {@link StateClient.find} of the caller's own aggregate: `undefined` until
|
|
115
117
|
* the caller's first command creates it.
|
|
116
118
|
*/
|
|
117
|
-
findOwn(options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S> | undefined>;
|
|
119
|
+
findOwn(options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A> | undefined>;
|
|
118
120
|
/** {@link StateClient.trace} of the caller's own aggregate. */
|
|
119
|
-
traceOwn(options?: StateTraceOptions): Promise<StateTrace<S, E>>;
|
|
121
|
+
traceOwn(options?: StateTraceOptions): Promise<StateTrace<S, E, A>>;
|
|
120
122
|
}
|
package/dist/state.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { RequestOptions, WowClientOptions } from './http.js';
|
|
2
|
-
import { AggregateType, CommittedEvent, StateTrace, Snapshot } from './wire.js';
|
|
2
|
+
import { AccessValues, AggregateType, CommittedEvent, StateTrace, Snapshot } from './wire.js';
|
|
3
3
|
/** Which state a read asks for: the latest, at a version or as of a time. */
|
|
4
4
|
export interface StateReadOptions extends RequestOptions {
|
|
5
5
|
/** The committed state at this version. */
|
|
@@ -26,9 +26,9 @@ export interface StateReadOptions extends RequestOptions {
|
|
|
26
26
|
* A state read: the snapshot and its entity tag, or `notModified` when the
|
|
27
27
|
* state held is current, with its tag and the version the tag names.
|
|
28
28
|
*/
|
|
29
|
-
export type StateReadResult<S> = {
|
|
29
|
+
export type StateReadResult<S, A = AccessValues> = {
|
|
30
30
|
readonly notModified: false;
|
|
31
|
-
readonly snapshot: Snapshot<S>;
|
|
31
|
+
readonly snapshot: Snapshot<S, A>;
|
|
32
32
|
readonly etag?: string;
|
|
33
33
|
} | {
|
|
34
34
|
readonly notModified: true;
|
|
@@ -54,8 +54,9 @@ export interface StateTraceOptions extends RequestOptions {
|
|
|
54
54
|
*
|
|
55
55
|
* @typeParam S - The state's type.
|
|
56
56
|
* @typeParam E - The events' type of a trace.
|
|
57
|
+
* @typeParam A - The aggregate's access attributes.
|
|
57
58
|
*/
|
|
58
|
-
export declare class StateClient<S = unknown, E extends CommittedEvent = CommittedEvent> {
|
|
59
|
+
export declare class StateClient<S = unknown, E extends CommittedEvent = CommittedEvent, A = AccessValues> {
|
|
59
60
|
readonly type: AggregateType;
|
|
60
61
|
readonly client: WowClientOptions;
|
|
61
62
|
/**
|
|
@@ -64,32 +65,32 @@ export declare class StateClient<S = unknown, E extends CommittedEvent = Committ
|
|
|
64
65
|
*/
|
|
65
66
|
constructor(type: AggregateType, client?: WowClientOptions);
|
|
66
67
|
/** The state the options ask for. */
|
|
67
|
-
read(id: string, options?: StateReadOptions): Promise<StateReadResult<S>>;
|
|
68
|
+
read(id: string, options?: StateReadOptions): Promise<StateReadResult<S, A>>;
|
|
68
69
|
/** The latest state, or the one the options ask for, without a cache. */
|
|
69
|
-
get(id: string, options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S>>;
|
|
70
|
+
get(id: string, options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A>>;
|
|
70
71
|
/**
|
|
71
72
|
* The latest state, or the one the options ask for, or `undefined` when the
|
|
72
73
|
* aggregate does not exist (404 `wow.aggregate.not-found`), as Kotlin's
|
|
73
74
|
* `SnapshotQueries.state` answers null. Any other 404, such as a route that
|
|
74
75
|
* does not exist, rejects.
|
|
75
76
|
*/
|
|
76
|
-
find(id: string, options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S> | undefined>;
|
|
77
|
+
find(id: string, options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A> | undefined>;
|
|
77
78
|
/** The commits from `from` to `to`, each with its events and the state after it. */
|
|
78
|
-
trace(id: string, options?: StateTraceOptions): Promise<StateTrace<S, E>>;
|
|
79
|
+
trace(id: string, options?: StateTraceOptions): Promise<StateTrace<S, E, A>>;
|
|
79
80
|
/**
|
|
80
81
|
* {@link StateClient.read} of the aggregate `id`, or by no id of the
|
|
81
82
|
* caller's own ({@link OwnStateClient}).
|
|
82
83
|
*/
|
|
83
|
-
protected readAt(id: string | undefined, options: StateReadOptions): Promise<StateReadResult<S>>;
|
|
84
|
+
protected readAt(id: string | undefined, options: StateReadOptions): Promise<StateReadResult<S, A>>;
|
|
84
85
|
/** {@link StateClient.get} of the aggregate `id`, or of the caller's own. */
|
|
85
|
-
protected getAt(id: string | undefined, options: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S>>;
|
|
86
|
+
protected getAt(id: string | undefined, options: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A>>;
|
|
86
87
|
/** {@link StateClient.find} of the aggregate `id`, or of the caller's own. */
|
|
87
|
-
protected findAt(id: string | undefined, options: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S> | undefined>;
|
|
88
|
+
protected findAt(id: string | undefined, options: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A> | undefined>;
|
|
88
89
|
/** {@link StateClient.trace} of the aggregate `id`, or of the caller's own. */
|
|
89
|
-
protected traceAt(id: string | undefined, options: StateTraceOptions): Promise<StateTrace<S, E>>;
|
|
90
|
-
/** The exact numbers of a state read: its state's. */
|
|
90
|
+
protected traceAt(id: string | undefined, options: StateTraceOptions): Promise<StateTrace<S, E, A>>;
|
|
91
|
+
/** The exact numbers of a state read: its state's and its access attributes'. */
|
|
91
92
|
private get stateNumbers();
|
|
92
|
-
/** The exact numbers of a trace: each commit's state and
|
|
93
|
+
/** The exact numbers of a trace: each commit's state, events and access attributes. */
|
|
93
94
|
private get traceNumbers();
|
|
94
95
|
private statePath;
|
|
95
96
|
}
|
|
@@ -104,17 +105,18 @@ export declare class StateClient<S = unknown, E extends CommittedEvent = Committ
|
|
|
104
105
|
*
|
|
105
106
|
* @typeParam S - The state's type.
|
|
106
107
|
* @typeParam E - The events' type of a trace.
|
|
108
|
+
* @typeParam A - The aggregate's access attributes.
|
|
107
109
|
*/
|
|
108
|
-
export declare class OwnStateClient<S = unknown, E extends CommittedEvent = CommittedEvent> extends StateClient<S, E> {
|
|
110
|
+
export declare class OwnStateClient<S = unknown, E extends CommittedEvent = CommittedEvent, A = AccessValues> extends StateClient<S, E, A> {
|
|
109
111
|
/** {@link StateClient.read} of the caller's own aggregate. */
|
|
110
|
-
readOwn(options?: StateReadOptions): Promise<StateReadResult<S>>;
|
|
112
|
+
readOwn(options?: StateReadOptions): Promise<StateReadResult<S, A>>;
|
|
111
113
|
/** {@link StateClient.get} of the caller's own aggregate. */
|
|
112
|
-
getOwn(options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S>>;
|
|
114
|
+
getOwn(options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A>>;
|
|
113
115
|
/**
|
|
114
116
|
* {@link StateClient.find} of the caller's own aggregate: `undefined` until
|
|
115
117
|
* the caller's first command creates it.
|
|
116
118
|
*/
|
|
117
|
-
findOwn(options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S> | undefined>;
|
|
119
|
+
findOwn(options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<Snapshot<S, A> | undefined>;
|
|
118
120
|
/** {@link StateClient.trace} of the caller's own aggregate. */
|
|
119
|
-
traceOwn(options?: StateTraceOptions): Promise<StateTrace<S, E>>;
|
|
121
|
+
traceOwn(options?: StateTraceOptions): Promise<StateTrace<S, E, A>>;
|
|
120
122
|
}
|
package/dist/state.es.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { eventNumbers as e } from "./exactJson.es.js";
|
|
2
|
-
import { FrameworkErrorCodes as
|
|
3
|
-
import { WowProblemError as
|
|
4
|
-
import { WowHeaders as
|
|
1
|
+
import { eventNumbers as e, recordNumbers as t } from "./exactJson.es.js";
|
|
2
|
+
import { FrameworkErrorCodes as n } from "./frameworkErrors.es.js";
|
|
3
|
+
import { WowProblemError as r } from "./problem.es.js";
|
|
4
|
+
import { WowHeaders as i, entityTag as a, send as o } from "./http.es.js";
|
|
5
5
|
// src/state.ts
|
|
6
6
|
var s = class {
|
|
7
7
|
constructor(e, t = {}) {
|
|
@@ -20,7 +20,7 @@ var s = class {
|
|
|
20
20
|
return this.traceAt(e, t);
|
|
21
21
|
}
|
|
22
22
|
async readAt(e, t) {
|
|
23
|
-
let n = t.at instanceof Date ? t.at.toISOString() : t.at,
|
|
23
|
+
let n = t.at instanceof Date ? t.at.toISOString() : t.at, r = await o(this.client, {
|
|
24
24
|
method: "GET",
|
|
25
25
|
path: this.statePath(e),
|
|
26
26
|
query: {
|
|
@@ -28,12 +28,13 @@ var s = class {
|
|
|
28
28
|
at: n,
|
|
29
29
|
minVersion: t.minVersion
|
|
30
30
|
},
|
|
31
|
-
headers: { [
|
|
31
|
+
headers: { [i.IF_NONE_MATCH]: t.ifNoneMatch === void 0 ? void 0 : `"${t.ifNoneMatch}"` },
|
|
32
|
+
numbers: { answer: this.stateNumbers },
|
|
32
33
|
options: t,
|
|
33
34
|
notModified: !0
|
|
34
|
-
});
|
|
35
|
+
}), s = r.response;
|
|
35
36
|
if (s.status === 304) {
|
|
36
|
-
let e =
|
|
37
|
+
let e = a(s) ?? t.ifNoneMatch ?? "";
|
|
37
38
|
return {
|
|
38
39
|
notModified: !0,
|
|
39
40
|
version: Number.parseInt(e, 10),
|
|
@@ -42,8 +43,8 @@ var s = class {
|
|
|
42
43
|
}
|
|
43
44
|
return {
|
|
44
45
|
notModified: !1,
|
|
45
|
-
snapshot: await
|
|
46
|
-
etag:
|
|
46
|
+
snapshot: await r.json(),
|
|
47
|
+
etag: a(s)
|
|
47
48
|
};
|
|
48
49
|
}
|
|
49
50
|
async getAt(e, t) {
|
|
@@ -51,16 +52,16 @@ var s = class {
|
|
|
51
52
|
if (n.notModified) throw Error("A read without If-None-Match was answered 304.");
|
|
52
53
|
return n.snapshot;
|
|
53
54
|
}
|
|
54
|
-
async findAt(e,
|
|
55
|
+
async findAt(e, t) {
|
|
55
56
|
try {
|
|
56
|
-
return await this.getAt(e,
|
|
57
|
+
return await this.getAt(e, t);
|
|
57
58
|
} catch (e) {
|
|
58
|
-
if (e instanceof
|
|
59
|
+
if (e instanceof r && e.status === 404 && e.code === n.AGGREGATE_NOT_FOUND) return;
|
|
59
60
|
throw e;
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
async traceAt(e, t) {
|
|
63
|
-
return
|
|
64
|
+
return (await o(this.client, {
|
|
64
65
|
method: "GET",
|
|
65
66
|
path: `${this.statePath(e)}/trace`,
|
|
66
67
|
query: {
|
|
@@ -68,19 +69,23 @@ var s = class {
|
|
|
68
69
|
to: t.to,
|
|
69
70
|
limit: t.limit
|
|
70
71
|
},
|
|
72
|
+
numbers: { answer: this.traceNumbers },
|
|
71
73
|
options: t
|
|
72
|
-
})
|
|
74
|
+
})).json();
|
|
73
75
|
}
|
|
74
76
|
get stateNumbers() {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
return t({
|
|
78
|
+
state: this.type.textNumbers?.state,
|
|
79
|
+
access: this.type.textNumbers?.access
|
|
80
|
+
});
|
|
77
81
|
}
|
|
78
82
|
get traceNumbers() {
|
|
79
|
-
let
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
83
|
+
let n = e(this.type), r = t({
|
|
84
|
+
state: this.type.textNumbers?.state,
|
|
85
|
+
events: n === void 0 ? void 0 : { "*": n },
|
|
86
|
+
access: this.type.textNumbers?.access
|
|
87
|
+
});
|
|
88
|
+
return r === void 0 ? void 0 : { commits: { "*": r } };
|
|
84
89
|
}
|
|
85
90
|
statePath(e) {
|
|
86
91
|
let t = `/${this.type.context}/${this.type.name}`;
|
package/dist/state.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.es.js","names":["StateClient","type","client","id","options","at","response","send","WowHeaders","etag","entityTag","json","result","error","WowProblemError","FrameworkErrorCodes","state","events","eventNumbers","base","OwnStateClient"],"sources":["../src/state.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n entityTag,\n json,\n type RequestOptions,\n send,\n type WowClientOptions,\n WowHeaders,\n} from './http.js';\nimport { eventNumbers, type TextNumbers } from './exactJson.js';\nimport { FrameworkErrorCodes, WowProblemError } from './problem.js';\nimport type {\n AggregateType,\n CommittedEvent,\n StateTrace,\n Snapshot,\n} from './wire.js';\n\n/** Which state a read asks for: the latest, at a version or as of a time. */\nexport interface StateReadOptions extends RequestOptions {\n /** The committed state at this version. */\n readonly version?: number;\n /** The committed state as of this instant (ISO-8601, or a `Date`). */\n readonly at?: string | Date;\n /**\n * The latest state at this version or later (review R4-Q, Q-13: read your\n * writes), such as the `version` of a command's result: a lagging snapshot is\n * caught up with the log. Not with `version` or `at`. One not reached within\n * the server's wait rejects with 409 `wow.query.min-version-not-reached`,\n * whose `version` is the current one.\n */\n readonly minVersion?: number;\n /**\n * The entity tag of the state already held, as a read answered it\n * (`etag`): a 304 then answers `notModified`. The tag is the version and a\n * digest of the body (`3-<digest>`), so it changes whenever what is read\n * does, not only with the version.\n */\n readonly ifNoneMatch?: string;\n}\n\n/**\n * A state read: the snapshot and its entity tag, or `notModified` when the\n * state held is current, with its tag and the version the tag names.\n */\nexport type StateReadResult<S> =\n | {\n readonly notModified: false;\n readonly snapshot: Snapshot<S>;\n readonly etag?: string;\n }\n | {\n readonly notModified: true;\n readonly version: number;\n readonly etag: string;\n };\n\n/** Which commits a trace reads. */\nexport interface StateTraceOptions extends RequestOptions {\n /** The first version; 1 when absent. */\n readonly from?: number;\n /** The last version; the latest when absent. */\n readonly to?: number;\n /** The most commits, at most 1000; 100 when absent. */\n readonly limit?: number;\n}\n\n/**\n * Reads an aggregate type's states:\n * `GET /{context}/{aggregate}/{id}/state` with `?version=`, `?at=` or `?minVersion=`, and\n * its trace, `…/state/trace`; {@link OwnStateClient} reads the caller's own\n * of a type whose id is its owner's. A state that does not exist rejects with 404\n * `wow.aggregate.not-found`, except through {@link StateClient.find}, which\n * answers `undefined`.\n *\n * @typeParam S - The state's type.\n * @typeParam E - The events' type of a trace.\n */\nexport class StateClient<\n S = unknown,\n E extends CommittedEvent = CommittedEvent,\n> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n readonly client: WowClientOptions = {},\n ) {}\n\n /** The state the options ask for. */\n read(\n id: string,\n options: StateReadOptions = {},\n ): Promise<StateReadResult<S>> {\n return this.readAt(id, options);\n }\n\n /** The latest state, or the one the options ask for, without a cache. */\n get(\n id: string,\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S>> {\n return this.getAt(id, options);\n }\n\n /**\n * The latest state, or the one the options ask for, or `undefined` when the\n * aggregate does not exist (404 `wow.aggregate.not-found`), as Kotlin's\n * `SnapshotQueries.state` answers null. Any other 404, such as a route that\n * does not exist, rejects.\n */\n find(\n id: string,\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S> | undefined> {\n return this.findAt(id, options);\n }\n\n /** The commits from `from` to `to`, each with its events and the state after it. */\n trace(\n id: string,\n options: StateTraceOptions = {},\n ): Promise<StateTrace<S, E>> {\n return this.traceAt(id, options);\n }\n\n /**\n * {@link StateClient.read} of the aggregate `id`, or by no id of the\n * caller's own ({@link OwnStateClient}).\n */\n protected async readAt(\n id: string | undefined,\n options: StateReadOptions,\n ): Promise<StateReadResult<S>> {\n const at =\n options.at instanceof Date ? options.at.toISOString() : options.at;\n const response = await send(this.client, {\n method: 'GET',\n path: this.statePath(id),\n query: { version: options.version, at, minVersion: options.minVersion },\n headers: {\n [WowHeaders.IF_NONE_MATCH]:\n options.ifNoneMatch === undefined\n ? undefined\n : `\"${options.ifNoneMatch}\"`,\n },\n options,\n notModified: true,\n });\n if (response.status === 304) {\n const etag = entityTag(response) ?? options.ifNoneMatch ?? '';\n return {\n notModified: true,\n version: Number.parseInt(etag, 10),\n etag,\n };\n }\n return {\n notModified: false,\n snapshot: await json<Snapshot<S>>(response, this.stateNumbers),\n etag: entityTag(response),\n };\n }\n\n /** {@link StateClient.get} of the aggregate `id`, or of the caller's own. */\n protected async getAt(\n id: string | undefined,\n options: Omit<StateReadOptions, 'ifNoneMatch'>,\n ): Promise<Snapshot<S>> {\n const result = await this.readAt(id, options);\n if (result.notModified) {\n throw new Error('A read without If-None-Match was answered 304.');\n }\n return result.snapshot;\n }\n\n /** {@link StateClient.find} of the aggregate `id`, or of the caller's own. */\n protected async findAt(\n id: string | undefined,\n options: Omit<StateReadOptions, 'ifNoneMatch'>,\n ): Promise<Snapshot<S> | undefined> {\n try {\n return await this.getAt(id, options);\n } catch (error) {\n if (\n error instanceof WowProblemError &&\n error.status === 404 &&\n error.code === FrameworkErrorCodes.AGGREGATE_NOT_FOUND\n ) {\n return undefined;\n }\n throw error;\n }\n }\n\n /** {@link StateClient.trace} of the aggregate `id`, or of the caller's own. */\n protected async traceAt(\n id: string | undefined,\n options: StateTraceOptions,\n ): Promise<StateTrace<S, E>> {\n return json<StateTrace<S, E>>(\n await send(this.client, {\n method: 'GET',\n path: `${this.statePath(id)}/trace`,\n query: { from: options.from, to: options.to, limit: options.limit },\n options,\n }),\n this.traceNumbers,\n );\n }\n\n /** The exact numbers of a state read: its state's. */\n private get stateNumbers(): TextNumbers | undefined {\n const state = this.type.textNumbers?.state;\n return state === undefined ? undefined : { state };\n }\n\n /** The exact numbers of a trace: each commit's state and events. */\n private get traceNumbers(): TextNumbers | undefined {\n const state = this.type.textNumbers?.state;\n const events = eventNumbers(this.type);\n if (state === undefined && events === undefined) return undefined;\n return {\n commits: {\n '*': {\n ...(state === undefined ? {} : { state }),\n ...(events === undefined ? {} : { events: { '*': events } }),\n },\n },\n };\n }\n\n private statePath(id: string | undefined): string {\n const base = `/${this.type.context}/${this.type.name}`;\n return id === undefined\n ? `${base}/state`\n : `${base}/${encodeURIComponent(id)}/state`;\n }\n}\n\n/**\n * Reads the states of an aggregate type whose id is its owner's\n * (`@AggregateRoot(idSource = OWNER)`, the Manifest's `idSource: \"owner\"`):\n * by id as {@link StateClient} does, for a caller with no owner, and the\n * caller's own by no id, `GET /{context}/{aggregate}/state` and\n * `…/state/trace`, its id the owner the caller reads as. An unauthenticated\n * caller is refused with 401 `wow.auth.unauthenticated`, one with no owner\n * with 400 `wow.query.malformed`.\n *\n * @typeParam S - The state's type.\n * @typeParam E - The events' type of a trace.\n */\nexport class OwnStateClient<\n S = unknown,\n E extends CommittedEvent = CommittedEvent,\n> extends StateClient<S, E> {\n /** {@link StateClient.read} of the caller's own aggregate. */\n readOwn(options: StateReadOptions = {}): Promise<StateReadResult<S>> {\n return this.readAt(undefined, options);\n }\n\n /** {@link StateClient.get} of the caller's own aggregate. */\n getOwn(\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S>> {\n return this.getAt(undefined, options);\n }\n\n /**\n * {@link StateClient.find} of the caller's own aggregate: `undefined` until\n * the caller's first command creates it.\n */\n findOwn(\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S> | undefined> {\n return this.findAt(undefined, options);\n }\n\n /** {@link StateClient.trace} of the caller's own aggregate. */\n traceOwn(options: StateTraceOptions = {}): Promise<StateTrace<S, E>> {\n return this.traceAt(undefined, options);\n }\n}\n"],"mappings":";;;;;AA0FA,IAAaA,IAAb,MAGE;CAKA,YACE,GACA,IAAoC,CAAC,GACrC;EADS,AADA,KAAA,O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,KAAK,OAAO,KAAA,GAAWA,CAAO;CACvC;CAGA,SAAS,IAA6B,CAAC,GAA8B;EACnE,OAAO,KAAK,QAAQ,KAAA,GAAWA,CAAO;CACxC;AACF"}
|
|
1
|
+
{"version":3,"file":"state.es.js","names":["StateClient","type","client","id","options","at","answer","send","WowHeaders","response","etag","entityTag","result","error","WowProblemError","FrameworkErrorCodes","recordNumbers","events","eventNumbers","commit","base","OwnStateClient"],"sources":["../src/state.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n entityTag,\n type RequestOptions,\n send,\n type WowClientOptions,\n WowHeaders,\n} from './http.js';\nimport { eventNumbers, recordNumbers, type TextNumbers } from './exactJson.js';\nimport { FrameworkErrorCodes, WowProblemError } from './problem.js';\nimport type {\n AccessValues,\n AggregateType,\n CommittedEvent,\n StateTrace,\n Snapshot,\n} from './wire.js';\n\n/** Which state a read asks for: the latest, at a version or as of a time. */\nexport interface StateReadOptions extends RequestOptions {\n /** The committed state at this version. */\n readonly version?: number;\n /** The committed state as of this instant (ISO-8601, or a `Date`). */\n readonly at?: string | Date;\n /**\n * The latest state at this version or later (review R4-Q, Q-13: read your\n * writes), such as the `version` of a command's result: a lagging snapshot is\n * caught up with the log. Not with `version` or `at`. One not reached within\n * the server's wait rejects with 409 `wow.query.min-version-not-reached`,\n * whose `version` is the current one.\n */\n readonly minVersion?: number;\n /**\n * The entity tag of the state already held, as a read answered it\n * (`etag`): a 304 then answers `notModified`. The tag is the version and a\n * digest of the body (`3-<digest>`), so it changes whenever what is read\n * does, not only with the version.\n */\n readonly ifNoneMatch?: string;\n}\n\n/**\n * A state read: the snapshot and its entity tag, or `notModified` when the\n * state held is current, with its tag and the version the tag names.\n */\nexport type StateReadResult<S, A = AccessValues> =\n | {\n readonly notModified: false;\n readonly snapshot: Snapshot<S, A>;\n readonly etag?: string;\n }\n | {\n readonly notModified: true;\n readonly version: number;\n readonly etag: string;\n };\n\n/** Which commits a trace reads. */\nexport interface StateTraceOptions extends RequestOptions {\n /** The first version; 1 when absent. */\n readonly from?: number;\n /** The last version; the latest when absent. */\n readonly to?: number;\n /** The most commits, at most 1000; 100 when absent. */\n readonly limit?: number;\n}\n\n/**\n * Reads an aggregate type's states:\n * `GET /{context}/{aggregate}/{id}/state` with `?version=`, `?at=` or `?minVersion=`, and\n * its trace, `…/state/trace`; {@link OwnStateClient} reads the caller's own\n * of a type whose id is its owner's. A state that does not exist rejects with 404\n * `wow.aggregate.not-found`, except through {@link StateClient.find}, which\n * answers `undefined`.\n *\n * @typeParam S - The state's type.\n * @typeParam E - The events' type of a trace.\n * @typeParam A - The aggregate's access attributes.\n */\nexport class StateClient<\n S = unknown,\n E extends CommittedEvent = CommittedEvent,\n A = AccessValues,\n> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n readonly client: WowClientOptions = {},\n ) {}\n\n /** The state the options ask for. */\n read(\n id: string,\n options: StateReadOptions = {},\n ): Promise<StateReadResult<S, A>> {\n return this.readAt(id, options);\n }\n\n /** The latest state, or the one the options ask for, without a cache. */\n get(\n id: string,\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S, A>> {\n return this.getAt(id, options);\n }\n\n /**\n * The latest state, or the one the options ask for, or `undefined` when the\n * aggregate does not exist (404 `wow.aggregate.not-found`), as Kotlin's\n * `SnapshotQueries.state` answers null. Any other 404, such as a route that\n * does not exist, rejects.\n */\n find(\n id: string,\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S, A> | undefined> {\n return this.findAt(id, options);\n }\n\n /** The commits from `from` to `to`, each with its events and the state after it. */\n trace(\n id: string,\n options: StateTraceOptions = {},\n ): Promise<StateTrace<S, E, A>> {\n return this.traceAt(id, options);\n }\n\n /**\n * {@link StateClient.read} of the aggregate `id`, or by no id of the\n * caller's own ({@link OwnStateClient}).\n */\n protected async readAt(\n id: string | undefined,\n options: StateReadOptions,\n ): Promise<StateReadResult<S, A>> {\n const at =\n options.at instanceof Date ? options.at.toISOString() : options.at;\n const answer = await send(this.client, {\n method: 'GET',\n path: this.statePath(id),\n query: { version: options.version, at, minVersion: options.minVersion },\n headers: {\n [WowHeaders.IF_NONE_MATCH]:\n options.ifNoneMatch === undefined\n ? undefined\n : `\"${options.ifNoneMatch}\"`,\n },\n numbers: { answer: this.stateNumbers },\n options,\n notModified: true,\n });\n const response = answer.response;\n if (response.status === 304) {\n const etag = entityTag(response) ?? options.ifNoneMatch ?? '';\n return {\n notModified: true,\n version: Number.parseInt(etag, 10),\n etag,\n };\n }\n return {\n notModified: false,\n snapshot: await answer.json<Snapshot<S, A>>(),\n etag: entityTag(response),\n };\n }\n\n /** {@link StateClient.get} of the aggregate `id`, or of the caller's own. */\n protected async getAt(\n id: string | undefined,\n options: Omit<StateReadOptions, 'ifNoneMatch'>,\n ): Promise<Snapshot<S, A>> {\n const result = await this.readAt(id, options);\n if (result.notModified) {\n throw new Error('A read without If-None-Match was answered 304.');\n }\n return result.snapshot;\n }\n\n /** {@link StateClient.find} of the aggregate `id`, or of the caller's own. */\n protected async findAt(\n id: string | undefined,\n options: Omit<StateReadOptions, 'ifNoneMatch'>,\n ): Promise<Snapshot<S, A> | undefined> {\n try {\n return await this.getAt(id, options);\n } catch (error) {\n if (\n error instanceof WowProblemError &&\n error.status === 404 &&\n error.code === FrameworkErrorCodes.AGGREGATE_NOT_FOUND\n ) {\n return undefined;\n }\n throw error;\n }\n }\n\n /** {@link StateClient.trace} of the aggregate `id`, or of the caller's own. */\n protected async traceAt(\n id: string | undefined,\n options: StateTraceOptions,\n ): Promise<StateTrace<S, E, A>> {\n const answer = await send(this.client, {\n method: 'GET',\n path: `${this.statePath(id)}/trace`,\n query: { from: options.from, to: options.to, limit: options.limit },\n numbers: { answer: this.traceNumbers },\n options,\n });\n return answer.json<StateTrace<S, E, A>>();\n }\n\n /** The exact numbers of a state read: its state's and its access attributes'. */\n private get stateNumbers(): TextNumbers | undefined {\n return recordNumbers({\n state: this.type.textNumbers?.state,\n access: this.type.textNumbers?.access,\n });\n }\n\n /** The exact numbers of a trace: each commit's state, events and access attributes. */\n private get traceNumbers(): TextNumbers | undefined {\n const events = eventNumbers(this.type);\n const commit = recordNumbers({\n state: this.type.textNumbers?.state,\n events: events === undefined ? undefined : { '*': events },\n access: this.type.textNumbers?.access,\n });\n return commit === undefined ? undefined : { commits: { '*': commit } };\n }\n\n private statePath(id: string | undefined): string {\n const base = `/${this.type.context}/${this.type.name}`;\n return id === undefined\n ? `${base}/state`\n : `${base}/${encodeURIComponent(id)}/state`;\n }\n}\n\n/**\n * Reads the states of an aggregate type whose id is its owner's\n * (`@AggregateRoot(idSource = OWNER)`, the Manifest's `idSource: \"owner\"`):\n * by id as {@link StateClient} does, for a caller with no owner, and the\n * caller's own by no id, `GET /{context}/{aggregate}/state` and\n * `…/state/trace`, its id the owner the caller reads as. An unauthenticated\n * caller is refused with 401 `wow.auth.unauthenticated`, one with no owner\n * with 400 `wow.query.malformed`.\n *\n * @typeParam S - The state's type.\n * @typeParam E - The events' type of a trace.\n * @typeParam A - The aggregate's access attributes.\n */\nexport class OwnStateClient<\n S = unknown,\n E extends CommittedEvent = CommittedEvent,\n A = AccessValues,\n> extends StateClient<S, E, A> {\n /** {@link StateClient.read} of the caller's own aggregate. */\n readOwn(options: StateReadOptions = {}): Promise<StateReadResult<S, A>> {\n return this.readAt(undefined, options);\n }\n\n /** {@link StateClient.get} of the caller's own aggregate. */\n getOwn(\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S, A>> {\n return this.getAt(undefined, options);\n }\n\n /**\n * {@link StateClient.find} of the caller's own aggregate: `undefined` until\n * the caller's first command creates it.\n */\n findOwn(\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<Snapshot<S, A> | undefined> {\n return this.findAt(undefined, options);\n }\n\n /** {@link StateClient.trace} of the caller's own aggregate. */\n traceOwn(options: StateTraceOptions = {}): Promise<StateTrace<S, E, A>> {\n return this.traceAt(undefined, options);\n }\n}\n"],"mappings":";;;;;AA2FA,IAAaA,IAAb,MAIE;CAKA,YACE,GACA,IAAoC,CAAC,GACrC;EADS,AADA,KAAA,OAAAC,GACA,KAAA,SAAAC;CACR;CAGH,KACE,GACA,IAA4B,CAAC,GACG;EAChC,OAAO,KAAK,OAAOC,GAAIC,CAAO;CAChC;CAGA,IACE,GACA,IAAiD,CAAC,GACzB;EACzB,OAAO,KAAK,MAAMD,GAAIC,CAAO;CAC/B;CAQA,KACE,GACA,IAAiD,CAAC,GACb;EACrC,OAAO,KAAK,OAAOD,GAAIC,CAAO;CAChC;CAGA,MACE,GACA,IAA6B,CAAC,GACA;EAC9B,OAAO,KAAK,QAAQD,GAAIC,CAAO;CACjC;CAMA,MAAgB,OACd,GACA,GACgC;EAChC,IAAMC,IACJD,EAAQ,cAAc,OAAOA,EAAQ,GAAG,YAAY,IAAIA,EAAQ,IAC5DE,IAAS,MAAMC,EAAK,KAAK,QAAQ;GACrC,QAAQ;GACR,MAAM,KAAK,UAAUJ,CAAE;GACvB,OAAO;IAAE,SAASC,EAAQ;IAAS;IAAI,YAAYA,EAAQ;GAAW;GACtE,SAAS,GACNI,EAAW,gBACVJ,EAAQ,gBAAgB,KAAA,IACpB,KAAA,IACA,IAAIA,EAAQ,YAAY,GAChC;GACA,SAAS,EAAE,QAAQ,KAAK,aAAa;GACrC;GACA,aAAa;EACf,CAAC,GACKK,IAAWH,EAAO;EACxB,IAAIG,EAAS,WAAW,KAAK;G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,IAAY,EAAE,SAAS,EAAE,KAAKA,EAAO,EAAE;CACvE;CAEA,UAAkB,GAAgC;EAChD,IAAMC,IAAO,IAAI,KAAK,KAAK,QAAQ,GAAG,KAAK,KAAK;EAChD,OAAOjB,MAAO,KAAA,IACV,GAAGiB,EAAK,UACR,GAAGA,EAAK,GAAG,mBAAmBjB,CAAE,EAAE;CACxC;AACF,GAeakB,IAAb,cAIUrB,EAAqB;CAE7B,QAAQ,IAA4B,CAAC,GAAmC;EACtE,OAAO,KAAK,OAAO,KAAA,GAAWI,CAAO;CACvC;CAGA,OACE,IAAiD,CAAC,GACzB;EACzB,OAAO,KAAK,MAAM,KAAA,GAAWA,CAAO;CACtC;CAMA,QACE,IAAiD,CAAC,GACb;EACrC,OAAO,KAAK,OAAO,KAAA,GAAWA,CAAO;CACvC;CAGA,SAAS,IAA6B,CAAC,GAAiC;EACtE,OAAO,KAAK,QAAQ,KAAA,GAAWA,CAAO;CACxC;AACF"}
|
package/dist/streams.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./exactJson.cjs"),t=require("./frameworkErrors.cjs"),n=require("./problem.cjs");let r=require("@ahoo-wang/fetcher-eventstream");var i=`row`,a=`error`,o=`end`,s=`message`;function c(
|
|
1
|
+
const e=require("./exactJson.cjs"),t=require("./frameworkErrors.cjs"),n=require("./problem.cjs");let r=require("@ahoo-wang/fetcher-eventstream");var i=`row`,a=`error`,o=`end`,s=`message`;function c(t,n){return(0,r.toServerSentEventStream)(t.response).pipeThrough(new TransformStream({transform(r,i){i.enqueue({...r,data:e.parseJson(r.data,n(r.event)?t.numbers:void 0)})}})).getReader()}function l(e,t){let r=c(e,()=>!0);return new ReadableStream({async pull(e){let i;try{i=await r.read()}catch(e){throw n.rememberRequestId(e,t),e}i.done?e.close():e.enqueue(i.value.data)},cancel(e){return r.cancel(e)}})}var u=`hold-expired`,d=`complete`;function f(e,t){return new Promise((n,r)=>{let i=()=>{clearTimeout(a),r(t?.reason)},a=setTimeout(()=>{t?.removeEventListener(`abort`,i),n()},e);t?.addEventListener(`abort`,i)})}function p(e){return new n.WowProblemError(n.isProblem(e)?e:{type:`about:blank`,title:`Query failed`,status:500})}function m(e,r,l={}){let{maxRetries:m=t.DEFAULT_RETRY.attempts-1,initialDelayMs:h=t.DEFAULT_RETRY.backoffMs,maxDelayMs:g=t.DEFAULT_RETRY.maxWaitMs,signal:_}=l,v=e=>c(e,e=>e===i||e===s),y=v(e),b=l.lastEventId,x=0,S=0,C=new AbortController,w=_===void 0?C.signal:AbortSignal.any([_,C.signal]);async function T(e,t){for(;;){if(w.aborted)throw w.reason;if(S++>=m)throw e;let i=t??Math.min(g,h*2**(S-1));i>0&&await f(i,w);try{let e=await r(b,x,w);if(w.aborted)throw await e?.response.body?.cancel(w.reason),w.reason;return e&&(y=v(e)),e!==void 0}catch(r){if(w.aborted||r instanceof n.WowProblemError&&r.retry===`none`)throw r;t=r.retryAfterMs,e=r}}}return new ReadableStream({async pull(e){for(;;){let t,r;try{let n=await y.read();if(n.done)t=TypeError(`The query stream was cut off.`);else{let{event:c,id:l,data:f}=n.value;if(c===i||c===s){l&&(b=l),x++,S=0,e.enqueue(l?{row:f,position:l}:{row:f});return}if(c===o){let n=f;if(n.reason===d){e.close();return}if(t=TypeError(`The query stream ended: ${n.reason}.`),!n.resumable)throw t;n.reason===u&&(r=0)}else if(c===a){let e=p(f);if(e.retry===`none`)throw e;t=e,r=e.retryAfterMs}else continue}}catch(e){if(e===t||e instanceof n.WowProblemError)throw e;t=e}if(C.signal.aborted)return;if(_?.aborted)throw _.reason;if(b===void 0&&x>0)throw t;if(!await T(t,r)){e.close();return}}},cancel(e){return C.abort(e),y.cancel(e)}})}exports.commandResults=l,exports.resumableQueryRows=m;
|
|
2
2
|
//# sourceMappingURL=streams.cjs.map
|