import {z} from 'zod' export type GetApiV3GfwTemplatesVersionsEditGroupIdOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const childQuestionModel = z.object({type: z.string(), label: z.object({en: z.string().optional()}), name: z.string(), defaultValue: z.string().optional(), values: z.string(), required: z.boolean(), order: z.number().optional(), conditionalValue: z.number().optional(), maxImageCount: z.number().optional()}); export const questionModel = z.object({type: z.string(), label: z.object({en: z.string().optional()}), name: z.string(), defaultValue: z.string().optional(), values: z.string(), required: z.boolean(), order: z.number().optional(), childQuestions: childQuestionModel.optional(), maxImageCount: z.number().optional()}); export const templateModel = z.object({name: z.string().optional(), user: z.string().optional(), languages: z.array(z.string()).optional(), defaultLanguage: z.string().optional(), public: z.boolean().optional(), status: z.string().optional(), createdAt: z.string().optional(), questions: z.array(questionModel).optional(), answersCount: z.number().int().optional(), isLatest: z.boolean().optional(), editGroupId: z.string().optional(), areas: z.array(z.object({id: z.string().optional(), name: z.string().optional()})).optional()}); export const getApiV3GfwTemplatesVersionsEditGroupIdResponse = z.object({data: z.array(z.object({id: z.string().optional(), type: z.string().optional(), attributes: templateModel.optional()})).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 }; /** * Get all versions of a group * * GET /v3/gfw/templates/versions/{editGroupId} */ export const getApiV3GfwTemplatesVersionsEditGroupId = async (options?: GetApiV3GfwTemplatesVersionsEditGroupIdOptions) => { const {baseUrl = "http://localhost:3000", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Authorization !== undefined) headers.set('authorization', `Bearer ${Authorization}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/gfw/templates/versions/${encodeURIComponent(String(args.editGroupId))}${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(getApiV3GfwTemplatesVersionsEditGroupIdResponse, await res.json()) };