import {z} from 'zod' export type GetApiSurveysSurveyIdResponsesArgs = {surveyId: string, limit?: number | undefined, lastEvaluated?: string | undefined, orderBy?: string | undefined}; export type GetApiSurveysSurveyIdResponsesOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiSurveysSurveyIdResponsesResponse = z.object({surveyResponses: z.array(z.object({subscriberId: z.string(), surveyResponseId: z.string(), surveyId: z.string(), surveyName: z.string(), channelSurveyId: z.string(), channelSurveyDescription: z.string().optional(), channel: z.enum(["on-site", "post-checkout", "post-review", "shareable-link"]), answeredQuestions: z.array(z.discriminatedUnion("type", [z.object({id: z.string(), text: z.string(), type: z.literal("text"), answer: z.string().optional()}), z.object({id: z.string(), text: z.string(), type: z.literal("single-select"), answer: z.string(), customAnswer: z.string().optional(), customAnswerOptionText: z.string().optional()}), z.object({id: z.string(), text: z.string(), type: z.literal("multi-select"), answer: z.array(z.string()), customAnswer: z.string().optional(), customAnswerOptionText: z.string().optional()}), z.object({id: z.string(), text: z.string(), type: z.enum(["five-point", "star"]), answer: z.number()}), z.object({id: z.string(), text: z.string(), type: z.literal("nps"), answer: z.number(), answerNpsCategory: z.enum(["promoter", "detractor", "passive"])}), z.object({id: z.string(), text: z.string(), type: z.literal("profile"), answer: z.discriminatedUnion("type", [z.object({type: z.literal("date-components"), value: z.object({day: z.number().int().gte(1).lte(31).optional(), month: z.number().int().gte(1).lte(12).optional(), year: z.number().int().gte(1900).lte(2100).optional()})}), z.object({type: z.literal("location"), value: z.object({countryCode: z.string(), countryName: z.string().optional(), zoneCode: z.string().optional()})}), z.object({type: z.literal("multi-value"), value: z.array(z.string())}), z.object({type: z.literal("single-value"), value: z.string()})]), profileQuestionId: z.string()}), z.object({id: z.string(), text: z.string(), type: z.literal("email"), answer: z.string(), marketingOptIn: z.boolean().optional()})])), customer: z.object({customerId: z.string().optional(), email: z.string().optional()}).optional(), dateCreated: z.string(), emailMarketingOptIn: z.boolean().optional(), isArchived: z.boolean().optional(), order: z.object({remoteOrderId: z.string().optional()}).optional(), reward: z.object({type: z.literal("coupon"), description: z.string(), value: z.string()}).optional()})).optional(), nextUrl: z.string().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 Survey Responses * * GET /surveys/{surveyId}/responses */ export const getApiSurveysSurveyIdResponses = async (args: GetApiSurveysSurveyIdResponsesArgs, options?: GetApiSurveysSurveyIdResponsesOptions) => { const {baseUrl = "https://api.okendo.io/enterprise", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.lastEvaluated != null) query.append("lastEvaluated", String(args.lastEvaluated)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/surveys/${encodeURIComponent(String(args.surveyId))}/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(getApiSurveysSurveyIdResponsesResponse, await res.json()) };