@peers-app/peers-sdk 0.26.5 → 0.27.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 +3 -1
- package/bin/peers-package.js +13 -0
- package/define/package.json +4 -0
- package/dist/contracts/__tests__/action-dispatch.test.js +56 -76
- package/dist/contracts/__tests__/also-implements.test.d.ts +1 -0
- package/dist/contracts/__tests__/also-implements.test.js +110 -0
- package/dist/contracts/__tests__/builder.test.js +45 -0
- package/dist/contracts/__tests__/contract-resolvers.test.js +134 -4
- package/dist/contracts/__tests__/frozen-contracts.test.d.ts +1 -0
- package/dist/contracts/__tests__/frozen-contracts.test.js +191 -0
- package/dist/contracts/__tests__/generic-tool-dispatch.test.js +13 -56
- package/dist/contracts/__tests__/host-contract-client.test.d.ts +1 -0
- package/dist/contracts/__tests__/host-contract-client.test.js +793 -0
- package/dist/contracts/__tests__/host-contract-transport.test.js +15 -0
- package/dist/contracts/__tests__/registry.test.js +68 -0
- package/dist/contracts/__tests__/target-dispatch.d.ts +11 -0
- package/dist/contracts/__tests__/target-dispatch.js +24 -0
- package/dist/contracts/__tests__/tool-catalog-service.test.js +37 -1
- package/dist/contracts/__tests__/tool-catalog.test.js +12 -3
- package/dist/contracts/__tests__/validate.test.js +177 -7
- package/dist/contracts/__tests__/workflow-context.test.js +0 -11
- package/dist/contracts/builder.d.ts +16 -2
- package/dist/contracts/builder.js +44 -3
- package/dist/contracts/contract-definition-schema.d.ts +251 -0
- package/dist/contracts/contract-definition-schema.js +48 -0
- package/dist/contracts/contract-provider-router.d.ts +2 -0
- package/dist/contracts/contract-provider-router.js +5 -2
- package/dist/contracts/contract-proxy.d.ts +52 -18
- package/dist/contracts/contract-proxy.js +144 -203
- package/dist/contracts/contract-resolvers.d.ts +6 -4
- package/dist/contracts/contract-resolvers.js +32 -9
- package/dist/contracts/frozen-contracts.d.ts +59 -0
- package/dist/contracts/frozen-contracts.js +86 -0
- package/dist/contracts/host-contract-client.d.ts +134 -0
- package/dist/contracts/host-contract-client.js +396 -0
- package/dist/contracts/host-contract-transport.d.ts +2 -0
- package/dist/contracts/host-contract-transport.js +8 -0
- package/dist/contracts/index.d.ts +6 -4
- package/dist/contracts/index.js +11 -5
- package/dist/contracts/registry.d.ts +31 -1
- package/dist/contracts/registry.js +100 -6
- package/dist/contracts/system/contract-catalog-contract.d.ts +61 -6
- package/dist/contracts/system/contract-catalog-contract.js +17 -5
- package/dist/contracts/tool-catalog-service.js +26 -6
- package/dist/contracts/tool-catalog.d.ts +1 -1
- package/dist/contracts/tool-catalog.js +4 -1
- package/dist/contracts/types.d.ts +32 -2
- package/dist/contracts/validate.d.ts +16 -1
- package/dist/contracts/validate.js +139 -25
- package/dist/data/change-tracking.d.ts +17 -5
- package/dist/data/change-tracking.js +22 -6
- package/dist/data/change-tracking.test.d.ts +1 -0
- package/dist/data/change-tracking.test.js +62 -0
- package/dist/data/group-members.d.ts +6 -1
- package/dist/data/group-members.js +12 -4
- package/dist/data/groups.d.ts +6 -1
- package/dist/data/groups.js +12 -4
- package/dist/data/orm/sql.data-source.d.ts +9 -0
- package/dist/data/orm/sql.data-source.js +47 -2
- package/dist/data/orm/sql.data-source.test.js +142 -0
- package/dist/data/orm/table-container.js +9 -9
- package/dist/data/orm/table-container.test.js +44 -0
- package/dist/data/orm/types.d.ts +41 -1
- package/dist/data/orm/types.js +159 -28
- package/dist/data/orm/types.test.js +129 -0
- package/dist/data/package-versions.d.ts +5 -1
- package/dist/data/package-versions.js +11 -4
- package/dist/data/packages.d.ts +7 -1
- package/dist/data/packages.js +13 -4
- package/dist/data/peer-types.d.ts +8 -62
- package/dist/data/persistent-vars.d.ts +4 -1
- package/dist/data/persistent-vars.js +7 -2
- package/dist/data/table-signing.test.d.ts +1 -0
- package/dist/data/table-signing.test.js +118 -0
- package/dist/data/tools.d.ts +29 -221
- package/dist/data/users.d.ts +1 -2
- package/dist/data/users.js +8 -5
- package/dist/define/also-implements.test.d.ts +1 -0
- package/dist/define/also-implements.test.js +123 -0
- package/dist/define/context.d.ts +77 -0
- package/dist/define/context.js +2 -0
- package/dist/define/contract-ref.d.ts +177 -0
- package/dist/define/contract-ref.js +67 -0
- package/dist/define/declare-contract.d.ts +167 -0
- package/dist/define/declare-contract.js +220 -0
- package/dist/define/declare-contract.test.d.ts +1 -0
- package/dist/define/declare-contract.test.js +573 -0
- package/dist/define/define-package.d.ts +203 -0
- package/dist/define/define-package.js +289 -0
- package/dist/define/define.test.d.ts +1 -0
- package/dist/define/define.test.js +1010 -0
- package/dist/define/empty-fields.d.ts +15 -0
- package/dist/define/empty-fields.js +134 -0
- package/dist/define/empty-fields.test.d.ts +1 -0
- package/dist/define/empty-fields.test.js +55 -0
- package/dist/define/events-observables.test.d.ts +1 -0
- package/dist/define/events-observables.test.js +1013 -0
- package/dist/define/events.d.ts +81 -0
- package/dist/define/events.js +81 -0
- package/dist/define/generated-field.d.ts +20 -0
- package/dist/define/generated-field.js +25 -0
- package/dist/define/index.d.ts +28 -0
- package/dist/define/index.js +62 -0
- package/dist/define/lazy-members.d.ts +8 -0
- package/dist/define/lazy-members.js +38 -0
- package/dist/define/manifest.d.ts +63 -0
- package/dist/define/manifest.js +653 -0
- package/dist/define/observables.d.ts +146 -0
- package/dist/define/observables.js +187 -0
- package/dist/define/serve-members.d.ts +21 -0
- package/dist/define/serve-members.js +152 -0
- package/dist/define/serve-members.test.d.ts +1 -0
- package/dist/define/serve-members.test.js +630 -0
- package/dist/define/serve.d.ts +22 -0
- package/dist/define/serve.js +58 -0
- package/dist/define/system-contracts.d.ts +41 -0
- package/dist/define/system-contracts.js +21 -0
- package/dist/define/table-handle.d.ts +13 -0
- package/dist/define/table-handle.js +36 -0
- package/dist/define/table.d.ts +128 -0
- package/dist/define/table.js +122 -0
- package/dist/define/tool.d.ts +129 -0
- package/dist/define/tool.js +91 -0
- package/dist/define/vars.d.ts +82 -0
- package/dist/define/vars.js +256 -0
- package/dist/define/vars.test.d.ts +1 -0
- package/dist/define/vars.test.js +39 -0
- package/dist/define/wire-value.d.ts +12 -0
- package/dist/define/wire-value.js +76 -0
- package/dist/define/worker-state.d.ts +15 -0
- package/dist/define/worker-state.js +45 -0
- package/dist/device/tx-encoding.test.js +10 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -1
- package/dist/observable.d.ts +42 -2
- package/dist/observable.js +25 -7
- package/dist/package-build/build-package.d.ts +60 -0
- package/dist/package-build/build-package.js +312 -0
- package/dist/package-build/build-package.test.d.ts +1 -0
- package/dist/package-build/build-package.test.js +204 -0
- package/dist/package-build/cli.d.ts +7 -0
- package/dist/package-build/cli.js +218 -0
- package/dist/package-build/contract-add.d.ts +35 -0
- package/dist/package-build/contract-add.js +97 -0
- package/dist/package-build/contract-add.test.d.ts +1 -0
- package/dist/package-build/contract-add.test.js +467 -0
- package/dist/package-build/contract-source.d.ts +59 -0
- package/dist/package-build/contract-source.js +471 -0
- package/dist/package-build/freeze-contracts.d.ts +56 -0
- package/dist/package-build/freeze-contracts.js +198 -0
- package/dist/package-build/freeze-contracts.test.d.ts +1 -0
- package/dist/package-build/freeze-contracts.test.js +355 -0
- package/dist/package-installer/package-creator.d.ts +4 -0
- package/dist/package-installer/package-creator.js +57 -16
- package/dist/package-installer/package-creator.test.js +86 -0
- package/dist/package-loader/contract-members.d.ts +53 -0
- package/dist/package-loader/contract-members.js +31 -0
- package/dist/package-loader/contract-package-loader.js +5 -1
- package/dist/package-loader/contract-package-loader.test.js +11 -0
- package/dist/package-loader/host-authority-loading.test.js +9 -5
- package/dist/package-loader/index.d.ts +1 -0
- package/dist/package-loader/index.js +1 -0
- package/dist/package-loader/isolated-artifact.d.ts +80 -17
- package/dist/package-loader/isolated-artifact.js +257 -51
- package/dist/package-loader/isolated-artifact.test.js +294 -21
- package/dist/package-loader/isolated-runtime.d.ts +22 -7
- package/dist/package-loader/isolated-table-gateway.js +30 -11
- package/dist/package-loader/isolated-table-gateway.test.js +243 -0
- package/dist/package-loader/package-loader.d.ts +8 -1
- package/dist/package-loader/package-loader.js +39 -20
- package/dist/package-loader/package-loader.test.js +47 -0
- package/dist/peer-id.d.ts +25 -0
- package/dist/peer-id.js +99 -0
- package/dist/serial-json.compat.test.d.ts +1 -0
- package/dist/serial-json.compat.test.js +369 -0
- package/dist/serial-json.d.ts +41 -5
- package/dist/serial-json.js +216 -157
- package/dist/serial-json.oracle.d.ts +21 -0
- package/dist/serial-json.oracle.js +222 -0
- package/dist/serial-json.test.js +315 -0
- package/dist/testing/index.d.ts +4 -0
- package/dist/testing/index.js +8 -0
- package/dist/testing/mock-host-contract.d.ts +123 -0
- package/dist/testing/mock-host-contract.js +434 -0
- package/dist/testing/mock-host-contract.test.d.ts +1 -0
- package/dist/testing/mock-host-contract.test.js +782 -0
- package/dist/types/field-type.d.ts +35 -18
- package/dist/types/field-type.js +4 -1
- package/dist/types/zod-types.d.ts +9 -0
- package/dist/types/zod-types.js +8 -2
- package/dist/utils.d.ts +1 -25
- package/dist/utils.js +10 -90
- package/package.json +9 -2
- package/testing/package.json +4 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const promises_1 = require("node:fs/promises");
|
|
4
|
+
const node_os_1 = require("node:os");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const ts = require("typescript");
|
|
7
|
+
const validate_1 = require("../contracts/validate");
|
|
8
|
+
const declare_contract_1 = require("../define/declare-contract");
|
|
9
|
+
const manifest_1 = require("../define/manifest");
|
|
10
|
+
const isolated_artifact_1 = require("../package-loader/isolated-artifact");
|
|
11
|
+
const peer_id_1 = require("../peer-id");
|
|
12
|
+
const field_type_1 = require("../types/field-type");
|
|
13
|
+
const build_package_1 = require("./build-package");
|
|
14
|
+
const cli_1 = require("./cli");
|
|
15
|
+
const contract_add_1 = require("./contract-add");
|
|
16
|
+
const contract_source_1 = require("./contract-source");
|
|
17
|
+
const sdkRoot = path.resolve(__dirname, "../..");
|
|
18
|
+
const contractId = (0, peer_id_1.newid)();
|
|
19
|
+
let root;
|
|
20
|
+
/** A declaration covering schema-inferred IR features. Hand-written IR is tested separately. */
|
|
21
|
+
const richDeclaration = `
|
|
22
|
+
import {
|
|
23
|
+
AccessLevel, declareContract, declareObservable, declareTool, defineEvent, defineTable, z,
|
|
24
|
+
zodPeerId,
|
|
25
|
+
} from "@peers-app/peers-sdk/define";
|
|
26
|
+
|
|
27
|
+
enum Level { Low = 1, High = 2 }
|
|
28
|
+
const place = z.object({
|
|
29
|
+
city: z.string().describe("City."),
|
|
30
|
+
unit: z.enum(["F", "C"]).optional(),
|
|
31
|
+
zone: z.string().nullish(),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const Places = defineTable({
|
|
35
|
+
name: "Places",
|
|
36
|
+
description: "Places.",
|
|
37
|
+
primaryKeyName: "placeId",
|
|
38
|
+
schema: z.object({
|
|
39
|
+
placeId: zodPeerId,
|
|
40
|
+
label: z.string(),
|
|
41
|
+
at: z.date().optional(),
|
|
42
|
+
tags: z.array(z.string()),
|
|
43
|
+
}),
|
|
44
|
+
versionNumber: 3,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export const Rich = declareContract({
|
|
48
|
+
contractId: ${JSON.stringify(contractId)},
|
|
49
|
+
version: 2,
|
|
50
|
+
name: "Rich Things",
|
|
51
|
+
description: "Every shape the IR carries.",
|
|
52
|
+
tables: [Places],
|
|
53
|
+
tools: [
|
|
54
|
+
declareTool({
|
|
55
|
+
name: "plan",
|
|
56
|
+
usageDescription: "Plan a trip.",
|
|
57
|
+
accessLevel: AccessLevel.Writer,
|
|
58
|
+
remoteAccessLevel: AccessLevel.Admin,
|
|
59
|
+
inputSchema: z.object({
|
|
60
|
+
place,
|
|
61
|
+
places: z.array(place).optional(),
|
|
62
|
+
level: z.nativeEnum(Level),
|
|
63
|
+
size: z.union([z.literal("small"), z.literal(3)]),
|
|
64
|
+
mode: z.literal("fast"),
|
|
65
|
+
when: z.date(),
|
|
66
|
+
anything: z.unknown().optional(),
|
|
67
|
+
count: z.number().int().min(1).default(5),
|
|
68
|
+
extra: z.record(z.string()).optional(),
|
|
69
|
+
}),
|
|
70
|
+
outputSchema: z.object({ ok: z.boolean(), ids: z.array(zodPeerId) }),
|
|
71
|
+
}),
|
|
72
|
+
declareTool({
|
|
73
|
+
name: "skim",
|
|
74
|
+
usageDescription: "Skim.",
|
|
75
|
+
accessLevel: AccessLevel.Reader,
|
|
76
|
+
authorityPropagation: "preserve",
|
|
77
|
+
inputSchema: z.object({}),
|
|
78
|
+
outputSchema: z.object({ note: z.string().optional() }),
|
|
79
|
+
}),
|
|
80
|
+
],
|
|
81
|
+
observables: [
|
|
82
|
+
declareObservable({
|
|
83
|
+
name: "state",
|
|
84
|
+
description: "Current state, or null.",
|
|
85
|
+
schema: z.object({ on: z.boolean(), place }).nullable(),
|
|
86
|
+
writable: true,
|
|
87
|
+
}),
|
|
88
|
+
declareObservable({ name: "count", description: "A count.", schema: z.number() }),
|
|
89
|
+
declareObservable({ name: "names", description: "Names.", schema: z.array(z.string()) }),
|
|
90
|
+
declareObservable({
|
|
91
|
+
name: "transformed",
|
|
92
|
+
description: "The original code's transform is not part of the IR.",
|
|
93
|
+
schema: z.number().transform(String),
|
|
94
|
+
}),
|
|
95
|
+
declareObservable({ name: "level", description: "A numeric enum.", schema: z.nativeEnum(Level) }),
|
|
96
|
+
],
|
|
97
|
+
events: [
|
|
98
|
+
defineEvent({
|
|
99
|
+
name: "place-changed",
|
|
100
|
+
description: "A place moved.",
|
|
101
|
+
payloadSchema: z.object({ placeId: zodPeerId, place, at: z.date() }),
|
|
102
|
+
}),
|
|
103
|
+
defineEvent({ name: "ping", description: "No payload." }),
|
|
104
|
+
],
|
|
105
|
+
});
|
|
106
|
+
`;
|
|
107
|
+
beforeEach(async () => {
|
|
108
|
+
root = await (0, promises_1.mkdtemp)(path.join((0, node_os_1.tmpdir)(), "peers-contract-add-test-"));
|
|
109
|
+
await (0, promises_1.mkdir)(path.join(root, "src"));
|
|
110
|
+
await (0, promises_1.mkdir)(path.join(root, "node_modules/@peers-app"), { recursive: true });
|
|
111
|
+
await (0, promises_1.symlink)(sdkRoot, path.join(root, "node_modules/@peers-app/peers-sdk"), "dir");
|
|
112
|
+
await (0, promises_1.symlink)(path.dirname(require.resolve("esbuild/package.json")), path.join(root, "node_modules/esbuild"), "dir");
|
|
113
|
+
await (0, promises_1.writeFile)(path.join(root, "package.json"), '{"name":"contract-add-probe","version":"1.0.0"}');
|
|
114
|
+
await (0, promises_1.writeFile)(path.join(root, "src/rich.ts"), richDeclaration);
|
|
115
|
+
});
|
|
116
|
+
afterEach(async () => {
|
|
117
|
+
await (0, promises_1.rm)(root, { recursive: true, force: true });
|
|
118
|
+
});
|
|
119
|
+
/** The IR of the declaration a module exports. */
|
|
120
|
+
async function irOf(file, exportName) {
|
|
121
|
+
const loaded = (await (0, build_package_1.loadPackageModule)(root, file));
|
|
122
|
+
const value = loaded[exportName];
|
|
123
|
+
if (!(0, declare_contract_1.isContractDeclaration)(value))
|
|
124
|
+
throw new Error(`${exportName} isn't a declaration`);
|
|
125
|
+
return (0, manifest_1.contractManifest)(value.declaration);
|
|
126
|
+
}
|
|
127
|
+
/** esbuild loads the generated file without typechecking; authors also compile it with tsc. */
|
|
128
|
+
function checkTypes(...files) {
|
|
129
|
+
const program = ts.createProgram(files, {
|
|
130
|
+
noEmit: true,
|
|
131
|
+
strict: true,
|
|
132
|
+
noUnusedLocals: true,
|
|
133
|
+
skipLibCheck: true,
|
|
134
|
+
target: ts.ScriptTarget.ES2022,
|
|
135
|
+
module: ts.ModuleKind.CommonJS,
|
|
136
|
+
types: [],
|
|
137
|
+
});
|
|
138
|
+
expect(ts.formatDiagnostics(ts.getPreEmitDiagnostics(program), {
|
|
139
|
+
getCurrentDirectory: () => root,
|
|
140
|
+
getCanonicalFileName: (file) => file,
|
|
141
|
+
getNewLine: () => "\n",
|
|
142
|
+
})).toBe("");
|
|
143
|
+
}
|
|
144
|
+
test("a generated declaration has the source's IR, member for member", async () => {
|
|
145
|
+
const source = path.join(root, "src/rich.ts");
|
|
146
|
+
const result = await (0, contract_add_1.addContract)({ source: `${source}#Rich`, root });
|
|
147
|
+
expect(path.relative(root, result.path)).toBe("src/contracts/rich-things.v2.ts");
|
|
148
|
+
expect(result).toMatchObject({ exportName: "RichThings", changed: true });
|
|
149
|
+
const generated = await (0, promises_1.readFile)(result.path, "utf8");
|
|
150
|
+
expect(generated).toMatch(/^\/\/ Generated by `peers-package contract add` from /);
|
|
151
|
+
expect(generated).toContain("export const richThingsPlaceChanged = defineEvent({");
|
|
152
|
+
expect(generated).toContain("export type RichThingsObservables = ContractObservableValuesOf<");
|
|
153
|
+
const original = await irOf(source, "Rich");
|
|
154
|
+
const copy = await irOf(result.path, "RichThings");
|
|
155
|
+
expect(copy).toEqual(original);
|
|
156
|
+
const types = path.join(root, "src/generated-types.ts");
|
|
157
|
+
await (0, promises_1.writeFile)(types, `
|
|
158
|
+
import type { RichThingsObservables, RichThingsRows, RichThingsEvents } from "./contracts/rich-things.v2";
|
|
159
|
+
export const allowed: RichThingsObservables["level"] = 1;
|
|
160
|
+
// @ts-expect-error The IR's numeric enum admits only 1 or 2.
|
|
161
|
+
export const refused: RichThingsObservables["level"] = 3;
|
|
162
|
+
export const row: RichThingsRows["Places"] = { placeId: "id", label: "Place", tags: [] };
|
|
163
|
+
export const event: RichThingsEvents["place-changed"] = { placeId: "id", place: { city: "City" }, at: new Date() };
|
|
164
|
+
// @ts-expect-error Member maps keep each row's field types.
|
|
165
|
+
export const badRow: RichThingsRows["Places"] = { placeId: "id", label: 4, tags: [] };
|
|
166
|
+
`);
|
|
167
|
+
checkTypes(result.path, types);
|
|
168
|
+
const loaded = (await (0, build_package_1.loadPackageModule)(root, result.path));
|
|
169
|
+
expect(loaded.RichThings.declaration.tools[1].definition.inputSchema.parse({ extra: 1 })).toEqual({ extra: 1 });
|
|
170
|
+
// A provider built from the copy registers a shape the original's frozen version accepts.
|
|
171
|
+
const definitions = [original, copy].map(({ contract, tables }) => (0, isolated_artifact_1.isolatedContractToDefinition)(contract, tables));
|
|
172
|
+
expect((0, validate_1.validateImmutability)(definitions[0], definitions[1])).toEqual({ valid: true, errors: [] });
|
|
173
|
+
expect((0, validate_1.validateImmutability)(definitions[1], definitions[0])).toEqual({ valid: true, errors: [] });
|
|
174
|
+
});
|
|
175
|
+
test("--check reports a missing, edited, or current declaration", async () => {
|
|
176
|
+
const source = `${path.join(root, "src/rich.ts")}#Rich`;
|
|
177
|
+
const check = () => (0, cli_1.main)(["contract", "add", source, "--root", root, "--check"]);
|
|
178
|
+
const errors = jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
179
|
+
const logs = jest.spyOn(console, "log").mockImplementation(() => undefined);
|
|
180
|
+
try {
|
|
181
|
+
expect(await check()).toBe(1);
|
|
182
|
+
await expect((0, promises_1.readFile)(path.join(root, "src/contracts/rich-things.v2.ts"))).rejects.toMatchObject({ code: "ENOENT" });
|
|
183
|
+
expect(await (0, cli_1.main)(["contract", "add", source, "--root", root])).toBe(0);
|
|
184
|
+
expect(await check()).toBe(0);
|
|
185
|
+
const file = path.join(root, "src/contracts/rich-things.v2.ts");
|
|
186
|
+
// The header names the source relative to the package, however the command spelled it.
|
|
187
|
+
expect(await (0, promises_1.readFile)(file, "utf8")).toMatch(/^\/\/ Generated by .* from src\/rich\.ts#Rich\./);
|
|
188
|
+
const fromCwd = path.relative(process.cwd(), path.join(root, "src/rich.ts"));
|
|
189
|
+
expect(await (0, cli_1.main)(["contract", "add", `${fromCwd}#Rich`, "--root", root, "--check"])).toBe(0);
|
|
190
|
+
const edited = `${await (0, promises_1.readFile)(file, "utf8")}// edited\n`;
|
|
191
|
+
await (0, promises_1.writeFile)(file, edited);
|
|
192
|
+
expect(await check()).toBe(1);
|
|
193
|
+
expect(await (0, promises_1.readFile)(file, "utf8")).toBe(edited);
|
|
194
|
+
expect(errors.mock.calls.at(-1)?.[0]).toMatch(/out of date\. Run: peers-package contract add/);
|
|
195
|
+
expect(await (0, cli_1.main)([
|
|
196
|
+
"contract",
|
|
197
|
+
"add",
|
|
198
|
+
source,
|
|
199
|
+
"--root",
|
|
200
|
+
root,
|
|
201
|
+
"--out-dir",
|
|
202
|
+
"custom folder",
|
|
203
|
+
"--name",
|
|
204
|
+
"Trips",
|
|
205
|
+
"--check",
|
|
206
|
+
])).toBe(1);
|
|
207
|
+
expect(errors.mock.calls.at(-1)?.[0]).toContain(`--root ${root} --out-dir 'custom folder' --name Trips`);
|
|
208
|
+
}
|
|
209
|
+
finally {
|
|
210
|
+
errors.mockRestore();
|
|
211
|
+
logs.mockRestore();
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
test("formats with the destination package's Biome and checks that formatted copy", async () => {
|
|
215
|
+
await (0, promises_1.mkdir)(path.join(root, "node_modules/.bin"));
|
|
216
|
+
await (0, promises_1.symlink)(path.join(sdkRoot, "node_modules/.bin/biome"), path.join(root, "node_modules/.bin/biome"));
|
|
217
|
+
await (0, promises_1.writeFile)(path.join(root, "biome.json"), JSON.stringify({ formatter: { indentStyle: "tab" } }));
|
|
218
|
+
const source = `${path.join(root, "src/rich.ts")}#Rich`;
|
|
219
|
+
// Without @biomejs/biome in the package's node_modules, the .bin shim runs.
|
|
220
|
+
const result = await (0, contract_add_1.addContract)({ source, root });
|
|
221
|
+
expect(await (0, promises_1.readFile)(result.path, "utf8")).toContain("\tcontractId:");
|
|
222
|
+
expect((await (0, contract_add_1.addContract)({ source, root, check: true })).changed).toBe(false);
|
|
223
|
+
// With it, Biome's Node script runs (Windows can't spawn the shim), formatting the same way.
|
|
224
|
+
await (0, promises_1.mkdir)(path.join(root, "node_modules/@biomejs"));
|
|
225
|
+
await (0, promises_1.symlink)(path.join(sdkRoot, "node_modules/@biomejs/biome"), path.join(root, "node_modules/@biomejs/biome"), "dir");
|
|
226
|
+
await (0, promises_1.rm)(path.join(root, "node_modules/.bin/biome"));
|
|
227
|
+
await (0, promises_1.writeFile)(path.join(root, "node_modules/.bin/biome"), "#!/bin/sh\nexit 1\n", {
|
|
228
|
+
mode: 0o755,
|
|
229
|
+
});
|
|
230
|
+
expect((await (0, contract_add_1.addContract)({ source, root, check: true })).changed).toBe(false);
|
|
231
|
+
});
|
|
232
|
+
test("reads a provider's manifest or artifact, choosing the contract when it provides several", async () => {
|
|
233
|
+
const { contract, tables } = await irOf(path.join(root, "src/rich.ts"), "Rich");
|
|
234
|
+
const other = {
|
|
235
|
+
contractId: (0, peer_id_1.newid)(),
|
|
236
|
+
version: 1,
|
|
237
|
+
name: "Other",
|
|
238
|
+
tools: [{ name: "noop", usageDescription: "Nothing.", inputFields: [], outputFields: [] }],
|
|
239
|
+
};
|
|
240
|
+
const manifest = {
|
|
241
|
+
packageId: (0, peer_id_1.newid)(),
|
|
242
|
+
version: "1.0.0",
|
|
243
|
+
guestApi: 3,
|
|
244
|
+
provides: [contract, other],
|
|
245
|
+
consumes: [],
|
|
246
|
+
tables,
|
|
247
|
+
};
|
|
248
|
+
expect(() => (0, contract_source_1.contractSourceFromManifest)(manifest)).toThrow(/choose one with --contract/);
|
|
249
|
+
expect(() => (0, contract_source_1.contractSourceFromManifest)(manifest, `${(0, peer_id_1.newid)()}@1`)).toThrow(/doesn't provide/);
|
|
250
|
+
expect((0, contract_source_1.contractSourceFromManifest)(manifest, `${contractId}@2`)).toEqual({
|
|
251
|
+
contract,
|
|
252
|
+
tables,
|
|
253
|
+
});
|
|
254
|
+
// What a provider exposes through a contract isn't part of the contract: it isn't generated.
|
|
255
|
+
const exposing = { ...contract, exposes: [{ contractId: other.contractId, version: 1 }] };
|
|
256
|
+
expect((0, contract_source_1.generateContractDeclaration)((0, contract_source_1.contractSourceFromManifest)({ ...manifest, provides: [exposing, other] }, `${contractId}@2`), { from: "x" }).source).toBe((0, contract_source_1.generateContractDeclaration)({ contract, tables }, { from: "x" }).source);
|
|
257
|
+
await (0, promises_1.writeFile)(path.join(root, "manifest.json"), `\uFEFF${JSON.stringify(manifest)}`);
|
|
258
|
+
const fromManifest = await (0, contract_add_1.addContract)({
|
|
259
|
+
source: path.join(root, "manifest.json"),
|
|
260
|
+
contract: `${contractId}@2`,
|
|
261
|
+
root,
|
|
262
|
+
outDir: "a",
|
|
263
|
+
});
|
|
264
|
+
await (0, promises_1.writeFile)(path.join(root, "package.bundle.js"),
|
|
265
|
+
// Serializing validates, which fills in defaults on what it's given.
|
|
266
|
+
(0, isolated_artifact_1.serializeIsolatedPackageArtifact)({
|
|
267
|
+
manifest: structuredClone(manifest),
|
|
268
|
+
providerSource: "void 0;",
|
|
269
|
+
}));
|
|
270
|
+
const fromArtifact = await (0, contract_add_1.addContract)({
|
|
271
|
+
source: path.join(root, "package.bundle.js"),
|
|
272
|
+
contract: `${contractId}@2`,
|
|
273
|
+
root,
|
|
274
|
+
outDir: "b",
|
|
275
|
+
});
|
|
276
|
+
expect(await irOf(fromManifest.path, "RichThings")).toEqual({ contract, tables });
|
|
277
|
+
expect(await (0, promises_1.readFile)(fromArtifact.path, "utf8")).toBe((await (0, promises_1.readFile)(fromManifest.path, "utf8")).replace("from manifest.json.", "from package.bundle.js."));
|
|
278
|
+
const windowsArtifact = path.join(root, "windows.bundle.js");
|
|
279
|
+
await (0, promises_1.writeFile)(windowsArtifact, `\uFEFF\r\n${(await (0, promises_1.readFile)(path.join(root, "package.bundle.js"), "utf8")).replace(/\n/g, "\r\n")}`);
|
|
280
|
+
const fromWindows = await (0, contract_add_1.addContract)({
|
|
281
|
+
source: windowsArtifact,
|
|
282
|
+
contract: `${contractId}@2`,
|
|
283
|
+
root,
|
|
284
|
+
outDir: "c",
|
|
285
|
+
});
|
|
286
|
+
expect(await irOf(fromWindows.path, "RichThings")).toEqual({ contract, tables });
|
|
287
|
+
});
|
|
288
|
+
test("reads getContractDefinition's result, taking each tool's policy from it", async () => {
|
|
289
|
+
const { contract, tables } = await irOf(path.join(root, "src/rich.ts"), "Rich");
|
|
290
|
+
const definition = (0, isolated_artifact_1.isolatedContractToDefinition)(contract, tables);
|
|
291
|
+
const policy = [
|
|
292
|
+
{ name: "plan", accessLevel: 40 },
|
|
293
|
+
{ name: "skim", accessLevel: 20, authorityPropagation: "preserve" },
|
|
294
|
+
];
|
|
295
|
+
const source = (0, contract_source_1.contractSourceFromDefinition)({ definition, tools: policy });
|
|
296
|
+
// The registry keeps no table storage policy and no persistent-variable bindings.
|
|
297
|
+
expect(source.contract.tools).toEqual(contract.tools);
|
|
298
|
+
expect(source.tables.map(({ versionNumber: _v, ...table }) => table)).toEqual(tables.map(({ versionNumber: _v, ...table }) => table));
|
|
299
|
+
expect(() => (0, contract_source_1.contractSourceFromDefinition)({ definition })).toThrow(/no access level/);
|
|
300
|
+
expect(() => (0, contract_source_1.contractSourceFromDefinition)({ definition, tools: [policy[0]] })).toThrow(/Runtime reported no access level for tool "skim"/);
|
|
301
|
+
await (0, promises_1.writeFile)(path.join(root, "definition.json"), JSON.stringify(definition));
|
|
302
|
+
await expect((0, contract_add_1.addContract)({ source: path.join(root, "definition.json"), root })).rejects.toThrow(/use getContractDefinition's result/);
|
|
303
|
+
await (0, promises_1.writeFile)(path.join(root, "result.json"), JSON.stringify({ definition, tools: policy }));
|
|
304
|
+
const written = await (0, contract_add_1.addContract)({ source: path.join(root, "result.json"), root });
|
|
305
|
+
expect((await irOf(written.path, "RichThings")).contract.tools).toEqual(contract.tools);
|
|
306
|
+
});
|
|
307
|
+
test("names come from the contract unless given, and must be identifiers", async () => {
|
|
308
|
+
const { contract, tables } = await irOf(path.join(root, "src/rich.ts"), "Rich");
|
|
309
|
+
expect((0, contract_source_1.generateContractDeclaration)({ contract, tables }, { from: "x" }).exportName).toBe("RichThings");
|
|
310
|
+
expect((0, contract_source_1.generateContractDeclaration)({ contract, tables }, { from: "x", exportName: "Trips" }).source).toContain("export const Trips = declareContract(");
|
|
311
|
+
const acronym = (0, contract_source_1.generateContractDeclaration)({ contract: { ...contract, name: "HTTPService" }, tables }, { from: "x" });
|
|
312
|
+
expect(acronym.fileName).toBe("http-service.v2.ts");
|
|
313
|
+
expect(acronym.source).toContain("export const httpServicePlaceChanged = defineEvent(");
|
|
314
|
+
expect(() => (0, contract_source_1.generateContractDeclaration)({ contract, tables }, { from: "x", exportName: "not ok" })).toThrow(/usable export name/);
|
|
315
|
+
for (const exportName of ["class", "z", "declareContract", "AccessLevel"]) {
|
|
316
|
+
expect(() => (0, contract_source_1.generateContractDeclaration)({ contract, tables }, { from: "x", exportName })).toThrow(/usable export name/);
|
|
317
|
+
}
|
|
318
|
+
for (const exportName of ["$", "_"]) {
|
|
319
|
+
const generated = (0, contract_source_1.generateContractDeclaration)({ contract, tables }, { from: "x", exportName });
|
|
320
|
+
const file = path.join(root, `src/${exportName}.ts`);
|
|
321
|
+
await (0, promises_1.writeFile)(file, generated.source);
|
|
322
|
+
expect(await irOf(file, exportName)).toEqual({ contract, tables });
|
|
323
|
+
}
|
|
324
|
+
await expect((0, contract_add_1.addContract)({ source: `${path.join(root, "src/rich.ts")}#Missing`, root })).rejects.toThrow(/exports no declaration or contract named Missing/);
|
|
325
|
+
});
|
|
326
|
+
test("hand-written IR survives generation, including stored defaults, blobs and opaque objects", async () => {
|
|
327
|
+
const fields = [
|
|
328
|
+
{ name: "key", type: field_type_1.FieldType.id, subType: "record" },
|
|
329
|
+
{ name: "selectedId", type: field_type_1.FieldType.id, enumValues: [contractId] },
|
|
330
|
+
{ name: "bytes", type: field_type_1.FieldType.blob },
|
|
331
|
+
{ name: "opaque", type: field_type_1.FieldType.object },
|
|
332
|
+
{ name: "literal", type: field_type_1.FieldType.any, enumValues: ["only"] },
|
|
333
|
+
{ name: "repeated", type: field_type_1.FieldType.string, enumValues: ["same", "same"] },
|
|
334
|
+
{ name: "flag", type: field_type_1.FieldType.boolean, enumValues: [true, false] },
|
|
335
|
+
{ name: "oneFlag", type: field_type_1.FieldType.boolean, enumValues: [false, false] },
|
|
336
|
+
{ name: "numericChoice", type: field_type_1.FieldType.number, enumValues: [1, 2] },
|
|
337
|
+
{ name: "numericChoices", type: field_type_1.FieldType.number, enumValues: [1, 2], isArray: true },
|
|
338
|
+
{
|
|
339
|
+
name: "nestedChoice",
|
|
340
|
+
type: field_type_1.FieldType.object,
|
|
341
|
+
fields: [{ name: "choice", type: field_type_1.FieldType.number, enumValues: [1, 2], optional: true }],
|
|
342
|
+
},
|
|
343
|
+
{ name: "count", type: field_type_1.FieldType.number, defaultValue: 5, optional: true },
|
|
344
|
+
{ name: "requiredDefault", type: field_type_1.FieldType.number, defaultValue: 7 },
|
|
345
|
+
{ name: "plainAny", type: field_type_1.FieldType.any },
|
|
346
|
+
{ name: "anyItems", type: field_type_1.FieldType.any, isArray: true },
|
|
347
|
+
{ name: "explicit", type: field_type_1.FieldType.string, optional: false, isArray: false, description: "" },
|
|
348
|
+
{ name: "__proto__", type: field_type_1.FieldType.string },
|
|
349
|
+
{
|
|
350
|
+
name: "nested",
|
|
351
|
+
type: field_type_1.FieldType.object,
|
|
352
|
+
fields: [
|
|
353
|
+
{ name: "answer", type: field_type_1.FieldType.number, defaultValue: 42 },
|
|
354
|
+
{ name: "choice", type: field_type_1.FieldType.number, enumValues: [1, 2] },
|
|
355
|
+
],
|
|
356
|
+
defaultValue: JSON.parse('{"answer":42,"choice":1,"__proto__":{"kept":true}}'),
|
|
357
|
+
},
|
|
358
|
+
];
|
|
359
|
+
const source = {
|
|
360
|
+
contract: {
|
|
361
|
+
contractId,
|
|
362
|
+
version: 1,
|
|
363
|
+
name: "Stored Fields",
|
|
364
|
+
tools: [
|
|
365
|
+
{
|
|
366
|
+
name: "go",
|
|
367
|
+
usageDescription: "Go.",
|
|
368
|
+
suggestedAccessLevel: 20,
|
|
369
|
+
inputFields: fields,
|
|
370
|
+
outputFields: fields,
|
|
371
|
+
},
|
|
372
|
+
],
|
|
373
|
+
tables: [{ name: "Records" }],
|
|
374
|
+
events: [{ name: "changed", description: "Changed.", payloadFields: fields }],
|
|
375
|
+
observables: [
|
|
376
|
+
{
|
|
377
|
+
name: "explicit",
|
|
378
|
+
description: "Explicit flags.",
|
|
379
|
+
valueType: field_type_1.FieldType.number,
|
|
380
|
+
value: { type: field_type_1.FieldType.number, optional: false },
|
|
381
|
+
writable: false,
|
|
382
|
+
},
|
|
383
|
+
],
|
|
384
|
+
},
|
|
385
|
+
tables: [{ name: "Records", description: "Records.", primaryKeyName: "key", fields }],
|
|
386
|
+
};
|
|
387
|
+
const generated = (0, contract_source_1.generateContractDeclaration)(source, { from: "manifest.json" });
|
|
388
|
+
const file = path.join(root, "src/stored.ts");
|
|
389
|
+
await (0, promises_1.writeFile)(file, generated.source);
|
|
390
|
+
const copy = await irOf(file, "StoredFields");
|
|
391
|
+
expect(copy).toEqual(source);
|
|
392
|
+
const types = path.join(root, "src/stored-types.ts");
|
|
393
|
+
await (0, promises_1.writeFile)(types, `
|
|
394
|
+
import type { StoredFieldsEvents, StoredFieldsRows } from "./stored";
|
|
395
|
+
// @ts-expect-error Required any fields still need a value.
|
|
396
|
+
export const missingEventValue: Pick<StoredFieldsEvents["changed"], "plainAny"> = {};
|
|
397
|
+
// @ts-expect-error Required any row fields still need a value.
|
|
398
|
+
export const missingRowValue: Pick<StoredFieldsRows["Records"], "plainAny"> = {};
|
|
399
|
+
export const event: Pick<StoredFieldsEvents["changed"], "plainAny"> = { plainAny: null };
|
|
400
|
+
`);
|
|
401
|
+
checkTypes(file, types);
|
|
402
|
+
const definitions = [source, copy].map(({ contract, tables }) => (0, isolated_artifact_1.isolatedContractToDefinition)(contract, tables));
|
|
403
|
+
expect((0, validate_1.validateImmutability)(definitions[0], definitions[1])).toEqual({ valid: true, errors: [] });
|
|
404
|
+
expect((0, validate_1.validateImmutability)(definitions[1], definitions[0])).toEqual({ valid: true, errors: [] });
|
|
405
|
+
const loaded = (await (0, build_package_1.loadPackageModule)(root, file));
|
|
406
|
+
const schema = loaded.StoredFields.declaration.tools[0].definition.inputSchema;
|
|
407
|
+
expect(schema.shape.bytes.safeParse(new Uint8Array([1, 2])).success).toBe(true);
|
|
408
|
+
expect(schema.shape.literal.safeParse("different").success).toBe(false);
|
|
409
|
+
expect(schema.shape.oneFlag.safeParse(false).success).toBe(true);
|
|
410
|
+
expect(schema.shape.oneFlag.safeParse(true).success).toBe(false);
|
|
411
|
+
expect(schema.shape.numericChoice.safeParse(3).success).toBe(false);
|
|
412
|
+
expect(schema.shape.numericChoices.safeParse([1, 2]).success).toBe(true);
|
|
413
|
+
expect(schema.pick({ plainAny: true }).safeParse({}).success).toBe(false);
|
|
414
|
+
expect(schema.shape.anyItems.safeParse([undefined, null, 1]).success).toBe(true);
|
|
415
|
+
expect(schema.shape.count.parse(undefined)).toBe(5);
|
|
416
|
+
expect(Object.hasOwn(schema.shape, "__proto__")).toBe(true);
|
|
417
|
+
expect(Object.hasOwn(schema.shape.nested._def.defaultValue(), "__proto__")).toBe(true);
|
|
418
|
+
});
|
|
419
|
+
test("source labels and member names stay data in generated TypeScript", async () => {
|
|
420
|
+
const { contract, tables } = await irOf(path.join(root, "src/rich.ts"), "Rich");
|
|
421
|
+
const named = {
|
|
422
|
+
...contract,
|
|
423
|
+
observables: [
|
|
424
|
+
{
|
|
425
|
+
name: '*/\nthrow new Error("injected")\n/*',
|
|
426
|
+
description: "A value.",
|
|
427
|
+
valueType: field_type_1.FieldType.string,
|
|
428
|
+
writable: false,
|
|
429
|
+
},
|
|
430
|
+
],
|
|
431
|
+
};
|
|
432
|
+
const generated = (0, contract_source_1.generateContractDeclaration)({ contract: named, tables }, { from: 'file\nthrow new Error("injected")\n//source' });
|
|
433
|
+
const file = path.join(root, "src/escaped.ts");
|
|
434
|
+
await (0, promises_1.writeFile)(file, generated.source);
|
|
435
|
+
expect((await irOf(file, "RichThings")).contract.observables).toEqual(named.observables);
|
|
436
|
+
});
|
|
437
|
+
test("selectors accept case-sensitive ids without rounding the version", () => {
|
|
438
|
+
const upper = contractId.toUpperCase();
|
|
439
|
+
expect((0, contract_source_1.parseContractSelector)(`${upper}@2`)).toEqual({ contractId: upper, version: 2 });
|
|
440
|
+
expect(() => (0, contract_source_1.parseContractSelector)(`${contractId}@9007199254740993`)).toThrow(/Expected/);
|
|
441
|
+
});
|
|
442
|
+
test("JSON sources cannot inject code or paths through numeric metadata", async () => {
|
|
443
|
+
const { contract, tables } = await irOf(path.join(root, "src/rich.ts"), "Rich");
|
|
444
|
+
const injected = '1, injected: (() => { throw new Error("injected"); })()';
|
|
445
|
+
const source = path.join(root, "unsafe.json");
|
|
446
|
+
const writeManifest = async (provided, definitions) => (0, promises_1.writeFile)(source, JSON.stringify({ provides: [provided], tables: definitions }));
|
|
447
|
+
for (const version of [injected, "../../outside", 0, 1.5, Number.MAX_SAFE_INTEGER + 1]) {
|
|
448
|
+
await writeManifest({ ...contract, version }, tables);
|
|
449
|
+
await expect((0, contract_add_1.addContract)({ source, root })).rejects.toThrow(/contract version/i);
|
|
450
|
+
}
|
|
451
|
+
for (const key of ["suggestedAccessLevel", "remoteAccessLevel"]) {
|
|
452
|
+
for (const level of [injected, -1, 101, 1.5]) {
|
|
453
|
+
await writeManifest({ ...contract, tools: [{ ...contract.tools[0], [key]: level }] }, tables);
|
|
454
|
+
await expect((0, contract_add_1.addContract)({ source, root })).rejects.toThrow(/access level/i);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
await writeManifest(contract, [{ ...tables[0], versionNumber: injected }]);
|
|
458
|
+
await expect((0, contract_add_1.addContract)({ source, root })).rejects.toThrow(/table version/i);
|
|
459
|
+
await (0, promises_1.writeFile)(source, JSON.stringify({
|
|
460
|
+
definition: { ...(0, isolated_artifact_1.isolatedContractToDefinition)(contract, tables), version: injected },
|
|
461
|
+
tools: contract.tools.map((tool) => ({ name: tool.name, accessLevel: 20 })),
|
|
462
|
+
}));
|
|
463
|
+
await expect((0, contract_add_1.addContract)({ source, root })).rejects.toThrow(/contract version/i);
|
|
464
|
+
await expect((0, promises_1.readFile)(path.join(root, "src/contracts/rich-things.v2.ts"))).rejects.toMatchObject({
|
|
465
|
+
code: "ENOENT",
|
|
466
|
+
});
|
|
467
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { IContractDefinitionWithPolicy } from "../contracts/system/contract-catalog-contract";
|
|
2
|
+
import type { IIsolatedPackageManifest, IIsolatedPackageTable, IIsolatedProvidedContract } from "../package-loader/isolated-artifact";
|
|
3
|
+
/** A contract as the generator reads it: its manifest entry and its tables' definitions. */
|
|
4
|
+
export interface IContractSource {
|
|
5
|
+
/** The contract, as a provider's manifest carries it. */
|
|
6
|
+
contract: IIsolatedProvidedContract;
|
|
7
|
+
/** Definitions of the tables the contract exposes. */
|
|
8
|
+
tables: IIsolatedPackageTable[];
|
|
9
|
+
}
|
|
10
|
+
/** A generated declaration file. */
|
|
11
|
+
export interface IGeneratedContractDeclaration {
|
|
12
|
+
/** File name: the contract's name in kebab case and its version (`voice-skill.v1.ts`). */
|
|
13
|
+
fileName: string;
|
|
14
|
+
/** The declaration's export name (`VoiceSkill`). */
|
|
15
|
+
exportName: string;
|
|
16
|
+
/** TypeScript source, before formatting. */
|
|
17
|
+
source: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The contract in a provider's manifest, by `contractId@version` (or the only one).
|
|
21
|
+
*
|
|
22
|
+
* @param manifest - A package's manifest.
|
|
23
|
+
* @param selector - `contractId@version`; optional when the package provides one contract.
|
|
24
|
+
*/
|
|
25
|
+
export declare function contractSourceFromManifest(manifest: IIsolatedPackageManifest, selector?: string): IContractSource;
|
|
26
|
+
/**
|
|
27
|
+
* A registered contract definition, with the active provider's tool policy, as the generator
|
|
28
|
+
* reads it.
|
|
29
|
+
*
|
|
30
|
+
* @param input - `getContractDefinition`'s result.
|
|
31
|
+
*/
|
|
32
|
+
export declare function contractSourceFromDefinition(input: IContractDefinitionWithPolicy): IContractSource;
|
|
33
|
+
/**
|
|
34
|
+
* Parse `contractId@version`.
|
|
35
|
+
*
|
|
36
|
+
* @param selector - The contract's id and version.
|
|
37
|
+
*/
|
|
38
|
+
export declare function parseContractSelector(selector: string): {
|
|
39
|
+
contractId: string;
|
|
40
|
+
version: number;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Write a contract's declaration as TypeScript: a `declareContract(...)` call whose zod schemas
|
|
44
|
+
* give the same IR, for a package that implements or calls a contract it doesn't own. The file
|
|
45
|
+
* is meant to be checked in and regenerated, not edited.
|
|
46
|
+
*
|
|
47
|
+
* What the IR carries comes through: names, types, optional fields, arrays, nested object
|
|
48
|
+
* fields, enum and literal values, descriptions, and each tool's access level and authority
|
|
49
|
+
* propagation. Refinements (a regex, a minimum) and defaults made by code aren't in the IR, so
|
|
50
|
+
* generated schemas check only what the IR describes. Objects keep keys their fields don't list.
|
|
51
|
+
*
|
|
52
|
+
* @param input - The contract and its tables.
|
|
53
|
+
* @param options - Where it came from (for the file's header), and an export name to use
|
|
54
|
+
* instead of the one made from the contract's name.
|
|
55
|
+
*/
|
|
56
|
+
export declare function generateContractDeclaration(input: IContractSource, options: {
|
|
57
|
+
from: string;
|
|
58
|
+
exportName?: string;
|
|
59
|
+
}): IGeneratedContractDeclaration;
|