@sanity/workbench 0.1.0-alpha.5 → 0.1.0-alpha.7

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 (43) hide show
  1. package/dist/{log.js → _chunks-es/index.js} +1 -1
  2. package/dist/_chunks-es/index.js.map +1 -0
  3. package/dist/_internal.d.ts +13 -6
  4. package/dist/_internal.js +21 -5
  5. package/dist/_internal.js.map +1 -1
  6. package/dist/core.d.ts +906 -0
  7. package/dist/core.js +642 -0
  8. package/dist/core.js.map +1 -0
  9. package/package.json +11 -5
  10. package/src/_exports/core.ts +1 -0
  11. package/src/_internal/index.ts +2 -1
  12. package/src/_internal/render.test.ts +56 -1
  13. package/src/_internal/render.ts +52 -28
  14. package/src/core/__tests__/__fixtures__.ts +248 -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 +52 -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/core/media-libraries.test.ts +38 -0
  25. package/src/core/media-libraries.ts +83 -0
  26. package/src/core/organizations.test.ts +134 -0
  27. package/src/core/organizations.ts +115 -0
  28. package/src/core/projects.test.ts +248 -0
  29. package/src/core/projects.ts +114 -0
  30. package/src/core/shared/urls.test.ts +182 -0
  31. package/src/core/shared/urls.ts +128 -0
  32. package/src/core/user-applications/core-app.test.ts +151 -0
  33. package/src/core/user-applications/core-app.ts +57 -0
  34. package/src/core/user-applications/studio.test.ts +928 -0
  35. package/src/core/user-applications/studio.ts +656 -0
  36. package/src/core/user-applications/user-application.test.ts +126 -0
  37. package/src/core/user-applications/user-application.ts +76 -0
  38. package/src/vite-env.d.ts +8 -0
  39. package/dist/log.d.ts +0 -48
  40. package/dist/log.js.map +0 -1
  41. package/src/_exports/log.ts +0 -1
  42. /package/src/{log → core/log}/index.test.ts +0 -0
  43. /package/src/{log → core/log}/index.ts +0 -0
