@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.
- package/dist/_chunks-es/index.js +34 -0
- package/dist/_chunks-es/index.js.map +1 -0
- package/dist/_internal.d.ts +20 -4
- package/dist/_internal.js +34 -26
- package/dist/_internal.js.map +1 -1
- package/dist/core.d.ts +1646 -0
- package/dist/core.js +791 -0
- package/dist/core.js.map +1 -0
- package/package.json +25 -10
- package/src/_exports/_internal.ts +1 -0
- package/src/_exports/core.ts +1 -0
- package/src/_internal/index.ts +2 -1
- package/src/_internal/render.ts +82 -38
- package/src/core/applications/application-list.ts +104 -0
- package/src/core/applications/application.ts +95 -0
- package/src/core/canvases.ts +92 -0
- package/src/core/config.ts +34 -0
- package/src/core/env.ts +43 -0
- package/src/core/index.ts +13 -0
- package/src/core/log/index.ts +92 -0
- package/src/core/media-libraries.ts +93 -0
- package/src/core/organizations.ts +115 -0
- package/src/core/projects.ts +114 -0
- package/src/core/shared/urls.ts +129 -0
- package/src/core/user-applications/core-app.ts +131 -0
- package/src/core/user-applications/studios/index.ts +3 -0
- package/src/core/user-applications/studios/schemas.ts +111 -0
- package/src/core/user-applications/studios/studio.ts +504 -0
- package/src/core/user-applications/studios/workspace.ts +147 -0
- package/src/core/user-applications/user-application.ts +182 -0
- package/dist/_internal.cjs +0 -38
- package/dist/_internal.cjs.map +0 -1
- package/dist/_internal.d.cts +0 -30
- package/src/env.d.ts +0 -7
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { CanvasResource as ProtocolCanvasResource } from "@sanity/message-protocol";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
import { AbstractApplication } from "./applications/application";
|
|
5
|
+
import { getSanityDomain } from "./env";
|
|
6
|
+
import { OrganizationId } from "./organizations";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Canvas ID schema, branded for type safety.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
const CanvasId = z.string().nonempty().brand("CanvasId");
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Canvas ID type, branded for type safety.
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export type CanvasId = z.output<typeof CanvasId>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Validates and brands a string as a CanvasId.
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export function brandCanvasId(id: string): CanvasId {
|
|
25
|
+
return CanvasId.parse(id);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const Canvas = z.object({
|
|
29
|
+
id: CanvasId,
|
|
30
|
+
organizationId: OrganizationId,
|
|
31
|
+
status: z.enum(["active", "provisioning"]),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Represents a Canvas resource as returned from the API.
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export type Canvas = z.output<typeof Canvas>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Validates and parses a raw API response into a branded
|
|
42
|
+
* Canvas.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export function parseCanvas(data: unknown): Canvas {
|
|
46
|
+
return Canvas.parse(data);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Whilst the constructor takes an organization's canvas resource the existance
|
|
51
|
+
* therefore implies the organization has access to the canvas application which
|
|
52
|
+
* is what workbench most importantly wants to know.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export class CanvasApplication extends AbstractApplication<"canvas"> {
|
|
56
|
+
readonly canvas: Canvas;
|
|
57
|
+
|
|
58
|
+
constructor(canvas: Canvas) {
|
|
59
|
+
super("canvas");
|
|
60
|
+
|
|
61
|
+
this.canvas = canvas;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get id(): CanvasId {
|
|
65
|
+
return this.canvas.id;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get href(): string {
|
|
69
|
+
return "canvas";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get title(): string {
|
|
73
|
+
return "Canvas";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get url(): URL {
|
|
77
|
+
return new URL(
|
|
78
|
+
`https://canvas.${getSanityDomain()}/o/${this.canvas.organizationId}`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get isFederated(): boolean {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
toProtocolResource(): ProtocolCanvasResource {
|
|
87
|
+
return {
|
|
88
|
+
type: "canvas",
|
|
89
|
+
id: this.id,
|
|
90
|
+
} satisfies ProtocolCanvasResource;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { OrganizationId } from "./organizations";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Workbench configuration.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
interface Config {
|
|
9
|
+
/**
|
|
10
|
+
* The organization ID to use when rendering the workbench.
|
|
11
|
+
*/
|
|
12
|
+
organizationId?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The resolved configuration after processing
|
|
17
|
+
* and validating the provided config.
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
type ResolvedConfig = Omit<Config, "organizationId"> & {
|
|
22
|
+
organizationId: OrganizationId;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Options for rendering a remote module, such as a user application.
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
interface RemoteModuleRenderOptions {
|
|
31
|
+
reactStrictMode?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type { Config, ResolvedConfig, RemoteModuleRenderOptions };
|
package/src/core/env.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
declare const __SANITY_STAGING__: boolean | undefined;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns the Sanity domain based on the `__SANITY_STAGING__` runtime-time flag.
|
|
5
|
+
* If the flag is set to `true`, the staging domain is returned.
|
|
6
|
+
* Otherwise, the production domain is returned.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export function getSanityDomain(): string {
|
|
11
|
+
if (getSanityEnv() === "staging") {
|
|
12
|
+
return "sanity.work";
|
|
13
|
+
}
|
|
14
|
+
return "sanity.io";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns the API host based on the `__SANITY_STAGING__` runtime-time flag.
|
|
19
|
+
* If the flag is set to `true`, the staging API host is returned.
|
|
20
|
+
* Otherwise, the production API host is returned.
|
|
21
|
+
*
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export function getApiHost(): string | undefined {
|
|
25
|
+
return `https://api.${getSanityDomain()}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns the current Sanity environment based on the `__SANITY_STAGING__` runtime-time flag.
|
|
30
|
+
* If the flag is set to `true`, "staging" is returned.
|
|
31
|
+
* Otherwise, "production" is returned.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export function getSanityEnv(): "staging" | "production" {
|
|
36
|
+
if (
|
|
37
|
+
typeof __SANITY_STAGING__ !== "undefined" &&
|
|
38
|
+
__SANITY_STAGING__ === true
|
|
39
|
+
) {
|
|
40
|
+
return "staging";
|
|
41
|
+
}
|
|
42
|
+
return "production";
|
|
43
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from "./applications/application";
|
|
2
|
+
export * from "./applications/application-list";
|
|
3
|
+
export * from "./canvases";
|
|
4
|
+
export * from "./config";
|
|
5
|
+
export * from "./env";
|
|
6
|
+
export * from "./log";
|
|
7
|
+
export * from "./media-libraries";
|
|
8
|
+
export * from "./organizations";
|
|
9
|
+
export * from "./projects";
|
|
10
|
+
export { joinUrlPaths } from "./shared/urls";
|
|
11
|
+
export * from "./user-applications/core-app";
|
|
12
|
+
export * from "./user-applications/studios";
|
|
13
|
+
export * from "./user-applications/user-application";
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Log levels in order of verbosity (least to most)
|
|
3
|
+
* - none: Silent
|
|
4
|
+
* - error: Critical failures that prevent operation
|
|
5
|
+
* - warn: Issues that may cause problems but don't stop execution
|
|
6
|
+
* - info: High-level informational messages (default)
|
|
7
|
+
* - debug: Detailed debugging information (maintainer level)
|
|
8
|
+
* - trace: Very detailed tracing — sets `internal: true` on context
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export type LogLevel = "none" | "error" | "warn" | "info" | "debug";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Namespaces organize logs by functional domain.
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export type LogNamespace = string;
|
|
18
|
+
|
|
19
|
+
type LogContext = { [key: string]: unknown };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export interface Logger {
|
|
25
|
+
error: (message: string, context?: LogContext) => void;
|
|
26
|
+
warn: (message: string, context?: LogContext) => void;
|
|
27
|
+
info: (message: string, context?: LogContext) => void;
|
|
28
|
+
debug: (message: string, context?: LogContext) => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const LEVELS: readonly LogLevel[] = ["none", "error", "warn", "info", "debug"];
|
|
32
|
+
|
|
33
|
+
interface LoggerOptions {
|
|
34
|
+
namespace?: LogNamespace;
|
|
35
|
+
context?: LogContext;
|
|
36
|
+
logLevel?: LogLevel;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
export function createLogger({
|
|
43
|
+
namespace,
|
|
44
|
+
context: baseContext,
|
|
45
|
+
logLevel = "info",
|
|
46
|
+
}: LoggerOptions = {}): Logger {
|
|
47
|
+
function isLevelEnabled(level: LogLevel): boolean {
|
|
48
|
+
return LEVELS.indexOf(level) <= LEVELS.indexOf(logLevel);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function logAtLevel(
|
|
52
|
+
level: LogLevel,
|
|
53
|
+
message: string,
|
|
54
|
+
context?: LogContext,
|
|
55
|
+
): void {
|
|
56
|
+
if (!isLevelEnabled(level)) return;
|
|
57
|
+
|
|
58
|
+
const merged =
|
|
59
|
+
(baseContext ?? context) ? { ...baseContext, ...context } : undefined;
|
|
60
|
+
const args: unknown[] = [
|
|
61
|
+
...(namespace ? [`[${namespace}]`] : []),
|
|
62
|
+
message,
|
|
63
|
+
...(merged ? [merged] : []),
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
if (level === "error") console.error(...args);
|
|
67
|
+
else if (level === "warn") console.warn(...args);
|
|
68
|
+
// oxlint-disable-next-line no-console
|
|
69
|
+
else if (level === "info") console.info(...args);
|
|
70
|
+
// oxlint-disable-next-line no-console
|
|
71
|
+
else console.debug(...args);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
error: (message, context) => logAtLevel("error", message, context),
|
|
76
|
+
warn: (message, context) => logAtLevel("warn", message, context),
|
|
77
|
+
info: (message, context) => logAtLevel("info", message, context),
|
|
78
|
+
debug: (message, context) => logAtLevel("debug", message, context),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Shared workbench logger instance. Use this from both the workbench host
|
|
84
|
+
* and its remotes so lifecycle and diagnostic logs appear under a single
|
|
85
|
+
* namespace.
|
|
86
|
+
*
|
|
87
|
+
* @public
|
|
88
|
+
*/
|
|
89
|
+
export const logger: Logger = createLogger({
|
|
90
|
+
namespace: "sanity-workbench",
|
|
91
|
+
logLevel: "debug",
|
|
92
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { MediaResource as ProtocolMediaResource } from "@sanity/message-protocol";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
import { AbstractApplication } from "./applications/application";
|
|
5
|
+
import { getSanityDomain } from "./env";
|
|
6
|
+
import { OrganizationId } from "./organizations";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Canvas ID schema, branded for type safety.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
const MediaLibraryId = z.string().nonempty().brand("MediaLibraryId");
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* MediaLibrary ID type, branded for type safety.
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export type MediaLibraryId = z.output<typeof MediaLibraryId>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Validates and brands a string as a MediaLibraryId.
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export function brandMediaLibraryId(id: string): MediaLibraryId {
|
|
25
|
+
return MediaLibraryId.parse(id);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const MediaLibrary = z.object({
|
|
29
|
+
id: MediaLibraryId,
|
|
30
|
+
organizationId: OrganizationId,
|
|
31
|
+
status: z.enum(["active", "provisioning"]),
|
|
32
|
+
aclMode: z.enum(["private", "public"]),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Represents a MediaLibrary resource as returned from the API.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export type MediaLibrary = z.output<typeof MediaLibrary>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Validates and parses a raw API response into a branded
|
|
43
|
+
* MediaLibrary.
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
export function parseMediaLibrary(data: unknown): MediaLibrary {
|
|
47
|
+
return MediaLibrary.parse(data);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Whilst the constructor takes an organization's media library resource the existance
|
|
52
|
+
* therefore implies the organization has access to the media library application which
|
|
53
|
+
* is what workbench most importantly wants to know.
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
export class MediaLibraryApplication extends AbstractApplication<"media-library"> {
|
|
57
|
+
readonly library: MediaLibrary;
|
|
58
|
+
|
|
59
|
+
constructor(library: MediaLibrary) {
|
|
60
|
+
super("media-library");
|
|
61
|
+
|
|
62
|
+
this.library = library;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get id(): string {
|
|
66
|
+
return this.library.id;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get href(): string {
|
|
70
|
+
return "media";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get title(): string {
|
|
74
|
+
return "Media Library";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get isFederated(): boolean {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get url(): URL {
|
|
82
|
+
return new URL(
|
|
83
|
+
`https://media.${getSanityDomain()}/${this.library.organizationId}`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
toProtocolResource(): ProtocolMediaResource {
|
|
88
|
+
return {
|
|
89
|
+
type: "media-library",
|
|
90
|
+
id: this.id,
|
|
91
|
+
} satisfies ProtocolMediaResource;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Organization ID schema, branded for type safety.
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export const OrganizationId = z.string().nonempty().brand("OrganizationId");
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Organization ID type, branded for type safety.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export type OrganizationId = z.output<typeof OrganizationId>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Validates and brands a string as an OrganizationId.
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
export function brandOrganizationId(id: string): OrganizationId {
|
|
20
|
+
return OrganizationId.parse(id);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const OrganizationMember = z.object({
|
|
24
|
+
sanityUserId: z.string(),
|
|
25
|
+
isCurrentUser: z.boolean(),
|
|
26
|
+
user: z.object({
|
|
27
|
+
id: z.string(),
|
|
28
|
+
displayName: z.string(),
|
|
29
|
+
familyName: z.string(),
|
|
30
|
+
givenName: z.string(),
|
|
31
|
+
middleName: z.string().nullable(),
|
|
32
|
+
imageUrl: z.string().nullable(),
|
|
33
|
+
email: z.string(),
|
|
34
|
+
loginProvider: z.string(),
|
|
35
|
+
}),
|
|
36
|
+
roles: z.array(
|
|
37
|
+
z.object({
|
|
38
|
+
name: z.string(),
|
|
39
|
+
title: z.string(),
|
|
40
|
+
description: z.string().optional(),
|
|
41
|
+
}),
|
|
42
|
+
),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export type OrganizationMember = z.output<typeof OrganizationMember>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Organization schema — validates and brands API responses
|
|
52
|
+
* from the `/organizations/:id` endpoint.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export const Organization = z.object({
|
|
56
|
+
id: OrganizationId,
|
|
57
|
+
name: z.string(),
|
|
58
|
+
slug: z.string().nullable(),
|
|
59
|
+
createdAt: z.string(),
|
|
60
|
+
updatedAt: z.string(),
|
|
61
|
+
dashboardStatus: z.enum(["enabled", "disabled"]),
|
|
62
|
+
aiFeaturesStatus: z.enum(["enabled", "disabled"]),
|
|
63
|
+
defaultRoleName: z.string(),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Represents an organization with optional members and
|
|
68
|
+
* features arrays depending on the generic parameters.
|
|
69
|
+
* - `Organization` — base fields only (default)
|
|
70
|
+
* - `Organization<true>` — includes `members`
|
|
71
|
+
* - `Organization<true, true>` — includes both
|
|
72
|
+
* @public
|
|
73
|
+
*/
|
|
74
|
+
export type Organization<
|
|
75
|
+
IncludeMembers extends boolean = true,
|
|
76
|
+
IncludeFeatures extends boolean = true,
|
|
77
|
+
> = z.output<typeof Organization> &
|
|
78
|
+
(IncludeMembers extends true ? { members: OrganizationMember[] } : unknown) &
|
|
79
|
+
(IncludeFeatures extends true ? { features: string[] } : unknown);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Validates and parses a raw API response into a branded
|
|
83
|
+
* Organization. The options control which schema is used —
|
|
84
|
+
* matching what the API returns based on query params.
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
export function parseOrganization<
|
|
88
|
+
IncludeMembers extends boolean = true,
|
|
89
|
+
IncludeFeatures extends boolean = true,
|
|
90
|
+
>(
|
|
91
|
+
data: unknown,
|
|
92
|
+
options?: {
|
|
93
|
+
includeMembers?: IncludeMembers;
|
|
94
|
+
includeFeatures?: IncludeFeatures;
|
|
95
|
+
},
|
|
96
|
+
): Organization<IncludeMembers, IncludeFeatures> {
|
|
97
|
+
const includeMembers = options?.includeMembers ?? true;
|
|
98
|
+
const includeFeatures = options?.includeFeatures ?? true;
|
|
99
|
+
|
|
100
|
+
const extensions = {
|
|
101
|
+
...(includeMembers && {
|
|
102
|
+
members: z.array(OrganizationMember),
|
|
103
|
+
}),
|
|
104
|
+
...(includeFeatures && {
|
|
105
|
+
features: z.array(z.string()),
|
|
106
|
+
}),
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const schema =
|
|
110
|
+
Object.keys(extensions).length > 0
|
|
111
|
+
? Organization.extend(extensions)
|
|
112
|
+
: Organization;
|
|
113
|
+
|
|
114
|
+
return schema.parse(data) as Organization<IncludeMembers, IncludeFeatures>;
|
|
115
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import { OrganizationId } from "./organizations";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Project ID schema, branded for type safety.
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export const ProjectId = z.string().nonempty().brand("ProjectId");
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Project ID type, branded for type safety.
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export type ProjectId = z.output<typeof ProjectId>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Validates and brands a string as a ProjectId.
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export function brandProjectId(id: string): ProjectId {
|
|
22
|
+
return ProjectId.parse(id);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const ProjectMember = z.object({
|
|
26
|
+
id: z.string(),
|
|
27
|
+
createdAt: z.string(),
|
|
28
|
+
updatedAt: z.string(),
|
|
29
|
+
isCurrentUser: z.boolean(),
|
|
30
|
+
isRobot: z.boolean(),
|
|
31
|
+
roles: z.array(
|
|
32
|
+
z.object({
|
|
33
|
+
name: z.string(),
|
|
34
|
+
title: z.string(),
|
|
35
|
+
description: z.string(),
|
|
36
|
+
}),
|
|
37
|
+
),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @public
|
|
42
|
+
*/
|
|
43
|
+
export type ProjectMember = z.output<typeof ProjectMember>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Project schema — validates and brands API responses
|
|
47
|
+
* from the `/projects/:id` endpoint.
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
export const Project = z.object({
|
|
51
|
+
id: ProjectId,
|
|
52
|
+
displayName: z.string(),
|
|
53
|
+
studioHost: z.string().nullable(),
|
|
54
|
+
organizationId: OrganizationId,
|
|
55
|
+
metadata: z.object({
|
|
56
|
+
color: z.string().optional(),
|
|
57
|
+
externalStudioHost: z.string().optional(),
|
|
58
|
+
initialTemplate: z.string().optional(),
|
|
59
|
+
cliInitializedAt: z.string().optional(),
|
|
60
|
+
integration: z.literal(["manage", "cli"]),
|
|
61
|
+
}),
|
|
62
|
+
isBlocked: z.boolean(),
|
|
63
|
+
isDisabled: z.boolean(),
|
|
64
|
+
isDisabledByUser: z.boolean(),
|
|
65
|
+
activityFeedEnabled: z.boolean(),
|
|
66
|
+
createdAt: z.string(),
|
|
67
|
+
updatedAt: z.string(),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Represents a Sanity project with optional members and
|
|
72
|
+
* features arrays depending on the generic parameters.
|
|
73
|
+
* By default, neither members nor features are included.
|
|
74
|
+
* - `Project` — base fields only (default)
|
|
75
|
+
* - `Project<true>` — includes `members`
|
|
76
|
+
* - `Project<true, true>` — includes both
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
export type Project<
|
|
80
|
+
IncludeMembers extends boolean = true,
|
|
81
|
+
IncludeFeatures extends boolean = true,
|
|
82
|
+
> = z.output<typeof Project> &
|
|
83
|
+
(IncludeMembers extends true ? { members: ProjectMember[] } : unknown) &
|
|
84
|
+
(IncludeFeatures extends true ? { features: string[] } : unknown);
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Validates and parses a raw API response into a branded
|
|
88
|
+
* Project. The options control which schema is used —
|
|
89
|
+
* matching what the API returns based on query params.
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
export function parseProject<
|
|
93
|
+
IncludeMembers extends boolean = true,
|
|
94
|
+
IncludeFeatures extends boolean = true,
|
|
95
|
+
>(
|
|
96
|
+
data: unknown,
|
|
97
|
+
options?: {
|
|
98
|
+
includeMembers?: IncludeMembers;
|
|
99
|
+
includeFeatures?: IncludeFeatures;
|
|
100
|
+
},
|
|
101
|
+
): Project<IncludeMembers, IncludeFeatures> {
|
|
102
|
+
const includeMembers = options?.includeMembers ?? true;
|
|
103
|
+
const includeFeatures = options?.includeFeatures ?? true;
|
|
104
|
+
|
|
105
|
+
const extensions = {
|
|
106
|
+
...(includeMembers && { members: z.array(ProjectMember) }),
|
|
107
|
+
...(includeFeatures && { features: z.array(z.string()) }),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const schema =
|
|
111
|
+
Object.keys(extensions).length > 0 ? Project.extend(extensions) : Project;
|
|
112
|
+
|
|
113
|
+
return schema.parse(data) as Project<IncludeMembers, IncludeFeatures>;
|
|
114
|
+
}
|