import {z} from 'zod' export type GetApiV2KnowledgebaseGlobalArticlesArgs = {articleName?: string | undefined, includeArchived?: boolean | undefined}; export type GetApiV2KnowledgebaseGlobalArticlesOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2KnowledgebaseGlobalArticlesResponse = z.array(z.object({id: z.number().int().optional(), organizationId: z.number().int().optional(), parentFolderId: z.number().int().optional(), name: z.string().optional(), isNinjaArticle: z.boolean().optional(), isArchived: z.boolean().optional(), attachments: z.array(z.object({id: z.number().int().optional(), uploadStatus: z.enum(["PROCESSING", "SUCCESS", "FAILURE", "SUSPICIOUS"]).optional(), metadata: z.object({name: z.string().optional(), mimeType: z.string().optional(), size: z.string().optional(), extension: z.string().optional(), contentId: z.string().optional(), inline: z.boolean().optional()}).optional()})).optional(), content: z.object({html: z.string().optional(), text: z.string().optional()}).optional(), createTime: z.number().optional(), updateTime: z.number().optional(), archivedTime: z.number().optional(), path: 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 }; /** * Lists global knowledge base articles * * GET /v2/knowledgebase/global/articles * * Also accepts auth: sessionKey */ export const getApiV2KnowledgebaseGlobalArticles = async (args: GetApiV2KnowledgebaseGlobalArticlesArgs, options: GetApiV2KnowledgebaseGlobalArticlesOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.articleName != null) query.append("articleName", String(args.articleName)) if (args.includeArchived != null) query.append("includeArchived", String(args.includeArchived)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/knowledgebase/global/articles${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(getApiV2KnowledgebaseGlobalArticlesResponse, await res.json()) };