@sanity/workbench 0.1.0-alpha.1 → 0.1.0-alpha.10

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,181 @@
1
+ // eslint-disable-next-line no-restricted-imports
2
+ import type { Resource as ProtocolResource } from "@sanity/message-protocol";
3
+ import { z } from "zod";
4
+
5
+ import {
6
+ AbstractApplication,
7
+ type AbstractApplicationType,
8
+ } from "../applications/application";
9
+ import type { CoreAppUserApplicationManifest } from "./core-app";
10
+ import type { ClientManifest } from "./studios/schemas";
11
+
12
+ /**
13
+ * User application ID schema, branded for type safety.
14
+ * @public
15
+ */
16
+ export const UserApplicationId = z
17
+ .string()
18
+ .nonempty()
19
+ .brand("UserApplicationId");
20
+
21
+ /**
22
+ * User application ID type, branded for type safety.
23
+ * @public
24
+ */
25
+ export type UserApplicationId = z.output<typeof UserApplicationId>;
26
+
27
+ /**
28
+ * Validates and brands a string as a UserApplicationId.
29
+ * @public
30
+ */
31
+ export function brandUserApplicationId(id: string): UserApplicationId {
32
+ return UserApplicationId.parse(id);
33
+ }
34
+
35
+ const LocalUserApplicationBase = z.object({
36
+ host: z.string(),
37
+ port: z.number(),
38
+ /** The `deployment.appId` from the application's `sanity.cli.ts`, when set. */
39
+ id: z.string().optional(),
40
+ });
41
+
42
+ /**
43
+ * Raw data for a local application discovered by the CLI dev server.
44
+ *
45
+ * The CLI forwards the full studio or app manifest so the workbench can
46
+ * derive icons, titles, workspaces and schema references the same way it
47
+ * does for deployed applications. The manifest shape is discriminated on
48
+ * `type`: studios receive a `ClientManifest`, core apps a `CoreAppUserApplicationManifest`.
49
+ *
50
+ * When set on a `UserApplication`, `isLocal` is `true` and `url`/`isFederated`
51
+ * honour the local dev-server instead of the deployed application's host.
52
+ *
53
+ * @public
54
+ */
55
+ export const LocalUserApplication = z.discriminatedUnion("type", [
56
+ LocalUserApplicationBase.extend({
57
+ type: z.literal("studio"),
58
+ manifest: z.custom<ClientManifest>().optional(),
59
+ }),
60
+ LocalUserApplicationBase.extend({
61
+ type: z.literal("coreApp"),
62
+ manifest: z.custom<CoreAppUserApplicationManifest>().optional(),
63
+ }),
64
+ ]);
65
+
66
+ /**
67
+ * @public
68
+ */
69
+ export type LocalUserApplication = z.output<typeof LocalUserApplication>;
70
+
71
+ /**
72
+ * @public
73
+ */
74
+ export const UserApplicationBase = z.object({
75
+ id: UserApplicationId,
76
+ appHost: z.string(),
77
+ urlType: z.enum(["internal", "external"]),
78
+ createdAt: z.string(),
79
+ updatedAt: z.string(),
80
+ dashboardStatus: z.enum(["default", "disabled"]),
81
+ });
82
+
83
+ /**
84
+ * @public
85
+ */
86
+ export type UserApplicationBase = z.output<typeof UserApplicationBase>;
87
+
88
+ /**
89
+ * @public
90
+ */
91
+ export const ActiveDeployment = z.object({
92
+ id: z.string(),
93
+ version: z.string(),
94
+ isActiveDeployment: z.boolean(),
95
+ userApplicationId: z.string(),
96
+ isAutoUpdating: z.boolean(),
97
+ size: z.number(),
98
+ deployedAt: z.string(),
99
+ deployedBy: z.string(),
100
+ createdAt: z.string(),
101
+ updatedAt: z.string(),
102
+ });
103
+
104
+ /**
105
+ * @public
106
+ */
107
+ export abstract class UserApplication<
108
+ TUserApplication extends UserApplicationBase,
109
+ TType extends Extract<AbstractApplicationType, "coreApp" | "studio">,
110
+ TProtocolResource extends ProtocolResource = Extract<
111
+ ProtocolResource,
112
+ { type: TType }
113
+ >,
114
+ > extends AbstractApplication<TType, TProtocolResource> {
115
+ readonly application: TUserApplication;
116
+
117
+ readonly id: UserApplicationId;
118
+
119
+ /**
120
+ * For local applications (`isLocal === true`), the deployed application
121
+ * that shares the same `id` — if one was passed at construction. `null`
122
+ * for deployed applications or when no remote twin was provided.
123
+ */
124
+ readonly remoteApplication: this | null;
125
+
126
+ readonly #isLocal: boolean;
127
+
128
+ constructor(
129
+ application: TUserApplication,
130
+ type: TType,
131
+ options: {
132
+ isLocal?: boolean;
133
+ remoteApplication?: UserApplication<
134
+ TUserApplication,
135
+ TType,
136
+ TProtocolResource
137
+ > | null;
138
+ } = {},
139
+ ) {
140
+ super(type);
141
+ this.application = application;
142
+ this.id = brandUserApplicationId(application.id);
143
+ this.#isLocal = options.isLocal ?? false;
144
+ this.remoteApplication =
145
+ (options.remoteApplication as this | undefined) ?? null;
146
+ }
147
+
148
+ abstract get subtitle(): string | undefined;
149
+
150
+ /**
151
+ * Local dev servers are rendered as federated remotes; deployed applications
152
+ * are rendered in an iframe.
153
+ */
154
+ get isFederated(): boolean {
155
+ return this.isLocal;
156
+ }
157
+
158
+ override get isLocal(): boolean {
159
+ return this.#isLocal;
160
+ }
161
+
162
+ /**
163
+ * @returns A fully resolved URL instance for the application.
164
+ * For internal applications, constructs a URL using the Sanity studio domain
165
+ * pattern resolved from the environment at the consuming app's build time.
166
+ * For external applications (including local dev servers), returns the
167
+ * provided app host as a URL.
168
+ */
169
+ get url(): URL {
170
+ if (this.application.urlType === "internal") {
171
+ if (import.meta.env.VITE_SANITY_ENV === "production") {
172
+ return new URL(`https://${this.application.appHost}.sanity.studio`);
173
+ }
174
+ return new URL(
175
+ `https://${this.application.appHost}.studio.${import.meta.env.VITE_SANITY_DOMAIN}`,
176
+ );
177
+ }
178
+
179
+ return new URL(this.application.appHost);
180
+ }
181
+ }
@@ -0,0 +1,8 @@
1
+ interface ImportMetaEnv {
2
+ readonly VITE_SANITY_ENV: "production" | "staging";
3
+ readonly VITE_SANITY_DOMAIN: "sanity.work" | "sanity.io";
4
+ }
5
+
6
+ interface ImportMeta {
7
+ readonly env: ImportMetaEnv;
8
+ }
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var runtime = require("@sanity/federation/runtime");
4
- const REMOTE_NAME = "workbench-remote", REMOTE_MODULE = "./App";
5
- async function renderWorkbench(rootElement, config, options) {
6
- if (!rootElement)
7
- throw new Error("Missing root element to mount application into");
8
- const remoteUrl = (void 0).SANITY_INTERNAL_WORKBENCH_REMOTE_URL;
9
- if (!remoteUrl)
10
- throw new Error("SANITY_INTERNAL_WORKBENCH_REMOTE_URL is not set");
11
- const mf = runtime.createInstance({
12
- name: "sanity-workbench"
13
- });
14
- mf.registerRemotes([
15
- {
16
- name: REMOTE_NAME,
17
- entry: remoteUrl
18
- }
19
- ]);
20
- let remoteModule;
21
- try {
22
- remoteModule = await mf.loadRemote(
23
- `${REMOTE_NAME}/${REMOTE_MODULE}`
24
- );
25
- } catch (error) {
26
- throw new Error(
27
- `Failed to load remote workbench module from "${remoteUrl}"`,
28
- { cause: error }
29
- );
30
- }
31
- if (!remoteModule || typeof remoteModule.render != "function")
32
- throw new Error(
33
- `Remote module from "${remoteUrl}" did not expose a render function`
34
- );
35
- return remoteModule.render(rootElement, config, options);
36
- }
37
- exports.renderWorkbench = renderWorkbench;
38
- //# sourceMappingURL=_internal.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_internal.cjs","sources":["../src/_internal/render.ts"],"sourcesContent":["import { createInstance } from \"@sanity/federation/runtime\";\n\nconst REMOTE_NAME = \"workbench-remote\";\nconst REMOTE_MODULE = \"./App\";\n\n/**\n * @public\n */\nexport type Config = undefined;\n\n/**\n * @public\n */\nexport interface RenderWorkbenchOptions {\n reactStrictMode?: boolean;\n}\n\ninterface RemoteModule {\n render(\n rootElement: HTMLElement,\n config?: Config,\n options?: RenderWorkbenchOptions,\n ): () => void;\n}\n\n/**\n * Creates a Module Federation instance, loads a remote workbench\n * application, and renders it into the provided root element.\n *\n * @param rootElement - The DOM element to render into\n * @param config - Workbench configuration (reserved for future use)\n * @param options - Rendering options forwarded to the remote\n * @returns A cleanup function that unmounts the remote application\n *\n * @public\n */\nexport async function renderWorkbench(\n rootElement: HTMLElement,\n config?: Config,\n options?: RenderWorkbenchOptions,\n): Promise<() => void> {\n if (!rootElement) {\n throw new Error(\"Missing root element to mount application into\");\n }\n\n const remoteUrl = import.meta.env.SANITY_INTERNAL_WORKBENCH_REMOTE_URL;\n if (!remoteUrl) {\n throw new Error(\"SANITY_INTERNAL_WORKBENCH_REMOTE_URL is not set\");\n }\n\n const mf = createInstance({\n name: \"sanity-workbench\",\n });\n\n mf.registerRemotes([\n {\n name: REMOTE_NAME,\n entry: remoteUrl,\n },\n ]);\n\n let remoteModule: RemoteModule | null;\n try {\n remoteModule = await mf.loadRemote<RemoteModule>(\n `${REMOTE_NAME}/${REMOTE_MODULE}`,\n );\n } catch (error) {\n throw new Error(\n `Failed to load remote workbench module from \"${remoteUrl}\"`,\n { cause: error },\n );\n }\n\n if (!remoteModule || typeof remoteModule.render !== \"function\") {\n throw new Error(\n `Remote module from \"${remoteUrl}\" did not expose a render function`,\n );\n }\n\n return remoteModule.render(rootElement, config, options);\n}\n"],"names":["createInstance"],"mappings":";;;AAEA,MAAM,cAAc,oBACd,gBAAgB;AAiCtB,eAAsB,gBACpB,aACA,QACA,SACqB;AACrB,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,gDAAgD;AAGlE,QAAM,YAAY,SAAgB;AAClC,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,iDAAiD;AAGnE,QAAM,KAAKA,QAAAA,eAAe;AAAA,IACxB,MAAM;AAAA,EAAA,CACP;AAED,KAAG,gBAAgB;AAAA,IACjB;AAAA,MACE,MAAM;AAAA,MACN,OAAO;AAAA,IAAA;AAAA,EACT,CACD;AAED,MAAI;AACJ,MAAI;AACF,mBAAe,MAAM,GAAG;AAAA,MACtB,GAAG,WAAW,IAAI,aAAa;AAAA,IAAA;AAAA,EAEnC,SAAS,OAAO;AACd,UAAM,IAAI;AAAA,MACR,gDAAgD,SAAS;AAAA,MACzD,EAAE,OAAO,MAAA;AAAA,IAAM;AAAA,EAEnB;AAEA,MAAI,CAAC,gBAAgB,OAAO,aAAa,UAAW;AAClD,UAAM,IAAI;AAAA,MACR,uBAAuB,SAAS;AAAA,IAAA;AAIpC,SAAO,aAAa,OAAO,aAAa,QAAQ,OAAO;AACzD;;"}
@@ -1,30 +0,0 @@
1
- /**
2
- * @public
3
- */
4
- export declare type Config = undefined;
5
-
6
- /**
7
- * Creates a Module Federation instance, loads a remote workbench
8
- * application, and renders it into the provided root element.
9
- *
10
- * @param rootElement - The DOM element to render into
11
- * @param config - Workbench configuration (reserved for future use)
12
- * @param options - Rendering options forwarded to the remote
13
- * @returns A cleanup function that unmounts the remote application
14
- *
15
- * @public
16
- */
17
- export declare function renderWorkbench(
18
- rootElement: HTMLElement,
19
- config?: Config,
20
- options?: RenderWorkbenchOptions,
21
- ): Promise<() => void>;
22
-
23
- /**
24
- * @public
25
- */
26
- export declare interface RenderWorkbenchOptions {
27
- reactStrictMode?: boolean;
28
- }
29
-
30
- export {};
package/src/env.d.ts DELETED
@@ -1,7 +0,0 @@
1
- interface ImportMetaEnv {
2
- readonly SANITY_INTERNAL_WORKBENCH_REMOTE_URL: string;
3
- }
4
-
5
- interface ImportMeta {
6
- readonly env: ImportMetaEnv;
7
- }