import {z} from 'zod' export type VideoPostObject = Array<{client_user_network_id?: number | undefined, title?: string | undefined, message?: string | undefined, postFormat?: 2 | undefined, customUrl?: string | undefined, noCache?: (0 | 1) | undefined, tags?: Array | undefined, board?: string | undefined, mediaObjects?: (Array<{type?: 'VIDEO' | undefined, url?: string | undefined}>) | undefined, is_retweet?: (0 | 1) | undefined, network_post_id?: string | undefined, post_id?: number | undefined, make_threads?: (0 | 1) | undefined, ignore_link?: (0 | 1) | undefined}>; export type CreateApiNetworkPostCreateArgs = {body: VideoPostObject}; export type CreateApiNetworkPostCreateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiNetworkPostCreateResponse = z.array(z.object({error: z.number().int().optional(), network_id: z.number().int().optional(), type: z.number().int().optional(), client_user_network_id: z.number().int().optional(), extra: z.object({expired: z.number().int().optional()}).optional(), video_token: z.string().optional(), video_upload_type: 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 }; /** * Get a Video Token * * POST /network/post/create/ */ export const createApiNetworkPostCreate = async (args: CreateApiNetworkPostCreateArgs, options?: CreateApiNetworkPostCreateOptions) => { const {baseUrl = "https://api.blog2social.com/rest/v1.0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/network/post/create/${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(createApiNetworkPostCreateResponse, await res.json()) };