import {z} from 'zod' export type GetApiV2UserChannelsChannelIdArgs = {channelId: unknown}; export type GetApiV2UserChannelsChannelIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const beezUPCommonChannelId = z.string(); export const beezUPCommonChannelName = z.string(); export const beezUPCommonHttpUrl = z.string(); export const channelInfo = z.object({channelId: beezUPCommonChannelId, channelName: beezUPCommonChannelName, channelLogoUrl: beezUPCommonHttpUrl.optional(), channelDescription: z.string().optional(), beezUPOffer: z.string().optional(), salesContact: z.object({name: z.string().optional(), email: z.string().optional(), phoneNumber: z.string().optional()}).optional(), details: z.object({costs: z.string().optional(), businessModel: z.string().optional(), channelType: z.string().optional(), trackingType: z.string().optional(), category: z.string().optional(), homeUrl: z.string().optional(), subscriptionLink: z.string().optional()}).optional(), keyNumbers: z.object({products: z.string().optional(), categories: z.string().optional(), stores: z.string().optional(), viewsPerMonth: z.string().optional()}).optional(), technicalContact: z.object({name: z.string().optional(), phoneNumber: z.string().optional(), email: z.string().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 }; /** * Get channel information * * GET /v2/user/channels/{channelId} */ export const getApiV2UserChannelsChannelId = async (args: GetApiV2UserChannelsChannelIdArgs, options?: GetApiV2UserChannelsChannelIdOptions) => { const {baseUrl = "https://api.beezup.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/user/channels/${encodeURIComponent(String(args.channelId))}${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(channelInfo, await res.json()) };