@sanity/workbench 0.1.0-alpha.6 → 0.1.0-alpha.8

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.
Files changed (47) hide show
  1. package/dist/{log.js → _chunks-es/index.js} +7 -2
  2. package/dist/_chunks-es/index.js.map +1 -0
  3. package/dist/_internal.d.ts +13 -6
  4. package/dist/_internal.js +20 -9
  5. package/dist/_internal.js.map +1 -1
  6. package/dist/core.d.ts +1464 -0
  7. package/dist/core.js +744 -0
  8. package/dist/core.js.map +1 -0
  9. package/package.json +12 -4
  10. package/src/_exports/core.ts +1 -0
  11. package/src/_internal/index.ts +2 -1
  12. package/src/_internal/render.test.ts +91 -4
  13. package/src/_internal/render.ts +53 -33
  14. package/src/core/__tests__/__fixtures__.ts +245 -0
  15. package/src/core/applications/application-list.test.ts +222 -0
  16. package/src/core/applications/application-list.ts +103 -0
  17. package/src/core/applications/application.ts +78 -0
  18. package/src/core/applications/local-application.test.ts +93 -0
  19. package/src/core/applications/local-application.ts +56 -0
  20. package/src/core/canvases.test.ts +38 -0
  21. package/src/core/canvases.ts +81 -0
  22. package/src/core/config.ts +34 -0
  23. package/src/core/index.ts +12 -0
  24. package/src/{log → core/log}/index.ts +12 -0
  25. package/src/core/media-libraries.test.ts +38 -0
  26. package/src/core/media-libraries.ts +83 -0
  27. package/src/core/organizations.test.ts +134 -0
  28. package/src/core/organizations.ts +115 -0
  29. package/src/core/projects.test.ts +248 -0
  30. package/src/core/projects.ts +114 -0
  31. package/src/core/shared/urls.test.ts +182 -0
  32. package/src/core/shared/urls.ts +128 -0
  33. package/src/core/user-applications/core-app.test.ts +236 -0
  34. package/src/core/user-applications/core-app.ts +113 -0
  35. package/src/core/user-applications/studios/index.ts +3 -0
  36. package/src/core/user-applications/studios/schemas.test.ts +113 -0
  37. package/src/core/user-applications/studios/schemas.ts +106 -0
  38. package/src/core/user-applications/studios/studio.test.ts +997 -0
  39. package/src/core/user-applications/studios/studio.ts +498 -0
  40. package/src/core/user-applications/studios/workspace.ts +143 -0
  41. package/src/core/user-applications/user-application.test.ts +125 -0
  42. package/src/core/user-applications/user-application.ts +107 -0
  43. package/src/vite-env.d.ts +8 -0
  44. package/dist/log.d.ts +0 -48
  45. package/dist/log.js.map +0 -1
  46. package/src/_exports/log.ts +0 -1
  47. /package/src/{log → core/log}/index.test.ts +0 -0
@@ -23,7 +23,12 @@ function createLogger({
23
23
  debug: (message, context) => logAtLevel("debug", message, context)
24
24
  };
25
25
  }
26
+ const logger = createLogger({
27
+ namespace: "sanity-workbench",
28
+ logLevel: "debug"
29
+ });
26
30
  export {
27
- createLogger
31
+ createLogger,
32
+ logger
28
33
  };
