import {z} from 'zod' export type GetApiCompaniesCompanySlugPurchasesDraftsArgs = {page?: number | undefined, pageSize?: number | undefined}; export type GetApiCompaniesCompanySlugPurchasesDraftsOptions = {baseUrl?: string | undefined, fikenApiOauth?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({streetAddress: z.string().optional(), streetAddressLine2: z.string().optional(), city: z.string().optional(), postCode: z.string().optional(), country: z.string()}); export const contactPerson = z.object({contactPersonId: z.number().int().optional(), name: z.string(), email: z.string(), phoneNumber: z.string().optional(), address: address.optional()}); export const contactNote = z.object({author: z.string().optional(), note: z.string().optional()}); export const attachment = z.object({uuid: z.string().optional(), filename: z.string().optional(), identifier: z.string().optional(), downloadUrl: z.string().optional(), downloadUrlWithFikenNormalUserCredentials: z.string().optional(), comment: z.string().optional(), type: z.enum(["invoice", "reminder", "unspecified", "ocr", "bank_statement"]).optional()}); export const contact = z.object({contactId: z.number().int().optional(), createdDate: z.string().optional(), lastModifiedDate: z.string().optional(), name: z.string().min(1).max(200), email: z.string().max(200).optional(), organizationNumber: z.string().max(200).optional(), customerNumber: z.number().int().optional(), customerAccountCode: z.string().optional(), phoneNumber: z.string().max(20).optional(), memberNumber: z.number().optional(), memberNumberString: z.string().optional(), supplierNumber: z.number().int().optional(), supplierAccountCode: z.string().optional(), customer: z.boolean().optional(), supplier: z.boolean().optional(), bankAccountNumber: z.string().max(11).optional(), contactPerson: z.array(contactPerson).optional(), notes: z.array(contactNote).optional(), currency: z.string().optional(), language: z.string().nullable().optional(), inactive: z.boolean().optional(), daysUntilInvoicingDueDate: z.number().int().optional(), discount: z.number().optional(), address: address.optional(), groups: z.array(z.string()).optional(), documents: z.array(attachment).optional()}); export const projectResult = z.object({projectId: z.number().int().optional(), number: z.string().optional(), name: z.string().optional(), description: z.string().optional(), startDate: z.string().optional(), endDate: z.string().optional(), contact: contact.optional(), completed: z.boolean().optional()}); export const payment = z.object({paymentId: z.number().int().optional(), date: z.string(), account: z.string(), amount: z.number().int(), amountInNok: z.number().int().optional(), currency: z.string().optional(), fee: z.number().int().optional()}); export const draftLineResult = z.object({text: z.string().max(200).optional(), vatType: z.string().optional(), incomeAccount: z.string().optional(), net: z.number().int().optional(), gross: z.number().int().optional(), project: projectResult.optional()}); export const draftResult = z.object({draftId: z.number().int().optional(), uuid: z.string().optional(), invoiceIssueDate: z.string().optional(), dueDate: z.string().optional(), invoiceNumber: z.string().optional(), contact: contact.optional(), project: projectResult.optional(), cash: z.boolean().optional(), currency: z.string().optional(), kid: z.string().optional(), paid: z.boolean().optional(), attachments: z.array(attachment).optional(), payments: z.array(payment).optional(), lines: z.array(draftLineResult).optional()}); export const getApiCompaniesCompanySlugPurchasesDraftsResponse = z.array(draftResult); 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 all purchase drafts for given company. * * GET /companies/{companySlug}/purchases/drafts */ export const getApiCompaniesCompanySlugPurchasesDrafts = async (args: GetApiCompaniesCompanySlugPurchasesDraftsArgs, options?: GetApiCompaniesCompanySlugPurchasesDraftsOptions) => { 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 (fikenApiOauth !== undefined) headers.set('authorization', `Bearer ${fikenApiOauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companySlug))}/purchases/drafts${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(getApiCompaniesCompanySlugPurchasesDraftsResponse, await res.json()) };