import {z} from 'zod' export type GetApiFormResponsesArgs = {page?: number | undefined, limit?: number | undefined, form_id?: number | undefined}; export type GetApiFormResponsesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceIdentifier = z.object({type: z.string(), id: z.string()}); export const links = z.object({self: z.string().optional()}); export const formResponseResource = z.object({type: z.literal("form_responses").optional(), id: z.string().optional(), attributes: z.object({email: z.string().optional(), response: z.object({}).optional(), country: z.string().nullable().optional(), city: z.string().nullable().optional(), region: z.string().nullable().optional(), timezone: z.string().nullable().optional(), language: z.string().nullable().optional(), created_at: z.string().optional()}).optional(), relationships: z.object({forms: z.object({data: resourceIdentifier.optional()}).optional(), posts: z.object({data: resourceIdentifier.optional()}).optional(), blogs: z.object({data: resourceIdentifier.optional()}).optional()}).optional(), links: links.optional()}); export const meta = z.object({total: z.number().int().optional(), page: z.number().int().optional(), limit: z.number().int().optional(), totalPages: z.number().int().optional()}); export const formResponseListResponse = z.object({jsonapi: z.object({version: z.string()}), data: z.array(formResponseResource).optional(), meta: meta.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 }; /** * List Form Responses * * GET /form-responses */ export const getApiFormResponses = async (args: GetApiFormResponsesArgs, options?: GetApiFormResponsesOptions) => { const {baseUrl = "https://inblog.ai/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.form_id != null) query.append("form_id", String(args.form_id)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/form-responses${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(formResponseListResponse, await res.json()) };