@atscript/odata-client 0.0.2 → 0.0.4

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 CHANGED
@@ -29,10 +29,10 @@ server in between. It is a `ClientLike` of `@atscript/db-client` (every public m
29
29
  ## Installation
30
30
 
31
31
  ```bash
32
- pnpm add @atscript/odata-client @atscript/db-client @atscript/db @atscript/typescript @uniqu/core
32
+ pnpm add @atscript/odata-client @atscript/db-client @atscript/db @atscript/core @atscript/typescript @uniqu/core
33
33
  ```
34
34
 
35
- Needs `@atscript/db` and `@atscript/db-client` 0.1.200 or newer and `@atscript/typescript` 0.2.25
35
+ Needs `@atscript/db` and `@atscript/db-client` 0.1.201 or newer and `@atscript/typescript` 0.2.26
36
36
  or newer.
37
37
 
38
38
  The models come from the generator of `@atscript/odata`, and the screens from Atscript UI: see
@@ -199,16 +199,16 @@ where it takes parameters.
199
199
  `$actions` on a read and `availableActions(id)` list the row actions it offers on a row. No call
200
200
  is filtered by it: the service decides.
201
201
 
202
- Any operation is callable through the factory, typed by its handle of the model module's `ops`, or
203
- untyped by its name (one only the live service has):
202
+ Any operation is callable through the factory: `call` typed by its handle of the model module's
203
+ `ops`, `callByName` untyped by its name (one only the live service has):
204
204
 
205
205
  ```ts
206
206
  import { ops } from "./odata/sales.model.gen";
207
207
 
208
208
  const { value: price } = await odata.call(ops.GetPrice, { params: { Material: "M-100" } });
209
- const order = await odata("/odata/sales/OrderSet").one("4500000001");
209
+ const order = await odata.client(sales.Order).one("4500000001");
210
210
  await odata.call(ops.OrderSet.ReleaseOrder, { row: order!, params: { Note: "ok" } });
211
- await odata.call("Z_RECALC", { params: { Plant: "1000" } });
211
+ await odata.callByName("Z_RECALC", { params: { Plant: "1000" } });
212
212
  ```
213
213
 
214
214
  `row` sends its `__etag` as `If-Match` on a call that writes (a function, and a call on the
package/dist/index.cjs CHANGED
@@ -1684,6 +1684,18 @@ const ODataClient = ODataEntityClient;
1684
1684
  /** The URL prefix OData entity sets are addressed under unless the factory is given another. */
1685
1685
  const DEFAULT_ODATA_PREFIX = "/odata";
1686
1686
  const HTTP_PATH = "db.http.path";
1687
+ /** The entity set a generated type is of, read from its metadata: none for a type of no set. */
1688
+ function entitySetOf(type) {
1689
+ const metadata = type.metadata;
1690
+ const entitySet = metadata.get("db.table");
1691
+ if (typeof entitySet !== "string") return void 0;
1692
+ const declared = metadata.get("odata.valueHelpService");
1693
+ return {
1694
+ service: metadata.get("db.space"),
1695
+ entitySet,
1696
+ valueHelpService: typeof declared === "string" ? declared : void 0
1697
+ };
1698
+ }
1687
1699
  /** The key of a target among those of its registration: the URL segments after the name. */
1688
1700
  function targetKey(entitySet, valueHelpService) {
1689
1701
  return valueHelpService === void 0 ? entitySet : `${valueHelpService}\u0000${entitySet}`;
@@ -1697,11 +1709,9 @@ function entitySetTypes(name, service, source) {
1697
1709
  const types = /* @__PURE__ */ new Map();
1698
1710
  for (const candidate of Object.values(source)) {
1699
1711
  if (!(0, _atscript_typescript_utils.isAnnotatedType)(candidate)) continue;
1700
- const metadata = candidate.metadata;
1701
- const entitySet = metadata.get("db.table");
1702
- if (typeof entitySet !== "string") continue;
1703
- const declared = metadata.get("odata.valueHelpService");
1704
- const valueHelpService = typeof declared === "string" ? declared : void 0;
1712
+ const set = entitySetOf(candidate);
1713
+ if (!set) continue;
1714
+ const { entitySet, valueHelpService } = set;
1705
1715
  const engine = valueHelpService === void 0 ? service : service.valueHelpService(valueHelpService);
1706
1716
  if (!engine) throw new Error(`Service "${name}": the type "${candidate.id ?? entitySet}" is of value-help service "${valueHelpService}", which the service model does not have. Create the service from the model module generated with the types, which holds its value-help services.`);
1707
1717
  if (!Object.hasOwn(engine.model.entitySets, entitySet)) throw new Error(`Service "${name}": the type "${candidate.id ?? entitySet}" is for entity set "${entitySet}", which the service model does not have. Were the types generated from another service?`);
@@ -1883,12 +1893,35 @@ function createODataClientFactory(options) {
1883
1893
  }
1884
1894
  });
1885
1895
  });
1886
- factory.urlOf = (service, entitySet, valueHelpService) => {
1887
- const entry = registered.get(service);
1888
- if (!entry) return void 0;
1889
- if (!entry.targets) return urlFor(service, entitySet, valueHelpService);
1890
- return entry.targets.get(targetKey(entitySet, valueHelpService))?.url;
1896
+ /** The URL of an entity set of a registered service: none for one its known targets lack. */
1897
+ const urlOfSet = (entry, entitySet, valueHelpService) => entry.targets ? entry.targets.get(targetKey(entitySet, valueHelpService))?.url : urlFor(entry.name, entitySet, valueHelpService);
1898
+ /**
1899
+ * The URL of the entity set a generated type is of: the URL of the registered target the type
1900
+ * is, else (a live service not connected yet) read from its metadata, `@db.space` naming the
1901
+ * service.
1902
+ */
1903
+ const urlOfType = (type) => {
1904
+ for (const entry of registered.values()) for (const target of entry.targets?.values() ?? []) if (target.type === type) return target.url;
1905
+ const annotated = (0, _atscript_typescript_utils.isAnnotatedType)(type);
1906
+ const set = annotated ? entitySetOf(type) : void 0;
1907
+ const label = `urlOf(${annotated && type.id || (set?.entitySet ?? "type")})`;
1908
+ if (!set) throw new Error(`${label}: not the type of an entity set (no @db.table); pass an entity set type of the generated .as module`);
1909
+ const { service, entitySet, valueHelpService } = set;
1910
+ const entry = typeof service === "string" ? registered.get(service) : void 0;
1911
+ if (!entry) throw new Error(`${label}: the type is of service "${String(service)}", which this factory does not register (registered: ${[...registered.keys()].map((name) => `"${name}"`).join(", ")})`);
1912
+ const url = urlOfSet(entry, entitySet, valueHelpService);
1913
+ if (url === void 0) {
1914
+ const where = valueHelpService === void 0 ? "" : ` (value-help service "${valueHelpService}")`;
1915
+ throw new Error(`${label}: service "${entry.name}" has no entity set "${entitySet}"${where}; the live service may have dropped it`);
1916
+ }
1917
+ return url;
1891
1918
  };
1919
+ factory.urlOf = ((serviceOrType, entitySet, valueHelpService) => {
1920
+ if (typeof serviceOrType !== "string") return urlOfType(serviceOrType);
1921
+ const entry = registered.get(serviceOrType);
1922
+ return entry && urlOfSet(entry, entitySet, valueHelpService);
1923
+ });
1924
+ factory.client = ((type, callOptions) => factory(urlOfType(type), callOptions));
1892
1925
  factory.resolve = (url) => {
1893
1926
  const found = find(url);
1894
1927
  if (!found) return void 0;
@@ -1902,25 +1935,18 @@ function createODataClientFactory(options) {
1902
1935
  valueHelpService
1903
1936
  };
1904
1937
  };
1905
- factory.call = (async (operation, input = {}, opts) => {
1906
- let service;
1907
- if (typeof operation === "string") {
1908
- if (registered.size !== 1) throw new Error(`call("${operation}"): ${registered.size} services are registered; name one: { service, operation }`);
1909
- service = [...registered.keys()][0];
1910
- } else service = operation.service;
1938
+ /**
1939
+ * Calls an operation of a registered service: by its handle of `ops`, which names its form
1940
+ * (`call`), or by its name and binding (`callByName`).
1941
+ */
1942
+ const callOn = async (service, named, input = {}, opts) => {
1911
1943
  const entry = registered.get(service);
1912
1944
  if (!entry) throw new Error(`No service "${service}" is registered`);
1913
1945
  await connected(entry);
1914
1946
  const connection = entry.connection;
1915
- const { params, extra, row, entitySet } = input;
1947
+ const { params, extra, row } = input;
1916
1948
  const run = {
1917
- ...typeof operation === "string" ? {
1918
- operation,
1919
- entitySet
1920
- } : "paramsType" in operation ? { operation } : {
1921
- operation: operation.operation,
1922
- entitySet: operation.entitySet
1923
- },
1949
+ ...named,
1924
1950
  ...row ? { rows: [row] } : {},
1925
1951
  ...params ? { params } : {},
1926
1952
  ...extra ? { extra } : {}
@@ -1942,7 +1968,30 @@ function createODataClientFactory(options) {
1942
1968
  parameters
1943
1969
  });
1944
1970
  }
1971
+ };
1972
+ factory.call = ((operation, input, opts) => {
1973
+ if (typeof operation !== "object" || operation === null || !("paramsType" in operation)) {
1974
+ const name = typeof operation === "string" ? operation : JSON.stringify(operation);
1975
+ return Promise.reject(/* @__PURE__ */ new Error(`call(${name}): pass the operation's handle of the model module's \`ops\`; call an operation only the live service has with callByName()`));
1976
+ }
1977
+ const handle = operation;
1978
+ return callOn(handle.service, { operation: handle }, input, opts);
1945
1979
  });
1980
+ factory.callByName = async (operation, input, opts) => {
1981
+ if (typeof operation !== "string") {
1982
+ const { service, operation: name, entitySet } = operation;
1983
+ return callOn(service, {
1984
+ operation: name,
1985
+ entitySet
1986
+ }, input, opts);
1987
+ }
1988
+ if (registered.size !== 1) throw new Error(`callByName("${operation}"): ${registered.size} services are registered; name one: { service, operation }`);
1989
+ const [service] = registered.keys();
1990
+ return callOn(service, {
1991
+ operation,
1992
+ entitySet: input?.entitySet
1993
+ }, input, opts);
1994
+ };
1946
1995
  factory.createBatch = (service) => {
1947
1996
  if (service === void 0 && registered.size !== 1) throw new Error(`createBatch(): ${registered.size} services are registered; name the one the batch is of`);
1948
1997
  const name = service ?? [...registered.keys()][0];
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AtscriptClientShape, ClientError, ClientLike, ClientOptions, MetaResponse, ServerError, TClientCallOptions, TDbActionInfo } from "@atscript/db-client";
2
- import { ODataCallAnswer, ODataCallInit, ODataCallInput, ODataConnection, ODataMessageContext, ODataOperationConnection, ODataOperationRef, ODataService, ODataSourceOptions, ProtocolContext, ServiceModel } from "@atscript/odata";
2
+ import { ODataCallAnswer, ODataCallInit, ODataCallInput, ODataConnection, ODataMessageContext, ODataOperationConnection, ODataOperationRef, ODataService, ODataSourceOptions, ODataTypedCallInput, ProtocolContext, ServiceModel } from "@atscript/odata";
3
3
  import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
4
4
 
5
5
  //#region src/batch.d.ts
@@ -282,7 +282,7 @@ interface ODataOperationCallOptions {
282
282
  */
283
283
  locale?: string;
284
284
  }
285
- /** What an untyped call of an operation takes. */
285
+ /** What `callByName` (an untyped call of an operation) takes. */
286
286
  interface ODataUntypedCallInput extends ODataCallInput<Record<string, unknown>, Record<string, unknown>> {
287
287
  /** With a name, a bound operation: the entity set it is called on. */
288
288
  entitySet?: string;
@@ -383,7 +383,32 @@ interface ODataClientFactoryOptions {
383
383
  * clients take {@link ODataClientCallOptions}; a fallback client ignores `batch`.
384
384
  */
385
385
  interface ODataClientFactory {
386
- (url: string, options?: ODataClientFactoryCallOptions): ClientLike<AtscriptClientShape, Record<never, never>, ODataClientCallOptions>;
386
+ /**
387
+ * The client of a URL: an {@link ODataClient} for an entity set of a registered service, the
388
+ * fallback's client for any other. Generic like atscript-ui's `TypedClientFactory`:
389
+ * `odata<typeof sales.Order>(url)` is typed by the model, which nothing checks at runtime.
390
+ * Prefer {@link ODataClientFactory.client}, which takes the URL from the type.
391
+ */
392
+ <T extends AtscriptClientShape = AtscriptClientShape>(url: string, options?: ODataClientFactoryCallOptions): ClientLike<T, Record<never, never>, ODataClientCallOptions>;
393
+ /**
394
+ * The client of the entity set a generated type is of (`odata.client(sales.Order)`), typed by
395
+ * it: its URL is {@link ODataClientFactory.urlOf} the type. A type of a value-help service
396
+ * (`@odata.valueHelpService`) gets the client of that service's entity set.
397
+ *
398
+ * @throws Error for a type that is no entity set of a registered service (see `urlOf`).
399
+ */
400
+ client<T extends TAtscriptAnnotatedType & AtscriptClientShape>(type: T, options?: ODataClientFactoryCallOptions): ODataClient<T>;
401
+ /**
402
+ * The URL of the entity set a generated type is of: the one the factory serves it at, under the
403
+ * name of the registration that holds it; for a live service not connected yet, read from its
404
+ * own metadata: the service (`@db.space`), the entity set (`@db.table`) and, for a type of a
405
+ * value-help service, `@odata.valueHelpService`. What atscript-ui components take as `url`.
406
+ *
407
+ * @throws Error for a type that is no entity set type (no `@db.table`), one of a service this
408
+ * factory does not register, and, once its service is connected, one of an entity set the live
409
+ * service does not have.
410
+ */
411
+ urlOf(type: TAtscriptAnnotatedType): string;
387
412
  /**
388
413
  * The URL of an entity set of a registered service, or of one of its value-help services
389
414
  * (`@odata.valueHelpService`), or `undefined` when it is not served. Before a live service is
@@ -410,7 +435,10 @@ interface ODataClientFactory {
410
435
  /**
411
436
  * Calls an operation of a registered service, typed by its handle of the model module's `ops`:
412
437
  * `call(ops.GetPrice, { params })`, on rows `call(ops.OrderSet.ReleaseOrder, { row, params })`
413
- * (the row's key, and its `__etag` as read). The service is connected first. The operation is
438
+ * (the row's key, and its `__etag` as read). The handle alone types the call: `params` is
439
+ * required when the operation has a required parameter, `row` when it is called on rows. An
440
+ * operation only the live service has is called with {@link ODataClientFactory.callByName}.
441
+ * The service is connected first. The operation is
414
442
  * found in the live service by the handle's name and binding, and its parameters are validated
415
443
  * against its live `<Name>Params` type: a handle of an operation the live service no longer has
416
444
  * fails (`ClientError` 400, `UNKNOWN_OPERATION` on the entry). `extra` (OData v2 only) sends
@@ -425,17 +453,18 @@ interface ODataClientFactory {
425
453
  * `opts.locale` is sent as `Accept-Language`, as the clients send atscript-ui's locale (the
426
454
  * registration's `language` is the `$metadata`'s only).
427
455
  */
428
- call<P, R, K>(operation: ODataOperationRef<P, R, K>, input?: ODataCallInput<P, K>, opts?: ODataOperationCallOptions): Promise<ODataCallAnswer<R>>;
456
+ call<P, R, K>(operation: ODataOperationRef<P, R, K>, ...input: CallArgs<NoInfer<ODataTypedCallInput<P, K>>>): Promise<ODataCallAnswer<R>>;
429
457
  /**
430
458
  * Calls an operation by its name, untyped: one the build does not know (only the live service
431
459
  * has it) or one of a registration without a model. A name (`"Ns.release"` for a v4 bound
432
460
  * operation, the import name otherwise) names an operation of the factory's only service, the
433
461
  * entity set of a bound one in `input.entitySet`; with several services, pass
434
- * `{ service, operation, entitySet? }`.
462
+ * `{ service, operation, entitySet? }`. An operation of the build is called with `call` and its
463
+ * handle, which types it.
435
464
  *
436
465
  * @throws Error for a name when several services are registered, and for a service that is not.
437
466
  */
438
- call(operation: string | ODataOperationName, input?: ODataUntypedCallInput, opts?: ODataOperationCallOptions): Promise<ODataCallAnswer>;
467
+ callByName(operation: string | ODataOperationName, input?: ODataUntypedCallInput, opts?: ODataOperationCallOptions): Promise<ODataCallAnswer>;
439
468
  /**
440
469
  * A new batch of a registered service (the name may be left out when only one is registered):
441
470
  * the calls of its clients given `{ batch }`, or `{ batch: batch.changeset() }` for writes
@@ -447,6 +476,11 @@ interface ODataClientFactory {
447
476
  */
448
477
  createBatch(service?: string): ODataClientBatch;
449
478
  }
479
+ /**
480
+ * What a typed `call` takes after the handle: the input `I`, required when the operation has a
481
+ * required parameter or is called on rows, and the call's options.
482
+ */
483
+ type CallArgs<I> = {} extends I ? [input?: I, opts?: ODataOperationCallOptions] : [input: I, opts?: ODataOperationCallOptions];
450
484
  /**
451
485
  * Creates the client factory atscript-ui routes through. A URL of the form
452
486
  * `<prefix>/<serviceName>/<EntitySet>` gets a {@link ODataClient} for that entity set; every other
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AtscriptClientShape, ClientError, ClientLike, ClientOptions, MetaResponse, ServerError, TClientCallOptions, TDbActionInfo, VersionMismatchError } from "@atscript/db-client";
2
- import { ODataCallAnswer, ODataCallInit, ODataCallInput, ODataConnection, ODataMessageContext, ODataOperationConnection, ODataOperationRef, ODataService, ODataSourceOptions, ProtocolContext, ServiceModel } from "@atscript/odata";
2
+ import { ODataCallAnswer, ODataCallInit, ODataCallInput, ODataConnection, ODataMessageContext, ODataOperationConnection, ODataOperationRef, ODataService, ODataSourceOptions, ODataTypedCallInput, ProtocolContext, ServiceModel } from "@atscript/odata";
3
3
  import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
4
4
 
5
5
  //#region src/batch.d.ts
@@ -282,7 +282,7 @@ interface ODataOperationCallOptions {
282
282
  */
283
283
  locale?: string;
284
284
  }
285
- /** What an untyped call of an operation takes. */
285
+ /** What `callByName` (an untyped call of an operation) takes. */
286
286
  interface ODataUntypedCallInput extends ODataCallInput<Record<string, unknown>, Record<string, unknown>> {
287
287
  /** With a name, a bound operation: the entity set it is called on. */
288
288
  entitySet?: string;
@@ -383,7 +383,32 @@ interface ODataClientFactoryOptions {
383
383
  * clients take {@link ODataClientCallOptions}; a fallback client ignores `batch`.
384
384
  */
385
385
  interface ODataClientFactory {
386
- (url: string, options?: ODataClientFactoryCallOptions): ClientLike<AtscriptClientShape, Record<never, never>, ODataClientCallOptions>;
386
+ /**
387
+ * The client of a URL: an {@link ODataClient} for an entity set of a registered service, the
388
+ * fallback's client for any other. Generic like atscript-ui's `TypedClientFactory`:
389
+ * `odata<typeof sales.Order>(url)` is typed by the model, which nothing checks at runtime.
390
+ * Prefer {@link ODataClientFactory.client}, which takes the URL from the type.
391
+ */
392
+ <T extends AtscriptClientShape = AtscriptClientShape>(url: string, options?: ODataClientFactoryCallOptions): ClientLike<T, Record<never, never>, ODataClientCallOptions>;
393
+ /**
394
+ * The client of the entity set a generated type is of (`odata.client(sales.Order)`), typed by
395
+ * it: its URL is {@link ODataClientFactory.urlOf} the type. A type of a value-help service
396
+ * (`@odata.valueHelpService`) gets the client of that service's entity set.
397
+ *
398
+ * @throws Error for a type that is no entity set of a registered service (see `urlOf`).
399
+ */
400
+ client<T extends TAtscriptAnnotatedType & AtscriptClientShape>(type: T, options?: ODataClientFactoryCallOptions): ODataClient<T>;
401
+ /**
402
+ * The URL of the entity set a generated type is of: the one the factory serves it at, under the
403
+ * name of the registration that holds it; for a live service not connected yet, read from its
404
+ * own metadata: the service (`@db.space`), the entity set (`@db.table`) and, for a type of a
405
+ * value-help service, `@odata.valueHelpService`. What atscript-ui components take as `url`.
406
+ *
407
+ * @throws Error for a type that is no entity set type (no `@db.table`), one of a service this
408
+ * factory does not register, and, once its service is connected, one of an entity set the live
409
+ * service does not have.
410
+ */
411
+ urlOf(type: TAtscriptAnnotatedType): string;
387
412
  /**
388
413
  * The URL of an entity set of a registered service, or of one of its value-help services
389
414
  * (`@odata.valueHelpService`), or `undefined` when it is not served. Before a live service is
@@ -410,7 +435,10 @@ interface ODataClientFactory {
410
435
  /**
411
436
  * Calls an operation of a registered service, typed by its handle of the model module's `ops`:
412
437
  * `call(ops.GetPrice, { params })`, on rows `call(ops.OrderSet.ReleaseOrder, { row, params })`
413
- * (the row's key, and its `__etag` as read). The service is connected first. The operation is
438
+ * (the row's key, and its `__etag` as read). The handle alone types the call: `params` is
439
+ * required when the operation has a required parameter, `row` when it is called on rows. An
440
+ * operation only the live service has is called with {@link ODataClientFactory.callByName}.
441
+ * The service is connected first. The operation is
414
442
  * found in the live service by the handle's name and binding, and its parameters are validated
415
443
  * against its live `<Name>Params` type: a handle of an operation the live service no longer has
416
444
  * fails (`ClientError` 400, `UNKNOWN_OPERATION` on the entry). `extra` (OData v2 only) sends
@@ -425,17 +453,18 @@ interface ODataClientFactory {
425
453
  * `opts.locale` is sent as `Accept-Language`, as the clients send atscript-ui's locale (the
426
454
  * registration's `language` is the `$metadata`'s only).
427
455
  */
428
- call<P, R, K>(operation: ODataOperationRef<P, R, K>, input?: ODataCallInput<P, K>, opts?: ODataOperationCallOptions): Promise<ODataCallAnswer<R>>;
456
+ call<P, R, K>(operation: ODataOperationRef<P, R, K>, ...input: CallArgs<NoInfer<ODataTypedCallInput<P, K>>>): Promise<ODataCallAnswer<R>>;
429
457
  /**
430
458
  * Calls an operation by its name, untyped: one the build does not know (only the live service
431
459
  * has it) or one of a registration without a model. A name (`"Ns.release"` for a v4 bound
432
460
  * operation, the import name otherwise) names an operation of the factory's only service, the
433
461
  * entity set of a bound one in `input.entitySet`; with several services, pass
434
- * `{ service, operation, entitySet? }`.
462
+ * `{ service, operation, entitySet? }`. An operation of the build is called with `call` and its
463
+ * handle, which types it.
435
464
  *
436
465
  * @throws Error for a name when several services are registered, and for a service that is not.
437
466
  */
438
- call(operation: string | ODataOperationName, input?: ODataUntypedCallInput, opts?: ODataOperationCallOptions): Promise<ODataCallAnswer>;
467
+ callByName(operation: string | ODataOperationName, input?: ODataUntypedCallInput, opts?: ODataOperationCallOptions): Promise<ODataCallAnswer>;
439
468
  /**
440
469
  * A new batch of a registered service (the name may be left out when only one is registered):
441
470
  * the calls of its clients given `{ batch }`, or `{ batch: batch.changeset() }` for writes
@@ -447,6 +476,11 @@ interface ODataClientFactory {
447
476
  */
448
477
  createBatch(service?: string): ODataClientBatch;
449
478
  }
479
+ /**
480
+ * What a typed `call` takes after the handle: the input `I`, required when the operation has a
481
+ * required parameter or is called on rows, and the call's options.
482
+ */
483
+ type CallArgs<I> = {} extends I ? [input?: I, opts?: ODataOperationCallOptions] : [input: I, opts?: ODataOperationCallOptions];
450
484
  /**
451
485
  * Creates the client factory atscript-ui routes through. A URL of the form
452
486
  * `<prefix>/<serviceName>/<EntitySet>` gets a {@link ODataClient} for that entity set; every other
package/dist/index.mjs CHANGED
@@ -1683,6 +1683,18 @@ const ODataClient = ODataEntityClient;
1683
1683
  /** The URL prefix OData entity sets are addressed under unless the factory is given another. */
1684
1684
  const DEFAULT_ODATA_PREFIX = "/odata";
1685
1685
  const HTTP_PATH = "db.http.path";
1686
+ /** The entity set a generated type is of, read from its metadata: none for a type of no set. */
1687
+ function entitySetOf(type) {
1688
+ const metadata = type.metadata;
1689
+ const entitySet = metadata.get("db.table");
1690
+ if (typeof entitySet !== "string") return void 0;
1691
+ const declared = metadata.get("odata.valueHelpService");
1692
+ return {
1693
+ service: metadata.get("db.space"),
1694
+ entitySet,
1695
+ valueHelpService: typeof declared === "string" ? declared : void 0
1696
+ };
1697
+ }
1686
1698
  /** The key of a target among those of its registration: the URL segments after the name. */
1687
1699
  function targetKey(entitySet, valueHelpService) {
1688
1700
  return valueHelpService === void 0 ? entitySet : `${valueHelpService}\u0000${entitySet}`;
@@ -1696,11 +1708,9 @@ function entitySetTypes(name, service, source) {
1696
1708
  const types = /* @__PURE__ */ new Map();
1697
1709
  for (const candidate of Object.values(source)) {
1698
1710
  if (!isAnnotatedType(candidate)) continue;
1699
- const metadata = candidate.metadata;
1700
- const entitySet = metadata.get("db.table");
1701
- if (typeof entitySet !== "string") continue;
1702
- const declared = metadata.get("odata.valueHelpService");
1703
- const valueHelpService = typeof declared === "string" ? declared : void 0;
1711
+ const set = entitySetOf(candidate);
1712
+ if (!set) continue;
1713
+ const { entitySet, valueHelpService } = set;
1704
1714
  const engine = valueHelpService === void 0 ? service : service.valueHelpService(valueHelpService);
1705
1715
  if (!engine) throw new Error(`Service "${name}": the type "${candidate.id ?? entitySet}" is of value-help service "${valueHelpService}", which the service model does not have. Create the service from the model module generated with the types, which holds its value-help services.`);
1706
1716
  if (!Object.hasOwn(engine.model.entitySets, entitySet)) throw new Error(`Service "${name}": the type "${candidate.id ?? entitySet}" is for entity set "${entitySet}", which the service model does not have. Were the types generated from another service?`);
@@ -1882,12 +1892,35 @@ function createODataClientFactory(options) {
1882
1892
  }
1883
1893
  });
1884
1894
  });
1885
- factory.urlOf = (service, entitySet, valueHelpService) => {
1886
- const entry = registered.get(service);
1887
- if (!entry) return void 0;
1888
- if (!entry.targets) return urlFor(service, entitySet, valueHelpService);
1889
- return entry.targets.get(targetKey(entitySet, valueHelpService))?.url;
1895
+ /** The URL of an entity set of a registered service: none for one its known targets lack. */
1896
+ const urlOfSet = (entry, entitySet, valueHelpService) => entry.targets ? entry.targets.get(targetKey(entitySet, valueHelpService))?.url : urlFor(entry.name, entitySet, valueHelpService);
1897
+ /**
1898
+ * The URL of the entity set a generated type is of: the URL of the registered target the type
1899
+ * is, else (a live service not connected yet) read from its metadata, `@db.space` naming the
1900
+ * service.
1901
+ */
1902
+ const urlOfType = (type) => {
1903
+ for (const entry of registered.values()) for (const target of entry.targets?.values() ?? []) if (target.type === type) return target.url;
1904
+ const annotated = isAnnotatedType(type);
1905
+ const set = annotated ? entitySetOf(type) : void 0;
1906
+ const label = `urlOf(${annotated && type.id || (set?.entitySet ?? "type")})`;
1907
+ if (!set) throw new Error(`${label}: not the type of an entity set (no @db.table); pass an entity set type of the generated .as module`);
1908
+ const { service, entitySet, valueHelpService } = set;
1909
+ const entry = typeof service === "string" ? registered.get(service) : void 0;
1910
+ if (!entry) throw new Error(`${label}: the type is of service "${String(service)}", which this factory does not register (registered: ${[...registered.keys()].map((name) => `"${name}"`).join(", ")})`);
1911
+ const url = urlOfSet(entry, entitySet, valueHelpService);
1912
+ if (url === void 0) {
1913
+ const where = valueHelpService === void 0 ? "" : ` (value-help service "${valueHelpService}")`;
1914
+ throw new Error(`${label}: service "${entry.name}" has no entity set "${entitySet}"${where}; the live service may have dropped it`);
1915
+ }
1916
+ return url;
1890
1917
  };
1918
+ factory.urlOf = ((serviceOrType, entitySet, valueHelpService) => {
1919
+ if (typeof serviceOrType !== "string") return urlOfType(serviceOrType);
1920
+ const entry = registered.get(serviceOrType);
1921
+ return entry && urlOfSet(entry, entitySet, valueHelpService);
1922
+ });
1923
+ factory.client = ((type, callOptions) => factory(urlOfType(type), callOptions));
1891
1924
  factory.resolve = (url) => {
1892
1925
  const found = find(url);
1893
1926
  if (!found) return void 0;
@@ -1901,25 +1934,18 @@ function createODataClientFactory(options) {
1901
1934
  valueHelpService
1902
1935
  };
1903
1936
  };
1904
- factory.call = (async (operation, input = {}, opts) => {
1905
- let service;
1906
- if (typeof operation === "string") {
1907
- if (registered.size !== 1) throw new Error(`call("${operation}"): ${registered.size} services are registered; name one: { service, operation }`);
1908
- service = [...registered.keys()][0];
1909
- } else service = operation.service;
1937
+ /**
1938
+ * Calls an operation of a registered service: by its handle of `ops`, which names its form
1939
+ * (`call`), or by its name and binding (`callByName`).
1940
+ */
1941
+ const callOn = async (service, named, input = {}, opts) => {
1910
1942
  const entry = registered.get(service);
1911
1943
  if (!entry) throw new Error(`No service "${service}" is registered`);
1912
1944
  await connected(entry);
1913
1945
  const connection = entry.connection;
1914
- const { params, extra, row, entitySet } = input;
1946
+ const { params, extra, row } = input;
1915
1947
  const run = {
1916
- ...typeof operation === "string" ? {
1917
- operation,
1918
- entitySet
1919
- } : "paramsType" in operation ? { operation } : {
1920
- operation: operation.operation,
1921
- entitySet: operation.entitySet
1922
- },
1948
+ ...named,
1923
1949
  ...row ? { rows: [row] } : {},
1924
1950
  ...params ? { params } : {},
1925
1951
  ...extra ? { extra } : {}
@@ -1941,7 +1967,30 @@ function createODataClientFactory(options) {
1941
1967
  parameters
1942
1968
  });
1943
1969
  }
1970
+ };
1971
+ factory.call = ((operation, input, opts) => {
1972
+ if (typeof operation !== "object" || operation === null || !("paramsType" in operation)) {
1973
+ const name = typeof operation === "string" ? operation : JSON.stringify(operation);
1974
+ return Promise.reject(/* @__PURE__ */ new Error(`call(${name}): pass the operation's handle of the model module's \`ops\`; call an operation only the live service has with callByName()`));
1975
+ }
1976
+ const handle = operation;
1977
+ return callOn(handle.service, { operation: handle }, input, opts);
1944
1978
  });
1979
+ factory.callByName = async (operation, input, opts) => {
1980
+ if (typeof operation !== "string") {
1981
+ const { service, operation: name, entitySet } = operation;
1982
+ return callOn(service, {
1983
+ operation: name,
1984
+ entitySet
1985
+ }, input, opts);
1986
+ }
1987
+ if (registered.size !== 1) throw new Error(`callByName("${operation}"): ${registered.size} services are registered; name one: { service, operation }`);
1988
+ const [service] = registered.keys();
1989
+ return callOn(service, {
1990
+ operation,
1991
+ entitySet: input?.entitySet
1992
+ }, input, opts);
1993
+ };
1945
1994
  factory.createBatch = (service) => {
1946
1995
  if (service === void 0 && registered.size !== 1) throw new Error(`createBatch(): ${registered.size} services are registered; name the one the batch is of`);
1947
1996
  const name = service ?? [...registered.keys()][0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/odata-client",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Browser client that talks OData directly and looks like an Atscript DB client.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -37,28 +37,28 @@
37
37
  "access": "public"
38
38
  },
39
39
  "dependencies": {
40
- "@atscript/odata": "^0.0.2"
40
+ "@atscript/odata": "^0.0.4"
41
41
  },
42
42
  "devDependencies": {
43
- "@atscript/core": "^0.2.25",
44
- "@atscript/db": "^0.1.200",
45
- "@atscript/db-client": "^0.1.200",
46
- "@atscript/typescript": "^0.2.25",
47
- "@atscript/ui": "^0.1.195",
48
- "@atscript/ui-table": "^0.1.195",
43
+ "@atscript/core": "^0.2.28",
44
+ "@atscript/db": "^0.1.204",
45
+ "@atscript/db-client": "^0.1.204",
46
+ "@atscript/typescript": "^0.2.28",
47
+ "@atscript/ui": "^0.1.202",
48
+ "@atscript/ui-table": "^0.1.202",
49
49
  "@uniqu/core": "^0.2.2",
50
- "unplugin-atscript": "^0.2.25"
50
+ "unplugin-atscript": "^0.2.28"
51
51
  },
52
52
  "peerDependencies": {
53
- "@atscript/db": "^0.1.200",
54
- "@atscript/db-client": "^0.1.200",
55
- "@atscript/typescript": "^0.2.25",
53
+ "@atscript/db": "^0.1.204",
54
+ "@atscript/db-client": "^0.1.204",
55
+ "@atscript/typescript": "^0.2.28",
56
56
  "@uniqu/core": "^0.2.2"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "vp pack",
60
60
  "dev": "vp pack --watch",
61
- "test": "vp test",
61
+ "test": "vp test --typecheck",
62
62
  "check": "vp check"
63
63
  }
64
64
  }