import {z} from 'zod' export type GetApiCompaniesCompanySlugInboxArgs = {page?: number | undefined, pageSize?: number | undefined, sortBy?: ('createdDate asc' | 'createdDate desc' | 'name asc' | 'name desc') | undefined, status?: ('all' | 'unused' | 'used') | undefined, name?: string | undefined}; export type GetApiCompaniesCompanySlugInboxOptions = {baseUrl?: string | undefined, fikenApiOauth?: string | undefined, fetch?: typeof fetch | undefined}; export const inboxResult = z.object({documentId: z.number().int().optional(), name: z.string().optional(), description: z.string().optional(), filename: z.string().optional(), status: z.boolean().optional(), createdAt: z.string().optional(), documentUrl: z.string().optional(), documentUrlWithFikenNormalUserCredentials: z.string().optional()}); export const getApiCompaniesCompanySlugInboxResponse = z.array(inboxResult); 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 }; /** * Returns the contents of the inbox for given company. * * GET /companies/{companySlug}/inbox */ export const getApiCompaniesCompanySlugInbox = async (args: GetApiCompaniesCompanySlugInboxArgs, options?: GetApiCompaniesCompanySlugInboxOptions) => { const {baseUrl = "https://api.fiken.no/api/v2", fetch: fetchFn = fetch, fikenApiOauth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.status != null) query.append("status", String(args.status)) if (args.name != null) query.append("name", String(args.name)) if (fikenApiOauth !== undefined) headers.set('authorization', `Bearer ${fikenApiOauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companySlug))}/inbox${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(getApiCompaniesCompanySlugInboxResponse, await res.json()) };