@epcc-sdk/sdks-cart-checkout-order 0.3.0 → 0.5.0
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 +91 -1
- package/dist/client/mcp-support.gen.cjs +98 -0
- package/dist/client/mcp-support.gen.cjs.map +1 -0
- package/dist/client/mcp-support.gen.mjs +63 -0
- package/dist/client/mcp-support.gen.mjs.map +1 -0
- package/dist/client/mcp.gen.cjs +2426 -0
- package/dist/client/mcp.gen.cjs.map +1 -0
- package/dist/client/mcp.gen.mjs +2497 -0
- package/dist/client/mcp.gen.mjs.map +1 -0
- package/dist/client/zod.gen.cjs +2951 -1419
- package/dist/client/zod.gen.cjs.map +1 -1
- package/dist/client/zod.gen.mjs +2267 -745
- package/dist/client/zod.gen.mjs.map +1 -1
- package/dist/index.d.cts +3 -328
- package/dist/index.d.ts +3 -328
- package/dist/mcp.cjs +23 -0
- package/dist/mcp.cjs.map +1 -0
- package/dist/mcp.d.cts +17247 -0
- package/dist/mcp.d.ts +17247 -0
- package/dist/mcp.mjs +2 -0
- package/dist/mcp.mjs.map +1 -0
- package/dist/types.gen-Boa6zDxY.d.cts +328 -0
- package/dist/types.gen-Boa6zDxY.d.ts +328 -0
- package/dist/zod.d.cts +9138 -156922
- package/dist/zod.d.ts +9138 -156922
- package/package.json +21 -3
package/README.md
CHANGED
|
@@ -173,7 +173,7 @@ interceptor: an interceptor cannot see the response, so it can never retry a 401
|
|
|
173
173
|
## Validation schemas
|
|
174
174
|
|
|
175
175
|
Zod schemas for every request body, path and response are published under the `/zod`
|
|
176
|
-
subpath. `zod` is an optional peer dependency (
|
|
176
|
+
subpath. `zod` is an optional peer dependency (4.x), so the root entry never imports it.
|
|
177
177
|
|
|
178
178
|
```ts
|
|
179
179
|
import { zCartResponse } from "@epcc-sdk/sdks-cart-checkout-order/zod";
|
|
@@ -181,6 +181,96 @@ import { zCartResponse } from "@epcc-sdk/sdks-cart-checkout-order/zod";
|
|
|
181
181
|
const cart = zCartResponse.parse(data?.data[0]);
|
|
182
182
|
```
|
|
183
183
|
|
|
184
|
+
## MCP endpoint tools
|
|
185
|
+
|
|
186
|
+
The `/mcp` subpath turns each of the 60 cart, checkout and order operations into a Model Context
|
|
187
|
+
Protocol (MCP) tool. The main entry imports none of it. `zod` 4 and `@modelcontextprotocol/sdk` are
|
|
188
|
+
needed only for this subpath.
|
|
189
|
+
|
|
190
|
+
Each operation has one tool factory, named after its function with `Tool` added, such as
|
|
191
|
+
`getCartsTool`. A factory returns `{ name, config, handler }`, the three arguments that
|
|
192
|
+
`McpServer.registerTool` takes. `tools()` returns all 60 in operation order. A handler returns an
|
|
193
|
+
API error as an `isError` result and does not throw.
|
|
194
|
+
|
|
195
|
+
Register one tool:
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
|
|
199
|
+
import { getCartsTool } from "@epcc-sdk/sdks-cart-checkout-order/mcp"
|
|
200
|
+
|
|
201
|
+
const server = new McpServer({ name: "cart-checkout-order", version: "1.0.0" })
|
|
202
|
+
const tool = getCartsTool()
|
|
203
|
+
server.registerTool(tool.name, tool.config, tool.handler)
|
|
204
|
+
```
|
|
205
|
+
A tool built without a client calls through the package's module level `client`, like an operation
|
|
206
|
+
does. That client has no credentials until you configure it. Configure it once:
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
import { client, createCartCheckoutOrderClient } from "@epcc-sdk/sdks-cart-checkout-order"
|
|
210
|
+
|
|
211
|
+
client.setConfig(
|
|
212
|
+
createCartCheckoutOrderClient({ baseUrl, clientId, clientSecret }).getConfig(),
|
|
213
|
+
)
|
|
214
|
+
```
|
|
215
|
+
Register every tool:
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
import * as cartCheckoutOrder from "@epcc-sdk/sdks-cart-checkout-order/mcp"
|
|
219
|
+
|
|
220
|
+
cartCheckoutOrder
|
|
221
|
+
.tools()
|
|
222
|
+
.forEach((t) => server.registerTool(t.name, t.config, t.handler))
|
|
223
|
+
```
|
|
224
|
+
Give the tools a client, and set headers on it once. Every tool then sends them:
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
const client = createCartCheckoutOrderClient({
|
|
228
|
+
baseUrl,
|
|
229
|
+
clientId,
|
|
230
|
+
clientSecret,
|
|
231
|
+
config: { headers: { "EP-Store-Id": storeId, "EP-Org-Id": orgId } },
|
|
232
|
+
})
|
|
233
|
+
cartCheckoutOrder
|
|
234
|
+
.tools({ client })
|
|
235
|
+
.forEach((t) => server.registerTool(t.name, t.config, t.handler))
|
|
236
|
+
```
|
|
237
|
+
Register the read-only tools only:
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
cartCheckoutOrder
|
|
241
|
+
.tools({ client })
|
|
242
|
+
.filter((t) => t.config._meta["com.elasticpath/operation"].risk === "read")
|
|
243
|
+
.forEach((t) => server.registerTool(t.name, t.config, t.handler))
|
|
244
|
+
```
|
|
245
|
+
Rename or reword a tool by passing a changed argument:
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
const tool = getCartsTool({ client })
|
|
249
|
+
server.registerTool(
|
|
250
|
+
"elastic_path_list_carts",
|
|
251
|
+
{ ...tool.config, title: "List carts" },
|
|
252
|
+
tool.handler,
|
|
253
|
+
)
|
|
254
|
+
```
|
|
255
|
+
Wrap a handler. The arguments keep the operation's types:
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
server.registerTool(tool.name, tool.config, async (args, extra) => {
|
|
259
|
+
console.log("listing carts", args.query)
|
|
260
|
+
return tool.handler(args, extra)
|
|
261
|
+
})
|
|
262
|
+
```
|
|
263
|
+
Each tool is named `cart_checkout_order_<snake_case_function>`, and its arguments are grouped under
|
|
264
|
+
`path`, `query`, `headers` and `body`. The `path`, `query` and `headers` groups reject keys the spec
|
|
265
|
+
does not list. The `body` keeps them, so an attribute the spec does not list still reaches the API.
|
|
266
|
+
The client supplies the `Authorization` header, so no tool asks for it. Cancelling the MCP request
|
|
267
|
+
cancels the HTTP request.
|
|
268
|
+
|
|
269
|
+
`config._meta["com.elasticpath/operation"]` holds the operation's `id`, `method`, `path` and `risk`.
|
|
270
|
+
The risk is `read`, `create`, `update` or `delete`, and the annotations follow from it.
|
|
271
|
+
`anonymizeOrders` is a `POST` that replaces customer data in existing orders and cannot be undone,
|
|
272
|
+
so a spec override gives it the `update` risk, which marks it destructive.
|
|
273
|
+
|
|
184
274
|
## Build URL
|
|
185
275
|
|
|
186
276
|
If you need to access the compiled URL, you can use the buildUrl() method. It's loosely typed by default to accept almost any value; in practice, you will want to pass a type hint.
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var mcp_support_gen_exports = {};
|
|
30
|
+
__export(mcp_support_gen_exports, {
|
|
31
|
+
loose: () => loose,
|
|
32
|
+
toToolResult: () => toToolResult
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(mcp_support_gen_exports);
|
|
35
|
+
var z = __toESM(require("zod"), 1);
|
|
36
|
+
const messageOf = (error) => error instanceof Error ? error.message : JSON.stringify(error);
|
|
37
|
+
const toToolResult = async (request) => {
|
|
38
|
+
let text;
|
|
39
|
+
try {
|
|
40
|
+
const result = await request;
|
|
41
|
+
if (result.error === void 0) {
|
|
42
|
+
const structuredContent = typeof result.data === "object" && result.data !== null && !Array.isArray(result.data) ? result.data : { data: result.data ?? null };
|
|
43
|
+
return {
|
|
44
|
+
structuredContent,
|
|
45
|
+
content: [{ type: "text", text: JSON.stringify(structuredContent) }]
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
text = result.response ? `HTTP ${result.response.status}: ${JSON.stringify(result.error)}` : messageOf(result.error);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
text = messageOf(error);
|
|
51
|
+
}
|
|
52
|
+
return { isError: true, content: [{ type: "text", text }] };
|
|
53
|
+
};
|
|
54
|
+
const loose = (schema) => {
|
|
55
|
+
const def = schema._zod.def;
|
|
56
|
+
const copy = (changes) => schema.clone({ ...def, ...changes }, { parent: true });
|
|
57
|
+
if (def.type === "object") {
|
|
58
|
+
return copy({
|
|
59
|
+
catchall: z.unknown(),
|
|
60
|
+
shape: Object.fromEntries(
|
|
61
|
+
Object.keys(def.shape).map((key) => [key, loose(def.shape[key])])
|
|
62
|
+
)
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (def.type === "array") {
|
|
66
|
+
return copy({ element: loose(def.element) });
|
|
67
|
+
}
|
|
68
|
+
if (def.type === "record") {
|
|
69
|
+
return copy({ valueType: loose(def.valueType) });
|
|
70
|
+
}
|
|
71
|
+
if (def.type === "lazy") {
|
|
72
|
+
return copy({ getter: () => loose(def.getter()) });
|
|
73
|
+
}
|
|
74
|
+
if ([
|
|
75
|
+
"optional",
|
|
76
|
+
"nullable",
|
|
77
|
+
"default",
|
|
78
|
+
"readonly",
|
|
79
|
+
"nonoptional",
|
|
80
|
+
"catch",
|
|
81
|
+
"prefault"
|
|
82
|
+
].includes(def.type)) {
|
|
83
|
+
return copy({ innerType: loose(def.innerType) });
|
|
84
|
+
}
|
|
85
|
+
if (def.type === "union") {
|
|
86
|
+
return copy({ options: def.options.map(loose) });
|
|
87
|
+
}
|
|
88
|
+
if (def.type === "intersection") {
|
|
89
|
+
return copy({ left: loose(def.left), right: loose(def.right) });
|
|
90
|
+
}
|
|
91
|
+
return schema;
|
|
92
|
+
};
|
|
93
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
94
|
+
0 && (module.exports = {
|
|
95
|
+
loose,
|
|
96
|
+
toToolResult
|
|
97
|
+
});
|
|
98
|
+
//# sourceMappingURL=mcp-support.gen.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/mcp-support.gen.ts"],"sourcesContent":["import type { ToolCallback } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport type { AnySchema } from \"@modelcontextprotocol/sdk/server/zod-compat.js\"\nimport type {\n CallToolResult,\n ToolAnnotations,\n} from \"@modelcontextprotocol/sdk/types.js\"\nimport * as z from \"zod\"\n\nexport type OperationMeta = {\n id: string\n method: string\n path: string\n risk: \"read\" | \"create\" | \"update\" | \"delete\"\n}\n\nexport type ToolConfig = {\n title?: string\n description?: string\n inputSchema: z.ZodObject<Record<string, z.ZodType<any>>>\n outputSchema?: AnySchema\n annotations?: ToolAnnotations\n _meta: {\n \"com.elasticpath/operation\": OperationMeta\n }\n}\n\nexport type Tool = {\n readonly name: string\n readonly config: ToolConfig\n readonly handler: ToolCallback<AnySchema>\n}\n\nconst messageOf = (error: unknown) =>\n error instanceof Error ? error.message : JSON.stringify(error)\n\nexport const toToolResult = async (\n request: Promise<{\n data?: unknown\n error?: unknown\n response?: Response\n }>,\n): Promise<CallToolResult> => {\n let text: string\n try {\n const result = await request\n if (result.error === undefined) {\n const structuredContent =\n typeof result.data === \"object\" &&\n result.data !== null &&\n !Array.isArray(result.data)\n ? (result.data as Record<string, unknown>)\n : { data: result.data ?? null }\n return {\n structuredContent,\n content: [{ type: \"text\", text: JSON.stringify(structuredContent) }],\n }\n }\n text = result.response\n ? `HTTP ${result.response.status}: ${JSON.stringify(result.error)}`\n : messageOf(result.error)\n } catch (error) {\n text = messageOf(error)\n }\n return { isError: true, content: [{ type: \"text\", text }] }\n}\n\nexport const loose = <T extends z.ZodType>(schema: T): T => {\n const def: any = schema._zod.def\n const copy = (changes: object): T =>\n schema.clone({ ...def, ...changes }, { parent: true })\n if (def.type === \"object\") {\n return copy({\n catchall: z.unknown(),\n shape: Object.fromEntries(\n Object.keys(def.shape).map((key) => [key, loose(def.shape[key])]),\n ),\n })\n }\n if (def.type === \"array\") {\n return copy({ element: loose(def.element) })\n }\n if (def.type === \"record\") {\n return copy({ valueType: loose(def.valueType) })\n }\n if (def.type === \"lazy\") {\n return copy({ getter: () => loose(def.getter()) })\n }\n if (\n [\n \"optional\",\n \"nullable\",\n \"default\",\n \"readonly\",\n \"nonoptional\",\n \"catch\",\n \"prefault\",\n ].includes(def.type)\n ) {\n return copy({ innerType: loose(def.innerType) })\n }\n if (def.type === \"union\") {\n return copy({ options: def.options.map(loose) })\n }\n if (def.type === \"intersection\") {\n return copy({ left: loose(def.left), right: loose(def.right) })\n }\n return schema\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AA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,MAAM,IAAI,OAAO,EAAE,CAAC;AAAA,EAC7C;AACA,MAAI,IAAI,SAAS,UAAU;AACzB,WAAO,KAAK,EAAE,WAAW,MAAM,IAAI,SAAS,EAAE,CAAC;AAAA,EACjD;AACA,MAAI,IAAI,SAAS,QAAQ;AACvB,WAAO,KAAK,EAAE,QAAQ,MAAM,MAAM,IAAI,OAAO,CAAC,EAAE,CAAC;AAAA,EACnD;AACA,MACE;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,EAAE,SAAS,IAAI,IAAI,GACnB;AACA,WAAO,KAAK,EAAE,WAAW,MAAM,IAAI,SAAS,EAAE,CAAC;AAAA,EACjD;AACA,MAAI,IAAI,SAAS,SAAS;AACxB,WAAO,KAAK,EAAE,SAAS,IAAI,QAAQ,IAAI,KAAK,EAAE,CAAC;AAAA,EACjD;AACA,MAAI,IAAI,SAAS,gBAAgB;AAC/B,WAAO,KAAK,EAAE,MAAM,MAAM,IAAI,IAAI,GAAG,OAAO,MAAM,IAAI,KAAK,EAAE,CAAC;AAAA,EAChE;AACA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
const messageOf = (error) => error instanceof Error ? error.message : JSON.stringify(error);
|
|
3
|
+
const toToolResult = async (request) => {
|
|
4
|
+
let text;
|
|
5
|
+
try {
|
|
6
|
+
const result = await request;
|
|
7
|
+
if (result.error === void 0) {
|
|
8
|
+
const structuredContent = typeof result.data === "object" && result.data !== null && !Array.isArray(result.data) ? result.data : { data: result.data ?? null };
|
|
9
|
+
return {
|
|
10
|
+
structuredContent,
|
|
11
|
+
content: [{ type: "text", text: JSON.stringify(structuredContent) }]
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
text = result.response ? `HTTP ${result.response.status}: ${JSON.stringify(result.error)}` : messageOf(result.error);
|
|
15
|
+
} catch (error) {
|
|
16
|
+
text = messageOf(error);
|
|
17
|
+
}
|
|
18
|
+
return { isError: true, content: [{ type: "text", text }] };
|
|
19
|
+
};
|
|
20
|
+
const loose = (schema) => {
|
|
21
|
+
const def = schema._zod.def;
|
|
22
|
+
const copy = (changes) => schema.clone({ ...def, ...changes }, { parent: true });
|
|
23
|
+
if (def.type === "object") {
|
|
24
|
+
return copy({
|
|
25
|
+
catchall: z.unknown(),
|
|
26
|
+
shape: Object.fromEntries(
|
|
27
|
+
Object.keys(def.shape).map((key) => [key, loose(def.shape[key])])
|
|
28
|
+
)
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (def.type === "array") {
|
|
32
|
+
return copy({ element: loose(def.element) });
|
|
33
|
+
}
|
|
34
|
+
if (def.type === "record") {
|
|
35
|
+
return copy({ valueType: loose(def.valueType) });
|
|
36
|
+
}
|
|
37
|
+
if (def.type === "lazy") {
|
|
38
|
+
return copy({ getter: () => loose(def.getter()) });
|
|
39
|
+
}
|
|
40
|
+
if ([
|
|
41
|
+
"optional",
|
|
42
|
+
"nullable",
|
|
43
|
+
"default",
|
|
44
|
+
"readonly",
|
|
45
|
+
"nonoptional",
|
|
46
|
+
"catch",
|
|
47
|
+
"prefault"
|
|
48
|
+
].includes(def.type)) {
|
|
49
|
+
return copy({ innerType: loose(def.innerType) });
|
|
50
|
+
}
|
|
51
|
+
if (def.type === "union") {
|
|
52
|
+
return copy({ options: def.options.map(loose) });
|
|
53
|
+
}
|
|
54
|
+
if (def.type === "intersection") {
|
|
55
|
+
return copy({ left: loose(def.left), right: loose(def.right) });
|
|
56
|
+
}
|
|
57
|
+
return schema;
|
|
58
|
+
};
|
|
59
|
+
export {
|
|
60
|
+
loose,
|
|
61
|
+
toToolResult
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=mcp-support.gen.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/mcp-support.gen.ts"],"sourcesContent":["import type { ToolCallback } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport type { AnySchema } from \"@modelcontextprotocol/sdk/server/zod-compat.js\"\nimport type {\n CallToolResult,\n ToolAnnotations,\n} from \"@modelcontextprotocol/sdk/types.js\"\nimport * as z from \"zod\"\n\nexport type OperationMeta = {\n id: string\n method: string\n path: string\n risk: \"read\" | \"create\" | \"update\" | \"delete\"\n}\n\nexport type ToolConfig = {\n title?: string\n description?: string\n inputSchema: z.ZodObject<Record<string, z.ZodType<any>>>\n outputSchema?: AnySchema\n annotations?: ToolAnnotations\n _meta: {\n \"com.elasticpath/operation\": OperationMeta\n }\n}\n\nexport type Tool = {\n readonly name: string\n readonly config: ToolConfig\n readonly handler: ToolCallback<AnySchema>\n}\n\nconst messageOf = (error: unknown) =>\n error instanceof Error ? error.message : JSON.stringify(error)\n\nexport const toToolResult = async (\n request: Promise<{\n data?: unknown\n error?: unknown\n response?: Response\n }>,\n): Promise<CallToolResult> => {\n let text: string\n try {\n const result = await request\n if (result.error === undefined) {\n const structuredContent =\n typeof result.data === \"object\" &&\n result.data !== null &&\n !Array.isArray(result.data)\n ? (result.data as Record<string, unknown>)\n : { data: result.data ?? null }\n return {\n structuredContent,\n content: [{ type: \"text\", text: JSON.stringify(structuredContent) }],\n }\n }\n text = result.response\n ? `HTTP ${result.response.status}: ${JSON.stringify(result.error)}`\n : messageOf(result.error)\n } catch (error) {\n text = messageOf(error)\n }\n return { isError: true, content: [{ type: \"text\", text }] }\n}\n\nexport const loose = <T extends z.ZodType>(schema: T): T => {\n const def: any = schema._zod.def\n const copy = (changes: object): T =>\n schema.clone({ ...def, ...changes }, { parent: true })\n if (def.type === \"object\") {\n return copy({\n catchall: z.unknown(),\n shape: Object.fromEntries(\n Object.keys(def.shape).map((key) => [key, loose(def.shape[key])]),\n ),\n })\n }\n if (def.type === \"array\") {\n return copy({ element: loose(def.element) })\n }\n if (def.type === \"record\") {\n return copy({ valueType: loose(def.valueType) })\n }\n if (def.type === \"lazy\") {\n return copy({ getter: () => loose(def.getter()) })\n }\n if (\n [\n \"optional\",\n \"nullable\",\n \"default\",\n \"readonly\",\n \"nonoptional\",\n \"catch\",\n \"prefault\",\n ].includes(def.type)\n ) {\n return copy({ innerType: loose(def.innerType) })\n }\n if (def.type === \"union\") {\n return copy({ options: def.options.map(loose) })\n }\n if (def.type === \"intersection\") {\n return copy({ left: loose(def.left), right: loose(def.right) })\n }\n return schema\n}\n"],"mappings":"AAMA,YAAY,OAAO;AA0BnB,MAAM,YAAY,CAAC,UACjB,iBAAiB,QAAQ,MAAM,UAAU,KAAK,UAAU,KAAK;AAExD,MAAM,eAAe,OAC1B,YAK4B;AAC5B,MAAI;AACJ,MAAI;AACF,UAAM,SAAS,MAAM;AACrB,QAAI,OAAO,UAAU,QAAW;AAC9B,YAAM,oBACJ,OAAO,OAAO,SAAS,YACvB,OAAO,SAAS,QAChB,CAAC,MAAM,QAAQ,OAAO,IAAI,IACrB,OAAO,OACR,EAAE,MAAM,OAAO,QAAQ,KAAK;AAClC,aAAO;AAAA,QACL;AAAA,QACA,SAAS,CAAC,EAAE,MAAM,QAAQ,MAAM,KAAK,UAAU,iBAAiB,EAAE,CAAC;AAAA,MACrE;AAAA,IACF;AACA,WAAO,OAAO,WACV,QAAQ,OAAO,SAAS,MAAM,KAAK,KAAK,UAAU,OAAO,KAAK,CAAC,KAC/D,UAAU,OAAO,KAAK;AAAA,EAC5B,SAAS,OAAO;AACd,WAAO,UAAU,KAAK;AAAA,EACxB;AACA,SAAO,EAAE,SAAS,MAAM,SAAS,CAAC,EAAE,MAAM,QAAQ,KAAK,CAAC,EAAE;AAC5D;AAEO,MAAM,QAAQ,CAAsB,WAAiB;AAC1D,QAAM,MAAW,OAAO,KAAK;AAC7B,QAAM,OAAO,CAAC,YACZ,OAAO,MAAM,EAAE,GAAG,KAAK,GAAG,QAAQ,GAAG,EAAE,QAAQ,KAAK,CAAC;AACvD,MAAI,IAAI,SAAS,UAAU;AACzB,WAAO,KAAK;AAAA,MACV,UAAU,EAAE,QAAQ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