import {z} from 'zod' export type NewsCategory = 'bugfix' | 'company_news' | 'event' | 'maintenance' | 'update'; export type NewsRenderOptions = {is_promo_store?: boolean | undefined}; export type TokenType = 'attribute' | 'custom_attribute' | 'event_attribute' | 'custom_event_attribute' | 'operator' | 'number' | 'date' | 'string' | 'boolean' | 'list' | 'variable' | 'function' | 'virtual_currency' | 'store'; export type Token = {type: TokenType, value: unknown}; export type Expression = {tokens?: Array | undefined, name?: string | undefined, variables?: Array | undefined}; export type PinScope = 'global' | 'collection'; export type NewsUpdate = {slug?: string | undefined, title?: string | undefined, description?: string | undefined, text?: string | undefined, html?: string | undefined, image_url?: (string | '') | undefined, tags?: string | undefined, order_id?: number | undefined, category?: NewsCategory | undefined, published_at?: number | undefined, datetime?: number | undefined, render_options?: NewsRenderOptions | undefined, video_url?: string | undefined, is_featured?: boolean | undefined, collection_id?: string | undefined, requirements_expression?: Expression | undefined, featured_image_url?: (string | '') | undefined, is_pinned?: boolean | undefined, pin_scope?: PinScope | undefined, pinned_image_url?: (string | '') | undefined, pinned_webview_image_url?: (string | '') | undefined, custom_url?: (string | '') | undefined, button_text?: string | undefined}; export type UpdateApiV1NewsNewsIdArgs = {news_id: string, body: NewsUpdate}; export type UpdateApiV1NewsNewsIdOptions = {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 }; /** * Update News * * PUT /v1/news/{news_id} */ export const updateApiV1NewsNewsId = async (args: UpdateApiV1NewsNewsIdArgs, options?: UpdateApiV1NewsNewsIdOptions) => { const {baseUrl = "https://api.aghanim.com/s2s", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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: "PUT", 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(newsRead, await res.json()) };