import {z} from 'zod' export type CreateApiV1NameFilesystemWriteFileArgs = {name: string, body: {path: string, content: string, createParentDirectories?: boolean | undefined, overwrite?: (boolean | null) | undefined, mode?: string | undefined}}; export type CreateApiV1NameFilesystemWriteFileOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const v1FileType = z.enum(["FILE_TYPE_UNSPECIFIED", "FILE_TYPE_REGULAR", "FILE_TYPE_DIRECTORY", "FILE_TYPE_SYMLINK"]); export const v1FileInfo = z.object({path: z.string().optional(), size: z.string().optional(), type: v1FileType.optional(), modifyTime: z.string().optional(), mode: z.string().optional()}); export const v1WriteFileResponse = z.object({fileInfo: v1FileInfo.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Writes content to a file, creating it if it doesn't exist. * If the path is a symbolic link, the target file is written to (symlink is followed). * * POST /v1/{name}/filesystem:writeFile */ export const createApiV1NameFilesystemWriteFile = async (args: CreateApiV1NameFilesystemWriteFileArgs, options: CreateApiV1NameFilesystemWriteFileOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.name))}/filesystem:writeFile${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(v1WriteFileResponse, await res.json()) };