import {z} from 'zod' export type GetApiTemplatesAppIdAppIdLimitLimitOffsetOffsetArgs = {app_id: string, limit?: number | undefined, offset?: number | undefined, Authorization: string, 'Content-Type': string, channel?: ('push' | 'email' | 'sms') | undefined, name?: string | undefined}; export type GetApiTemplatesAppIdAppIdLimitLimitOffsetOffsetOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiTemplatesAppIdAppIdLimitLimitOffsetOffsetResponse = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), templates: z.array(z.object({id: z.string().optional(), name: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()})).optional(), total_count: z.number().int().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 }; /** * View templates * * GET /templates?app_id={app_id}&limit={limit}&offset={offset} */ export const getApiTemplatesAppIdAppIdLimitLimitOffsetOffset = async (args: GetApiTemplatesAppIdAppIdLimitLimitOffsetOffsetArgs, options?: GetApiTemplatesAppIdAppIdLimitLimitOffsetOffsetOptions) => { const {baseUrl = "https://api.onesignal.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.app_id != null) query.append("app_id", String(args.app_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.channel != null) query.append("channel", String(args.channel)) if (args.name != null) query.append("name", String(args.name)) const url = `${baseUrl.replace(/\/+$/, '')}/templates?app_id=${encodeURIComponent(String(args.app_id))}&limit=${encodeURIComponent(String(args.limit))}&offset=${encodeURIComponent(String(args.offset))}${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(getApiTemplatesAppIdAppIdLimitLimitOffsetOffsetResponse, await res.json()) };