29
- //# sourceMappingURL=log.js.map
34
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/core/log/index.ts"],"sourcesContent":["/**\n * Log levels in order of verbosity (least to most)\n * - none: Silent\n * - error: Critical failures that prevent operation\n * - warn: Issues that may cause problems but don't stop execution\n * - info: High-level informational messages (default)\n * - debug: Detailed debugging information (maintainer level)\n * - trace: Very detailed tracing — sets `internal: true` on context\n * @public\n */\nexport type LogLevel = \"none\" | \"error\" | \"warn\" | \"info\" | \"debug\";\n\n/**\n * Namespaces organize logs by functional domain.\n * @internal\n */\nexport type LogNamespace = string;\n\ntype LogContext = { [key: string]: unknown };\n\n/**\n * @public\n */\nexport interface Logger {\n error: (message: string, context?: LogContext) => void;\n warn: (message: string, context?: LogContext) => void;\n info: (message: string, context?: LogContext) => void;\n debug: (message: string, context?: LogContext) => void;\n}\n\nconst LEVELS: readonly LogLevel[] = [\"none\", \"error\", \"warn\", \"info\", \"debug\"];\n\ninterface LoggerOptions {\n namespace?: LogNamespace;\n context?: LogContext;\n logLevel?: LogLevel;\n}\n\n/**\n * @public\n */\nexport function createLogger({\n namespace,\n context: baseContext,\n logLevel = \"info\",\n}: LoggerOptions = {}): Logger {\n function isLevelEnabled(level: LogLevel): boolean {\n return LEVELS.indexOf(level) <= LEVELS.indexOf(logLevel);\n }\n\n function logAtLevel(\n level: LogLevel,\n message: string,\n context?: LogContext,\n ): void {\n if (!isLevelEnabled(level)) return;\n\n const merged =\n (baseContext ?? context) ? { ...baseContext, ...context } : undefined;\n const args: unknown[] = [\n ...(namespace ? [`[${namespace}]`] : []),\n message,\n ...(merged ? [merged] : []),\n ];\n\n if (level === \"error\") console.error(...args);\n else if (level === \"warn\") console.warn(...args);\n // oxlint-disable-next-line no-console\n else if (level === \"info\") console.info(...args);\n // oxlint-disable-next-line no-console\n else console.debug(...args);\n }\n\n return {\n error: (message, context) => logAtLevel(\"error\", message, context),\n warn: (message, context) => logAtLevel(\"warn\", message, context),\n info: (message, context) => logAtLevel(\"info\", message, context),\n debug: (message, context) => logAtLevel(\"debug\", message, context),\n };\n}\n\n/**\n * Shared workbench logger instance. Use this from both the workbench host\n * and its remotes so lifecycle and diagnostic logs appear under a single\n * namespace.\n *\n * @public\n */\nexport const logger: Logger = createLogger({\n namespace: \"sanity-workbench\",\n logLevel: \"debug\",\n});\n"],"names":[],"mappings":"AA8BA,MAAM,SAA8B,CAAC,QAAQ,SAAS,QAAQ,QAAQ,OAAO;AAWtE,SAAS,aAAa;AAAA,EAC3B;AAAA,EACA,SAAS;AAAA,EACT,WAAW;AACb,IAAmB,IAAY;AAC7B,WAAS,eAAe,OAA0B;AAChD,WAAO,OAAO,QAAQ,KAAK,KAAK,OAAO,QAAQ,QAAQ;AAAA,EACzD;AAEA,WAAS,WACP,OACA,SACA,SACM;AACN,QAAI,CAAC,eAAe,KAAK,EAAG;AAE5B,UAAM,SACH,eAAe,UAAW,EAAE,GAAG,aAAa,GAAG,QAAA,IAAY,QACxD,OAAkB;AAAA,MACtB,GAAI,YAAY,CAAC,IAAI,SAAS,GAAG,IAAI,CAAA;AAAA,MACrC;AAAA,MACA,GAAI,SAAS,CAAC,MAAM,IAAI,CAAA;AAAA,IAAC;AAGvB,cAAU,UAAS,QAAQ,MAAM,GAAG,IAAI,IACnC,UAAU,SAAQ,QAAQ,KAAK,GAAG,IAAI,IAEtC,UAAU,SAAQ,QAAQ,KAAK,GAAG,IAAI,IAE1C,QAAQ,MAAM,GAAG,IAAI;AAAA,EAC5B;AAEA,SAAO;AAAA,IACL,OAAO,CAAC,SAAS,YAAY,WAAW,SAAS,SAAS,OAAO;AAAA,IACjE,MAAM,CAAC,SAAS,YAAY,WAAW,QAAQ,SAAS,OAAO;AAAA,IAC/D,MAAM,CAAC,SAAS,YAAY,WAAW,QAAQ,SAAS,OAAO;AAAA,IAC/D,OAAO,CAAC,SAAS,YAAY,WAAW,SAAS,SAAS,OAAO;AAAA,EAAA;AAErE;AASO,MAAM,SAAiB,aAAa;AAAA,EACzC,WAAW;AAAA,EACX,UAAU;AACZ,CAAC;"}
@@ -3,12 +3,21 @@
3
3
  *
4
4
  * @public
5
5
  */
