import {z} from 'zod' export type SitesCreateWebviewSessionRequestBody = {channel: string, language?: string | undefined, currency?: string | undefined, customer?: {merchant_id: string, email: string} | undefined}; export type CreateApiV2SitesSiteIdWebviewsSessionsArgs = {site_id: number, body: SitesCreateWebviewSessionRequestBody}; export type CreateApiV2SitesSiteIdWebviewsSessionsOptions = {baseUrl?: string | undefined, ProgramApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const sitesCreateWebviewSessionResponseBody = z.object({webview_session: z.object({url: z.string(), expires_at: z.string()})}); 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 }; /** * POST /v2/sites/{site_id}/webviews/sessions * * Also accepts auth: SiteTokenSecret */ export const createApiV2SitesSiteIdWebviewsSessions = async (args: CreateApiV2SitesSiteIdWebviewsSessionsArgs, options?: CreateApiV2SitesSiteIdWebviewsSessionsOptions) => { const {baseUrl = "https://api.loyaltylion.com", fetch: fetchFn = fetch, ProgramApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ProgramApiKey !== undefined) headers.set('authorization', `Bearer ${ProgramApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/sites/${encodeURIComponent(String(args.site_id))}/webviews/sessions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(sitesCreateWebviewSessionResponseBody, await res.json()) };