|
| 1 | +import { z } from "zod"; |
| 2 | + |
| 3 | +import type { CoderApi } from "./coderApi"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Thrown when a 2xx response body does not match the shape the extension |
| 7 | + * needs, which almost always means the URL does not point at a Coder |
| 8 | + * deployment (a proxy error page, a different service, a partial body). |
| 9 | + */ |
| 10 | +export class InvalidApiResponseError extends Error { |
| 11 | + constructor( |
| 12 | + public readonly endpoint: string, |
| 13 | + url: string | undefined, |
| 14 | + options?: { cause?: unknown }, |
| 15 | + ) { |
| 16 | + super( |
| 17 | + `${url ?? "The deployment"} did not return a valid Coder API response ` + |
| 18 | + `for ${endpoint}. Check that the URL points to a Coder deployment.`, |
| 19 | + options, |
| 20 | + ); |
| 21 | + this.name = "InvalidApiResponseError"; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Validate a response body, returning the original value with the caller's |
| 27 | + * type. |
| 28 | + * |
| 29 | + * Schemas list only the fields the extension reads and use looseObject so |
| 30 | + * unknown fields pass through. Require a field only if every deployment back |
| 31 | + * to Coder 0.25 sends it; anything newer must be .optional(). |
| 32 | + * |
| 33 | + * @throws {InvalidApiResponseError} naming the endpoint when validation fails. |
| 34 | + */ |
| 35 | +export function parseApiResponse<T>( |
| 36 | + schema: z.ZodType<unknown>, |
| 37 | + data: T, |
| 38 | + endpoint: string, |
| 39 | + url?: string, |
| 40 | +): T { |
| 41 | + const result = schema.safeParse(data); |
| 42 | + if (!result.success) { |
| 43 | + throw new InvalidApiResponseError(endpoint, url, { cause: result.error }); |
| 44 | + } |
| 45 | + return data; |
| 46 | +} |
| 47 | + |
| 48 | +export const UserSchema = z.looseObject({ |
| 49 | + id: z.string(), |
| 50 | + username: z.string(), |
| 51 | + roles: z.array(z.looseObject({ name: z.string() })), |
| 52 | +}); |
| 53 | + |
| 54 | +const WorkspaceAgentSchema = z.looseObject({ |
| 55 | + id: z.string(), |
| 56 | + name: z.string(), |
| 57 | + status: z.string(), |
| 58 | + operating_system: z.string(), |
| 59 | +}); |
| 60 | + |
| 61 | +const WorkspaceResourceSchema = z.looseObject({ |
| 62 | + agents: z.array(WorkspaceAgentSchema).nullable().optional(), |
| 63 | +}); |
| 64 | + |
| 65 | +export const WorkspaceSchema = z.looseObject({ |
| 66 | + id: z.string(), |
| 67 | + name: z.string(), |
| 68 | + owner_name: z.string(), |
| 69 | + template_id: z.string(), |
| 70 | + latest_build: z.looseObject({ |
| 71 | + id: z.string(), |
| 72 | + status: z.string(), |
| 73 | + template_version_id: z.string(), |
| 74 | + resources: z.array(WorkspaceResourceSchema), |
| 75 | + }), |
| 76 | +}); |
| 77 | + |
| 78 | +/** waitForBuild reads the identifiers to poll and the job status to stop. */ |
| 79 | +export const WorkspaceBuildSchema = z.looseObject({ |
| 80 | + workspace_owner_name: z.string(), |
| 81 | + workspace_name: z.string(), |
| 82 | + build_number: z.number(), |
| 83 | + job: z.looseObject({ status: z.string() }), |
| 84 | +}); |
| 85 | + |
| 86 | +export const TemplateSchema = z.looseObject({ |
| 87 | + active_version_id: z.string(), |
| 88 | +}); |
| 89 | + |
| 90 | +export const WorkspaceResourcesSchema = z.array(WorkspaceResourceSchema); |
| 91 | + |
| 92 | +export const SSHConfigResponseSchema = z.looseObject({ |
| 93 | + ssh_config_options: z.record(z.string(), z.string()), |
| 94 | +}); |
| 95 | + |
| 96 | +/** |
| 97 | + * The schema each SDK method's response must match, applied by CoderApi. Add a |
| 98 | + * pair to validate another method; the name doubles as the endpoint in the |
| 99 | + * error. Pairs, not an object, so iterating keeps the names as literal types. |
| 100 | + * OAuth endpoints are plain axios calls and pass their schema at the call site. |
| 101 | + */ |
| 102 | +export const VALIDATED_RESPONSES = [ |
| 103 | + ["getAuthenticatedUser", UserSchema], |
| 104 | + ["getDeploymentSSHConfig", SSHConfigResponseSchema], |
| 105 | + ["getTemplate", TemplateSchema], |
| 106 | + ["getTemplateVersionResources", WorkspaceResourcesSchema], |
| 107 | + ["getWorkspace", WorkspaceSchema], |
| 108 | + ["getWorkspaceByOwnerAndName", WorkspaceSchema], |
| 109 | + ["getWorkspaceBuildByNumber", WorkspaceBuildSchema], |
| 110 | + ["startWorkspace", WorkspaceBuildSchema], |
| 111 | + ["stopWorkspace", WorkspaceBuildSchema], |
| 112 | +] as const satisfies ReadonlyArray<readonly [keyof CoderApi, z.ZodType]>; |
| 113 | + |
| 114 | +/** |
| 115 | + * The methods above, reduced to what the wrapper needs. CoderApi satisfies this |
| 116 | + * with no assertion: `never` parameters accept any signature, and one uniform |
| 117 | + * value type is what allows assigning by a name held in a variable. |
| 118 | + */ |
| 119 | +export type ValidatedMethods = Record< |
| 120 | + (typeof VALIDATED_RESPONSES)[number][0], |
| 121 | + (...args: never[]) => Promise<unknown> |
| 122 | +>; |
0 commit comments