6
- export declare type Config = {
6
+ export declare interface Config {
7
7
  /**
8
8
  * The organization ID to use when rendering the workbench.
9
9
  */
10
- organizationId: string | undefined;
11
- };
10
+ organizationId?: string;
11
+ }
12
+
13
+ /**
14
+ * Options for rendering a remote module, such as a user application.
15
+ *
16
+ * @public
17
+ */
18
+ declare interface RemoteModuleRenderOptions {
19
+ reactStrictMode?: boolean;
20
+ }
12
21
 
13
22
  /**
14
23
  * Creates a Module Federation instance, loads a remote workbench
@@ -32,8 +41,6 @@ export declare function renderWorkbench(
32
41
  *
33
42
  * @public
34
43
  */
35
- export declare interface RenderWorkbenchOptions {
36
- reactStrictMode?: boolean;
37
- }
44
+ export declare interface RenderWorkbenchOptions extends RemoteModuleRenderOptions {}
38
45
 
39
46
  export {};
package/dist/_internal.js CHANGED
@@ -1,16 +1,12 @@
1
1
  import { createInstance } from "@sanity/federation/runtime";
2
2
  import { log } from "@sanity/federation/runtime/plugins/log";
3
- import { createLogger } from "./log.js";
4
- const logger = createLogger({
5
- namespace: "sanity-workbench",
6
- logLevel: "debug"
7
- }), REMOTE_NAME = "workbench-remote", REMOTE_MODULE = "App";
3
+ import { BehaviorSubject } from "rxjs";
4
+ import { logger } from "./_chunks-es/index.js";
5
+ const REMOTE_NAME = "workbench-remote", REMOTE_MODULE = "App", LOCAL_APPS_HMR_EVENT = "sanity:workbench:local-applications", LOCAL_APPS_HMR_REQUEST = "sanity:workbench:get-local-applications";
8
6
  async function renderWorkbench(rootElement, config, options) {
9
7
  if (!rootElement)
10
8
  throw new Error("Missing root element to mount application into");
11
- const remoteUrl = import.meta.env.SANITY_INTERNAL_WORKBENCH_REMOTE_URL;
12
- if (!remoteUrl)
13
- throw new Error("SANITY_INTERNAL_WORKBENCH_REMOTE_URL is not set");
9
+ const remoteUrl = import.meta.env.SANITY_INTERNAL_WORKBENCH_REMOTE_URL ?? "https://workbench-remote.sanity.dev/mf-manifest.json";
14
10
  let remoteModule = await createInstance({
15
11
  name: "sanity-workbench",
16
12
  plugins: [log(logger.debug)],
@@ -27,7 +23,22 @@ async function renderWorkbench(rootElement, config, options) {
27
23
  throw new Error(
28
24
  `Remote module "${REMOTE_NAME}/${REMOTE_MODULE}" did not expose a render function`
29
25
  );
30
- return remoteModule.render(rootElement, { config }, options);
26
+ let localApplications, cleanupHmr = () => {
27
+ };
28
+ if (import.meta.hot) {
29
+ const localApps$ = new BehaviorSubject([]), handler = (payload) => {
30
+ localApps$.next(payload.applications);
31
+ };
32
+ import.meta.hot.on(LOCAL_APPS_HMR_EVENT, handler), import.meta.hot.send(LOCAL_APPS_HMR_REQUEST), localApplications = localApps$, cleanupHmr = () => import.meta.hot?.off(LOCAL_APPS_HMR_EVENT, handler);
33
+ }
34
+ const unmount = remoteModule.render(
35
+ rootElement,
36
+ { config, localApplications },
37
+ options
38
+ );
39
+ return () => {
40
+ cleanupHmr(), unmount();
41
+ };
31
42
  }
32
43
  export {
33
44
  renderWorkbench
@@ -1 +1 @@
1
- {"version":3,"file":"_internal.js","sources":["../src/_internal/render.ts"],"sourcesContent":["import { createInstance } from \"@sanity/federation/runtime\";\nimport { log } from \"@sanity/federation/runtime/plugins/log\";\nimport { createLogger } from \"../log\";\n\nconst logger = createLogger({\n namespace: \"sanity-workbench\",\n logLevel: \"debug\",\n});\n\n/**\n * Workbench configuration.\n *\n * @public\n */\nexport type Config = {\n /**\n * The organization ID to use when rendering the workbench.\n */\n organizationId: string | undefined;\n};\n\n/**\n * Options for rendering the workbench.\n *\n * @public\n */\nexport interface RenderWorkbenchOptions {\n reactStrictMode?: boolean;\n}\n\ndeclare global {\n interface ImportMetaEnv {\n readonly SANITY_INTERNAL_WORKBENCH_REMOTE_URL: string;\n }\n interface ImportMeta {\n readonly env: ImportMetaEnv;\n }\n}\n\n/**\n * Module defining the remote workbench application.\n *\n * @internal\n */\ninterface WorkbenchRemoteModule {\n render: (\n rootElement: HTMLElement,\n props: { config?: Config },\n options?: RenderWorkbenchOptions,\n ) => () => void;\n}\n\nconst REMOTE_NAME = \"workbench-remote\";\nconst REMOTE_MODULE = \"App\";\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) {\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\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 plugins: [log(logger.debug)],\n\n remotes: [\n {\n name: REMOTE_NAME,\n entry: remoteUrl,\n },\n ],\n });\n\n let remoteModule = await mf.loadRemote<WorkbenchRemoteModule>(\n `${REMOTE_NAME}/${REMOTE_MODULE}`,\n );\n\n if (!remoteModule || typeof remoteModule.render !== \"function\") {\n throw new Error(\n `Remote module \"${REMOTE_NAME}/${REMOTE_MODULE}\" did not expose a render function`,\n );\n }\n\n return remoteModule.render(rootElement, { config }, options);\n}\n"],"names":[],"mappings":";;;AAIA,MAAM,SAAS,aAAa;AAAA,EAC1B,WAAW;AAAA,EACX,UAAU;AACZ,CAAC,GA6CK,cAAc,oBACd,gBAAgB;AAatB,eAAsB,gBACpB,aACA,QACA,SACA;AACA,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,gDAAgD;AAGlE,QAAM,YAAY,YAAY,IAAI;AAElC,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,iDAAiD;AAenE,MAAI,eAAe,MAZR,eAAe;AAAA,IACxB,MAAM;AAAA,IACN,SAAS,CAAC,IAAI,OAAO,KAAK,CAAC;AAAA,IAE3B,SAAS;AAAA,MACP;AAAA,QACE,MAAM;AAAA,QACN,OAAO;AAAA,MAAA;AAAA,IACT;AAAA,EACF,CACD,EAE2B;AAAA,IAC1B,GAAG,WAAW,IAAI,aAAa;AAAA,EAAA;AAGjC,MAAI,CAAC,gBAAgB,OAAO,aAAa,UAAW;AAClD,UAAM,IAAI;AAAA,MACR,kBAAkB,WAAW,IAAI,aAAa;AAAA,IAAA;AAIlD,SAAO,aAAa,OAAO,aAAa,EAAE,OAAA,GAAU,OAAO;AAC7D;"}
1
+ {"version":3,"file":"_internal.js","sources":["../src/_internal/render.ts"],"sourcesContent":["/// <reference types=\"vite/client\" />\n\nimport { createInstance } from \"@sanity/federation/runtime\";\nimport { log } from \"@sanity/federation/runtime/plugins/log\";\nimport { BehaviorSubject, type Observable } from \"rxjs\";\n\nimport type { LocalApplicationData } from \"../core/applications/local-application\";\nimport type { Config, RemoteModuleRenderOptions } from \"../core/config\";\nimport { logger } from \"../core/log\";\n\n/**\n * Options for rendering the workbench.\n *\n * @public\n */\nexport interface RenderWorkbenchOptions extends RemoteModuleRenderOptions {}\n\ndeclare global {\n interface ImportMetaEnv {\n readonly SANITY_INTERNAL_WORKBENCH_REMOTE_URL: string;\n }\n interface ImportMeta {\n readonly env: ImportMetaEnv;\n }\n}\n\ntype RemoteModule<TProps extends any> = {\n render: (\n rootElement: HTMLElement,\n props: TProps,\n options?: RenderWorkbenchOptions,\n ) => () => void;\n};\n\n/**\n * Module defining the remote workbench application.\n *\n * @internal\n */\ntype WorkbenchRemoteModule = RemoteModule<{\n config?: Config;\n localApplications?: Observable<LocalApplicationData[]>;\n}>;\n\nconst REMOTE_NAME = \"workbench-remote\";\nconst REMOTE_MODULE = \"App\";\n\nconst LOCAL_APPS_HMR_EVENT = \"sanity:workbench:local-applications\";\nconst LOCAL_APPS_HMR_REQUEST = \"sanity:workbench:get-local-applications\";\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) {\n if (!rootElement) {\n throw new Error(\"Missing root element to mount application into\");\n }\n\n const remoteUrl =\n import.meta.env.SANITY_INTERNAL_WORKBENCH_REMOTE_URL ??\n \"https://workbench-remote.sanity.dev/mf-manifest.json\";\n\n const mf = createInstance({\n name: \"sanity-workbench\",\n plugins: [log(logger.debug)],\n\n remotes: [\n {\n name: REMOTE_NAME,\n entry: remoteUrl,\n },\n ],\n });\n\n let remoteModule = await mf.loadRemote<WorkbenchRemoteModule>(\n `${REMOTE_NAME}/${REMOTE_MODULE}`,\n );\n\n if (!remoteModule || typeof remoteModule.render !== \"function\") {\n throw new Error(\n `Remote module \"${REMOTE_NAME}/${REMOTE_MODULE}\" did not expose a render function`,\n );\n }\n\n let localApplications = undefined;\n let cleanupHmr = () => {};\n\n if (import.meta.hot) {\n const localApps$ = new BehaviorSubject<LocalApplicationData[]>([]);\n\n const handler = (payload: { applications: LocalApplicationData[] }) => {\n localApps$.next(payload.applications);\n };\n\n import.meta.hot.on(LOCAL_APPS_HMR_EVENT, handler);\n import.meta.hot.send(LOCAL_APPS_HMR_REQUEST);\n\n localApplications = localApps$;\n\n cleanupHmr = () => import.meta.hot?.off(LOCAL_APPS_HMR_EVENT, handler);\n }\n\n const unmount = remoteModule.render(\n rootElement,\n { config, localApplications },\n options,\n );\n\n return () => {\n cleanupHmr();\n unmount();\n };\n}\n"],"names":[],"mappings":";;;;AA4CA,MAAM,cAAc,oBACd,gBAAgB,OAEhB,uBAAuB,uCACvB,yBAAyB;AAa/B,eAAsB,gBACpB,aACA,QACA,SACA;AACA,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,gDAAgD;AAGlE,QAAM,YACJ,YAAY,IAAI,wCAChB;AAcF,MAAI,eAAe,MAZR,eAAe;AAAA,IACxB,MAAM;AAAA,IACN,SAAS,CAAC,IAAI,OAAO,KAAK,CAAC;AAAA,IAE3B,SAAS;AAAA,MACP;AAAA,QACE,MAAM;AAAA,QACN,OAAO;AAAA,MAAA;AAAA,IACT;AAAA,EACF,CACD,EAE2B;AAAA,IAC1B,GAAG,WAAW,IAAI,aAAa;AAAA,EAAA;AAGjC,MAAI,CAAC,gBAAgB,OAAO,aAAa,UAAW;AAClD,UAAM,IAAI;AAAA,MACR,kBAAkB,WAAW,IAAI,aAAa;AAAA,IAAA;AAIlD,MAAI,mBACA,aAAa,MAAM;AAAA,EAAC;AAExB,MAAI,YAAY,KAAK;AACnB,UAAM,aAAa,IAAI,gBAAwC,CAAA,CAAE,GAE3D,UAAU,CAAC,YAAsD;AACrE,iBAAW,KAAK,QAAQ,YAAY;AAAA,IACtC;AAEA,gBAAY,IAAI,GAAG,sBAAsB,OAAO,GAChD,YAAY,IAAI,KAAK,sBAAsB,GAE3C,oBAAoB,YAEpB,aAAa,MAAM,YAAY,KAAK,IAAI,sBAAsB,OAAO;AAAA,EACvE;AAEA,QAAM,UAAU,aAAa;AAAA,IAC3B;AAAA,IACA,EAAE,QAAQ,kBAAA;AAAA,IACV;AAAA,EAAA;AAGF,SAAO,MAAM;AACX,eAAA,GACA,QAAA;AAAA,EACF;AACF;"}