import {z} from 'zod' export type CreateApiNetworkPropertiesArgs = {body: {service_token: string, access_token: string}}; export type CreateApiNetworkPropertiesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiNetworkPropertiesResponse = z.array(z.object({network_id: z.number().int().optional(), network_type: z.number().int().optional(), instant_sharing: z.number().int().optional(), image_format: z.string().optional(), image_max_width: z.number().int().optional(), image_max_height: z.number().int().optional(), image_default_convert: z.string().optional(), force_download_image: z.number().int().optional(), character_limit: z.number().int().optional(), character_limit_title: z.number().int().optional(), html_support: z.string().nullable().optional(), emoji_support: z.number().int().optional(), video_support: z.number().int().optional(), video_upload_type: z.number().int().optional(), video_format: z.string().optional(), video_max_length: z.number().int().optional(), video_min_length: z.number().int().optional(), video_max_size: z.number().int().optional(), video_max_size_original: z.number().int().optional(), reel_support: z.object({reel_format: z.string().optional(), reel_max_length: z.number().int().optional(), reel_min_length: z.number().int().optional(), reel_min_framerate: z.number().int().optional(), reel_max_framerate: z.number().int().optional(), reel_aspect_ratio_x: z.number().int().optional(), reel_aspect_ratio_y: z.number().int().optional(), reel_min_resolution_x: z.number().int().optional(), reel_min_resolution_y: z.number().int().optional(), reel_max_resolution_x: z.number().int().optional()}).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 }; /** * List Network Properties * * POST /network/properties */ export const createApiNetworkProperties = async (args: CreateApiNetworkPropertiesArgs, options?: CreateApiNetworkPropertiesOptions) => { 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/properties${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(createApiNetworkPropertiesResponse, await res.json()) };