import {z} from 'zod' export type GetApiV1NewsNewsIdArgs = {news_id: string}; export type GetApiV1NewsNewsIdOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const newsCategory = z.enum(["bugfix", "company_news", "event", "maintenance", "update"]); export const newsRenderOptions = z.object({is_promo_store: z.boolean().optional()}); export const tokenType = z.enum(["attribute", "custom_attribute", "event_attribute", "custom_event_attribute", "operator", "number", "date", "string", "boolean", "list", "variable", "function", "virtual_currency", "store"]); export const token = z.object({type: tokenType, value: z.unknown()}); export const expression: z.ZodType = z.object({tokens: z.array(token).optional(), name: z.string().optional(), variables: z.array(z.lazy(() => expression)).optional()}); export const pinScope = z.enum(["global", "collection"]); export const newsRead = z.object({date: z.string().optional(), image_url: z.union([z.string().min(1).max(65536), z.literal("")]).optional(), tags: z.string(), order_id: z.number().int().optional(), category: newsCategory, published_at: z.number().int().optional(), datetime: z.number().int(), render_options: newsRenderOptions.optional(), video_url: z.string().min(1).max(65536).optional(), is_featured: z.boolean().optional(), collection_id: z.string().max(32).optional(), requirements_expression: expression.optional(), featured_image_url: z.union([z.string().min(1).max(65536), z.literal("")]).optional(), is_pinned: z.boolean().optional(), pin_scope: pinScope.optional(), pinned_image_url: z.union([z.string().min(1).max(65536), z.literal("")]).optional(), pinned_webview_image_url: z.union([z.string().min(1).max(65536), z.literal("")]).optional(), custom_url: z.union([z.string().min(1).max(65536), z.literal("")]).optional(), button_text: z.string().max(255).optional(), id: z.string().max(32), slug: z.string(), title: z.string(), description: z.string().optional(), text: z.string(), html: 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 }; /** * Get News Post * * GET /v1/news/{news_id} */ export const getApiV1NewsNewsId = async (args: GetApiV1NewsNewsIdArgs, options?: GetApiV1NewsNewsIdOptions) => { const {baseUrl = "https://api.aghanim.com/s2s", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/news/${encodeURIComponent(String(args.news_id))}${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(newsRead, await res.json()) };