import {z} from 'zod' export type GetApiApiProcessProcessIdFileFileIdArgs = {processId: string, fileId: string}; export type GetApiApiProcessProcessIdFileFileIdOptions = {baseUrl?: string | undefined, SwisscomSignOAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const coordinateSignaturePosition = z.object({locator: z.enum(["COORDINATES", "TAG"]), signer: z.string().max(128), pageNumber: z.number().int(), positionX: z.number().int().gte(0), positionY: z.number().int().gte(0), stickerProvider: z.enum(["VISUAL_SIGNATURE_PROVIDER", "CUSTOM_VISUAL_SIGNATURE_PROVIDER"]).optional()}); export const tagSignaturePosition = z.object({locator: z.enum(["COORDINATES", "TAG"]), signer: z.string().max(128), offsetX: z.number().int().gte(-200).lte(200).optional(), offsetY: z.number().int().gte(-200).lte(200).optional(), stickerProvider: z.enum(["VISUAL_SIGNATURE_PROVIDER", "CUSTOM_VISUAL_SIGNATURE_PROVIDER"]).optional()}); export const fileAttachment = z.object({id: z.string().optional(), name: z.string().max(255), content: z.string().max(55924053), contentType: z.string().max(50), externalIdentifier: z.string().max(50).optional(), signaturePositions: z.array(z.union([coordinateSignaturePosition, tagSignaturePosition])).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 }; /** * Download process file * * GET /api/process/{processId}/file/{fileId} */ export const getApiApiProcessProcessIdFileFileId = async (args: GetApiApiProcessProcessIdFileFileIdArgs, options?: GetApiApiProcessProcessIdFileFileIdOptions) => { const {baseUrl = "https://sign.swisscom.ch/system", fetch: fetchFn = fetch, SwisscomSignOAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (SwisscomSignOAuth2 !== undefined) headers.set('authorization', `Bearer ${SwisscomSignOAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/process/${encodeURIComponent(String(args.processId))}/file/${encodeURIComponent(String(args.fileId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(fileAttachment, await res.json()) };