@atscript/moost-odata 0.0.3 → 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
@@ -45,7 +45,7 @@ Typed at build time, live at runtime: the controllers are declared over the gene
45
45
 
46
46
  ```bash
47
47
  pnpm add @atscript/moost-odata @atscript/db-odata @atscript/odata \
48
- @atscript/moost-db @atscript/db @atscript/typescript @uniqu/core \
48
+ @atscript/moost-db @atscript/db @atscript/core @atscript/typescript @uniqu/core \
49
49
  moost @moostjs/event-http @wooksjs/event-core @wooksjs/event-http @wooksjs/http-body
50
50
  ```
51
51
 
@@ -0,0 +1,51 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _atscript_db_client = require("@atscript/db-client");
3
+ //#region src/client.ts
4
+ /**
5
+ * The browser's caller of a Moost server's operations controller, the leaf you declare over the
6
+ * generated operations base (`@Controller("api/sales/ops")`): `baseUrl` is that controller's
7
+ * path. It ships no OData and no server code, only `fetch`:
8
+ *
9
+ * ```ts
10
+ * import { operationsClient } from "@atscript/moost-odata/client";
11
+ * import { ops } from "./odata/sales.model.gen";
12
+ *
13
+ * const salesOps = operationsClient("/api/sales/ops");
14
+ * const { value } = await salesOps.call(ops.GetPrice, { Material: "M-100" });
15
+ * ```
16
+ *
17
+ * Importing `ops` costs the page the handles only: the bundler drops the model they sit next to.
18
+ */
19
+ function operationsClient(baseUrl, options = {}) {
20
+ const base = baseUrl.replace(/\/+$/, "");
21
+ return { async call(operation, params) {
22
+ if (operation.entitySet !== void 0) throw new Error(`operationsClient: "${operation.operation}" is bound to "${operation.entitySet}"; the operations controller serves unbound operations only, a bound one is a db action of its table's controller`);
23
+ const url = `${base}/${encodeURIComponent(operation.operation)}`;
24
+ const init = {
25
+ method: "POST",
26
+ headers: {
27
+ ...typeof options.headers === "function" ? await options.headers() : options.headers,
28
+ "Content-Type": "application/json"
29
+ },
30
+ body: JSON.stringify(params ?? {})
31
+ };
32
+ let res;
33
+ try {
34
+ res = await (options.fetch ?? fetch)(url, init);
35
+ } catch (cause) {
36
+ throw new _atscript_db_client.TransportError("POST", url, cause instanceof Error ? cause.message : String(cause), cause);
37
+ }
38
+ if (!res.ok) {
39
+ const body = await res.json().catch(() => ({
40
+ message: res.statusText || `HTTP ${res.status}`,
41
+ statusCode: res.status
42
+ }));
43
+ throw new _atscript_db_client.ClientError(res.status, body);
44
+ }
45
+ return res.json().catch((cause) => {
46
+ throw new _atscript_db_client.TransportError("POST", url, `HTTP ${res.status} body is not JSON`, cause);
47
+ });
48
+ } };
49
+ }
50
+ //#endregion
51
+ exports.operationsClient = operationsClient;
@@ -0,0 +1,43 @@
1
+ import { ClientOptions } from "@atscript/db-client";
2
+ import { ODataCallAnswer, ODataOperationRef } from "@atscript/odata";
3
+
4
+ //#region src/client.d.ts
5
+ /**
6
+ * Options of {@link operationsClient}: the `fetch` and `headers` of `@atscript/db-client`'s
7
+ * `ClientOptions`, as the page's tables have them.
8
+ */
9
+ type ODataOperationsClientOptions = Pick<ClientOptions, "fetch" | "headers">;
10
+ /** The caller of a Moost server's operations routes; see {@link operationsClient}. */
11
+ interface ODataOperationsClient {
12
+ /**
13
+ * Calls an unbound operation by its handle of the model module's `ops` (`ops.GetPrice`):
14
+ * `POST <baseUrl>/<operation>`, the parameters as the JSON body. The handle types the call:
15
+ * `params` is required when the operation has a required parameter, and the answer's `value`
16
+ * is its result. Answers what the route answers, `{ value, message?, messages?, details? }`.
17
+ *
18
+ * @throws `ClientError` of `@atscript/db-client` for an answer that is not `2xx` (its JSON body,
19
+ * the status text for a body that is none: a `400` with the parameters' entries, `404` for an
20
+ * operation the live service no longer has), `TransportError` when the server cannot be reached
21
+ * or its answer is no JSON, and `Error` for a handle of a bound operation (a db action of its
22
+ * table's controller: `client.action(name, ids, input)`).
23
+ */
24
+ call<P, R>(operation: ODataOperationRef<P, R>, ...params: {} extends P ? [params?: NoInfer<P>] : [params: NoInfer<P>]): Promise<ODataCallAnswer<R>>;
25
+ }
26
+ /**
27
+ * The browser's caller of a Moost server's operations controller, the leaf you declare over the
28
+ * generated operations base (`@Controller("api/sales/ops")`): `baseUrl` is that controller's
29
+ * path. It ships no OData and no server code, only `fetch`:
30
+ *
31
+ * ```ts
32
+ * import { operationsClient } from "@atscript/moost-odata/client";
33
+ * import { ops } from "./odata/sales.model.gen";
34
+ *
35
+ * const salesOps = operationsClient("/api/sales/ops");
36
+ * const { value } = await salesOps.call(ops.GetPrice, { Material: "M-100" });
37
+ * ```
38
+ *
39
+ * Importing `ops` costs the page the handles only: the bundler drops the model they sit next to.
40
+ */
41
+ declare function operationsClient(baseUrl: string, options?: ODataOperationsClientOptions): ODataOperationsClient;
42
+ //#endregion
43
+ export { ODataOperationsClient, ODataOperationsClientOptions, operationsClient };
@@ -0,0 +1,43 @@
1
+ import { ODataCallAnswer, ODataOperationRef } from "@atscript/odata";
2
+ import { ClientOptions } from "@atscript/db-client";
3
+
4
+ //#region src/client.d.ts
5
+ /**
6
+ * Options of {@link operationsClient}: the `fetch` and `headers` of `@atscript/db-client`'s
7
+ * `ClientOptions`, as the page's tables have them.
8
+ */
9
+ type ODataOperationsClientOptions = Pick<ClientOptions, "fetch" | "headers">;
10
+ /** The caller of a Moost server's operations routes; see {@link operationsClient}. */
11
+ interface ODataOperationsClient {
12
+ /**
13
+ * Calls an unbound operation by its handle of the model module's `ops` (`ops.GetPrice`):
14
+ * `POST <baseUrl>/<operation>`, the parameters as the JSON body. The handle types the call:
15
+ * `params` is required when the operation has a required parameter, and the answer's `value`
16
+ * is its result. Answers what the route answers, `{ value, message?, messages?, details? }`.
17
+ *
18
+ * @throws `ClientError` of `@atscript/db-client` for an answer that is not `2xx` (its JSON body,
19
+ * the status text for a body that is none: a `400` with the parameters' entries, `404` for an
20
+ * operation the live service no longer has), `TransportError` when the server cannot be reached
21
+ * or its answer is no JSON, and `Error` for a handle of a bound operation (a db action of its
22
+ * table's controller: `client.action(name, ids, input)`).
23
+ */
24
+ call<P, R>(operation: ODataOperationRef<P, R>, ...params: {} extends P ? [params?: NoInfer<P>] : [params: NoInfer<P>]): Promise<ODataCallAnswer<R>>;
25
+ }
26
+ /**
27
+ * The browser's caller of a Moost server's operations controller, the leaf you declare over the
28
+ * generated operations base (`@Controller("api/sales/ops")`): `baseUrl` is that controller's
29
+ * path. It ships no OData and no server code, only `fetch`:
30
+ *
31
+ * ```ts
32
+ * import { operationsClient } from "@atscript/moost-odata/client";
33
+ * import { ops } from "./odata/sales.model.gen";
34
+ *
35
+ * const salesOps = operationsClient("/api/sales/ops");
36
+ * const { value } = await salesOps.call(ops.GetPrice, { Material: "M-100" });
37
+ * ```
38
+ *
39
+ * Importing `ops` costs the page the handles only: the bundler drops the model they sit next to.
40
+ */
41
+ declare function operationsClient(baseUrl: string, options?: ODataOperationsClientOptions): ODataOperationsClient;
42
+ //#endregion
43
+ export { ODataOperationsClient, ODataOperationsClientOptions, operationsClient };
@@ -0,0 +1,50 @@
1
+ import { ClientError, TransportError } from "@atscript/db-client";
2
+ //#region src/client.ts
3
+ /**
4
+ * The browser's caller of a Moost server's operations controller, the leaf you declare over the
5
+ * generated operations base (`@Controller("api/sales/ops")`): `baseUrl` is that controller's
6
+ * path. It ships no OData and no server code, only `fetch`:
7
+ *
8
+ * ```ts
9
+ * import { operationsClient } from "@atscript/moost-odata/client";
10
+ * import { ops } from "./odata/sales.model.gen";
11
+ *
12
+ * const salesOps = operationsClient("/api/sales/ops");
13
+ * const { value } = await salesOps.call(ops.GetPrice, { Material: "M-100" });
14
+ * ```
15
+ *
16
+ * Importing `ops` costs the page the handles only: the bundler drops the model they sit next to.
17
+ */
18
+ function operationsClient(baseUrl, options = {}) {
19
+ const base = baseUrl.replace(/\/+$/, "");
20
+ return { async call(operation, params) {
21
+ if (operation.entitySet !== void 0) throw new Error(`operationsClient: "${operation.operation}" is bound to "${operation.entitySet}"; the operations controller serves unbound operations only, a bound one is a db action of its table's controller`);
22
+ const url = `${base}/${encodeURIComponent(operation.operation)}`;
23
+ const init = {
24
+ method: "POST",
25
+ headers: {
26
+ ...typeof options.headers === "function" ? await options.headers() : options.headers,
27
+ "Content-Type": "application/json"
28
+ },
29
+ body: JSON.stringify(params ?? {})
30
+ };
31
+ let res;
32
+ try {
33
+ res = await (options.fetch ?? fetch)(url, init);
34
+ } catch (cause) {
35
+ throw new TransportError("POST", url, cause instanceof Error ? cause.message : String(cause), cause);
36
+ }
37
+ if (!res.ok) {
38
+ const body = await res.json().catch(() => ({
39
+ message: res.statusText || `HTTP ${res.status}`,
40
+ statusCode: res.status
41
+ }));
42
+ throw new ClientError(res.status, body);
43
+ }
44
+ return res.json().catch((cause) => {
45
+ throw new TransportError("POST", url, `HTTP ${res.status} body is not JSON`, cause);
46
+ });
47
+ } };
48
+ }
49
+ //#endregion
50
+ export { operationsClient };
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { EventContext } from "@wooksjs/event-core";
2
1
  import { ODataCallAnswer, ODataCallInit, ODataExtraParams, ODataOperationGrouping, ODataOperationRef, ODataRowsAnswer } from "@atscript/odata";
