import {z} from 'zod' export type CollectionArticle = {article: number, order?: (number | null) | undefined, section: number, title?: (string | null) | undefined, spoiler?: (string | null) | undefined}; export type HelpdeskCollection = {_id?: (number | null) | undefined, icon?: (string | null) | undefined, title: string, spoiler?: (string | null) | undefined, language?: (string | null) | undefined, sections?: (Array | null) | undefined, order?: (number | null) | undefined, slug?: (string | null) | undefined, desk: string, departmentId?: (number | null) | undefined, previous?: HelpdeskCollection | undefined, articles?: (Array | null) | undefined, createdAt: unknown, updatedAt: unknown}; export type CreateApiV1HelpcenterCollectionsArgs = {body: HelpdeskCollection}; export type CreateApiV1HelpcenterCollectionsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const collectionArticle = z.object({article: z.number().int(), order: z.number().int().nullable().optional(), section: z.number().int(), title: z.string().nullable().optional(), spoiler: z.string().nullable().optional()}); export const helpdeskCollection: z.ZodType = z.object({_id: z.number().int().nullable().optional(), icon: z.string().nullable().optional(), title: z.string(), spoiler: z.string().nullable().optional(), language: z.string().nullable().optional(), sections: z.array(z.string()).nullable().optional(), order: z.number().int().nullable().optional(), slug: z.string().nullable().optional(), desk: z.string(), departmentId: z.number().int().nullable().optional(), previous: z.lazy(() => helpdeskCollection.optional()), articles: z.array(collectionArticle).nullable().optional(), createdAt: z.unknown(), updatedAt: z.unknown()}); 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 }; /** * Create collection * * POST /v1/helpcenter/collections * * Also accepts auth: API Key */ export const createApiV1HelpcenterCollections = async (args: CreateApiV1HelpcenterCollectionsArgs, options: CreateApiV1HelpcenterCollectionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/helpcenter/collections${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(helpdeskCollection, await res.json()) };