import {z} from 'zod' export type GetApiFormsArgs = {page?: number | undefined, limit?: number | undefined}; export type GetApiFormsOptions = {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 formResource = z.object({type: z.literal("forms").optional(), id: z.string().optional(), attributes: z.object({title: z.string().optional(), form_setting: z.object({}).optional(), magnet_type: z.enum(["NONE", "DOWNLOAD", "LINK"]).optional(), magnet_url: z.string().nullable().optional(), magnet_file_src: z.string().nullable().optional(), submit_email_setting: z.object({}).nullable().optional(), thx_msg_setting: z.object({}).nullable().optional(), created_at: z.string().optional(), response_count: z.number().int().optional()}).optional(), relationships: z.object({form_responses: z.object({data: z.array(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 formListResponse = z.object({jsonapi: z.object({version: z.string()}), data: z.array(formResource).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 Forms * * GET /forms */ export const getApiForms = async (args: GetApiFormsArgs, options?: GetApiFormsOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/forms${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(formListResponse, await res.json()) };