import {z} from 'zod' export type ApiKeyField = string; export type ApiKeyOnly = {apikey: ApiKeyField}; export type CreateApiSiebatchdV1ValidateArgs = {body: ApiKeyOnly}; export type CreateApiSiebatchdV1ValidateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const channelDescription = z.object({description: z.string().optional()}); export const createApiSiebatchdV1ValidateResponse = z.object({profile: z.object({username: z.string().optional(), apikey: z.string().optional(), over_quota_action: z.string().optional(), channels: z.object({}).optional(), not_really: z.boolean().optional(), siebatch: z.record(z.string(), channelDescription).optional(), anomalies: z.object({}).optional(), state_quota: z.string().optional()}).optional(), _status: z.literal("OK").optional(), _message: 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 }; /** * Validate API Key * * POST /siebatchd/v1/validate */ export const createApiSiebatchdV1Validate = async (args: CreateApiSiebatchdV1ValidateArgs, options?: CreateApiSiebatchdV1ValidateOptions) => { const {baseUrl = "https://batch.sie-remote.net", 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(/\/+$/, '')}/siebatchd/v1/validate${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(createApiSiebatchdV1ValidateResponse, await res.json()) };