@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/queries.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queries.cjs","names":["json","resumableQueryRows","send","WowHeaders","entityTag","WowProblemError","FrameworkErrorCodes","eventNumbers","require_exactJson","require_frameworkErrors","require_problem","require_http","require_streams","QueryModelClient","path","client","numbers","query","options","controller","positioned","resume","limit","open","received","lastEventId","first","error","isNoMatch","response","version","tag","accept","queryPath","type","model","SnapshotQueryClient","EventQueryClient","events","DatasetClient","context","dataset"],"sources":["../src/queries.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 type { AggregationQuery } from './dsl/aggregation/index.js';\nimport type { CursorPage, CursorQuery } from './dsl/cursorQuery.js';\nimport type { Projected } from './dsl/projection.js';\nimport { eventNumbers, type TextNumbers } from './exactJson.js';\nimport type {\n DescriptorResult,\n QueryModelDescriptor,\n} from './dsl/descriptor.js';\nimport type { FilterCapable } from './dsl/filter/index.js';\nimport { FrameworkErrorCodes, WowProblemError } from './problem.js';\nimport type {\n FilterListQuery,\n FilterSingleQuery,\n PagedQuery,\n PagedList,\n} from './dsl/queryable.js';\nimport {\n entityTag,\n json,\n type RequestOptions,\n send,\n type WowClientOptions,\n WowHeaders,\n} from './http.js';\nimport type {\n AggregateType,\n DatasetEntry,\n EventStreamField,\n EventStream,\n CommittedEvent,\n Snapshot,\n SnapshotSystemField,\n} from './wire.js';\nimport {\n resumableQueryRows,\n type Positioned,\n type StreamResumeOptions,\n} from './streams.js';\n\n/** How a list is streamed: the request's options, and how it resumes. */\nexport interface StreamOptions extends RequestOptions {\n /** How the stream resumes (decision D253); `false` never resumes it. */\n readonly resume?: StreamResumeOptions | false;\n}\n\n/** A count of a query route. */\nexport interface CountQuery<\n FIELDS extends string = string,\n> extends FilterCapable<FIELDS> {\n /** The most matches to count; the budget's default when absent. */\n limit?: number;\n}\n\n/** What a count answers: exact below its limit, else the limit itself. */\nexport interface CountResult {\n readonly count: number;\n readonly exact: boolean;\n}\n\n/**\n * Queries one query model of a Wow service: `POST {path}` with a body\n * whose `kind` names the operation, `GET {path}` for the capability\n * descriptor. A query written with literal `include`\n * paths answers records narrowed to them ({@link Projected}, review R4-Q,\n * Q-14).\n *\n * @typeParam R - The record's type.\n * @typeParam FIELDS - The field paths a filter, sort or projection takes.\n */\nexport class QueryModelClient<R, FIELDS extends string = string> {\n /**\n * @param path - The query route, `/{context}/{aggregate}/{model}/query`\n * @param client - How the service is reached\n * @param numbers - The numbers of each record read as their exact text\n * (review R4-Q, Q-9)\n */\n constructor(\n readonly path: string,\n readonly client: WowClientOptions = {},\n readonly numbers?: TextNumbers,\n ) {}\n\n /** The marks of a list of records. */\n private get listNumbers(): TextNumbers | undefined {\n return this.numbers === undefined ? undefined : { '*': this.numbers };\n }\n\n /** The marks of a page of records. */\n private get pageNumbers(): TextNumbers | undefined {\n return this.numbers === undefined\n ? undefined\n : { list: { '*': this.numbers } };\n }\n\n /** The records that match, at most `limit` (the server's default when absent). */\n list<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<Projected<R, INCLUDE>[]>;\n list(query: FilterListQuery<FIELDS>, options?: RequestOptions): Promise<R[]>;\n async list(\n query: FilterListQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return json<R[]>(await this.post('list', query, options), this.listNumbers);\n }\n\n /**\n * The records that match, as server-sent events read one at a time; a\n * failure after the first record errors the stream with its problem. The\n * stream resumes where it stopped (decision D253) when the server ends it\n * to be resumed, when it is cut off and when it fails with a retryable\n * problem: see {@link StreamResumeOptions}. A `limit` counts every record\n * read, resumed or not.\n */\n stream<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Projected<R, INCLUDE>>>;\n stream(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<ReadableStream<R>>;\n async stream(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<unknown> {\n return (await this.streamRows(query, options)).pipeThrough(\n new TransformStream<Positioned<R>, R>({\n transform(positioned, controller) {\n controller.enqueue(positioned.row);\n },\n }),\n );\n }\n\n /**\n * The records that match as {@link stream} reads them, each with its\n * position: keep the last one read to resume the stream later\n * ({@link StreamResumeOptions.lastEventId}), on another page or after a\n * restart. Kotlin's `RowQueries.stream` answers these rows (`Positioned`).\n */\n streamRows<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Positioned<Projected<R, INCLUDE>>>>;\n streamRows(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Positioned<R>>>;\n async streamRows(\n query: FilterListQuery<FIELDS>,\n options: StreamOptions = {},\n ): Promise<ReadableStream<Positioned<R>>> {\n const resume =\n options.resume === false ? { maxRetries: 0 } : options.resume;\n const limit = query.limit;\n const open = (lastEventId: string | undefined, received: number) => {\n return this.post(\n 'list',\n limit === undefined ? query : { ...query, limit: limit - received },\n options,\n 'text/event-stream',\n lastEventId,\n );\n };\n const first = await open(resume?.lastEventId, 0);\n return resumableQueryRows<R>(\n first,\n (lastEventId, received) =>\n limit !== undefined && received >= limit\n ? Promise.resolve(undefined)\n : open(lastEventId, received),\n {\n ...resume,\n ...(options.signal === undefined ? {} : { signal: options.signal }),\n ...(this.numbers === undefined ? {} : { numbers: this.numbers }),\n },\n );\n }\n\n /** One page of the records that match, and their total. */\n paged<const INCLUDE extends FIELDS>(\n query: Including<PagedQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<PagedList<Projected<R, INCLUDE>>>;\n paged(\n query: PagedQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<PagedList<R>>;\n async paged(\n query: PagedQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return json<PagedList<R>>(\n await this.post('paged', query, options),\n this.pageNumbers,\n );\n }\n\n /** One page of a forward-only cursor over the records that match. */\n cursor<const INCLUDE extends FIELDS>(\n query: Including<CursorQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<CursorPage<Projected<R, INCLUDE>>>;\n cursor(\n query: CursorQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<CursorPage<R>>;\n async cursor(\n query: CursorQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return json<CursorPage<R>>(\n await this.post('cursor', query, options),\n this.pageNumbers,\n );\n }\n\n /**\n * The first record that matches, or `undefined` when none does: the\n * service's 404 `wow.query.not-found`, as Kotlin's `QueryApi.single`\n * answers `null`. Any other 404, of a dataset or a route that does not\n * exist, still rejects.\n */\n single<const INCLUDE extends FIELDS>(\n query: Including<FilterSingleQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<Projected<R, INCLUDE> | undefined>;\n single(\n query: FilterSingleQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<R | undefined>;\n async single(\n query: FilterSingleQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n try {\n return json<R>(await this.post('single', query, options), this.numbers);\n } catch (error) {\n if (isNoMatch(error)) return undefined;\n throw error;\n }\n }\n\n /** How many records match, bounded by the count's limit. */\n async count(\n query: CountQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<CountResult> {\n return json<CountResult>(await this.post('count', query, options));\n }\n\n /** The groups and metrics of an aggregation, one row per group. */\n async aggregate<ROW = Record<string, unknown>>(\n query: AggregationQuery<FIELDS, string>,\n options?: RequestOptions,\n ): Promise<ROW[]> {\n return json<ROW[]>(await this.post('aggregate', query, options));\n }\n\n /**\n * The model's capability descriptor; with the `version` already held, a\n * 304 answers `notModified`.\n */\n async describe(\n version?: string,\n options?: RequestOptions,\n ): Promise<DescriptorResult> {\n const response = await send(this.client, {\n method: 'GET',\n path: this.path,\n headers: {\n [WowHeaders.IF_NONE_MATCH]:\n version === undefined ? undefined : `\"${version}\"`,\n },\n options,\n notModified: true,\n });\n const tag = entityTag(response);\n if (response.status === 304) {\n return { notModified: true, version: tag ?? version ?? '' };\n }\n return {\n notModified: false,\n ...(tag === undefined ? {} : { version: tag }),\n descriptor: await json<QueryModelDescriptor>(response),\n };\n }\n\n private post(\n kind: string,\n query: object,\n options?: RequestOptions,\n accept?: string,\n lastEventId?: string,\n ): Promise<Response> {\n return send(this.client, {\n method: 'POST',\n path: this.path,\n headers: { Accept: accept, [WowHeaders.LAST_EVENT_ID]: lastEventId },\n body: { ...query, kind },\n options,\n });\n }\n}\n\n/**\n * Whether `error` is a 404 that answers \"no match\": a query that found no\n * record, or an aggregate that does not exist. A 404 of another code, or of\n * none (a route that does not exist), is a mistake the caller must hear of.\n */\nfunction isNoMatch(error: unknown): boolean {\n return (\n error instanceof WowProblemError &&\n error.status === 404 &&\n (error.code === FrameworkErrorCodes.QUERY_NOT_FOUND ||\n error.code === FrameworkErrorCodes.AGGREGATE_NOT_FOUND)\n );\n}\n\n/**\n * A query whose projection includes the literal paths `INCLUDE`: a query\n * client narrows its records to them ({@link Projected}).\n */\nexport type Including<Q, INCLUDE extends string> = Q & {\n readonly projection: { readonly include: readonly INCLUDE[] };\n};\n\n/** The query route of a model of an aggregate type. */\nexport function queryPath(type: AggregateType, model: string): string {\n return `/${type.context}/${type.name}/${model}/query`;\n}\n\n/**\n * Queries an aggregate type's snapshots, the default read\n * model; only where the aggregate keeps them.\n *\n * @typeParam S - The state's type.\n * @typeParam STATE_FIELDS - The `state.*` paths of the state.\n */\nexport class SnapshotQueryClient<\n S = unknown,\n STATE_FIELDS extends string = `state.${string}`,\n> extends QueryModelClient<Snapshot<S>, SnapshotSystemField | STATE_FIELDS> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n client?: WowClientOptions,\n ) {\n super(\n queryPath(type, 'snapshot'),\n client,\n type.textNumbers?.state === undefined\n ? undefined\n : { state: type.textNumbers.state },\n );\n }\n}\n\n/**\n * Queries an aggregate type's event streams on the same\n * engine as its snapshots: one record per commit, `count` counts commits, a\n * condition on an event is an `elementMatch` on `events`, and per-event\n * analytics expand `events` in an aggregation (`elements`). A filter need\n * not name the aggregate, but one that does not reads the log the service\n * appends to: the budget bounds it.\n *\n * @typeParam E - The events' type.\n */\nexport class EventQueryClient<\n E extends CommittedEvent = CommittedEvent,\n> extends QueryModelClient<EventStream<E>, EventStreamField> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n client?: WowClientOptions,\n ) {\n const events = eventNumbers(type);\n super(\n queryPath(type, 'events'),\n client,\n events === undefined ? undefined : { events: { '*': events } },\n );\n }\n}\n\n/**\n * The datasets of a Wow service: registered at run\n * time, so not in the Manifest. Lists those the caller sees and queries\n * each as a snapshot model is queried.\n */\nexport class DatasetClient {\n /** @param client - How the service is reached */\n constructor(readonly client: WowClientOptions = {}) {}\n\n /** The datasets the caller sees, by name. */\n async list(options?: RequestOptions): Promise<DatasetEntry[]> {\n return json<DatasetEntry[]>(\n await send(this.client, { method: 'GET', path: '/wow/dataset', options }),\n );\n }\n\n /**\n * The query client of the dataset `context.dataset`.\n *\n * @typeParam R - The row's type.\n * @typeParam FIELDS - The dataset's field paths.\n */\n dataset<R = Record<string, unknown>, FIELDS extends string = string>(\n context: string,\n dataset: string,\n ): QueryModelClient<R, FIELDS> {\n return new QueryModelClient<R, FIELDS>(\n `/wow/dataset/${encodeURIComponent(context)}/${encodeURIComponent(dataset)}/query`,\n this.client,\n );\n }\n}\n"],"mappings":"A,oJAkFA,IAAaa,EAAb,KAAiE,CAO/D,YACE,EACA,EAAoC,CAAC,EACrC,EACA,CAHS,KAAA,KAAAC,EACA,KAAA,OAAAC,EACA,KAAA,QAAAC,CACR,CAGH,IAAY,aAAuC,CACjD,OAAO,KAAK,UAAY,IAAA,GAAY,IAAA,GAAY,CAAE,IAAK,KAAK,OAAQ,CACtE,CAGA,IAAY,aAAuC,CACjD,OAAO,KAAK,UAAY,IAAA,GACpB,IA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,EAAAA,oBAAoB,iBAClCqB,EAAM,OAASrB,EAAAA,oBAAoB,oBAEzC,CAWA,SAAgB2B,EAAU,EAAqB,EAAuB,CACpE,MAAO,IAAIC,EAAK,QAAQ,GAAGA,EAAK,KAAK,GAAGC,EAAM,OAChD,CASA,IAAaC,EAAb,cAGUvB,CAAkE,CAK1E,YACE,EACA,EACA,CACA,MACEoB,EAAUC,EAAM,UAAU,EAC1BnB,EACAmB,EAAK,aAAa,QAAU,IAAA,GACxB,IAAA,GACA,CAAE,MAAOA,EAAK,YAAY,KAAM,CACtC,EATS,KAAA,KAAAA,CAUX,CACF,EAYaG,EAAb,cAEUxB,CAAmD,CAK3D,YACE,EACA,EACA,CACA,IAAMyB,EAAS/B,EAAAA,aAAa2B,CAAI,EAChC,MACED,EAAUC,EAAM,QAAQ,EACxBnB,EACAuB,IAAW,IAAA,GAAY,IAAA,GAAY,CAAE,OAAQ,CAAE,IAAKA,CAAO,CAAE,CAC/D,EARS,KAAA,KAAAJ,CASX,CACF,EAOaK,EAAb,KAA2B,CAEzB,YAAY,EAAoC,CAAC,EAAG,CAA/B,KAAA,OAAAxB,CAAgC,CAGrD,MAAM,KAAK,EAAmD,CAC5D,OAAOf,EAAAA,KACL,MAAME,EAAAA,KAAK,KAAK,OAAQ,CAAE,OAAQ,MAAO,KAAM,eAAgB,SAAQ,CAAC,CAC1E,CACF,CAQA,QACE,EACA,EAC6B,CAC7B,OAAO,IAAIW,EACT,gBAAgB,mBAAmB2B,CAAO,EAAE,GAAG,mBAAmBC,CAAO,EAAE,QAC3E,KAAK,MACP,CACF,CACF,E"}
|
|
1
|
+
{"version":3,"file":"queries.cjs","names":["resumableQueryRows","send","WowHeaders","entityTag","WowProblemError","FrameworkErrorCodes","recordNumbers","eventNumbers","require_exactJson","require_frameworkErrors","require_problem","require_http","require_streams","QueryModelClient","path","client","numbers","query","options","controller","positioned","resume","limit","open","received","signal","lastEventId","first","error","isNoMatch","answer","version","tag","accept","queryPath","type","model","SnapshotQueryClient","EventQueryClient","events","DatasetClient","context","dataset"],"sources":["../src/queries.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 type { AggregationQuery } from './dsl/aggregation/index.js';\nimport type { CursorPage, CursorQuery } from './dsl/cursorQuery.js';\nimport type { Projected } from './dsl/projection.js';\nimport { eventNumbers, recordNumbers, type TextNumbers } from './exactJson.js';\nimport type {\n DescriptorResult,\n QueryModelDescriptor,\n} from './dsl/descriptor.js';\nimport type { FilterCapable } from './dsl/filter/index.js';\nimport { FrameworkErrorCodes, WowProblemError } from './problem.js';\nimport type {\n FilterListQuery,\n FilterSingleQuery,\n PagedQuery,\n PagedList,\n} from './dsl/queryable.js';\nimport {\n type Answer,\n entityTag,\n type RequestOptions,\n send,\n type WowClientOptions,\n WowHeaders,\n} from './http.js';\nimport type {\n AccessField,\n AccessValues,\n AggregateType,\n DatasetEntry,\n EventStreamField,\n EventStream,\n CommittedEvent,\n Snapshot,\n SnapshotSystemField,\n} from './wire.js';\nimport {\n resumableQueryRows,\n type Positioned,\n type StreamResumeOptions,\n} from './streams.js';\n\n/** How a list is streamed: the request's options, and how it resumes. */\nexport interface StreamOptions extends RequestOptions {\n /** How the stream resumes (decision D253); `false` never resumes it. */\n readonly resume?: StreamResumeOptions | false;\n}\n\n/** A count of a query route. */\nexport interface CountQuery<\n FIELDS extends string = string,\n> extends FilterCapable<FIELDS> {\n /** The most matches to count; the budget's default when absent. */\n limit?: number;\n}\n\n/** What a count answers: exact below its limit, else the limit itself. */\nexport interface CountResult {\n readonly count: number;\n readonly exact: boolean;\n}\n\n/**\n * Queries one query model of a Wow service: `POST {path}` with a body\n * whose `kind` names the operation, `GET {path}` for the capability\n * descriptor. A query written with literal `include`\n * paths answers records narrowed to them ({@link Projected}, review R4-Q,\n * Q-14).\n *\n * @typeParam R - The record's type.\n * @typeParam FIELDS - The field paths a filter, sort or projection takes.\n */\nexport class QueryModelClient<R, FIELDS extends string = string> {\n /**\n * @param path - The query route, `/{context}/{aggregate}/{model}/query`\n * @param client - How the service is reached\n * @param numbers - The numbers of each record read as their exact text\n * (review R4-Q, Q-9)\n */\n constructor(\n readonly path: string,\n readonly client: WowClientOptions = {},\n readonly numbers?: TextNumbers,\n ) {}\n\n /** The marks of a list of records. */\n private get listNumbers(): TextNumbers | undefined {\n return this.numbers === undefined ? undefined : { '*': this.numbers };\n }\n\n /** The marks of a page of records. */\n private get pageNumbers(): TextNumbers | undefined {\n return this.numbers === undefined\n ? undefined\n : { list: { '*': this.numbers } };\n }\n\n /** The records that match, at most `limit` (the server's default when absent). */\n list<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<Projected<R, INCLUDE>[]>;\n list(query: FilterListQuery<FIELDS>, options?: RequestOptions): Promise<R[]>;\n async list(\n query: FilterListQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return (await this.post('list', query, this.listNumbers, options)).json();\n }\n\n /**\n * The records that match, as server-sent events read one at a time; a\n * failure after the first record errors the stream with its problem. The\n * stream resumes where it stopped (decision D253) when the server ends it\n * to be resumed, when it is cut off and when it fails with a retryable\n * problem: see {@link StreamResumeOptions}. A `limit` counts every record\n * read, resumed or not.\n */\n stream<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Projected<R, INCLUDE>>>;\n stream(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<ReadableStream<R>>;\n async stream(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<unknown> {\n return (await this.streamRows(query, options)).pipeThrough(\n new TransformStream<Positioned<R>, R>({\n transform(positioned, controller) {\n controller.enqueue(positioned.row);\n },\n }),\n );\n }\n\n /**\n * The records that match as {@link stream} reads them, each with its\n * position: keep the last one read to resume the stream later\n * ({@link StreamResumeOptions.lastEventId}), on another page or after a\n * restart. Kotlin's `RowQueries.stream` answers these rows (`Positioned`).\n */\n streamRows<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Positioned<Projected<R, INCLUDE>>>>;\n streamRows(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Positioned<R>>>;\n async streamRows(\n query: FilterListQuery<FIELDS>,\n options: StreamOptions = {},\n ): Promise<ReadableStream<Positioned<R>>> {\n const resume =\n options.resume === false ? { maxRetries: 0 } : options.resume;\n const limit = query.limit;\n const open = (\n lastEventId: string | undefined,\n received: number,\n signal: AbortSignal | undefined,\n ) => {\n return this.post(\n 'list',\n limit === undefined ? query : { ...query, limit: limit - received },\n this.numbers,\n signal === undefined ? options : { ...options, signal },\n 'text/event-stream',\n lastEventId,\n );\n };\n const first = await open(resume?.lastEventId, 0, options.signal);\n return resumableQueryRows<R>(\n first,\n (lastEventId, received, signal) =>\n limit !== undefined && received >= limit\n ? Promise.resolve(undefined)\n : open(lastEventId, received, signal),\n {\n ...resume,\n ...(options.signal === undefined ? {} : { signal: options.signal }),\n },\n );\n }\n\n /** One page of the records that match, and their total. */\n paged<const INCLUDE extends FIELDS>(\n query: Including<PagedQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<PagedList<Projected<R, INCLUDE>>>;\n paged(\n query: PagedQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<PagedList<R>>;\n async paged(\n query: PagedQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return (await this.post('paged', query, this.pageNumbers, options)).json();\n }\n\n /** One page of a forward-only cursor over the records that match. */\n cursor<const INCLUDE extends FIELDS>(\n query: Including<CursorQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<CursorPage<Projected<R, INCLUDE>>>;\n cursor(\n query: CursorQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<CursorPage<R>>;\n async cursor(\n query: CursorQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return (await this.post('cursor', query, this.pageNumbers, options)).json();\n }\n\n /**\n * The first record that matches, or `undefined` when none does: the\n * service's 404 `wow.query.not-found` or `wow.aggregate.not-found`, as\n * Kotlin's `QueryApi.single` answers `null`. Any other 404, of a dataset\n * or a route that does not exist, still rejects.\n */\n single<const INCLUDE extends FIELDS>(\n query: Including<FilterSingleQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<Projected<R, INCLUDE> | undefined>;\n single(\n query: FilterSingleQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<R | undefined>;\n async single(\n query: FilterSingleQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n try {\n return (await this.post('single', query, this.numbers, options)).json();\n } catch (error) {\n if (isNoMatch(error)) return undefined;\n throw error;\n }\n }\n\n /** How many records match, bounded by the count's limit. */\n async count(\n query: CountQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<CountResult> {\n // A count is an integer far below 2^53: no exact numbers.\n return (await this.post('count', query, undefined, options)).json();\n }\n\n /** The groups and metrics of an aggregation, one row per group. */\n async aggregate<ROW = Record<string, unknown>>(\n query: AggregationQuery<FIELDS, string>,\n options?: RequestOptions,\n ): Promise<ROW[]> {\n // Its rows are the query's own shape, which no schema declares.\n return (await this.post('aggregate', query, undefined, options)).json();\n }\n\n /**\n * The model's capability descriptor; with the `version` already held, a\n * 304 answers `notModified`.\n */\n async describe(\n version?: string,\n options?: RequestOptions,\n ): Promise<DescriptorResult> {\n const answer = await send(this.client, {\n method: 'GET',\n path: this.path,\n headers: {\n [WowHeaders.IF_NONE_MATCH]:\n version === undefined ? undefined : `\"${version}\"`,\n },\n numbers: {},\n options,\n notModified: true,\n });\n const tag = entityTag(answer.response);\n if (answer.response.status === 304) {\n return { notModified: true, version: tag ?? version ?? '' };\n }\n return {\n notModified: false,\n ...(tag === undefined ? {} : { version: tag }),\n descriptor: await answer.json<QueryModelDescriptor>(),\n };\n }\n\n /** Posts a query of `kind`, its answer's exact numbers `numbers`. */\n private post(\n kind: string,\n query: object,\n numbers: TextNumbers | undefined,\n options?: RequestOptions,\n accept?: string,\n lastEventId?: string,\n ): Promise<Answer> {\n return send(this.client, {\n method: 'POST',\n path: this.path,\n headers: { Accept: accept, [WowHeaders.LAST_EVENT_ID]: lastEventId },\n body: { ...query, kind },\n numbers: { answer: numbers },\n options,\n });\n }\n}\n\n/**\n * Whether `error` is a 404 that answers \"no match\": a query that found no\n * record, or an aggregate that does not exist. A 404 of another code, or of\n * none (a route that does not exist), is a mistake the caller must hear of.\n */\nfunction isNoMatch(error: unknown): boolean {\n return (\n error instanceof WowProblemError &&\n error.status === 404 &&\n (error.code === FrameworkErrorCodes.QUERY_NOT_FOUND ||\n error.code === FrameworkErrorCodes.AGGREGATE_NOT_FOUND)\n );\n}\n\n/**\n * A query whose projection includes the literal paths `INCLUDE`: a query\n * client narrows its records to them ({@link Projected}).\n */\nexport type Including<Q, INCLUDE extends string> = Q & {\n readonly projection: { readonly include: readonly INCLUDE[] };\n};\n\n/** The query route of a model of an aggregate type. */\nexport function queryPath(type: AggregateType, model: string): string {\n return `/${type.context}/${type.name}/${model}/query`;\n}\n\n/**\n * Queries an aggregate type's snapshots, the default read\n * model; only where the aggregate keeps them.\n *\n * @typeParam S - The state's type.\n * @typeParam STATE_FIELDS - The `state.*` paths of the state.\n * @typeParam A - Its access attributes, whose `access.<name>` paths a query\n * names ({@link AccessField}); {@link NoAccess} for an aggregate that\n * declares none.\n */\nexport class SnapshotQueryClient<\n S = unknown,\n STATE_FIELDS extends string = `state.${string}`,\n A = AccessValues,\n> extends QueryModelClient<\n Snapshot<S, A>,\n SnapshotSystemField | STATE_FIELDS | AccessField<A>\n> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n client?: WowClientOptions,\n ) {\n super(\n queryPath(type, 'snapshot'),\n client,\n recordNumbers({\n state: type.textNumbers?.state,\n access: type.textNumbers?.access,\n }),\n );\n }\n}\n\n/**\n * Queries an aggregate type's event streams on the same\n * engine as its snapshots: one record per commit, `count` counts commits, a\n * condition on an event is an `elementMatch` on `events`, and per-event\n * analytics expand `events` in an aggregation (`elements`). A filter need\n * not name the aggregate, but one that does not reads the log the service\n * appends to: the budget bounds it.\n *\n * @typeParam E - The events' type.\n * @typeParam A - The aggregate's declared access attributes, whose\n * `access.<name>` paths a query names beside the built-in `access.owner`\n * and `access.space`.\n */\nexport class EventQueryClient<\n E extends CommittedEvent = CommittedEvent,\n A = AccessValues,\n> extends QueryModelClient<\n EventStream<E, A>,\n EventStreamField | AccessField<A>\n> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n client?: WowClientOptions,\n ) {\n const events = eventNumbers(type);\n super(\n queryPath(type, 'events'),\n client,\n recordNumbers({\n events: events === undefined ? undefined : { '*': events },\n access: type.textNumbers?.access,\n }),\n );\n }\n}\n\n/**\n * The datasets of a Wow service: registered at run\n * time, so not in the Manifest. Lists those the caller sees and queries\n * each as a snapshot model is queried.\n */\nexport class DatasetClient {\n /** @param client - How the service is reached */\n constructor(readonly client: WowClientOptions = {}) {}\n\n /** The datasets the caller sees, by name. */\n async list(options?: RequestOptions): Promise<DatasetEntry[]> {\n const answer = await send(this.client, {\n method: 'GET',\n path: '/wow/dataset',\n numbers: {},\n options,\n });\n return answer.json();\n }\n\n /**\n * The query client of the dataset `context.dataset`. A dataset is\n * registered at run time, so no Manifest declares its rows' exact numbers:\n * a caller that knows them passes them.\n *\n * @typeParam R - The row's type.\n * @typeParam FIELDS - The dataset's field paths.\n * @param numbers - The exact numbers of each row ({@link TextNumbers})\n */\n dataset<R = Record<string, unknown>, FIELDS extends string = string>(\n context: string,\n dataset: string,\n numbers?: TextNumbers,\n ): QueryModelClient<R, FIELDS> {\n return new QueryModelClient<R, FIELDS>(\n `/wow/dataset/${encodeURIComponent(context)}/${encodeURIComponent(dataset)}/query`,\n this.client,\n numbers,\n );\n }\n}\n"],"mappings":"A,oJAoFA,IAAaa,EAAb,KAAiE,CAO/D,YACE,EACA,EAAoC,CAAC,EACrC,EACA,CAHS,KAAA,KAAAC,EACA,KAAA,OAAAC,EACA,KAAA,QAAAC,CACR,CAGH,IAAY,aAAuC,CACjD,OAAO,KAAK,UAAY,IAAA,GAAY,IAAA,GAAY,CAAE,IAAK,KAAK,OAAQ,CACtE,CAGA,IAAY,aAAuC,CACjD,OAAO,KAAK,UAAY,IAAA,GACpB,IAAA,GACA,CAAE,KAAM,CAAE,IAAK,KAAK,OAAQ,CAAE,CACpC,CAQA,MAAM,KACJ,EACA,EACkB,CAClB,OAAQ,MAAM,KAAK,KAAK,OAAQC,EAAO,KAAK,YAAaC,CAAO,EAAA,CAAG,KAAK,CAC1E,CAkBA,MAAM,OACJ,EACA,EACkB,CAClB,OAAQ,MAAM,KAAK,WAAWD,EAAOC,CAAO,EAAA,CAAG,YAC7C,IAAI,gBAAkC,CACpC,UAAU,EAAY,EAAY,CAChCC,EAAW,QAAQC,EAAW,GAAG,CACnC,CACF,CAAC,CACH,CACF,CAgBA,MAAM,WACJ,EACA,EAAyB,CAAC,EACc,CACxC,IAAMC,EACJH,EAAQ,SAAW,GAAQ,CAAE,WAAY,CAAE,EAAIA,EAAQ,OACnDI,EAAQL,EAAM,MACdM,GACJ,EACA,EACA,IAEO,KAAK,KACV,OACAD,IAAU,IAAA,GAAYL,EAAQ,CAAE,GAAGA,EAAO,MAAOK,EAAQE,CAAS,EAClE,KAAK,QACLC,IAAW,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AW,IAAA,GAAY,IAAA,GAAY,CAAE,IAAKA,CAAO,EACzD,OAAQJ,EAAK,aAAa,MAC5B,CAAC,CACH,EAXS,KAAA,KAAAA,CAYX,CACF,EAOaK,EAAb,KAA2B,CAEzB,YAAY,EAAoC,CAAC,EAAG,CAA/B,KAAA,OAAAzB,CAAgC,CAGrD,MAAM,KAAK,EAAmD,CAO5D,OAAO,MANcd,EAAAA,KAAK,KAAK,OAAQ,CACrC,OAAQ,MACR,KAAM,eACN,QAAS,CAAC,EACV,SACF,CAAC,EAAA,CACa,KAAK,CACrB,CAWA,QACE,EACA,EACA,EAC6B,CAC7B,OAAO,IAAIY,EACT,gBAAgB,mBAAmB4B,CAAO,EAAE,GAAG,mBAAmBC,CAAO,EAAE,QAC3E,KAAK,OACL1B,CACF,CACF,CACF,E"}
|
package/dist/queries.d.cts
CHANGED
|
@@ -6,7 +6,7 @@ import { DescriptorResult } from './dsl/descriptor.cjs';
|
|
|
6
6
|
import { FilterCapable } from './dsl/filter/index.cjs';
|
|
7
7
|
import { FilterListQuery, FilterSingleQuery, PagedQuery, PagedList } from './dsl/queryable.cjs';
|
|
8
8
|
import { RequestOptions, WowClientOptions } from './http.cjs';
|
|
9
|
-
import { AggregateType, DatasetEntry, EventStreamField, EventStream, CommittedEvent, Snapshot, SnapshotSystemField } from './wire.cjs';
|
|
9
|
+
import { AccessField, AccessValues, AggregateType, DatasetEntry, EventStreamField, EventStream, CommittedEvent, Snapshot, SnapshotSystemField } from './wire.cjs';
|
|
10
10
|
import { Positioned, StreamResumeOptions } from './streams.cjs';
|
|
11
11
|
/** How a list is streamed: the request's options, and how it resumes. */
|
|
12
12
|
export interface StreamOptions extends RequestOptions {
|
|
@@ -77,9 +77,9 @@ export declare class QueryModelClient<R, FIELDS extends string = string> {
|
|
|
77
77
|
cursor(query: CursorQuery<FIELDS>, options?: RequestOptions): Promise<CursorPage<R>>;
|
|
78
78
|
/**
|
|
79
79
|
* The first record that matches, or `undefined` when none does: the
|
|
80
|
-
* service's 404 `wow.query.not-found
|
|
81
|
-
* answers `null`. Any other 404, of a dataset
|
|
82
|
-
* exist, still rejects.
|
|
80
|
+
* service's 404 `wow.query.not-found` or `wow.aggregate.not-found`, as
|
|
81
|
+
* Kotlin's `QueryApi.single` answers `null`. Any other 404, of a dataset
|
|
82
|
+
* or a route that does not exist, still rejects.
|
|
83
83
|
*/
|
|
84
84
|
single<const INCLUDE extends FIELDS>(query: Including<FilterSingleQuery<FIELDS>, INCLUDE>, options?: RequestOptions): Promise<Projected<R, INCLUDE> | undefined>;
|
|
85
85
|
single(query: FilterSingleQuery<FIELDS>, options?: RequestOptions): Promise<R | undefined>;
|
|
@@ -92,6 +92,7 @@ export declare class QueryModelClient<R, FIELDS extends string = string> {
|
|
|
92
92
|
* 304 answers `notModified`.
|
|
93
93
|
*/
|
|
94
94
|
describe(version?: string, options?: RequestOptions): Promise<DescriptorResult>;
|
|
95
|
+
/** Posts a query of `kind`, its answer's exact numbers `numbers`. */
|
|
95
96
|
private post;
|
|
96
97
|
}
|
|
97
98
|
/**
|
|
@@ -111,8 +112,11 @@ export declare function queryPath(type: AggregateType, model: string): string;
|
|
|
111
112
|
*
|
|
112
113
|
* @typeParam S - The state's type.
|
|
113
114
|
* @typeParam STATE_FIELDS - The `state.*` paths of the state.
|
|
115
|
+
* @typeParam A - Its access attributes, whose `access.<name>` paths a query
|
|
116
|
+
* names ({@link AccessField}); {@link NoAccess} for an aggregate that
|
|
117
|
+
* declares none.
|
|
114
118
|
*/
|
|
115
|
-
export declare class SnapshotQueryClient<S = unknown, STATE_FIELDS extends string = `state.${string}
|
|
119
|
+
export declare class SnapshotQueryClient<S = unknown, STATE_FIELDS extends string = `state.${string}`, A = AccessValues> extends QueryModelClient<Snapshot<S, A>, SnapshotSystemField | STATE_FIELDS | AccessField<A>> {
|
|
116
120
|
readonly type: AggregateType;
|
|
117
121
|
/**
|
|
118
122
|
* @param type - The aggregate type
|
|
@@ -129,8 +133,11 @@ export declare class SnapshotQueryClient<S = unknown, STATE_FIELDS extends strin
|
|
|
129
133
|
* appends to: the budget bounds it.
|
|
130
134
|
*
|
|
131
135
|
* @typeParam E - The events' type.
|
|
136
|
+
* @typeParam A - The aggregate's declared access attributes, whose
|
|
137
|
+
* `access.<name>` paths a query names beside the built-in `access.owner`
|
|
138
|
+
* and `access.space`.
|
|
132
139
|
*/
|
|
133
|
-
export declare class EventQueryClient<E extends CommittedEvent = CommittedEvent> extends QueryModelClient<EventStream<E>, EventStreamField
|
|
140
|
+
export declare class EventQueryClient<E extends CommittedEvent = CommittedEvent, A = AccessValues> extends QueryModelClient<EventStream<E, A>, EventStreamField | AccessField<A>> {
|
|
134
141
|
readonly type: AggregateType;
|
|
135
142
|
/**
|
|
136
143
|
* @param type - The aggregate type
|
|
@@ -150,10 +157,13 @@ export declare class DatasetClient {
|
|
|
150
157
|
/** The datasets the caller sees, by name. */
|
|
151
158
|
list(options?: RequestOptions): Promise<DatasetEntry[]>;
|
|
152
159
|
/**
|
|
153
|
-
* The query client of the dataset `context.dataset`.
|
|
160
|
+
* The query client of the dataset `context.dataset`. A dataset is
|
|
161
|
+
* registered at run time, so no Manifest declares its rows' exact numbers:
|
|
162
|
+
* a caller that knows them passes them.
|
|
154
163
|
*
|
|
155
164
|
* @typeParam R - The row's type.
|
|
156
165
|
* @typeParam FIELDS - The dataset's field paths.
|
|
166
|
+
* @param numbers - The exact numbers of each row ({@link TextNumbers})
|
|
157
167
|
*/
|
|
158
|
-
dataset<R = Record<string, unknown>, FIELDS extends string = string>(context: string, dataset: string): QueryModelClient<R, FIELDS>;
|
|
168
|
+
dataset<R = Record<string, unknown>, FIELDS extends string = string>(context: string, dataset: string, numbers?: TextNumbers): QueryModelClient<R, FIELDS>;
|
|
159
169
|
}
|
package/dist/queries.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { DescriptorResult } from './dsl/descriptor.js';
|
|
|
6
6
|
import { FilterCapable } from './dsl/filter/index.js';
|
|
7
7
|
import { FilterListQuery, FilterSingleQuery, PagedQuery, PagedList } from './dsl/queryable.js';
|
|
8
8
|
import { RequestOptions, WowClientOptions } from './http.js';
|
|
9
|
-
import { AggregateType, DatasetEntry, EventStreamField, EventStream, CommittedEvent, Snapshot, SnapshotSystemField } from './wire.js';
|
|
9
|
+
import { AccessField, AccessValues, AggregateType, DatasetEntry, EventStreamField, EventStream, CommittedEvent, Snapshot, SnapshotSystemField } from './wire.js';
|
|
10
10
|
import { Positioned, StreamResumeOptions } from './streams.js';
|
|
11
11
|
/** How a list is streamed: the request's options, and how it resumes. */
|
|
12
12
|
export interface StreamOptions extends RequestOptions {
|
|
@@ -77,9 +77,9 @@ export declare class QueryModelClient<R, FIELDS extends string = string> {
|
|
|
77
77
|
cursor(query: CursorQuery<FIELDS>, options?: RequestOptions): Promise<CursorPage<R>>;
|
|
78
78
|
/**
|
|
79
79
|
* The first record that matches, or `undefined` when none does: the
|
|
80
|
-
* service's 404 `wow.query.not-found
|
|
81
|
-
* answers `null`. Any other 404, of a dataset
|
|
82
|
-
* exist, still rejects.
|
|
80
|
+
* service's 404 `wow.query.not-found` or `wow.aggregate.not-found`, as
|
|
81
|
+
* Kotlin's `QueryApi.single` answers `null`. Any other 404, of a dataset
|
|
82
|
+
* or a route that does not exist, still rejects.
|
|
83
83
|
*/
|
|
84
84
|
single<const INCLUDE extends FIELDS>(query: Including<FilterSingleQuery<FIELDS>, INCLUDE>, options?: RequestOptions): Promise<Projected<R, INCLUDE> | undefined>;
|
|
85
85
|
single(query: FilterSingleQuery<FIELDS>, options?: RequestOptions): Promise<R | undefined>;
|
|
@@ -92,6 +92,7 @@ export declare class QueryModelClient<R, FIELDS extends string = string> {
|
|
|
92
92
|
* 304 answers `notModified`.
|
|
93
93
|
*/
|
|
94
94
|
describe(version?: string, options?: RequestOptions): Promise<DescriptorResult>;
|
|
95
|
+
/** Posts a query of `kind`, its answer's exact numbers `numbers`. */
|
|
95
96
|
private post;
|
|
96
97
|
}
|
|
97
98
|
/**
|
|
@@ -111,8 +112,11 @@ export declare function queryPath(type: AggregateType, model: string): string;
|
|
|
111
112
|
*
|
|
112
113
|
* @typeParam S - The state's type.
|
|
113
114
|
* @typeParam STATE_FIELDS - The `state.*` paths of the state.
|
|
115
|
+
* @typeParam A - Its access attributes, whose `access.<name>` paths a query
|
|
116
|
+
* names ({@link AccessField}); {@link NoAccess} for an aggregate that
|
|
117
|
+
* declares none.
|
|
114
118
|
*/
|
|
115
|
-
export declare class SnapshotQueryClient<S = unknown, STATE_FIELDS extends string = `state.${string}
|
|
119
|
+
export declare class SnapshotQueryClient<S = unknown, STATE_FIELDS extends string = `state.${string}`, A = AccessValues> extends QueryModelClient<Snapshot<S, A>, SnapshotSystemField | STATE_FIELDS | AccessField<A>> {
|
|
116
120
|
readonly type: AggregateType;
|
|
117
121
|
/**
|
|
118
122
|
* @param type - The aggregate type
|
|
@@ -129,8 +133,11 @@ export declare class SnapshotQueryClient<S = unknown, STATE_FIELDS extends strin
|
|
|
129
133
|
* appends to: the budget bounds it.
|
|
130
134
|
*
|
|
131
135
|
* @typeParam E - The events' type.
|
|
136
|
+
* @typeParam A - The aggregate's declared access attributes, whose
|
|
137
|
+
* `access.<name>` paths a query names beside the built-in `access.owner`
|
|
138
|
+
* and `access.space`.
|
|
132
139
|
*/
|
|
133
|
-
export declare class EventQueryClient<E extends CommittedEvent = CommittedEvent> extends QueryModelClient<EventStream<E>, EventStreamField
|
|
140
|
+
export declare class EventQueryClient<E extends CommittedEvent = CommittedEvent, A = AccessValues> extends QueryModelClient<EventStream<E, A>, EventStreamField | AccessField<A>> {
|
|
134
141
|
readonly type: AggregateType;
|
|
135
142
|
/**
|
|
136
143
|
* @param type - The aggregate type
|
|
@@ -150,10 +157,13 @@ export declare class DatasetClient {
|
|
|
150
157
|
/** The datasets the caller sees, by name. */
|
|
151
158
|
list(options?: RequestOptions): Promise<DatasetEntry[]>;
|
|
152
159
|
/**
|
|
153
|
-
* The query client of the dataset `context.dataset`.
|
|
160
|
+
* The query client of the dataset `context.dataset`. A dataset is
|
|
161
|
+
* registered at run time, so no Manifest declares its rows' exact numbers:
|
|
162
|
+
* a caller that knows them passes them.
|
|
154
163
|
*
|
|
155
164
|
* @typeParam R - The row's type.
|
|
156
165
|
* @typeParam FIELDS - The dataset's field paths.
|
|
166
|
+
* @param numbers - The exact numbers of each row ({@link TextNumbers})
|
|
157
167
|
*/
|
|
158
|
-
dataset<R = Record<string, unknown>, FIELDS extends string = string>(context: string, dataset: string): QueryModelClient<R, FIELDS>;
|
|
168
|
+
dataset<R = Record<string, unknown>, FIELDS extends string = string>(context: string, dataset: string, numbers?: TextNumbers): QueryModelClient<R, FIELDS>;
|
|
159
169
|
}
|
package/dist/queries.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
|
import { resumableQueryRows as s } from "./streams.es.js";
|
|
6
6
|
// src/queries.ts
|
|
7
7
|
var c = class {
|
|
@@ -15,7 +15,7 @@ var c = class {
|
|
|
15
15
|
return this.numbers === void 0 ? void 0 : { list: { "*": this.numbers } };
|
|
16
16
|
}
|
|
17
17
|
async list(e, t) {
|
|
18
|
-
return
|
|
18
|
+
return (await this.post("list", e, this.listNumbers, t)).json();
|
|
19
19
|
}
|
|
20
20
|
async stream(e, t) {
|
|
21
21
|
return (await this.streamRows(e, t)).pipeThrough(new TransformStream({ transform(e, t) {
|
|
@@ -23,97 +23,108 @@ var c = class {
|
|
|
23
23
|
} }));
|
|
24
24
|
}
|
|
25
25
|
async streamRows(e, t = {}) {
|
|
26
|
-
let n = t.resume === !1 ? { maxRetries: 0 } : t.resume, r = e.limit, i = (n, i) => this.post("list", r === void 0 ? e : {
|
|
26
|
+
let n = t.resume === !1 ? { maxRetries: 0 } : t.resume, r = e.limit, i = (n, i, a) => this.post("list", r === void 0 ? e : {
|
|
27
27
|
...e,
|
|
28
28
|
limit: r - i
|
|
29
|
-
},
|
|
30
|
-
|
|
29
|
+
}, this.numbers, a === void 0 ? t : {
|
|
30
|
+
...t,
|
|
31
|
+
signal: a
|
|
32
|
+
}, "text/event-stream", n), a = await i(n?.lastEventId, 0, t.signal);
|
|
33
|
+
return s(a, (e, t, n) => r !== void 0 && t >= r ? Promise.resolve(void 0) : i(e, t, n), {
|
|
31
34
|
...n,
|
|
32
|
-
...t.signal === void 0 ? {} : { signal: t.signal }
|
|
33
|
-
...this.numbers === void 0 ? {} : { numbers: this.numbers }
|
|
35
|
+
...t.signal === void 0 ? {} : { signal: t.signal }
|
|
34
36
|
});
|
|
35
37
|
}
|
|
36
38
|
async paged(e, t) {
|
|
37
|
-
return
|
|
39
|
+
return (await this.post("paged", e, this.pageNumbers, t)).json();
|
|
38
40
|
}
|
|
39
41
|
async cursor(e, t) {
|
|
40
|
-
return
|
|
42
|
+
return (await this.post("cursor", e, this.pageNumbers, t)).json();
|
|
41
43
|
}
|
|
42
44
|
async single(e, t) {
|
|
43
45
|
try {
|
|
44
|
-
return
|
|
46
|
+
return (await this.post("single", e, this.numbers, t)).json();
|
|
45
47
|
} catch (e) {
|
|
46
48
|
if (l(e)) return;
|
|
47
49
|
throw e;
|
|
48
50
|
}
|
|
49
51
|
}
|
|
50
52
|
async count(e, t) {
|
|
51
|
-
return
|
|
53
|
+
return (await this.post("count", e, void 0, t)).json();
|
|
52
54
|
}
|
|
53
55
|
async aggregate(e, t) {
|
|
54
|
-
return
|
|
56
|
+
return (await this.post("aggregate", e, void 0, t)).json();
|
|
55
57
|
}
|
|
56
58
|
async describe(e, t) {
|
|
57
59
|
let n = await o(this.client, {
|
|
58
60
|
method: "GET",
|
|
59
61
|
path: this.path,
|
|
60
|
-
headers: { [
|
|
62
|
+
headers: { [i.IF_NONE_MATCH]: e === void 0 ? void 0 : `"${e}"` },
|
|
63
|
+
numbers: {},
|
|
61
64
|
options: t,
|
|
62
65
|
notModified: !0
|
|
63
|
-
}),
|
|
64
|
-
return n.status === 304 ? {
|
|
66
|
+
}), r = a(n.response);
|
|
67
|
+
return n.response.status === 304 ? {
|
|
65
68
|
notModified: !0,
|
|
66
|
-
version:
|
|
69
|
+
version: r ?? e ?? ""
|
|
67
70
|
} : {
|
|
68
71
|
notModified: !1,
|
|
69
|
-
...
|
|
70
|
-
descriptor: await
|
|
72
|
+
...r === void 0 ? {} : { version: r },
|
|
73
|
+
descriptor: await n.json()
|
|
71
74
|
};
|
|
72
75
|
}
|
|
73
|
-
post(e, t, n,
|
|
76
|
+
post(e, t, n, r, a, s) {
|
|
74
77
|
return o(this.client, {
|
|
75
78
|
method: "POST",
|
|
76
79
|
path: this.path,
|
|
77
80
|
headers: {
|
|
78
|
-
Accept:
|
|
79
|
-
[
|
|
81
|
+
Accept: a,
|
|
82
|
+
[i.LAST_EVENT_ID]: s
|
|
80
83
|
},
|
|
81
84
|
body: {
|
|
82
85
|
...t,
|
|
83
86
|
kind: e
|
|
84
87
|
},
|
|
85
|
-
|
|
88
|
+
numbers: { answer: n },
|
|
89
|
+
options: r
|
|
86
90
|
});
|
|
87
91
|
}
|
|
88
92
|
};
|
|
89
93
|
function l(e) {
|
|
90
|
-
return e instanceof
|
|
94
|
+
return e instanceof r && e.status === 404 && (e.code === n.QUERY_NOT_FOUND || e.code === n.AGGREGATE_NOT_FOUND);
|
|
91
95
|
}
|
|
92
96
|
function u(e, t) {
|
|
93
97
|
return `/${e.context}/${e.name}/${t}/query`;
|
|
94
98
|
}
|
|
95
99
|
var d = class extends c {
|
|
96
|
-
constructor(e,
|
|
97
|
-
super(u(e, "snapshot"),
|
|
100
|
+
constructor(e, n) {
|
|
101
|
+
super(u(e, "snapshot"), n, t({
|
|
102
|
+
state: e.textNumbers?.state,
|
|
103
|
+
access: e.textNumbers?.access
|
|
104
|
+
})), this.type = e;
|
|
98
105
|
}
|
|
99
106
|
}, f = class extends c {
|
|
100
|
-
constructor(
|
|
101
|
-
let
|
|
102
|
-
super(u(
|
|
107
|
+
constructor(n, r) {
|
|
108
|
+
let i = e(n);
|
|
109
|
+
super(u(n, "events"), r, t({
|
|
110
|
+
events: i === void 0 ? void 0 : { "*": i },
|
|
111
|
+
access: n.textNumbers?.access
|
|
112
|
+
})), this.type = n;
|
|
103
113
|
}
|
|
104
114
|
}, p = class {
|
|
105
115
|
constructor(e = {}) {
|
|
106
116
|
this.client = e;
|
|
107
117
|
}
|
|
108
118
|
async list(e) {
|
|
109
|
-
return
|
|
119
|
+
return (await o(this.client, {
|
|
110
120
|
method: "GET",
|
|
111
121
|
path: "/wow/dataset",
|
|
122
|
+
numbers: {},
|
|
112
123
|
options: e
|
|
113
|
-
}));
|
|
124
|
+
})).json();
|
|
114
125
|
}
|
|
115
|
-
dataset(e, t) {
|
|
116
|
-
return new c(`/wow/dataset/${encodeURIComponent(e)}/${encodeURIComponent(t)}/query`, this.client);
|
|
126
|
+
dataset(e, t, n) {
|
|
127
|
+
return new c(`/wow/dataset/${encodeURIComponent(e)}/${encodeURIComponent(t)}/query`, this.client, n);
|
|
117
128
|
}
|
|
118
129
|
};
|
|
119
130
|
export { p as DatasetClient, f as EventQueryClient, c as QueryModelClient, d as SnapshotQueryClient, u as queryPath };
|
package/dist/queries.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queries.es.js","names":["QueryModelClient","path","client","numbers","json","query","options","positioned","resume","limit","open","received","lastEventId","first","resumableQueryRows","error","isNoMatch","response","send","WowHeaders","version","tag","entityTag","accept","WowProblemError","FrameworkErrorCodes","queryPath","type","model","SnapshotQueryClient","EventQueryClient","events","eventNumbers","DatasetClient","context","dataset"],"sources":["../src/queries.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 type { AggregationQuery } from './dsl/aggregation/index.js';\nimport type { CursorPage, CursorQuery } from './dsl/cursorQuery.js';\nimport type { Projected } from './dsl/projection.js';\nimport { eventNumbers, type TextNumbers } from './exactJson.js';\nimport type {\n DescriptorResult,\n QueryModelDescriptor,\n} from './dsl/descriptor.js';\nimport type { FilterCapable } from './dsl/filter/index.js';\nimport { FrameworkErrorCodes, WowProblemError } from './problem.js';\nimport type {\n FilterListQuery,\n FilterSingleQuery,\n PagedQuery,\n PagedList,\n} from './dsl/queryable.js';\nimport {\n entityTag,\n json,\n type RequestOptions,\n send,\n type WowClientOptions,\n WowHeaders,\n} from './http.js';\nimport type {\n AggregateType,\n DatasetEntry,\n EventStreamField,\n EventStream,\n CommittedEvent,\n Snapshot,\n SnapshotSystemField,\n} from './wire.js';\nimport {\n resumableQueryRows,\n type Positioned,\n type StreamResumeOptions,\n} from './streams.js';\n\n/** How a list is streamed: the request's options, and how it resumes. */\nexport interface StreamOptions extends RequestOptions {\n /** How the stream resumes (decision D253); `false` never resumes it. */\n readonly resume?: StreamResumeOptions | false;\n}\n\n/** A count of a query route. */\nexport interface CountQuery<\n FIELDS extends string = string,\n> extends FilterCapable<FIELDS> {\n /** The most matches to count; the budget's default when absent. */\n limit?: number;\n}\n\n/** What a count answers: exact below its limit, else the limit itself. */\nexport interface CountResult {\n readonly count: number;\n readonly exact: boolean;\n}\n\n/**\n * Queries one query model of a Wow service: `POST {path}` with a body\n * whose `kind` names the operation, `GET {path}` for the capability\n * descriptor. A query written with literal `include`\n * paths answers records narrowed to them ({@link Projected}, review R4-Q,\n * Q-14).\n *\n * @typeParam R - The record's type.\n * @typeParam FIELDS - The field paths a filter, sort or projection takes.\n */\nexport class QueryModelClient<R, FIELDS extends string = string> {\n /**\n * @param path - The query route, `/{context}/{aggregate}/{model}/query`\n * @param client - How the service is reached\n * @param numbers - The numbers of each record read as their exact text\n * (review R4-Q, Q-9)\n */\n constructor(\n readonly path: string,\n readonly client: WowClientOptions = {},\n readonly numbers?: TextNumbers,\n ) {}\n\n /** The marks of a list of records. */\n private get listNumbers(): TextNumbers | undefined {\n return this.numbers === undefined ? undefined : { '*': this.numbers };\n }\n\n /** The marks of a page of records. */\n private get pageNumbers(): TextNumbers | undefined {\n return this.numbers === undefined\n ? undefined\n : { list: { '*': this.numbers } };\n }\n\n /** The records that match, at most `limit` (the server's default when absent). */\n list<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<Projected<R, INCLUDE>[]>;\n list(query: FilterListQuery<FIELDS>, options?: RequestOptions): Promise<R[]>;\n async list(\n query: FilterListQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return json<R[]>(await this.post('list', query, options), this.listNumbers);\n }\n\n /**\n * The records that match, as server-sent events read one at a time; a\n * failure after the first record errors the stream with its problem. The\n * stream resumes where it stopped (decision D253) when the server ends it\n * to be resumed, when it is cut off and when it fails with a retryable\n * problem: see {@link StreamResumeOptions}. A `limit` counts every record\n * read, resumed or not.\n */\n stream<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Projected<R, INCLUDE>>>;\n stream(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<ReadableStream<R>>;\n async stream(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<unknown> {\n return (await this.streamRows(query, options)).pipeThrough(\n new TransformStream<Positioned<R>, R>({\n transform(positioned, controller) {\n controller.enqueue(positioned.row);\n },\n }),\n );\n }\n\n /**\n * The records that match as {@link stream} reads them, each with its\n * position: keep the last one read to resume the stream later\n * ({@link StreamResumeOptions.lastEventId}), on another page or after a\n * restart. Kotlin's `RowQueries.stream` answers these rows (`Positioned`).\n */\n streamRows<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Positioned<Projected<R, INCLUDE>>>>;\n streamRows(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Positioned<R>>>;\n async streamRows(\n query: FilterListQuery<FIELDS>,\n options: StreamOptions = {},\n ): Promise<ReadableStream<Positioned<R>>> {\n const resume =\n options.resume === false ? { maxRetries: 0 } : options.resume;\n const limit = query.limit;\n const open = (lastEventId: string | undefined, received: number) => {\n return this.post(\n 'list',\n limit === undefined ? query : { ...query, limit: limit - received },\n options,\n 'text/event-stream',\n lastEventId,\n );\n };\n const first = await open(resume?.lastEventId, 0);\n return resumableQueryRows<R>(\n first,\n (lastEventId, received) =>\n limit !== undefined && received >= limit\n ? Promise.resolve(undefined)\n : open(lastEventId, received),\n {\n ...resume,\n ...(options.signal === undefined ? {} : { signal: options.signal }),\n ...(this.numbers === undefined ? {} : { numbers: this.numbers }),\n },\n );\n }\n\n /** One page of the records that match, and their total. */\n paged<const INCLUDE extends FIELDS>(\n query: Including<PagedQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<PagedList<Projected<R, INCLUDE>>>;\n paged(\n query: PagedQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<PagedList<R>>;\n async paged(\n query: PagedQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return json<PagedList<R>>(\n await this.post('paged', query, options),\n this.pageNumbers,\n );\n }\n\n /** One page of a forward-only cursor over the records that match. */\n cursor<const INCLUDE extends FIELDS>(\n query: Including<CursorQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<CursorPage<Projected<R, INCLUDE>>>;\n cursor(\n query: CursorQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<CursorPage<R>>;\n async cursor(\n query: CursorQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return json<CursorPage<R>>(\n await this.post('cursor', query, options),\n this.pageNumbers,\n );\n }\n\n /**\n * The first record that matches, or `undefined` when none does: the\n * service's 404 `wow.query.not-found`, as Kotlin's `QueryApi.single`\n * answers `null`. Any other 404, of a dataset or a route that does not\n * exist, still rejects.\n */\n single<const INCLUDE extends FIELDS>(\n query: Including<FilterSingleQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<Projected<R, INCLUDE> | undefined>;\n single(\n query: FilterSingleQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<R | undefined>;\n async single(\n query: FilterSingleQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n try {\n return json<R>(await this.post('single', query, options), this.numbers);\n } catch (error) {\n if (isNoMatch(error)) return undefined;\n throw error;\n }\n }\n\n /** How many records match, bounded by the count's limit. */\n async count(\n query: CountQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<CountResult> {\n return json<CountResult>(await this.post('count', query, options));\n }\n\n /** The groups and metrics of an aggregation, one row per group. */\n async aggregate<ROW = Record<string, unknown>>(\n query: AggregationQuery<FIELDS, string>,\n options?: RequestOptions,\n ): Promise<ROW[]> {\n return json<ROW[]>(await this.post('aggregate', query, options));\n }\n\n /**\n * The model's capability descriptor; with the `version` already held, a\n * 304 answers `notModified`.\n */\n async describe(\n version?: string,\n options?: RequestOptions,\n ): Promise<DescriptorResult> {\n const response = await send(this.client, {\n method: 'GET',\n path: this.path,\n headers: {\n [WowHeaders.IF_NONE_MATCH]:\n version === undefined ? undefined : `\"${version}\"`,\n },\n options,\n notModified: true,\n });\n const tag = entityTag(response);\n if (response.status === 304) {\n return { notModified: true, version: tag ?? version ?? '' };\n }\n return {\n notModified: false,\n ...(tag === undefined ? {} : { version: tag }),\n descriptor: await json<QueryModelDescriptor>(response),\n };\n }\n\n private post(\n kind: string,\n query: object,\n options?: RequestOptions,\n accept?: string,\n lastEventId?: string,\n ): Promise<Response> {\n return send(this.client, {\n method: 'POST',\n path: this.path,\n headers: { Accept: accept, [WowHeaders.LAST_EVENT_ID]: lastEventId },\n body: { ...query, kind },\n options,\n });\n }\n}\n\n/**\n * Whether `error` is a 404 that answers \"no match\": a query that found no\n * record, or an aggregate that does not exist. A 404 of another code, or of\n * none (a route that does not exist), is a mistake the caller must hear of.\n */\nfunction isNoMatch(error: unknown): boolean {\n return (\n error instanceof WowProblemError &&\n error.status === 404 &&\n (error.code === FrameworkErrorCodes.QUERY_NOT_FOUND ||\n error.code === FrameworkErrorCodes.AGGREGATE_NOT_FOUND)\n );\n}\n\n/**\n * A query whose projection includes the literal paths `INCLUDE`: a query\n * client narrows its records to them ({@link Projected}).\n */\nexport type Including<Q, INCLUDE extends string> = Q & {\n readonly projection: { readonly include: readonly INCLUDE[] };\n};\n\n/** The query route of a model of an aggregate type. */\nexport function queryPath(type: AggregateType, model: string): string {\n return `/${type.context}/${type.name}/${model}/query`;\n}\n\n/**\n * Queries an aggregate type's snapshots, the default read\n * model; only where the aggregate keeps them.\n *\n * @typeParam S - The state's type.\n * @typeParam STATE_FIELDS - The `state.*` paths of the state.\n */\nexport class SnapshotQueryClient<\n S = unknown,\n STATE_FIELDS extends string = `state.${string}`,\n> extends QueryModelClient<Snapshot<S>, SnapshotSystemField | STATE_FIELDS> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n client?: WowClientOptions,\n ) {\n super(\n queryPath(type, 'snapshot'),\n client,\n type.textNumbers?.state === undefined\n ? undefined\n : { state: type.textNumbers.state },\n );\n }\n}\n\n/**\n * Queries an aggregate type's event streams on the same\n * engine as its snapshots: one record per commit, `count` counts commits, a\n * condition on an event is an `elementMatch` on `events`, and per-event\n * analytics expand `events` in an aggregation (`elements`). A filter need\n * not name the aggregate, but one that does not reads the log the service\n * appends to: the budget bounds it.\n *\n * @typeParam E - The events' type.\n */\nexport class EventQueryClient<\n E extends CommittedEvent = CommittedEvent,\n> extends QueryModelClient<EventStream<E>, EventStreamField> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n client?: WowClientOptions,\n ) {\n const events = eventNumbers(type);\n super(\n queryPath(type, 'events'),\n client,\n events === undefined ? undefined : { events: { '*': events } },\n );\n }\n}\n\n/**\n * The datasets of a Wow service: registered at run\n * time, so not in the Manifest. Lists those the caller sees and queries\n * each as a snapshot model is queried.\n */\nexport class DatasetClient {\n /** @param client - How the service is reached */\n constructor(readonly client: WowClientOptions = {}) {}\n\n /** The datasets the caller sees, by name. */\n async list(options?: RequestOptions): Promise<DatasetEntry[]> {\n return json<DatasetEntry[]>(\n await send(this.client, { method: 'GET', path: '/wow/dataset', options }),\n );\n }\n\n /**\n * The query client of the dataset `context.dataset`.\n *\n * @typeParam R - The row's type.\n * @typeParam FIELDS - The dataset's field paths.\n */\n dataset<R = Record<string, unknown>, FIELDS extends string = string>(\n context: string,\n dataset: string,\n ): QueryModelClient<R, FIELDS> {\n return new QueryModelClient<R, FIELDS>(\n `/wow/dataset/${encodeURIComponent(context)}/${encodeURIComponent(dataset)}/query`,\n this.client,\n );\n }\n}\n"],"mappings":";;;;;;AAkFA,IAAaA,IAAb,MAAiE;CAO/D,YACE,GACA,IAAoC,CAAC,GACrC,GACA;EADS,AAFA,KAAA,OAAAC,GACA,KAAA,SAAAC,GACA,KAAA,UAAAC;CACR;CAGH,IAAY,cAAuC;EACjD,OAAO,KAAK,YAAY,KAAA,IAAY,KAAA,IAAY,EAAE,KAAK,KAAK,QAAQ;CACtE;CAGA,IAAY,cAAuC;EACjD,OAAO,KAAK,YAAY,KAAA,IACpB,KAAA,IACA,EAAE,MAAM,EA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IAAaC,IAAb,cAGU7B,EAAkE;CAK1E,YACE,GACA,GACA;EAFS,AAGT,MACE0B,EAAUC,GAAM,UAAU,GAC1BzB,GACAyB,EAAK,aAAa,UAAU,KAAA,IACxB,KAAA,IACA,EAAE,OAAOA,EAAK,YAAY,MAAM,CACtC,GATS,KAAA,OAAAA;CAUX;AACF,GAYaG,IAAb,cAEU9B,EAAmD;CAK3D,YACE,GACA,GACA;EACA,IAAM+B,IAASC,EAAaL,CAAI;EAHvB,AAIT,MACED,EAAUC,GAAM,QAAQ,GACxBzB,GACA6B,MAAW,KAAA,IAAY,KAAA,IAAY,EAAE,QAAQ,EAAE,KAAKA,EAAO,EAAE,CAC/D,GARS,KAAA,OAAAJ;CASX;AACF,GAOaM,IAAb,MAA2B;CAEzB,YAAY,IAAoC,CAAC,GAAG;EAA/B,KAAA,SAAA/B;CAAgC;CAGrD,MAAM,KAAK,GAAmD;EAC5D,OAAOE,EACL,MAAMc,EAAK,KAAK,QAAQ;GAAE,QAAQ;GAAO,MAAM;GAAgB;EAAQ,CAAC,CAC1E;CACF;CAQA,QACE,GACA,GAC6B;EAC7B,OAAO,IAAIlB,EACT,gBAAgB,mBAAmBkC,CAAO,EAAE,GAAG,mBAAmBC,CAAO,EAAE,SAC3E,KAAK,MACP;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"queries.es.js","names":["QueryModelClient","path","client","numbers","query","options","positioned","resume","limit","open","received","signal","lastEventId","first","resumableQueryRows","error","isNoMatch","answer","send","WowHeaders","version","tag","entityTag","accept","WowProblemError","FrameworkErrorCodes","queryPath","type","model","SnapshotQueryClient","recordNumbers","EventQueryClient","events","eventNumbers","DatasetClient","context","dataset"],"sources":["../src/queries.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 type { AggregationQuery } from './dsl/aggregation/index.js';\nimport type { CursorPage, CursorQuery } from './dsl/cursorQuery.js';\nimport type { Projected } from './dsl/projection.js';\nimport { eventNumbers, recordNumbers, type TextNumbers } from './exactJson.js';\nimport type {\n DescriptorResult,\n QueryModelDescriptor,\n} from './dsl/descriptor.js';\nimport type { FilterCapable } from './dsl/filter/index.js';\nimport { FrameworkErrorCodes, WowProblemError } from './problem.js';\nimport type {\n FilterListQuery,\n FilterSingleQuery,\n PagedQuery,\n PagedList,\n} from './dsl/queryable.js';\nimport {\n type Answer,\n entityTag,\n type RequestOptions,\n send,\n type WowClientOptions,\n WowHeaders,\n} from './http.js';\nimport type {\n AccessField,\n AccessValues,\n AggregateType,\n DatasetEntry,\n EventStreamField,\n EventStream,\n CommittedEvent,\n Snapshot,\n SnapshotSystemField,\n} from './wire.js';\nimport {\n resumableQueryRows,\n type Positioned,\n type StreamResumeOptions,\n} from './streams.js';\n\n/** How a list is streamed: the request's options, and how it resumes. */\nexport interface StreamOptions extends RequestOptions {\n /** How the stream resumes (decision D253); `false` never resumes it. */\n readonly resume?: StreamResumeOptions | false;\n}\n\n/** A count of a query route. */\nexport interface CountQuery<\n FIELDS extends string = string,\n> extends FilterCapable<FIELDS> {\n /** The most matches to count; the budget's default when absent. */\n limit?: number;\n}\n\n/** What a count answers: exact below its limit, else the limit itself. */\nexport interface CountResult {\n readonly count: number;\n readonly exact: boolean;\n}\n\n/**\n * Queries one query model of a Wow service: `POST {path}` with a body\n * whose `kind` names the operation, `GET {path}` for the capability\n * descriptor. A query written with literal `include`\n * paths answers records narrowed to them ({@link Projected}, review R4-Q,\n * Q-14).\n *\n * @typeParam R - The record's type.\n * @typeParam FIELDS - The field paths a filter, sort or projection takes.\n */\nexport class QueryModelClient<R, FIELDS extends string = string> {\n /**\n * @param path - The query route, `/{context}/{aggregate}/{model}/query`\n * @param client - How the service is reached\n * @param numbers - The numbers of each record read as their exact text\n * (review R4-Q, Q-9)\n */\n constructor(\n readonly path: string,\n readonly client: WowClientOptions = {},\n readonly numbers?: TextNumbers,\n ) {}\n\n /** The marks of a list of records. */\n private get listNumbers(): TextNumbers | undefined {\n return this.numbers === undefined ? undefined : { '*': this.numbers };\n }\n\n /** The marks of a page of records. */\n private get pageNumbers(): TextNumbers | undefined {\n return this.numbers === undefined\n ? undefined\n : { list: { '*': this.numbers } };\n }\n\n /** The records that match, at most `limit` (the server's default when absent). */\n list<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<Projected<R, INCLUDE>[]>;\n list(query: FilterListQuery<FIELDS>, options?: RequestOptions): Promise<R[]>;\n async list(\n query: FilterListQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return (await this.post('list', query, this.listNumbers, options)).json();\n }\n\n /**\n * The records that match, as server-sent events read one at a time; a\n * failure after the first record errors the stream with its problem. The\n * stream resumes where it stopped (decision D253) when the server ends it\n * to be resumed, when it is cut off and when it fails with a retryable\n * problem: see {@link StreamResumeOptions}. A `limit` counts every record\n * read, resumed or not.\n */\n stream<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Projected<R, INCLUDE>>>;\n stream(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<ReadableStream<R>>;\n async stream(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<unknown> {\n return (await this.streamRows(query, options)).pipeThrough(\n new TransformStream<Positioned<R>, R>({\n transform(positioned, controller) {\n controller.enqueue(positioned.row);\n },\n }),\n );\n }\n\n /**\n * The records that match as {@link stream} reads them, each with its\n * position: keep the last one read to resume the stream later\n * ({@link StreamResumeOptions.lastEventId}), on another page or after a\n * restart. Kotlin's `RowQueries.stream` answers these rows (`Positioned`).\n */\n streamRows<const INCLUDE extends FIELDS>(\n query: Including<FilterListQuery<FIELDS>, INCLUDE>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Positioned<Projected<R, INCLUDE>>>>;\n streamRows(\n query: FilterListQuery<FIELDS>,\n options?: StreamOptions,\n ): Promise<ReadableStream<Positioned<R>>>;\n async streamRows(\n query: FilterListQuery<FIELDS>,\n options: StreamOptions = {},\n ): Promise<ReadableStream<Positioned<R>>> {\n const resume =\n options.resume === false ? { maxRetries: 0 } : options.resume;\n const limit = query.limit;\n const open = (\n lastEventId: string | undefined,\n received: number,\n signal: AbortSignal | undefined,\n ) => {\n return this.post(\n 'list',\n limit === undefined ? query : { ...query, limit: limit - received },\n this.numbers,\n signal === undefined ? options : { ...options, signal },\n 'text/event-stream',\n lastEventId,\n );\n };\n const first = await open(resume?.lastEventId, 0, options.signal);\n return resumableQueryRows<R>(\n first,\n (lastEventId, received, signal) =>\n limit !== undefined && received >= limit\n ? Promise.resolve(undefined)\n : open(lastEventId, received, signal),\n {\n ...resume,\n ...(options.signal === undefined ? {} : { signal: options.signal }),\n },\n );\n }\n\n /** One page of the records that match, and their total. */\n paged<const INCLUDE extends FIELDS>(\n query: Including<PagedQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<PagedList<Projected<R, INCLUDE>>>;\n paged(\n query: PagedQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<PagedList<R>>;\n async paged(\n query: PagedQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return (await this.post('paged', query, this.pageNumbers, options)).json();\n }\n\n /** One page of a forward-only cursor over the records that match. */\n cursor<const INCLUDE extends FIELDS>(\n query: Including<CursorQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<CursorPage<Projected<R, INCLUDE>>>;\n cursor(\n query: CursorQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<CursorPage<R>>;\n async cursor(\n query: CursorQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n return (await this.post('cursor', query, this.pageNumbers, options)).json();\n }\n\n /**\n * The first record that matches, or `undefined` when none does: the\n * service's 404 `wow.query.not-found` or `wow.aggregate.not-found`, as\n * Kotlin's `QueryApi.single` answers `null`. Any other 404, of a dataset\n * or a route that does not exist, still rejects.\n */\n single<const INCLUDE extends FIELDS>(\n query: Including<FilterSingleQuery<FIELDS>, INCLUDE>,\n options?: RequestOptions,\n ): Promise<Projected<R, INCLUDE> | undefined>;\n single(\n query: FilterSingleQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<R | undefined>;\n async single(\n query: FilterSingleQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<unknown> {\n try {\n return (await this.post('single', query, this.numbers, options)).json();\n } catch (error) {\n if (isNoMatch(error)) return undefined;\n throw error;\n }\n }\n\n /** How many records match, bounded by the count's limit. */\n async count(\n query: CountQuery<FIELDS>,\n options?: RequestOptions,\n ): Promise<CountResult> {\n // A count is an integer far below 2^53: no exact numbers.\n return (await this.post('count', query, undefined, options)).json();\n }\n\n /** The groups and metrics of an aggregation, one row per group. */\n async aggregate<ROW = Record<string, unknown>>(\n query: AggregationQuery<FIELDS, string>,\n options?: RequestOptions,\n ): Promise<ROW[]> {\n // Its rows are the query's own shape, which no schema declares.\n return (await this.post('aggregate', query, undefined, options)).json();\n }\n\n /**\n * The model's capability descriptor; with the `version` already held, a\n * 304 answers `notModified`.\n */\n async describe(\n version?: string,\n options?: RequestOptions,\n ): Promise<DescriptorResult> {\n const answer = await send(this.client, {\n method: 'GET',\n path: this.path,\n headers: {\n [WowHeaders.IF_NONE_MATCH]:\n version === undefined ? undefined : `\"${version}\"`,\n },\n numbers: {},\n options,\n notModified: true,\n });\n const tag = entityTag(answer.response);\n if (answer.response.status === 304) {\n return { notModified: true, version: tag ?? version ?? '' };\n }\n return {\n notModified: false,\n ...(tag === undefined ? {} : { version: tag }),\n descriptor: await answer.json<QueryModelDescriptor>(),\n };\n }\n\n /** Posts a query of `kind`, its answer's exact numbers `numbers`. */\n private post(\n kind: string,\n query: object,\n numbers: TextNumbers | undefined,\n options?: RequestOptions,\n accept?: string,\n lastEventId?: string,\n ): Promise<Answer> {\n return send(this.client, {\n method: 'POST',\n path: this.path,\n headers: { Accept: accept, [WowHeaders.LAST_EVENT_ID]: lastEventId },\n body: { ...query, kind },\n numbers: { answer: numbers },\n options,\n });\n }\n}\n\n/**\n * Whether `error` is a 404 that answers \"no match\": a query that found no\n * record, or an aggregate that does not exist. A 404 of another code, or of\n * none (a route that does not exist), is a mistake the caller must hear of.\n */\nfunction isNoMatch(error: unknown): boolean {\n return (\n error instanceof WowProblemError &&\n error.status === 404 &&\n (error.code === FrameworkErrorCodes.QUERY_NOT_FOUND ||\n error.code === FrameworkErrorCodes.AGGREGATE_NOT_FOUND)\n );\n}\n\n/**\n * A query whose projection includes the literal paths `INCLUDE`: a query\n * client narrows its records to them ({@link Projected}).\n */\nexport type Including<Q, INCLUDE extends string> = Q & {\n readonly projection: { readonly include: readonly INCLUDE[] };\n};\n\n/** The query route of a model of an aggregate type. */\nexport function queryPath(type: AggregateType, model: string): string {\n return `/${type.context}/${type.name}/${model}/query`;\n}\n\n/**\n * Queries an aggregate type's snapshots, the default read\n * model; only where the aggregate keeps them.\n *\n * @typeParam S - The state's type.\n * @typeParam STATE_FIELDS - The `state.*` paths of the state.\n * @typeParam A - Its access attributes, whose `access.<name>` paths a query\n * names ({@link AccessField}); {@link NoAccess} for an aggregate that\n * declares none.\n */\nexport class SnapshotQueryClient<\n S = unknown,\n STATE_FIELDS extends string = `state.${string}`,\n A = AccessValues,\n> extends QueryModelClient<\n Snapshot<S, A>,\n SnapshotSystemField | STATE_FIELDS | AccessField<A>\n> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n client?: WowClientOptions,\n ) {\n super(\n queryPath(type, 'snapshot'),\n client,\n recordNumbers({\n state: type.textNumbers?.state,\n access: type.textNumbers?.access,\n }),\n );\n }\n}\n\n/**\n * Queries an aggregate type's event streams on the same\n * engine as its snapshots: one record per commit, `count` counts commits, a\n * condition on an event is an `elementMatch` on `events`, and per-event\n * analytics expand `events` in an aggregation (`elements`). A filter need\n * not name the aggregate, but one that does not reads the log the service\n * appends to: the budget bounds it.\n *\n * @typeParam E - The events' type.\n * @typeParam A - The aggregate's declared access attributes, whose\n * `access.<name>` paths a query names beside the built-in `access.owner`\n * and `access.space`.\n */\nexport class EventQueryClient<\n E extends CommittedEvent = CommittedEvent,\n A = AccessValues,\n> extends QueryModelClient<\n EventStream<E, A>,\n EventStreamField | AccessField<A>\n> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n client?: WowClientOptions,\n ) {\n const events = eventNumbers(type);\n super(\n queryPath(type, 'events'),\n client,\n recordNumbers({\n events: events === undefined ? undefined : { '*': events },\n access: type.textNumbers?.access,\n }),\n );\n }\n}\n\n/**\n * The datasets of a Wow service: registered at run\n * time, so not in the Manifest. Lists those the caller sees and queries\n * each as a snapshot model is queried.\n */\nexport class DatasetClient {\n /** @param client - How the service is reached */\n constructor(readonly client: WowClientOptions = {}) {}\n\n /** The datasets the caller sees, by name. */\n async list(options?: RequestOptions): Promise<DatasetEntry[]> {\n const answer = await send(this.client, {\n method: 'GET',\n path: '/wow/dataset',\n numbers: {},\n options,\n });\n return answer.json();\n }\n\n /**\n * The query client of the dataset `context.dataset`. A dataset is\n * registered at run time, so no Manifest declares its rows' exact numbers:\n * a caller that knows them passes them.\n *\n * @typeParam R - The row's type.\n * @typeParam FIELDS - The dataset's field paths.\n * @param numbers - The exact numbers of each row ({@link TextNumbers})\n */\n dataset<R = Record<string, unknown>, FIELDS extends string = string>(\n context: string,\n dataset: string,\n numbers?: TextNumbers,\n ): QueryModelClient<R, FIELDS> {\n return new QueryModelClient<R, FIELDS>(\n `/wow/dataset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F,CAAC,EAAA,CACa,KAAK;CACrB;CAWA,QACE,GACA,GACA,GAC6B;EAC7B,OAAO,IAAIlB,EACT,gBAAgB,mBAAmBmC,CAAO,EAAE,GAAG,mBAAmBC,CAAO,EAAE,SAC3E,KAAK,QACLjC,CACF;CACF;AACF"}
|
package/dist/routes.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./http.cjs"),t=require("./manifest.cjs");var n=/\{([A-Za-z][A-Za-z0-9]*)}/g,r=class{constructor(e={},n={}){this.client=e,this.manifest=
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./http.cjs"),t=require("./manifest.cjs");var n=/\{([A-Za-z][A-Za-z0-9]*)}/g,r=class{constructor(e={},n={}){this.client=e,this.manifest=new t.ManifestSource(e,n.manifest),this.numbers=n.textNumbers??{}}async call(t,r,i={},a={}){let o=(await this.manifest.read(a.signal)).contexts.find(e=>e.name===t)?.routes?.find(e=>e.name===r)?.route;if(o===void 0)throw TypeError(`The Manifest lists no service route ${r} of ${t}.`);let s=o.path.replace(n,(e,n)=>{let a=i.path?.[n];if(a===void 0)throw TypeError(`The service route ${t}.${r} needs its path variable ${n}.`);return encodeURIComponent(String(a))}),c=i.query?Object.fromEntries(Object.entries(i.query).map(([e,t])=>[e,t===void 0?void 0:String(t)])):void 0,l=this.numbers[`${t}.${r}`],u=await e.send(this.client,{method:o.method,path:s,...c===void 0?{}:{query:c},...i.body===void 0?{}:{body:i.body},...i.requestId===void 0?{}:{headers:{[e.WowHeaders.REQUEST_ID]:i.requestId}},numbers:{body:l?.request,answer:l?.response},options:a});return u.response.status===204?void 0:u.json()}};exports.ServiceRoutes=r;
|
|
2
2
|
//# sourceMappingURL=routes.cjs.map
|
package/dist/routes.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes.cjs","names":["
|
|
1
|
+
{"version":3,"file":"routes.cjs","names":["ManifestSource","send","WowHeaders","require_http","require_manifest","VARIABLE","ServiceRoutes","client","options","route","item","context","name","path","value","call","variable","query","key","numbers","answer"],"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":"A,6HAqEA,IAAMK,EAAW,6BAQJC,EAAb,KAA2B,CAQzB,YACE,EAAoC,CAAC,EACrC,EAAgC,CAAC,EACjC,CAFS,KAAA,OAAAC,EAGT,KAAK,SAAW,IAAIP,EAAAA,eAAeO,EAAQC,EAAQ,QAAQ,EAC3D,KAAK,QAAUA,EAAQ,aAAe,CAAC,CACzC,CAYA,MAAM,KACJ,EACA,EACA,EAAyB,CAAC,EAC1B,EAA0B,CAAC,EACH,CACxB,IAAMC,GAAS,MAAM,KAAK,SAAS,KAAKD,EAAQ,MAAM,EAAA,CAAG,SACtD,KAAK,GAAQE,EAAK,OAASC,CAAO,CAAC,EAClC,QAAQ,KAAK,GAAQD,EAAK,OAASE,CAAI,CAAC,EAAE,MAC9C,GAAIH,IAAU,IAAA,GACZ,MAAU,UACR,uCAAuCG,EAAK,MAAMD,EAAQ,EAC5D,EAEF,IAAME,EAAOJ,EAAM,KAAK,QAAQJ,GAAW,EAAG,IAAqB,CACjE,IAAMS,EAAQC,EAAK,OAAOC,GAC1B,GAAIF,IAAU,IAAA,GACZ,MAAU,UACR,qBAAqBH,EAAQ,GAAGC,EAAK,2BAA2BI,EAAS,EAC3E,EAEF,OAAO,mBAAmB,OAAOF,CAAK,CAAC,CACzC,CAAC,EACKG,EAAQF,EAAK,MACf,OAAO,YACL,OAAO,QAAQA,EAAK,KAAK,CAAC,CAAC,KAAK,CAACG,EAAKJ,KAAW,CAC/CI,EACAJ,IAAU,IAAA,GAAY,IAAA,GAAY,OAAOA,CAAK,CAChD,CAAC,CACH,EACA,IAAA,GACEK,EAAU,KAAK,QAAQ,GAAGR,EAAQ,GAAGC,KACrCQ,EAAS,MAAMnB,EAAAA,KAAK,KAAK,OAAQ,CACrC,OAAQQ,EAAM,OACd,OACA,GAAIQ,IAAU,IAAA,GAAY,CAAC,EAAI,CAAE,OAAM,EACvC,GAAIF,EAAK,OAAS,IAAA,GAAY,CAAC,EAAI,CAAE,KAAMA,EAAK,IAAK,EACrD,GAAIA,EAAK,YAAc,IAAA,GACnB,CAAC,EACD,CAAE,QAAS,EAAGb,EAAAA,WAAW,YAAaa,EAAK,SAAU,CAAE,EAC3D,QAAS,CAAE,KAAMI,GAAS,QAAS,OAAQA,GAAS,QAAS,EAC7D,SACF,CAAC,EACD,OAAOC,EAAO,SAAS,SAAW,IAAM,IAAA,GAAYA,EAAO,KAAQ,CACrE,CACF,E"}
|
package/dist/routes.d.cts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TextNumbers } from './exactJson.cjs';
|
|
1
2
|
import { RequestOptions, WowClientOptions } from './http.cjs';
|
|
2
3
|
import { RoutingManifest } from './manifest.cjs';
|
|
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
|
}
|