@zerolith/client 0.0.0-canary.20261102.g8292c77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # @zerolith/client
2
+
3
+ A typed HTTP client over a neutral contract. `zerolith-client` reads an OpenAPI 3.x document and writes one TypeScript module: a type per component schema, an `Api` interface with one entry per operation, and the `routes` table the client runs on. `createClient` takes that table and checks every call against it: the operation name, its path and query parameters, its body, and what it resolves to.
4
+
5
+ ```
6
+ npx zerolith-client openapi.json --out src/api.ts
7
+ ```
8
+
9
+ ```ts
10
+ import { createClient } from "@zerolith/client";
11
+ import { type Api, routes } from "./api.ts";
12
+
13
+ const api = createClient<Api>(routes, { baseUrl: "/api" });
14
+ const note = await api.call("getNote", { params: { id } });
15
+ const notes = api.query("listNotes", () => ({ query: { q: search.get() } }));
16
+ ```
17
+
18
+ A response outside 2xx rejects with a `ProblemError` carrying the RFC 9457 problem the server sent, or one built from the status line. `query` wraps an operation in a resource that calls again when the signals it reads change. A custom `transport` answers requests in process, for tests or an Electron bridge, and the client never assumes which server produced the document.
19
+
20
+ The guide is on the [docs site](https://zerolith.dev/guide/client/).
21
+
22
+ Status: alpha. Install with `npm install @zerolith/client@alpha`; the API may change before 1.0.
23
+
24
+ ## Documentation
25
+
26
+ - The [typed client guide](https://zerolith.dev/guide/client/) explains it with examples that run as tests.
27
+ - The [reference](https://zerolith.dev/reference/zerolith-client/) lists every export, generated from the source.
28
+ - New to zerolith? The [tutorial](https://zerolith.dev/learn/) builds an app step by step, and the [playground](https://zerolith.dev/playground/) runs code in the browser.
29
+
30
+ ## License
31
+
32
+ Apache-2.0. See `LICENSE`.
package/dist/cli.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ /** Runs the command with the given arguments and returns the exit code. */
3
+ export declare function main(argv: readonly string[]): Promise<number>;
4
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAoDA,2EAA2E;AAC3E,wBAAsB,IAAI,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAoCnE"}
package/dist/cli.js ADDED
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * `zerolith-client <openapi.json> [--out <file.ts>]`: writes the contract
4
+ * module for an OpenAPI document to a file, or to standard output.
5
+ */
6
+ import { readFile, writeFile } from "node:fs/promises";
7
+ import { realpathSync } from "node:fs";
8
+ import process from "node:process";
9
+ import { pathToFileURL } from "node:url";
10
+ import { generateClient } from "./generate.js";
11
+ const USAGE = "usage: zerolith-client <openapi.json> [--out <file.ts>] [--name <Api>]\n";
12
+ function parse(argv) {
13
+ const parsed = { input: undefined, out: undefined, name: undefined };
14
+ for (let index = 0; index < argv.length; index++) {
15
+ const argument = argv[index] ?? "";
16
+ if (argument === "--out" || argument === "--name") {
17
+ const value = argv[index + 1];
18
+ if (value === undefined) {
19
+ throw new Error(`${argument} needs a value`);
20
+ }
21
+ parsed[argument === "--out" ? "out" : "name"] = value;
22
+ index++;
23
+ }
24
+ else if (argument.startsWith("--")) {
25
+ throw new Error(`unknown option ${argument}`);
26
+ }
27
+ else if (parsed.input === undefined) {
28
+ parsed.input = argument;
29
+ }
30
+ else {
31
+ throw new Error(`unexpected argument ${argument}`);
32
+ }
33
+ }
34
+ return parsed;
35
+ }
36
+ /** Runs the command with the given arguments and returns the exit code. */
37
+ export async function main(argv) {
38
+ let parsed;
39
+ try {
40
+ parsed = parse(argv);
41
+ }
42
+ catch (error) {
43
+ process.stderr.write(`zerolith-client: ${error instanceof Error ? error.message : String(error)}\n${USAGE}`);
44
+ return 2;
45
+ }
46
+ if (parsed.input === undefined) {
47
+ process.stderr.write(USAGE);
48
+ return 2;
49
+ }
50
+ try {
51
+ const document = JSON.parse(await readFile(parsed.input, "utf8"));
52
+ const code = generateClient(document, parsed.name === undefined ? {} : { apiName: parsed.name });
53
+ if (parsed.out === undefined) {
54
+ process.stdout.write(code);
55
+ }
56
+ else {
57
+ await writeFile(parsed.out, code);
58
+ }
59
+ }
60
+ catch (error) {
61
+ process.stderr.write(`zerolith-client: ${error instanceof Error ? error.message.replace(/^zerolith\/client: /, "") : String(error)}\n`);
62
+ return 1;
63
+ }
64
+ return 0;
65
+ }
66
+ /** True when node started this module, directly or through the bin link npm installs, which it resolves the way node resolves its entry. */
67
+ function started() {
68
+ const entry = process.argv[1];
69
+ try {
70
+ return entry !== undefined && pathToFileURL(realpathSync(entry)).href === import.meta.url;
71
+ }
72
+ catch {
73
+ return false;
74
+ }
75
+ }
76
+ if (started()) {
77
+ process.exitCode = await main(process.argv.slice(2));
78
+ }
79
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;;GAGG;AACH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,OAAO,MAAM,cAAc,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAwB,cAAc,EAAE,MAAM,eAAe,CAAC;AAErE,MAAM,KAAK,GAAG,0EAA0E,CAAC;AASzF,SAAS,KAAK,CAAC,IAAuB;IAElC,MAAM,MAAM,GAAc,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;IAChF,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,E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IAAI,CAAC,GAAG,CAAC;IAC9F,CAAC;IACD,MACA,CAAC;QACG,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC;AAED,IAAI,OAAO,EAAE,EACb,CAAC;IACG,OAAO,CAAC,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACzD,CAAC"}
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Generates the contract module from an OpenAPI 3.x document: one type per
3
+ * component schema, an `Api` interface with one entry per operation, and
4
+ * the `routes` table the client runs on. Pure string work, so it runs
5
+ * anywhere; the `zerolith-client` command wraps it for the shell.
6
+ */
7
+ /** A JSON Schema object as OpenAPI 3.1 and later carry it (draft 2020-12 plus the OAS vocabulary). */
8
+ export interface SchemaObject {
9
+ /** A reference to `#/components/schemas/<name>`, rendered as that component's generated type, intersected with the keywords beside it that shape a type, such as `properties`, as OpenAPI 3.1 allows; any other target fails generation. */
10
+ $ref?: string;
11
+ /** The JSON type, or since OpenAPI 3.1 an array of types rendered as a union; `integer` renders as `number`, and when it is absent the object or array keywords imply one, else the type is `unknown`. */
12
+ type?: string | string[];
13
+ /** The OpenAPI 3.0 flag that also allows `null`; when `true`, `null` joins the type. */
14
+ nullable?: boolean;
15
+ /** The allowed values, rendered as a union of their literal types; it takes precedence over `type`, and intersects with a composition keyword or `$ref` beside it. */
16
+ enum?: unknown[];
17
+ /** The single allowed value (OpenAPI 3.1 and later), rendered as its literal type; it takes precedence over `enum` and `type`, and intersects with a composition keyword or `$ref` beside it. */
18
+ const?: unknown;
19
+ /** The object's properties, each generated as a member that is optional unless `required` lists it, with the property's `description` as its doc comment. */
20
+ properties?: Record<string, SchemaObject>;
21
+ /** The names in `properties` that must be present; every other property is generated as optional. */
22
+ required?: string[];
23
+ /** The schema of keys beyond `properties`, rendered as a `Record<string, ...>` joined to them with `&`; `true` allows `unknown` values, and `false` makes an object without properties `Record<string, never>`. */
24
+ additionalProperties?: boolean | SchemaObject;
25
+ /** The schema of each array element, rendered as `T[]`, or `unknown[]` without it; beside `prefixItems` it types the elements after the tuple. */
26
+ items?: SchemaObject;
27
+ /** The schemas of the leading array elements by position (OpenAPI 3.1 and later), rendered as a tuple; without `items` the tuple has no rest element. */
28
+ prefixItems?: SchemaObject[];
29
+ /** Alternatives of which exactly one matches, rendered as a union; it takes precedence over `anyOf` and `allOf`, and the keywords beside it that shape a type, such as `properties`, intersect with it. */
30
+ oneOf?: SchemaObject[];
31
+ /** Alternatives of which at least one matches, rendered as a union unless `oneOf` is present; the keywords beside it that shape a type, such as `properties`, intersect with it. */
32
+ anyOf?: SchemaObject[];
33
+ /** Schemas that all match, rendered as an intersection unless `oneOf` or `anyOf` is present; the keywords beside it that shape a type, such as `properties`, join the intersection. */
34
+ allOf?: SchemaObject[];
35
+ /** Becomes the doc comment on a component schema's generated type, ahead of `title`, and on an object property's member. */
36
+ description?: string;
37
+ /** The doc comment on a component schema's generated type when `description` is absent; the type's name comes from the component's key, not from `title`. */
38
+ title?: string;
39
+ }
40
+ /** A Parameter Object or a reference to one. */
41
+ export interface ParameterObject {
42
+ /** A reference to `#/components/parameters/<name>`, which replaces this object; any other target, or a missing one, fails generation. */
43
+ $ref?: string;
44
+ /** The member's key in the generated `params` or `query`, matching the `{name}` placeholder for a path parameter; a parameter without `name` or `in` fails generation. */
45
+ name?: string;
46
+ /** Where the parameter travels: `path` fills `params`, `query` adds a member to `query`, `querystring` (OpenAPI 3.2) lets `query` take any key, and `header` and `cookie` are left out. */
47
+ in?: "query" | "querystring" | "header" | "path" | "cookie";
48
+ /** Makes a query parameter's member, and the whole `query` input, required; path parameters are generated as required either way, since OpenAPI demands `true` for them. */
49
+ required?: boolean;
50
+ /** The doc comment on the parameter's generated member. */
51
+ description?: string;
52
+ /** The schema of the value, rendered as the member's type; without it or `content`, the type is `unknown`. */
53
+ schema?: SchemaObject;
54
+ /** The single media type entry OpenAPI allows in place of `schema`; when `schema` is absent, the entry's `schema` gives the member's type. */
55
+ content?: Record<string, MediaTypeObject>;
56
+ }
57
+ /** A Media Type Object. */
58
+ export interface MediaTypeObject {
59
+ /** The content's schema, read for a JSON media type in a body and for any media type in a parameter's `content`; when absent, the type is `unknown`. */
60
+ schema?: SchemaObject;
61
+ }
62
+ /** A Request Body Object or a reference to one. */
63
+ export interface RequestBodyObject {
64
+ /** A reference to `#/components/requestBodies/<name>`, which replaces this object; any other target, or a missing one, fails generation. */
65
+ $ref?: string;
66
+ /** A brief description of the body; the generator does not carry it into the output. */
67
+ description?: string;
68
+ /** Makes the generated `body` input required; OpenAPI's default is `false`, which generates it as optional. */
69
+ required?: boolean;
70
+ /** The body by media type, where a JSON type takes precedence and gives its schema's type; otherwise `multipart/form-data` gives `FormData`, `application/x-www-form-urlencoded` gives `URLSearchParams`, `text/*` gives `string`, and any other type gives `BodyInit`. */
71
+ content?: Record<string, MediaTypeObject>;
72
+ }
73
+ /** A Response Object or a reference to one. */
74
+ export interface ResponseObject {
75
+ /** A reference to `#/components/responses/<name>`, which replaces this object; any other target, or a missing one, fails generation. */
76
+ $ref?: string;
77
+ /** A description of the response; the generator does not carry it into the output. */
78
+ description?: string;
79
+ /** The response body by media type: a JSON type, when present, gives its schema's type, `text/*` gives `string`, any other type gives `Blob`, as the client reads it, and no content gives `undefined`. */
80
+ content?: Record<string, MediaTypeObject>;
81
+ }
82
+ /** An Operation Object. */
83
+ export interface OperationObject {
84
+ /** Names the operation's entry in `Api` and `routes`, in camelCase; without it the name comes from the method and path, such as `putNotesById` for `PUT /notes/{id}`, and two entries with one name fail generation. */
85
+ operationId?: string;
86
+ /** The first paragraph of the doc comment on the operation's entry in `Api`. */
87
+ summary?: string;
88
+ /** Follows `summary` in the doc comment on the operation's entry in `Api`. */
89
+ description?: string;
90
+ /** When `true`, the doc comment on the operation's entry ends with `@deprecated`. */
91
+ deprecated?: boolean;
92
+ /** The operation's parameters, merged with the path item's; one with the same `name` and `in` as a path item parameter replaces it. */
93
+ parameters?: ParameterObject[];
94
+ /** The request body, generated as the entry's `body` input; without it the entry has no `body`. */
95
+ requestBody?: RequestBodyObject;
96
+ /** The responses by status code; `output` is the union of every 2xx response, including `2XX`, or of `default` when there is none, and `unknown` without either. */
97
+ responses?: Record<string, ResponseObject>;
98
+ }
99
+ /** A Path Item Object. */
100
+ export interface PathItemObject {
101
+ /** A reference to `#/components/pathItems/<name>`, which replaces this item; any other target, or a missing one, fails generation. */
102
+ $ref?: string;
103
+ /** A summary meant for every operation on the path; the generator does not carry it into the output. */
104
+ summary?: string;
105
+ /** A description meant for every operation on the path; the generator does not carry it into the output. */
106
+ description?: string;
107
+ /** Parameters shared by every operation on the path; an operation's own parameter with the same `name` and `in` replaces one. */
108
+ parameters?: ParameterObject[];
109
+ /** The `GET` operation on this path. */
110
+ get?: OperationObject;
111
+ /** The `PUT` operation on this path. */
112
+ put?: OperationObject;
113
+ /** The `POST` operation on this path. */
114
+ post?: OperationObject;
115
+ /** The `DELETE` operation on this path. */
116
+ delete?: OperationObject;
117
+ /** The `OPTIONS` operation on this path. */
118
+ options?: OperationObject;
119
+ /** The `HEAD` operation on this path. */
120
+ head?: OperationObject;
121
+ /** The `PATCH` operation on this path. */
122
+ patch?: OperationObject;
123
+ /** The `TRACE` operation on this path. */
124
+ trace?: OperationObject;
125
+ /** The `QUERY` operation on this path (OpenAPI 3.2): a safe, idempotent request that carries a body. */
126
+ query?: OperationObject;
127
+ /** Operations for other HTTP methods (OpenAPI 3.2), keyed by method name such as `COPY`; each key is lowercased, and one that is not a valid method token fails generation. */
128
+ additionalOperations?: Record<string, OperationObject>;
129
+ }
130
+ /** The parts of an OpenAPI document the generator reads. */
131
+ export interface OpenApiDocument {
132
+ /** The OpenAPI version, such as `3.1.0`; a value that does not start with `3.` fails generation. */
133
+ openapi: string;
134
+ /** The API's title and version, which the generated file's header comment names as its source. */
135
+ info?: {
136
+ title?: string;
137
+ version?: string;
138
+ };
139
+ /** Path templates mapped to their items; every operation under them becomes an entry of `Api` and `routes`, sorted by path. */
140
+ paths?: Record<string, PathItemObject>;
141
+ /** Reusable objects: each entry of `schemas` becomes an exported type named after its key, and `$ref` values resolve into the other sections. */
142
+ components?: {
143
+ schemas?: Record<string, SchemaObject>;
144
+ parameters?: Record<string, ParameterObject>;
145
+ requestBodies?: Record<string, RequestBodyObject>;
146
+ responses?: Record<string, ResponseObject>;
147
+ pathItems?: Record<string, PathItemObject>;
148
+ };
149
+ }
150
+ /** Options for `generateClient`. */
151
+ export interface GenerateOptions {
152
+ /** The module the generated file imports `RouteTable` from. Defaults to `@zerolith/client`. */
153
+ clientSpecifier?: string;
154
+ /** The name of the generated contract interface. Defaults to `Api`. */
155
+ apiName?: string;
156
+ }
157
+ /**
158
+ * Generates the contract module for an OpenAPI 3.x document given as a
159
+ * parsed object (JSON; convert YAML first). Schemas under
160
+ * `components.schemas` become exported types, every operation becomes an
161
+ * entry of the `Api` interface named by its `operationId` (or by its
162
+ * method and path), and `routes` carries the method and path of each.
163
+ * Header and cookie parameters, security, and non-local references are
164
+ * left out; the client sends headers through its options. A schema whose
165
+ * type name would clash is written under the first free name with a
166
+ * number added, as `Record2`, with a comment that says why, and every
167
+ * `$ref` to it uses that name: one that another schema takes first, as
168
+ * `note-item` and `note_item` both give `NoteItem`, one named like the
169
+ * interface of the operations or the `RouteTable` the module imports, and
170
+ * one named like a built-in type the module writes, such as `Record` for
171
+ * an object of free keys or `Blob` for a binary response. A schema named
172
+ * like a built-in the module does not write keeps its name.
173
+ */
174
+ export declare function generateClient(document: OpenApiDocument, options?: GenerateOptions): string;
175
+ //# sourceMappingURL=generate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,sGAAsG;AACtG,MAAM,WAAW,YAAY;IAEzB,4OAA4O;IAC5O,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,0MAA0M;IAC1M,IAAI,CAAC,EAAE,MAAM,G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