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

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,129 @@
1
+ /**
2
+ * Joins multiple path segments into a single path string.
3
+ * Handles null, undefined, and URL objects gracefully.
4
+ *
5
+ * @public
6
+ * @param paths - An array of path segments to join.
7
+ * @returns A single joined path string.
8
+ */
9
+ export const joinUrlPaths = (
10
+ ...paths: Array<string | URL | null | undefined>
11
+ ): string => {
12
+ let nextPath = null;
13
+
14
+ const safeJoinSegments = (
15
+ segment1: string | null | undefined,
16
+ segment2: string | null | undefined,
17
+ ): string => {
18
+ if (!segment1) {
19
+ if (!segment2) {
20
+ return "";
21
+ }
22
+
23
+ return segment2;
24
+ }
25
+
26
+ if (!segment2) {
27
+ return segment1;
28
+ }
29
+
30
+ if (segment1.endsWith("/") && segment2.startsWith("/")) {
31
+ return segment1 + segment2.slice(1);
32
+ }
33
+
34
+ if (segment1.endsWith("/") || segment2.startsWith("/")) {
35
+ return segment1 + segment2;
36
+ }
37
+
38
+ return `${segment1}/${segment2}`;
39
+ };
40
+
41
+ const validPaths = paths.filter(
42
+ (path) => path !== null && path !== undefined,
43
+ );
44
+
45
+ for (const path of validPaths) {
46
+ nextPath = safeJoinSegments(
47
+ nextPath,
48
+ path instanceof URL ? path.pathname : path,
49
+ );
50
+ }
51
+
52
+ return nextPath ?? "";
53
+ };
54
+
55
+ /**
56
+ * Returns a normalized path by ensuring it starts with a single leading slash
57
+ * and does not end with a trailing slash (unless it's the root path).
58
+ */
59
+ export function normalizePath(pathname: string): string {
60
+ if (!pathname.startsWith("/")) {
61
+ pathname = `/${pathname}`;
62
+ }
63
+
64
+ if (pathname !== "/" && pathname.endsWith("/")) {
65
+ pathname = pathname.slice(0, -1);
66
+ }
67
+
68
+ return pathname;
69
+ }
70
+
71
+ /**
72
+ * The pathname, search, and hash values of a URL.
73
+ */
74
+ interface Path {
75
+ /**
76
+ * A URL pathname, beginning with a /.
77
+ */
78
+ pathname: string;
79
+ /**
80
+ * A URL search string, beginning with a ?.
81
+ */
82
+ search: string;
83
+ /**
84
+ * A URL fragment identifier, beginning with a #.
85
+ */
86
+ hash: string;
87
+ }
88
+
89
+ /**
90
+ * Parses a string URL path into its separate pathname, search, and hash components.
91
+ */
92
+ export function parsePath(path: string): Partial<Path> {
93
+ let parsedPath: Partial<Path> = {};
94
+
95
+ if (path) {
96
+ let hashIndex = path.indexOf("#");
97
+ if (hashIndex >= 0) {
98
+ parsedPath.hash = path.substring(hashIndex);
99
+ path = path.substring(0, hashIndex);
100
+ }
101
+
102
+ let searchIndex = path.indexOf("?");
103
+ if (searchIndex >= 0) {
104
+ parsedPath.search = path.substring(searchIndex);
105
+ path = path.substring(0, searchIndex);
106
+ }
107
+
108
+ if (path) {
109
+ parsedPath.pathname = path;
110
+ }
111
+ }
112
+
113
+ return parsedPath;
114
+ }
115
+
116
+ /**
117
+ * Creates a string URL path from the given pathname, search, and hash components.
118
+ */
119
+ export function createPath({
120
+ pathname = "/",
121
+ search = "",
122
+ hash = "",
123
+ }: Partial<Path>) {
124
+ if (search && search !== "?")
125
+ pathname += search.charAt(0) === "?" ? search : `?${search}`;
126
+ if (hash && hash !== "#")
127
+ pathname += hash.charAt(0) === "#" ? hash : `#${hash}`;
128
+ return pathname;
129
+ }
@@ -0,0 +1,131 @@
1
+ // eslint-disable-next-line no-restricted-imports
2
+ import type { ApplicationResource as ProtocolApplicationResource } from "@sanity/message-protocol";
3
+ import { z } from "zod";
4
+
5
+ import { OrganizationId } from "../organizations";
6
+ import {
7
+ UserApplication,
8
+ UserApplicationBase,
9
+ ActiveDeployment,
10
+ } from "./user-application";
11
+
12
+ /**
13
+ * @public
14
+ */
15
+ export const CoreAppUserApplicationManifest = z.object({
16
+ version: z.string(),
17
+ icon: z.string().optional(),
18
+ title: z.string().optional(),
19
+ });
20
+
21
+ /**
22
+ * @public
23
+ */
24
+ export type CoreAppUserApplicationManifest = z.output<
25
+ typeof CoreAppUserApplicationManifest
26
+ >;
27
+
28
+ const CoreAppUserApplicationBase = UserApplicationBase.extend({
29
+ title: z.string(),
30
+ organizationId: OrganizationId,
31
+ type: z.literal("coreApp"),
32
+ manifest: CoreAppUserApplicationManifest.nullable().optional(),
33
+ });
34
+
35
+ const InternalCoreAppUserApplication = CoreAppUserApplicationBase.extend({
36
+ urlType: z.literal("internal"),
37
+ activeDeployment: ActiveDeployment.extend({
38
+ manifest: CoreAppUserApplicationManifest.nullable().optional(),
39
+ }).nullable(),
40
+ });
41
+
42
+ const ExternalCoreAppUserApplication = CoreAppUserApplicationBase.extend({
43
+ urlType: z.literal("external"),
44
+ activeDeployment: z.null(),
45
+ });
46
+
47
+ /**
48
+ * Core application schema — validates and brands API
49
+ * responses from the `/user-applications?appType=coreApp`
50
+ * endpoint. Uses a discriminated union on `urlType`.
51
+ * @public
52
+ */
53
+ const CoreAppUserApplication = z.discriminatedUnion("urlType", [
54
+ InternalCoreAppUserApplication,
55
+ ExternalCoreAppUserApplication,
56
+ ]);
57
+
58
+ /**
59
+ * @public
60
+ */
61
+ export type CoreAppUserApplication = z.output<typeof CoreAppUserApplication>;
62
+
63
+ /**
64
+ * Validates and parses a raw API response into a branded
65
+ * CoreAppUserApplicationData.
66
+ * @public
67
+ */
68
+ export function parseCoreApplication(data: unknown): CoreAppUserApplication {
69
+ return CoreAppUserApplication.parse(data);
70
+ }
71
+
72
+ /**
73
+ * @public
74
+ */
75
+ export class CoreAppApplication extends UserApplication<
76
+ CoreAppUserApplication,
77
+ "coreApp",
78
+ ProtocolApplicationResource
79
+ > {
80
+ readonly activeDeployment: CoreAppUserApplication["activeDeployment"];
81
+
82
+ constructor(
83
+ application: CoreAppUserApplication,
84
+ options: {
85
+ isLocal?: boolean;
86
+ remoteApplication?: CoreAppApplication | null;
87
+ } = {},
88
+ ) {
89
+ super(application, "coreApp", options);
90
+
91
+ this.activeDeployment = application.activeDeployment;
92
+ }
93
+
94
+ get href() {
95
+ return this.isLocal
96
+ ? `/local/${this.id}`
97
+ : `/application/${this.application.id}`;
98
+ }
99
+
100
+ get title() {
101
+ return this.activeDeployment?.manifest?.title ?? this.application.title;
102
+ }
103
+
104
+ get subtitle() {
105
+ return undefined;
106
+ }
107
+
108
+ get updatedAt() {
109
+ return this.application.updatedAt;
110
+ }
111
+
112
+ get<TKey extends keyof CoreAppUserApplication>(
113
+ attr: TKey,
114
+ ): CoreAppUserApplication[TKey] {
115
+ if (!(attr in this.application)) {
116
+ throw new Error(
117
+ `Attribute ${attr.toString()} does not exist on application ${this.application.id}`,
118
+ );
119
+ }
120
+
121
+ return this.application[attr];
122
+ }
123
+
124
+ toProtocolResource(): ProtocolApplicationResource {
125
+ return {
126
+ ...this.application,
127
+ type: "application",
128
+ url: this.url.toString(),
129
+ } as ProtocolApplicationResource;
130
+ }
131
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./schemas";
2
+ export * from "./studio";
3
+ export * from "./workspace";
@@ -0,0 +1,111 @@
1
+ import { z } from "zod";
2
+
3
+ import { ProjectId } from "../../projects";
4
+ import { ActiveDeployment, UserApplicationBase } from "../user-application";
5
+
6
+ const Workspace = z.object({
7
+ name: z.string(),
8
+ title: z.string(),
9
+ subtitle: z.string().optional(),
10
+ basePath: z.string(),
11
+ projectId: ProjectId,
12
+ dataset: z.string().optional(),
13
+ icon: z.string().nullable().optional(),
14
+ });
15
+
16
+ /**
17
+ * @public
18
+ */
19
+ export type Workspace = z.output<typeof Workspace>;
20
+
21
+ const ServerManifest = z.object({
22
+ buildId: z.string().optional(),
23
+ bundleVersion: z.string().optional(),
24
+ version: z.string().optional(),
25
+ workspaces: z
26
+ .array(
27
+ Workspace.extend({
28
+ dataset: z.string(),
29
+ schemaDescriptorId: z.string(),
30
+ }),
31
+ )
32
+ .optional(),
33
+ });
34
+
35
+ /**
36
+ * @public
37
+ */
38
+ export const ClientManifest = z.object({
39
+ version: z.number(),
40
+ createdAt: z.string(),
41
+ studioVersion: z.string().optional(),
42
+ workspaces: z.array(
43
+ Workspace.extend({
44
+ schema: z.string(),
45
+ tools: z.string().optional(),
46
+ }),
47
+ ),
48
+ });
49
+
50
+ /**
51
+ * @public
52
+ */
53
+ export type ClientManifest = z.output<typeof ClientManifest>;
54
+
55
+ const StudioUserApplicationBase = UserApplicationBase.extend({
56
+ title: z.string().nullable(),
57
+ projectId: ProjectId,
58
+ type: z.literal("studio"),
59
+ manifest: ClientManifest.nullable(),
60
+ manifestData: z.object({ value: ClientManifest }).nullable(),
61
+ autoUpdatingVersion: z.string().nullable(),
62
+ config: z.object({
63
+ "live-manifest": z
64
+ .object({
65
+ createdAt: z.string(),
66
+ updatedAt: z.string(),
67
+ updatedBy: z.string(),
68
+ value: ServerManifest,
69
+ })
70
+ .optional(),
71
+ }),
72
+ });
73
+
74
+ const InternalStudioUserApplication = StudioUserApplicationBase.extend({
75
+ urlType: z.literal("internal"),
76
+ activeDeployment: ActiveDeployment.extend({
77
+ manifest: ServerManifest.nullable(),
78
+ }),
79
+ });
80
+
81
+ const ExternalStudioUserApplication = StudioUserApplicationBase.extend({
82
+ urlType: z.literal("external"),
83
+ activeDeployment: z.null(),
84
+ });
85
+
86
+ /**
87
+ * Studio user application schema — validates and brands API
88
+ * responses from the `/user-applications?appType=studio`
89
+ * endpoint. Uses a discriminated union on `urlType`.
90
+ * @public
91
+ */
92
+ export const StudioUserApplication = z.discriminatedUnion("urlType", [
93
+ InternalStudioUserApplication,
94
+ ExternalStudioUserApplication,
95
+ ]);
96
+
97
+ /**
98
+ * @public
99
+ */
100
+ export type StudioUserApplication = z.output<typeof StudioUserApplication>;
101
+
102
+ /**
103
+ * Validates and parses a raw API response into a branded
104
+ * StudioUserApplication.
105
+ * @public
106
+ */
107
+ export function parseStudioUserApplication(
108
+ data: unknown,
109
+ ): StudioUserApplication {
110
+ return StudioUserApplication.parse(data);
111
+ }