2
+ import { EventContext } from "@wooksjs/event-core";
3
3
  import { AtscriptDbReadable, FilterExpr, TCrudPermissions } from "@atscript/db";
4
4
  import { ODataSpace } from "@atscript/db-odata";
5
5
  import { AsDbController, AsDbReadableController, TDbActionInfo, TDbRequestContext } from "@atscript/moost-db";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/moost-odata",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Moost integration for serving OData services as Atscript DB controllers.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -30,21 +30,31 @@
30
30
  "default": "./dist/index.cjs"
31
31
  }
32
32
  },
33
+ "./client": {
34
+ "import": {
35
+ "types": "./dist/client.d.mts",
36
+ "default": "./dist/client.mjs"
37
+ },
38
+ "require": {
39
+ "types": "./dist/client.d.cts",
40
+ "default": "./dist/client.cjs"
41
+ }
42
+ },
33
43
  "./package.json": "./package.json"
34
44
  },
35
45
  "publishConfig": {
36
46
  "access": "public"
37
47
  },
38
48
  "dependencies": {
39
- "@atscript/db-odata": "^0.0.3",
40
- "@atscript/odata": "^0.0.3"
49
+ "@atscript/db-odata": "^0.0.4",
50
+ "@atscript/odata": "^0.0.4"
41
51
  },
