import {z} from 'zod' export type CreateApiTemplatesIdCopyArgs = {id: string, body: {workspace_id: string, name?: string | undefined}}; export type CreateApiTemplatesIdCopyOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sealSummary = z.object({id: z.string(), lineage_id: z.string(), name: z.string(), version: z.number().int(), display_name: z.string()}); export const templateCopySealParticipant = z.object({id: z.string(), order: z.number().int().gte(1).nullable(), seal: sealSummary, seal_out_of_scope: z.boolean(), dropped: z.literal(false)}); export const sealParticipantDropped = z.object({id: z.literal("null").nullable(), order: z.literal(null).nullable(), seal: z.object({id: z.string(), lineage_id: z.string(), name: z.string(), version: z.number().int(), display_name: z.string()}).nullable(), seal_out_of_scope: z.literal(true), dropped: z.literal(true)}); export const templateCopyResponse = z.object({id: z.string(), name: z.string(), workspace_id: z.string(), created_date: z.string(), seal_participants: z.array(z.union([templateCopySealParticipant, sealParticipantDropped]))}); 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 }; /** * Copy template to another workspace * * POST /templates/{id}/copy */ export const createApiTemplatesIdCopy = async (args: CreateApiTemplatesIdCopyArgs, options?: CreateApiTemplatesIdCopyOptions) => { const {baseUrl = "https://api.firma.dev/functions/v1/signing-request-api", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/templates/${encodeURIComponent(String(args.id))}/copy${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(templateCopyResponse, await res.json()) };