import { PhotoAttachment } from "vk-io"; export const SMALL_SIZES = ['m', 's']; export const MEDIUM_SIZES = ['y', 'r', 'q', 'p', ...SMALL_SIZES]; export const LARGE_SIZES = ['w', 'z', ...MEDIUM_SIZES]; export async function fetchImage(url: string, params?: RequestInit) { const image = await fetch(url, params) .then(r => r.arrayBuffer()); const imageBuffer = Buffer.from(image); return imageBuffer; } /** * Example: * ```js * const SMALL_SIZES = ['m', 's']; * const MEDIUM_SIZES = ['y', 'r', 'q', 'p', ...SMALL_SIZES]; * const LARGE_SIZES = ['w', 'z', ...MEDIUM_SIZES]; * * const [size] = getAttachmentBySizes(attachment, LARGE_SIZES); * console.log(size) * ``` */ export function getAttachmentBySizes(photoAttachment: PhotoAttachment, sizeTypes: string[] = []) { const { sizes } = photoAttachment; if (!sizes) return []; return sizeTypes .map((sizeType) => ( sizes.find((size) => size.type === sizeType) )) .filter(Boolean); }