42
52
  "devDependencies": {
43
- "@atscript/core": "^0.2.26",
44
- "@atscript/db": "^0.1.201",
45
- "@atscript/db-client": "^0.1.201",
46
- "@atscript/moost-db": "^0.1.201",
47
- "@atscript/typescript": "^0.2.26",
53
+ "@atscript/core": "^0.2.28",
54
+ "@atscript/db": "^0.1.204",
55
+ "@atscript/db-client": "^0.1.204",
56
+ "@atscript/moost-db": "^0.1.204",
57
+ "@atscript/typescript": "^0.2.28",
48
58
  "@moostjs/event-http": "^0.7.13",
49
59
  "@uniqu/core": "^0.2.2",
50
60
  "@wooksjs/event-core": "^0.8.8",
@@ -52,12 +62,13 @@
52
62
  "@wooksjs/http-body": "^0.8.8",
53
63
  "moost": "^0.7.13",
54
64
  "typescript": "^5",
55
- "unplugin-atscript": "^0.2.26"
65
+ "unplugin-atscript": "^0.2.28"
56
66
  },
57
67
  "peerDependencies": {
58
- "@atscript/db": "^0.1.201",
59
- "@atscript/moost-db": "^0.1.201",
60
- "@atscript/typescript": "^0.2.26",
68
+ "@atscript/db": "^0.1.204",
69
+ "@atscript/db-client": "^0.1.204",
70
+ "@atscript/moost-db": "^0.1.204",
71
+ "@atscript/typescript": "^0.2.28",
61
72
  "@moostjs/event-http": "^0.7.13",
62
73
  "@uniqu/core": "^0.2.2",
63
74
  "@wooksjs/event-core": "^0.8.8",
@@ -65,10 +76,15 @@
65
76
  "@wooksjs/http-body": "^0.8.8",
66
77
  "moost": "^0.7.13"
67
78
  },
79
+ "peerDependenciesMeta": {
80
+ "@atscript/db-client": {
81
+ "optional": true
82
+ }
83
+ },
68
84
  "scripts": {
69
85
  "build": "vp pack",
70
86
  "dev": "vp pack --watch",
71
- "test": "vp test",
87
+ "test": "vp test --typecheck",
72
88
  "check": "vp check"
73
89
  }
74
90
  }