@@ -0,0 +1,656 @@
1
+ import type {
2
+ StudioApplication as StudioApplicationData,
3
+ // eslint-disable-next-line no-restricted-imports
4
+ StudioResource as ProtocolStudioResource,
5
+ WorkspaceManifest,
6
+ } from "@sanity/message-protocol";
7
+ import type { SemVer } from "semver";
8
+ import { coerce, gt, gte, lt, rsort, valid } from "semver";
9
+ import { z } from "zod";
10
+
11
+ import { AbstractApplication } from "../applications/application";
12
+ import {
13
+ brandProjectId,
14
+ ProjectId as ProjectIdSchema,
15
+ type Project,
16
+ type ProjectId,
17
+ } from "../projects";
18
+ import { joinUrlPaths, normalizePath } from "../shared/urls";
19
+ import { type UserApplicationId } from "./user-application";
20
+ import { UserApplication } from "./user-application";
21
+
22
+ const WORKSPACE_MANIFEST_SCHEMA = z.object({
23
+ name: z.string(),
24
+ title: z.string(),
25
+ subtitle: z.string().nullable().optional(),
26
+ basePath: z.string(),
27
+ projectId: ProjectIdSchema,
28
+ dataset: z.string(),
29
+ icon: z.string().optional().nullable(),
30
+ schema: z.string(),
31
+ tools: z.string().optional(),
32
+ });
33
+
34
+ const STUDIO_MANIFEST_SCHEMA = z
35
+ .object({
36
+ // data from manifest
37
+ version: z.number(),
38
+ createdAt: z.string(),
39
+ studioVersion: z.string().nullable().optional(),
40
+ // data from workspace object
41
+ workspaces: z.array(WORKSPACE_MANIFEST_SCHEMA),
42
+ })
43
+ .superRefine((data, ctx) => {
44
+ if (!data.version) {
45
+ ctx.addIssue({
46
+ code: "invalid_type",
47
+ message: "Manifest version is too old",
48
+ expected: "number",
49
+ });
50
+ }
51
+
52
+ if (data.version < 2) {
53
+ ctx.addIssue({
54
+ code: "invalid_value",
55
+ message: "Manifest version is too old",
56
+ values: [2, 3],
57
+ });
58
+ }
59
+
60
+ if (data.version >= 3 && !data.studioVersion) {
61
+ ctx.addIssue({
62
+ code: "invalid_type",
63
+ message: "Manifest version 3 or higher requires a `studioVersion`",
64
+ expected: "string",
65
+ });
66
+ }
67
+ });
68
+
69
+ const DEFAULT_WORKSPACE_DATA = {
70
+ name: "default",
71
+ title: "Default",
72
+ basePath: "/",
73
+ } as const satisfies Partial<Workspace>;
74
+
75
+ /**
76
+ * @internal
77
+ */
78
+ export class StudioApplication extends UserApplication<
79
+ StudioApplicationData,
80
+ "studio",
81
+ never
82
+ > {
83
+ /**
84
+ * Returns a list of studio workspaces based on the application manifest.
85
+ * If there is no manifest, or alternatively it is not valid, then we create a default workspace.
86
+ */
87
+ readonly workspaces: readonly StudioWorkspace[] = [];
88
+
89
+ readonly project: Project<false, false>;
90
+
91
+ /**
92
+ * @param application - The studio application to create a list of workspaces for
93
+ * @param projects - The projects available in the organization. It's not enough to just pass
94
+ * the project that associates with the application because that is the project the app is deployed in relation to.
95
+ * The workspaces may have different projects completely.
96
+ */
97
+ constructor(
98
+ application: StudioApplicationData,
99
+ projects: Project<false, false>[],
100
+ ) {
101
+ super(application, "studio");
102
+
103
+ /**
104
+ * If the application has a manifest, then we validate it and create
105
+ * a workspace for each manifest entry. Otherwise, we will create a "default"
106
+ * workspace which is pretty much identical to when a user has a single workspace
107
+ * studio application using the term "default" in places.
108
+ */
109
+ let workspaces: Workspace[] = [];
110
+ if (this.hasManifest) {
111
+ /**
112
+ * If the workspaces fail to parse we dont want to throw an error because
113
+ * this could potentially crash the app. Instead we log a warning and return an empty array.
114
+ */
115
+ try {
116
+ workspaces = STUDIO_MANIFEST_SCHEMA.parse(
117
+ application.manifestData?.value,
118
+ ).workspaces;
119
+ } catch (error) {
120
+ console.warn(
121
+ `Failed to parse manifest for application ${application.id}`,
122
+ error,
123
+ );
124
+ }
125
+ }
126
+
127
+ if (workspaces.length === 0) {
128
+ /**
129
+ * If the user application does not have a valid manifest or no workspaces,
130
+ * we attempt to get the manifest from the server-side maninfest first and then
131
+ * fall back to the live-manifest.
132
+ *
133
+ * In the future this should happen even before checking the traditional manifest,
134
+ * but for now we keep the current order to not introduce breaking changes.
135
+ *
136
+ * The live manifest is inherintly less reliable as it depends on which user has
137
+ * uploaded it, which is why this is wrapped in a feature flag for now.
138
+ */
139
+ const deploymentManifest = application.activeDeployment?.manifest;
140
+ // const liveManifest = growthbook.isOn('dashboard-use-live-manifest')
141
+ // ? application.config['live-manifest']?.value
142
+ // : null
143
+
144
+ const serverManifest = deploymentManifest; /*?? liveManifest*/
145
+ const serverManifestWorkspaces = serverManifest?.workspaces;
146
+
147
+ if (
148
+ Array.isArray(serverManifestWorkspaces) &&
149
+ serverManifestWorkspaces.length
150
+ ) {
151
+ workspaces = serverManifestWorkspaces.map((w) => ({
152
+ ...w,
153
+ projectId: brandProjectId(w.projectId),
154
+ }));
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Filter all the workspaces that have a project the user does not have access to
160
+ */
161
+ const workspacesWithProjectsMap = workspaces.reduce((acc, workspace) => {
162
+ const project = projects.find((p) => p.id === workspace.projectId);
163
+
164
+ if (project) {
165
+ acc.set(workspace, project);
166
+ } else {
167
+ console.warn(
168
+ `Project not found for application ${application.id} and workspace ${workspace.name}. This workspace has been omitted.`,
169
+ );
170
+ }
171
+
172
+ return acc;
173
+ }, new Map<Workspace, Project<false, false>>());
174
+
175
+ const brandedProjectId = brandProjectId(application.projectId);
176
+ const project = projects.find((p) => p.id === brandedProjectId);
177
+
178
+ if (!project) {
179
+ throw new Error(`Project not found for application ${application.id}`);
180
+ }
181
+
182
+ if (workspacesWithProjectsMap.size === 0) {
183
+ /**
184
+ * If there are still no workspaces, we create a default workspace.
185
+ */
186
+ workspacesWithProjectsMap.set(
187
+ {
188
+ ...DEFAULT_WORKSPACE_DATA,
189
+ projectId: brandedProjectId,
190
+ } satisfies Workspace,
191
+ project,
192
+ );
193
+ }
194
+
195
+ this.workspaces = Object.freeze(
196
+ Array.from(workspacesWithProjectsMap.entries()).map(([workspace, p]) => {
197
+ /**
198
+ * A workspace is considered the default if the dashboard generated it OR because
199
+ * the properties match that of the default workspace generated by the studio.
200
+ * Which is why these values will match because dashboard generates an identical
201
+ * workspace to the default studio one.
202
+ */
203
+ const isDefaultWorkspace =
204
+ workspace.name === DEFAULT_WORKSPACE_DATA.name &&
205
+ workspace.basePath === DEFAULT_WORKSPACE_DATA.basePath &&
206
+ workspace.title === DEFAULT_WORKSPACE_DATA.title;
207
+
208
+ return new StudioWorkspace(this, workspace, p, isDefaultWorkspace);
209
+ }),
210
+ );
211
+
212
+ this.project = project;
213
+ }
214
+
215
+ get href() {
216
+ return `/studio/${this.application.id}`;
217
+ }
218
+
219
+ get title() {
220
+ // Get the title from the user application, this has the highest precedence
221
+ const title = this.get("title");
222
+ if (title) {
223
+ return title;
224
+ }
225
+
226
+ // If there are multiple workspaces and the studio is internal, use the project display name
227
+ if (this.workspaces.length > 1 && this.get("urlType") === "internal") {
228
+ return this.project.displayName;
229
+ }
230
+
231
+ // Otherwise use the title of the first workspace
232
+ return this.workspaces[0].title;
233
+ }
234
+
235
+ get subtitle() {
236
+ return new URL(this.url).hostname;
237
+ }
238
+
239
+ get<TKey extends keyof StudioApplicationData>(
240
+ attr: TKey,
241
+ ): StudioApplicationData[TKey] {
242
+ if (!(attr in this.application)) {
243
+ throw new Error(
244
+ `Attribute ${attr.toString()} does not exist on studio ${this.application.id}`,
245
+ );
246
+ }
247
+
248
+ return this.application[attr];
249
+ }
250
+
251
+ get hasManifest(): boolean {
252
+ return (
253
+ typeof this.application.manifestData?.value?.version === "number" &&
254
+ this.application.manifestData?.value?.version >= 2
255
+ );
256
+ }
257
+
258
+ get hasSchema(): boolean {
259
+ // In this case we can not look at `this.workspaces` because it won't contain workspaces the user does
260
+ // not have access to. The application has a schema, even if the user can't access any of the workspaces,
261
+ // otherwise it would be displayed as not having a manifest in the setup guide and therefore considered
262
+ // partially compatible.
263
+ const workspaces = this.get("manifestData")?.value?.workspaces ?? [];
264
+
265
+ if (workspaces.length === 0) {
266
+ return false;
267
+ }
268
+
269
+ return workspaces.every((w) => Boolean(w.schema));
270
+ }
271
+
272
+ private resolveVersion() {
273
+ // const growthbook = getGrowthbook()
274
+
275
+ let version: string | undefined;
276
+
277
+ if (this.get("urlType") === "internal") {
278
+ version = this.get("activeDeployment")?.version;
279
+ } else {
280
+ const manifest = this.get("manifestData")?.value;
281
+ // The property 'studioVersion' exists only on external studios,
282
+ // starting from manifest.version 3
283
+ version =
284
+ manifest && "studioVersion" in manifest
285
+ ? manifest.studioVersion
286
+ : undefined;
287
+ }
288
+
289
+ /**
290
+ * Self-hosted studios are often not publicly accessible, which means that
291
+ * Brett often times cannot fetch the manifest to read the version. In this case
292
+ * we try to read the version from the deployment manifest or 'live-manifest', if it exists, to do
293
+ * everything we can to determine the version.
294
+ *
295
+ * Since the data of the live-maninfest can not be trusted, we also must validate
296
+ * that the version is a valid semver version before returning it. It always represents
297
+ * the last known version from when the studio was connected to Sanity's infrastructure,
298
+ * which might not be the version this studio is currently running (e.g. because the version
299
+ * has been downgraded).
300
+ */
301
+ const deploymentManifest = this.get("activeDeployment")?.manifest;
302
+ // const liveManifest = growthbook.isOn('dashboard-use-live-manifest')
303
+ // ? this.get('config')?.['live-manifest']?.value
304
+ // : null
305
+
306
+ const serverManifest = deploymentManifest; /*?? liveManifest*/
307
+ const bundleVersion = serverManifest?.bundleVersion;
308
+
309
+ if (bundleVersion && valid(coerce(bundleVersion))) {
310
+ if (!version || (version && gt(bundleVersion, version))) {
311
+ version = bundleVersion;
312
+ }
313
+ }
314
+
315
+ if (!version || !valid(coerce(version))) {
316
+ return null;
317
+ }
318
+
319
+ return coerce(version);
320
+ }
321
+
322
+ get version() {
323
+ const version = this.resolveVersion();
324
+ return version ? version.toString() : null;
325
+ }
326
+
327
+ get compatibilityStatus(): CompatibilityStatus {
328
+ return StudioApplication.resolveCompatibilityStatus(this);
329
+ }
330
+
331
+ /**
332
+ * Used to calculate the compatibility status of a given studio application.
333
+ * Optionally if you've resolved the version elsewhere provide that value to
334
+ * get the new compatibility status without mutating the application.
335
+ */
336
+ static resolveCompatibilityStatus(
337
+ application: StudioApplication,
338
+ version: string | null = application.version,
339
+ ): CompatibilityStatus {
340
+ if (
341
+ version === null ||
342
+ lt(version, StudioApplication.MinimumStudioVersion)
343
+ ) {
344
+ return StudioApplication.CompatibilityStatuses.UNKNOWN;
345
+ }
346
+
347
+ if (
348
+ !application.hasSchema ||
349
+ !application.hasManifest ||
350
+ StudioApplication.resolveIssues(application, version).length > 0
351
+ ) {
352
+ return StudioApplication.CompatibilityStatuses.PARTIALLY_COMPATIBLE;
353
+ }
354
+
355
+ return StudioApplication.CompatibilityStatuses.FULLY_COMPATIBLE;
356
+ }
357
+
358
+ get isAutoRedirecting(): boolean {
359
+ return StudioApplication.resolveIsAutoRedirecting(this);
360
+ }
361
+
362
+ /**
363
+ * Mirrors the `isRedirectable` function from Saison defined in
364
+ * https://github.com/sanity-io/saison/blob/83556405d23e07f6d3a71c76249c67e33fe1101f/src/utils/applications.ts
365
+ *
366
+ * Returns whether a studio application is auto-redirecting, meaning it can only be accessed in the context of
367
+ * Dashboard.
368
+ */
369
+ static resolveIsAutoRedirecting(
370
+ application: StudioApplication,
371
+ versionArg = application.version,
372
+ ) {
373
+ let version: string | SemVer | null = versionArg;
374
+
375
+ if (application.get("urlType") === "external" || !version) {
376
+ return false;
377
+ }
378
+
379
+ if (!application.get("activeDeployment")?.isAutoUpdating) {
380
+ // If the studio is not auto-updating, we need to check if the version supports
381
+ // workspace switcher (3.92.0+) otherwise it would not be redirected.
382
+ return gt(version, "3.92.0");
383
+ }
384
+
385
+ const autoUpdatingVersion = application.get("autoUpdatingVersion");
386
+
387
+ if (autoUpdatingVersion) {
388
+ if (["next", "stable", "latest"].includes(autoUpdatingVersion)) {
389
+ return true;
390
+ }
391
+
392
+ const autoUpdatingVersionPinnedVersion = coerce(autoUpdatingVersion);
393
+
394
+ if (autoUpdatingVersionPinnedVersion) {
395
+ version = autoUpdatingVersionPinnedVersion;
396
+ }
397
+ }
398
+
399
+ return gt(version, StudioApplication.MinimumStudioVersion);
400
+ }
401
+
402
+ /**
403
+ * Returns a list of issues that prevent the studio from functioning properly in the Dashboard.
404
+ * This static value depends on the version of the studio that comes from the `activeDeployment` property.
405
+ * As such, if the studio is auto-updating, this list will be incorrect & instead you should use
406
+ * the static method `resolveIssues` to get the correct issues by passing the resolved version.
407
+ */
408
+ get issues(): StudioIssues {
409
+ return StudioApplication.resolveIssues(this);
410
+ }
411
+
412
+ static resolveIssues(
413
+ application: StudioApplication,
414
+ version: string | null = application.version,
415
+ ): StudioIssues {
416
+ const issues: StudioIssues = StudioApplication.Features.filter(
417
+ (feature) => {
418
+ return !application.isFeatureSupported(feature.id, version);
419
+ },
420
+ );
421
+
422
+ if (!application.hasManifest) {
423
+ issues.push({
424
+ id: StudioApplication.StudioIssues.ISSUE_MANIFEST,
425
+ });
426
+ }
427
+
428
+ return issues;
429
+ }
430
+
431
+ protected isFeatureSupported(
432
+ feature: StudioDashboardIssue,
433
+ version = this.version,
434
+ ) {
435
+ const featureVersion = StudioApplication.Features.find(
436
+ (_) => _.id === feature,
437
+ )?.version;
438
+
439
+ if (!featureVersion || !version) {
440
+ return false;
441
+ }
442
+
443
+ return gte(version, featureVersion);
444
+ }
445
+
446
+ toProtocolResource(): never {
447
+ throw new Error(
448
+ "Studio application resources cannot be converted to protocol resources",
449
+ );
450
+ }
451
+
452
+ static CompatibilityStatuses = {
453
+ UNKNOWN: "unknown",
454
+ PARTIALLY_COMPATIBLE: "partially-compatible",
455
+ FULLY_COMPATIBLE: "fully-compatible",
456
+ } as const;
457
+
458
+ static StudioIssues = {
459
+ ISSUE_ACTIVITY: "ACTIVITY",
460
+ ISSUE_AGENT: "AGENT",
461
+ ISSUE_FAVORITES: "FAVORITES",
462
+ ISSUE_URL_SYNCING: "URL_SYNCING",
463
+ ISSUE_UI_ADJUSTMENT: "UI_ADJUSTMENT",
464
+ ISSUE_CONTENT_MAPPING: "CONTENT_MAPPING",
465
+ ISSUE_LOGIN: "LOGIN",
466
+ ISSUE_MANIFEST: "MANIFEST",
467
+ } as const;
468
+
469
+ static Features = [
470
+ {
471
+ id: StudioApplication.StudioIssues.ISSUE_AGENT,
472
+ version: "5.1.0",
473
+ },
474
+ {
475
+ id: StudioApplication.StudioIssues.ISSUE_FAVORITES,
476
+ version: "3.88.1",
477
+ },
478
+ {
479
+ id: StudioApplication.StudioIssues.ISSUE_ACTIVITY,
480
+ version: "3.88.1",
481
+ },
482
+ {
483
+ id: StudioApplication.StudioIssues.ISSUE_URL_SYNCING,
484
+ version: "3.75.0",
485
+ },
486
+ {
487
+ id: StudioApplication.StudioIssues.ISSUE_UI_ADJUSTMENT,
488
+ version: "3.78.1",
489
+ },
490
+ {
491
+ id: StudioApplication.StudioIssues.ISSUE_CONTENT_MAPPING,
492
+ version: "3.68.0",
493
+ },
494
+ {
495
+ id: StudioApplication.StudioIssues.ISSUE_LOGIN,
496
+ version: "2.28.0",
497
+ },
498
+ ] satisfies StudioIssues;
499
+
500
+ static MinimumStudioVersion = "2.28.0" as const;
501
+
502
+ static MinimumStudioVersionWithNoIssues = rsort(
503
+ StudioApplication.Features.map((feature) => feature.version),
504
+ ).at(0);
505
+ }
506
+
507
+ type CompatibilityStatus =
508
+ (typeof StudioApplication.CompatibilityStatuses)[keyof typeof StudioApplication.CompatibilityStatuses];
509
+
510
+ type StudioDashboardIssue =
511
+ (typeof StudioApplication.StudioIssues)[keyof typeof StudioApplication.StudioIssues];
512
+
513
+ type StudioIssues = Array<{
514
+ id: StudioDashboardIssue;
515
+ version?: string;
516
+ }>;
517
+
518
+ type Workspace = Omit<WorkspaceManifest, "dataset" | "schema" | "projectId"> & {
519
+ projectId: ProjectId;
520
+ dataset?: string;
521
+ schema?: string;
522
+ };
523
+
524
+ /**
525
+ * @internal
526
+ */
527
+ export class StudioWorkspace extends AbstractApplication<
528
+ "workspace",
529
+ ProtocolStudioResource
530
+ > {
531
+ /**
532
+ * Workspaces always belong to a studio application.
533
+ * They do not exist on their own & therefore can access
534
+ * information about the studio they're in via this property.
535
+ */
536
+ private readonly studioApplication: StudioApplication;
537
+ private readonly workspace: Workspace;
538
+ readonly project: Project<false, false>;
539
+ private readonly isDefaultWorkspace: boolean;
540
+
541
+ constructor(
542
+ studioApplication: StudioApplication,
543
+ workspace: Workspace,
544
+ project: Project<false, false>,
545
+ isDefaultWorkspace: boolean,
546
+ ) {
547
+ super("workspace");
548
+
549
+ this.studioApplication = studioApplication;
550
+ this.workspace = workspace;
551
+ this.project = project;
552
+ this.isDefaultWorkspace = isDefaultWorkspace;
553
+ }
554
+
555
+ /**
556
+ * The studio application that this workspace belongs to.
557
+ */
558
+ get studio() {
559
+ return this.studioApplication;
560
+ }
561
+
562
+ get id() {
563
+ return StudioWorkspace.makeId(this.studio.id, this.workspace.name);
564
+ }
565
+
566
+ get href() {
567
+ return joinUrlPaths(this.studio.href, this.workspace.name);
568
+ }
569
+
570
+ get title() {
571
+ /**
572
+ * If there's no manifest we will have created a single workspace for the application.
573
+ * In this circumstance we won't have a meaningful title, so instead we use the hostname of the appHost.
574
+ */
575
+ if (this.isDefaultWorkspace) {
576
+ return this.project.displayName;
577
+ }
578
+
579
+ return this.workspace.title;
580
+ }
581
+
582
+ get subtitle() {
583
+ if (this.isDefaultWorkspace) {
584
+ const isValidAppHost = URL.canParse(this.studio.get("appHost"));
585
+ const url = isValidAppHost
586
+ ? new URL(this.studio.get("appHost"))
587
+ : this.studio.url;
588
+
589
+ return url.hostname;
590
+ }
591
+
592
+ return this.get("subtitle");
593
+ }
594
+
595
+ get<TKey extends Exclude<keyof Workspace, "id" | "icon" | "title">>(
596
+ attr: TKey,
597
+ ): Workspace[TKey] {
598
+ return this.workspace[attr];
599
+ }
600
+
601
+ /**
602
+ * With comlink, studio-applications were not considered applications at all, only workspaces. This is partially why
603
+ * we create default workspaces when the application has no manifest or the manifest has no workspaces.
604
+ *
605
+ * Thereby, to create it we depend on a lot of information from the parent application even if it's duplicated across
606
+ * different workspaces.
607
+ */
608
+ toProtocolResource(): ProtocolStudioResource {
609
+ return {
610
+ ...this.workspace,
611
+ type: "studio",
612
+ userApplicationId: this.studio.id,
613
+ activeDeployment: this.studio.get("activeDeployment"),
614
+ autoUpdatingVersion: this.studio.get("autoUpdatingVersion"),
615
+ dashboardStatus: this.studio.get("dashboardStatus"),
616
+ url: this.studio.url.toString(),
617
+ href: this.href,
618
+ id: this.id,
619
+ hasManifest: true,
620
+ hasSchema: Boolean(this.workspace.schema),
621
+ manifest: this.studio.get("manifestData")?.value ?? null,
622
+ updatedAt: this.studio.get("updatedAt"),
623
+ version: this.studio.get("activeDeployment")?.version,
624
+ urlType: this.studio.get("urlType"),
625
+ config: this.studio.get("config"),
626
+ } satisfies ProtocolStudioResource;
627
+ }
628
+
629
+ /**
630
+ * @returns the URL to the workspace of a studio application.
631
+ */
632
+ get url(): URL {
633
+ const studioUrl = new URL(this.studio.url);
634
+ const normalizedUrlPath = normalizePath(studioUrl.pathname);
635
+
636
+ let finalBasePath = normalizePath(this.get("basePath"));
637
+
638
+ // the appHost may already contain the basepath for externally hosted studios
639
+ // or embedded studios, so we deduplicate the segments
640
+ if (finalBasePath.startsWith(normalizedUrlPath)) {
641
+ finalBasePath = finalBasePath.slice(normalizedUrlPath.length);
642
+ }
643
+
644
+ studioUrl.pathname = joinUrlPaths(normalizedUrlPath, finalBasePath);
645
+
646
+ return studioUrl;
647
+ }
648
+
649
+ static makeId(applicationId: UserApplicationId, workspaceName: string) {
650
+ return `${applicationId}-${workspaceName}`;
651
+ }
652
+
653
+ static splitId(id: string): [string, string] {
654
+ return id.split(new RegExp(/-(.*)/)).slice(0, 2) as [string, string];
655
+ }
656
+ }