import {z} from 'zod' export type FilePostForm = {url?: (string | null) | undefined, name?: (string | null) | undefined, description?: (string | null) | undefined}; export type CreateApiWorkspacesWorkspaceIdFilesByurlArgs = {workspaceId: string, body: FilePostForm}; export type CreateApiWorkspacesWorkspaceIdFilesByurlOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fileVersion = z.object({id: z.string().optional(), fileInfoId: z.string().optional(), version: z.number().optional(), size: z.number().int().optional(), width: z.number().nullable().optional(), height: z.number().nullable().optional(), createdOn: z.string().optional(), createdBy: z.string().optional(), updatedOn: z.string().optional(), updatedBy: z.string().optional()}); export const fileInfo = z.object({name: z.string().nullable().optional(), description: z.string().nullable().optional(), id: z.string().optional(), entityId: z.string().nullable().optional(), projectId: z.string().nullable().optional(), projectTemplateId: z.string().nullable().optional(), companyId: z.string().nullable().optional(), taskId: z.string().nullable().optional(), taskTemplateId: z.string().nullable().optional(), userId: z.string().nullable().optional(), documentId: z.string().nullable().optional(), fileName: z.string().nullable().optional(), entityType: z.string().nullable().optional(), mimeType: z.string().nullable().optional(), createdOn: z.string().optional(), createdBy: z.string().optional(), updatedOn: z.string().optional(), updatedBy: z.string().optional(), fileVersions: z.array(fileVersion).nullable().optional(), isCommentFile: z.boolean().optional(), externalProvider: z.string().nullable().optional(), externalFileUrl: z.string().nullable().optional(), isHiddenForConnectUsers: z.boolean().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 }; /** * Uploads a file from a public URL for the specified workspace. * * POST /workspaces/{workspaceId}/files/byurl */ export const createApiWorkspacesWorkspaceIdFilesByurl = async (args: CreateApiWorkspacesWorkspaceIdFilesByurlArgs, options?: CreateApiWorkspacesWorkspaceIdFilesByurlOptions) => { const {baseUrl = "https://api.awork.com/api/v1", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/workspaces/${encodeURIComponent(String(args.workspaceId))}/files/byurl${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(fileInfo, await res.json()) };