@edhspace/api 0.0.0-stage → 0.2.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.
@@ -0,0 +1,311 @@
1
+ // src/contracts.ts
2
+ import { z } from "zod";
3
+ var protocol = "edhspace.extension";
4
+ var apiVersion = 1;
5
+ var maxRequestBytes = 8192;
6
+ var maxResponseBytes = 262144;
7
+ var permissionSchema = z.enum([
8
+ "deck.read",
9
+ "selection.read",
10
+ "deck.propose",
11
+ "cards.inspect",
12
+ "cards.search",
13
+ "workspace.open",
14
+ "notifications.show",
15
+ "storage.read",
16
+ "storage.write",
17
+ "preferences.read",
18
+ "preferences.write",
19
+ "network.request"
20
+ ]);
21
+ var id = z.string().regex(/^[a-z0-9][a-z0-9.-]{0,99}$/);
22
+ var text = z.string().trim().min(1).max(300);
23
+ var token = z.uuid();
24
+ var networkEndpointSchema = z.strictObject({
25
+ id,
26
+ title: text,
27
+ url: z.string().max(2e3).refine((value) => {
28
+ try {
29
+ const url = new URL(value);
30
+ const host = url.hostname.replace(/\.$/, "");
31
+ return url.protocol === "https:" && !url.username && !url.password && !url.search && !url.hash && (!url.port || url.port === "443") && host.includes(".") && !host.includes(":") && !/^\d+(?:\.\d+){3}$/.test(host) && !/(?:^|\.)(?:localhost|local|internal|home\.arpa)$/.test(host) && url.pathname.endsWith("/");
32
+ } catch {
33
+ return false;
34
+ }
35
+ }, "Use a public HTTPS base URL ending in /, without credentials or query parameters."),
36
+ methods: z.array(z.enum(["GET", "POST"])).min(1).max(2).refine((values) => new Set(values).size === values.length)
37
+ });
38
+ var networkRequestSchema = z.strictObject({
39
+ endpoint: id,
40
+ path: z.string().max(500).default("").refine(
41
+ (value) => /^[a-zA-Z0-9._~/-]*$/.test(value) && !value.startsWith("/") && !value.includes("//") && !value.split("/").some((part) => part === "." || part === ".."),
42
+ "Use a relative API path without escaping its base URL."
43
+ ),
44
+ method: z.enum(["GET", "POST"]).default("GET"),
45
+ query: z.record(z.string().min(1).max(100), z.string().max(2e3)).default({}).refine((values) => Object.keys(values).length <= 20),
46
+ body: z.json().optional()
47
+ }).refine(
48
+ (value) => value.method !== "GET" || value.body === void 0,
49
+ "GET requests cannot have a body."
50
+ );
51
+ var preferenceSchema = z.discriminatedUnion("type", [
52
+ z.strictObject({
53
+ id,
54
+ title: text,
55
+ description: text.optional(),
56
+ type: z.literal("text"),
57
+ default: z.string().max(1e3)
58
+ }),
59
+ z.strictObject({
60
+ id,
61
+ title: text,
62
+ description: text.optional(),
63
+ type: z.literal("boolean"),
64
+ default: z.boolean()
65
+ }),
66
+ z.strictObject({
67
+ id,
68
+ title: text,
69
+ description: text.optional(),
70
+ type: z.literal("number"),
71
+ default: z.number().finite()
72
+ }),
73
+ z.strictObject({
74
+ id,
75
+ title: text,
76
+ description: text.optional(),
77
+ type: z.literal("select"),
78
+ default: id,
79
+ options: z.array(z.strictObject({ value: id, label: text })).min(1).max(30)
80
+ })
81
+ ]).refine(
82
+ (preference) => preference.type !== "select" || preference.options.some((option) => option.value === preference.default),
83
+ "Default must be a declared option."
84
+ );
85
+ var extensionManifestSchema = z.strictObject({
86
+ id,
87
+ apiVersion: z.literal(apiVersion),
88
+ version: z.string().regex(/^\d+\.\d+\.\d+$/),
89
+ title: text,
90
+ description: z.string().trim().min(1).max(1e3),
91
+ author: text,
92
+ permissions: z.array(permissionSchema).max(12).refine((items) => new Set(items).size === items.length),
93
+ panels: z.array(
94
+ z.strictObject({
95
+ id,
96
+ title: text,
97
+ location: z.enum(["workspace", "sidebar"]).optional()
98
+ })
99
+ ).min(1).max(6).refine((items) => new Set(items.map((item) => item.id)).size === items.length),
100
+ commands: z.array(z.strictObject({ id, title: text })).max(6).default([]),
101
+ preferences: z.array(preferenceSchema).max(20).default([]).refine((items) => new Set(items.map((item) => item.id)).size === items.length),
102
+ network: z.array(networkEndpointSchema).max(8).default([]).refine((items) => new Set(items.map((item) => item.id)).size === items.length)
103
+ }).refine(
104
+ (manifest) => manifest.permissions.includes("network.request") === manifest.network.length > 0,
105
+ "Declare network.request and its reviewed API endpoints together."
106
+ );
107
+ var extensionCardSchema = z.strictObject({
108
+ id: z.string().min(1).max(200),
109
+ name: text,
110
+ quantity: z.number().int().min(1).max(1e3),
111
+ manaCost: z.string().max(200).optional(),
112
+ manaValue: z.number().min(0).max(1e3).optional(),
113
+ typeLine: text.optional(),
114
+ commander: z.boolean().optional()
115
+ });
116
+ var extensionDeckSchema = z.strictObject({
117
+ id: token,
118
+ name: text,
119
+ format: z.string().min(1).max(50),
120
+ revision: z.string().min(1).max(200),
121
+ cards: z.array(extensionCardSchema).max(500)
122
+ });
123
+ var deckOperationSchema = z.discriminatedUnion("op", [
124
+ z.strictObject({
125
+ op: z.literal("add"),
126
+ cardId: text,
127
+ quantity: z.number().int().min(1).max(75),
128
+ category: id
129
+ }),
130
+ z.strictObject({
131
+ op: z.literal("remove"),
132
+ cardId: text,
133
+ quantity: z.number().int().min(1).max(75)
134
+ }),
135
+ z.strictObject({
136
+ op: z.literal("tag"),
137
+ cardIds: z.array(text).min(1).max(100),
138
+ tags: z.array(id).max(20)
139
+ })
140
+ ]);
141
+ var proposalReceiptSchema = z.discriminatedUnion("status", [
142
+ z.strictObject({
143
+ id: token,
144
+ status: z.enum(["pending", "dismissed"]),
145
+ revision: text.optional(),
146
+ undoId: token.optional()
147
+ }),
148
+ z.strictObject({ id: token, status: z.literal("applied"), revision: text, undoId: token })
149
+ ]);
150
+ var envelope = {
151
+ protocol: z.literal(protocol),
152
+ apiVersion: z.literal(apiVersion),
153
+ sessionId: token
154
+ };
155
+ var base = { ...envelope, type: z.literal("request"), id: token };
156
+ var requestSchema = z.discriminatedUnion("method", [
157
+ z.strictObject({ ...base, method: z.literal("network.request"), params: networkRequestSchema }),
158
+ z.strictObject({ ...base, method: z.literal("deck.read"), params: z.strictObject({}) }),
159
+ z.strictObject({ ...base, method: z.literal("selection.read"), params: z.strictObject({}) }),
160
+ z.strictObject({
161
+ ...base,
162
+ method: z.literal("deck.propose"),
163
+ params: z.strictObject({
164
+ expectedRevision: text,
165
+ operations: z.array(deckOperationSchema).min(1).max(75)
166
+ })
167
+ }),
168
+ z.strictObject({
169
+ ...base,
170
+ method: z.literal("cards.inspect"),
171
+ params: z.strictObject({ cardId: text })
172
+ }),
173
+ z.strictObject({
174
+ ...base,
175
+ method: z.literal("cards.search"),
176
+ params: z.strictObject({ query: text, page: z.number().int().min(1).max(20) })
177
+ }),
178
+ z.strictObject({
179
+ ...base,
180
+ method: z.literal("cards.get"),
181
+ params: z.strictObject({ name: text })
182
+ }),
183
+ z.strictObject({
184
+ ...base,
185
+ method: z.literal("workspace.open"),
186
+ params: z.strictObject({ panelId: id, location: z.enum(["workspace", "sidebar"]) })
187
+ }),
188
+ z.strictObject({
189
+ ...base,
190
+ method: z.literal("notifications.show"),
191
+ params: z.strictObject({ message: text, kind: z.enum(["info", "success", "error"]) })
192
+ }),
193
+ z.strictObject({
194
+ ...base,
195
+ method: z.literal("storage.get"),
196
+ params: z.strictObject({ key: id })
197
+ }),
198
+ z.strictObject({
199
+ ...base,
200
+ method: z.literal("storage.set"),
201
+ params: z.strictObject({ key: id, value: z.json() })
202
+ }),
203
+ z.strictObject({ ...base, method: z.literal("preferences.get"), params: z.strictObject({}) }),
204
+ z.strictObject({
205
+ ...base,
206
+ method: z.literal("preferences.set"),
207
+ params: z.strictObject({ key: id, value: z.json() })
208
+ })
209
+ ]);
210
+ var resultSchemas = {
211
+ "network.request": z.strictObject({
212
+ status: z.number().int().min(200).max(299),
213
+ data: z.json()
214
+ }),
215
+ "deck.read": extensionDeckSchema,
216
+ "selection.read": z.array(extensionCardSchema).max(100),
217
+ "deck.propose": proposalReceiptSchema,
218
+ "cards.inspect": z.strictObject({ opened: z.literal(true) }),
219
+ "cards.search": z.strictObject({
220
+ cards: z.array(extensionCardSchema).max(100),
221
+ nextPage: z.number().int().min(1).max(20).optional()
222
+ }),
223
+ "cards.get": extensionCardSchema.nullable(),
224
+ "workspace.open": z.strictObject({ panelId: id }),
225
+ "notifications.show": z.strictObject({ shown: z.literal(true) }),
226
+ "storage.get": z.json(),
227
+ "storage.set": z.strictObject({ saved: z.literal(true) }),
228
+ "preferences.get": z.record(z.string(), z.json()),
229
+ "preferences.set": z.strictObject({ saved: z.literal(true) })
230
+ };
231
+ var methodPermissions = {
232
+ "network.request": "network.request",
233
+ "deck.read": "deck.read",
234
+ "selection.read": "selection.read",
235
+ "deck.propose": "deck.propose",
236
+ "cards.inspect": "cards.inspect",
237
+ "cards.search": "cards.search",
238
+ "cards.get": "cards.search",
239
+ "workspace.open": "workspace.open",
240
+ "notifications.show": "notifications.show",
241
+ "storage.get": "storage.read",
242
+ "storage.set": "storage.write",
243
+ "preferences.get": "preferences.read",
244
+ "preferences.set": "preferences.write"
245
+ };
246
+ var errorCodeSchema = z.enum([
247
+ "invalid",
248
+ "denied",
249
+ "revoked",
250
+ "unavailable",
251
+ "rate-limited",
252
+ "replayed",
253
+ "timeout",
254
+ "disconnected"
255
+ ]);
256
+ var ExtensionError = class extends Error {
257
+ constructor(message, code) {
258
+ super(message);
259
+ this.code = code;
260
+ this.name = "ExtensionError";
261
+ }
262
+ };
263
+ var responseSchema = z.discriminatedUnion("ok", [
264
+ z.strictObject({
265
+ ...envelope,
266
+ type: z.literal("response"),
267
+ id: token,
268
+ ok: z.literal(true),
269
+ result: z.unknown()
270
+ }),
271
+ z.strictObject({
272
+ ...envelope,
273
+ type: z.literal("response"),
274
+ id: token,
275
+ ok: z.literal(false),
276
+ error: z.strictObject({ code: errorCodeSchema, message: z.string().max(300) })
277
+ })
278
+ ]);
279
+ function messageBytes(value) {
280
+ try {
281
+ const encoded = JSON.stringify(value);
282
+ if (!encoded) throw new Error("Missing message.");
283
+ return new TextEncoder().encode(encoded).byteLength;
284
+ } catch {
285
+ throw new ExtensionError("The extension message is not serializable.", "invalid");
286
+ }
287
+ }
288
+ function parseResult(method, value) {
289
+ const result = resultSchemas[method].safeParse(value);
290
+ if (!result.success)
291
+ throw new ExtensionError("The host returned an invalid result.", "invalid");
292
+ return result.data;
293
+ }
294
+
295
+ export {
296
+ protocol,
297
+ apiVersion,
298
+ maxRequestBytes,
299
+ maxResponseBytes,
300
+ permissionSchema,
301
+ networkEndpointSchema,
302
+ networkRequestSchema,
303
+ preferenceSchema,
304
+ extensionManifestSchema,
305
+ requestSchema,
306
+ methodPermissions,
307
+ ExtensionError,
308
+ responseSchema,
309
+ messageBytes,
310
+ parseResult
311
+ };
@@ -0,0 +1,136 @@
1
+ import * as zod from 'zod';
2
+ import { n as ExtensionInvoker, d as ExtensionPermission, D as DeckOperation, o as NetworkRequest, a as ExtensionArguments } from './contracts-r8d4RACz.js';
3
+
4
+ type ExtensionClient = ReturnType<typeof createExtensionClient>;
5
+ declare function createExtensionClient(invoke: ExtensionInvoker, sessionId: string, permissions: readonly ExtensionPermission[], timeoutMs?: number, networkTimeoutMs?: number): {
6
+ permissions: readonly ("deck.read" | "selection.read" | "deck.propose" | "cards.inspect" | "cards.search" | "workspace.open" | "notifications.show" | "storage.read" | "storage.write" | "preferences.read" | "preferences.write" | "network.request")[];
7
+ deck: {
8
+ read: () => Promise<{
9
+ id: string;
10
+ name: string;
11
+ format: string;
12
+ revision: string;
13
+ cards: {
14
+ id: string;
15
+ name: string;
16
+ quantity: number;
17
+ manaCost?: string | undefined;
18
+ manaValue?: number | undefined;
19
+ typeLine?: string | undefined;
20
+ commander?: boolean | undefined;
21
+ }[];
22
+ }>;
23
+ propose: (operations: DeckOperation[]) => Promise<{
24
+ id: string;
25
+ status: "pending" | "dismissed";
26
+ revision?: string | undefined;
27
+ undoId?: string | undefined;
28
+ } | {
29
+ id: string;
30
+ status: "applied";
31
+ revision: string;
32
+ undoId: string;
33
+ }>;
34
+ addCard: (cardId: string, category: string, quantity?: number) => Promise<{
35
+ id: string;
36
+ status: "pending" | "dismissed";
37
+ revision?: string | undefined;
38
+ undoId?: string | undefined;
39
+ } | {
40
+ id: string;
41
+ status: "applied";
42
+ revision: string;
43
+ undoId: string;
44
+ }>;
45
+ removeCard: (cardId: string, quantity?: number) => Promise<{
46
+ id: string;
47
+ status: "pending" | "dismissed";
48
+ revision?: string | undefined;
49
+ undoId?: string | undefined;
50
+ } | {
51
+ id: string;
52
+ status: "applied";
53
+ revision: string;
54
+ undoId: string;
55
+ }>;
56
+ applyTags: (cardIds: string[], tags: string[]) => Promise<{
57
+ id: string;
58
+ status: "pending" | "dismissed";
59
+ revision?: string | undefined;
60
+ undoId?: string | undefined;
61
+ } | {
62
+ id: string;
63
+ status: "applied";
64
+ revision: string;
65
+ undoId: string;
66
+ }>;
67
+ };
68
+ selection: {
69
+ read: () => Promise<{
70
+ id: string;
71
+ name: string;
72
+ quantity: number;
73
+ manaCost?: string | undefined;
74
+ manaValue?: number | undefined;
75
+ typeLine?: string | undefined;
76
+ commander?: boolean | undefined;
77
+ }[]>;
78
+ };
79
+ network: {
80
+ request: (request: NetworkRequest) => Promise<{
81
+ status: number;
82
+ data: zod.JSONType;
83
+ }>;
84
+ };
85
+ cards: {
86
+ inspect: (cardId: string) => Promise<{
87
+ opened: true;
88
+ }>;
89
+ search: (query: string, page?: number) => Promise<{
90
+ cards: {
91
+ id: string;
92
+ name: string;
93
+ quantity: number;
94
+ manaCost?: string | undefined;
95
+ manaValue?: number | undefined;
96
+ typeLine?: string | undefined;
97
+ commander?: boolean | undefined;
98
+ }[];
99
+ nextPage?: number | undefined;
100
+ }>;
101
+ getByName: (name: string) => Promise<{
102
+ id: string;
103
+ name: string;
104
+ quantity: number;
105
+ manaCost?: string | undefined;
106
+ manaValue?: number | undefined;
107
+ typeLine?: string | undefined;
108
+ commander?: boolean | undefined;
109
+ } | null>;
110
+ };
111
+ workspace: {
112
+ openPanel: (panelId: string, location?: "workspace" | "sidebar") => Promise<{
113
+ panelId: string;
114
+ }>;
115
+ };
116
+ notifications: {
117
+ show: (message: string, kind?: "info" | "success" | "error") => Promise<{
118
+ shown: true;
119
+ }>;
120
+ };
121
+ storage: {
122
+ get: (key: string) => Promise<zod.JSONType>;
123
+ set: (key: string, value: ExtensionArguments["storage.set"]["value"]) => Promise<{
124
+ saved: true;
125
+ }>;
126
+ };
127
+ preferences: {
128
+ get: () => Promise<Record<string, zod.JSONType>>;
129
+ set: (key: string, value: ExtensionArguments["preferences.set"]["value"]) => Promise<{
130
+ saved: true;
131
+ }>;
132
+ };
133
+ dispose: () => void;
134
+ };
135
+
136
+ export { type ExtensionClient as E, createExtensionClient as c };
@@ -0,0 +1,136 @@
1
+ import * as zod from 'zod';
2
+ import { n as ExtensionInvoker, d as ExtensionPermission, D as DeckOperation, o as NetworkRequest, a as ExtensionArguments } from './contracts-r8d4RACz.cjs';
3
+
4
+ type ExtensionClient = ReturnType<typeof createExtensionClient>;
5
+ declare function createExtensionClient(invoke: ExtensionInvoker, sessionId: string, permissions: readonly ExtensionPermission[], timeoutMs?: number, networkTimeoutMs?: number): {
6
+ permissions: readonly ("deck.read" | "selection.read" | "deck.propose" | "cards.inspect" | "cards.search" | "workspace.open" | "notifications.show" | "storage.read" | "storage.write" | "preferences.read" | "preferences.write" | "network.request")[];
7
+ deck: {
8
+ read: () => Promise<{
9
+ id: string;
10
+ name: string;
11
+ format: string;
12
+ revision: string;
13
+ cards: {
14
+ id: string;
15
+ name: string;
16
+ quantity: number;
17
+ manaCost?: string | undefined;
18
+ manaValue?: number | undefined;
19
+ typeLine?: string | undefined;
20
+ commander?: boolean | undefined;
21
+ }[];
22
+ }>;
23
+ propose: (operations: DeckOperation[]) => Promise<{
24
+ id: string;
25
+ status: "pending" | "dismissed";
26
+ revision?: string | undefined;
27
+ undoId?: string | undefined;
28
+ } | {
29
+ id: string;
30
+ status: "applied";
31
+ revision: string;
32
+ undoId: string;
33
+ }>;
34
+ addCard: (cardId: string, category: string, quantity?: number) => Promise<{
35
+ id: string;
36
+ status: "pending" | "dismissed";
37
+ revision?: string | undefined;
38
+ undoId?: string | undefined;
39
+ } | {
40
+ id: string;
41
+ status: "applied";
42
+ revision: string;
43
+ undoId: string;
44
+ }>;
45
+ removeCard: (cardId: string, quantity?: number) => Promise<{
46
+ id: string;
47
+ status: "pending" | "dismissed";
48
+ revision?: string | undefined;
49
+ undoId?: string | undefined;
50
+ } | {
51
+ id: string;
52
+ status: "applied";
53
+ revision: string;
54
+ undoId: string;
55
+ }>;
56
+ applyTags: (cardIds: string[], tags: string[]) => Promise<{
57
+ id: string;
58
+ status: "pending" | "dismissed";
59
+ revision?: string | undefined;
60
+ undoId?: string | undefined;
61
+ } | {
62
+ id: string;
63
+ status: "applied";
64
+ revision: string;
65
+ undoId: string;
66
+ }>;
67
+ };
68
+ selection: {
69
+ read: () => Promise<{
70
+ id: string;
71
+ name: string;
72
+ quantity: number;
73
+ manaCost?: string | undefined;
74
+ manaValue?: number | undefined;
75
+ typeLine?: string | undefined;
76
+ commander?: boolean | undefined;
77
+ }[]>;
78
+ };
79
+ network: {
80
+ request: (request: NetworkRequest) => Promise<{
81
+ status: number;
82
+ data: zod.JSONType;
83
+ }>;
84
+ };
85
+ cards: {
86
+ inspect: (cardId: string) => Promise<{
87
+ opened: true;
88
+ }>;
89
+ search: (query: string, page?: number) => Promise<{
90
+ cards: {
91
+ id: string;
92
+ name: string;
93
+ quantity: number;
94
+ manaCost?: string | undefined;
95
+ manaValue?: number | undefined;
96
+ typeLine?: string | undefined;
97
+ commander?: boolean | undefined;
98
+ }[];
99
+ nextPage?: number | undefined;
100
+ }>;
101
+ getByName: (name: string) => Promise<{
102
+ id: string;
103
+ name: string;
104
+ quantity: number;
105
+ manaCost?: string | undefined;
106
+ manaValue?: number | undefined;
107
+ typeLine?: string | undefined;
108
+ commander?: boolean | undefined;
109
+ } | null>;
110
+ };
111
+ workspace: {
112
+ openPanel: (panelId: string, location?: "workspace" | "sidebar") => Promise<{
113
+ panelId: string;
114
+ }>;
115
+ };
116
+ notifications: {
117
+ show: (message: string, kind?: "info" | "success" | "error") => Promise<{
118
+ shown: true;
119
+ }>;
120
+ };
121
+ storage: {
122
+ get: (key: string) => Promise<zod.JSONType>;
123
+ set: (key: string, value: ExtensionArguments["storage.set"]["value"]) => Promise<{
124
+ saved: true;
125
+ }>;
126
+ };
127
+ preferences: {
128
+ get: () => Promise<Record<string, zod.JSONType>>;
129
+ set: (key: string, value: ExtensionArguments["preferences.set"]["value"]) => Promise<{
130
+ saved: true;
131
+ }>;
132
+ };
133
+ dispose: () => void;
134
+ };
135
+
136
+ export { type ExtensionClient as E, createExtensionClient as c };