import {z} from 'zod' export type GetApiCompaniesCompanySlugOrderConfirmationsDraftsArgs = {page?: number | undefined, pageSize?: number | undefined}; export type GetApiCompaniesCompanySlugOrderConfirmationsDraftsOptions = {baseUrl?: string | undefined, fikenApiOauth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceishDraftLine = z.object({invoiceishDraftLineId: z.number().int().optional(), lastModifiedDate: z.string().optional(), productId: z.number().int().optional(), description: z.string().max(200).optional(), unitPrice: z.number().int().optional(), vatType: z.string().optional(), quantity: z.number(), discount: z.number().optional(), comment: z.string().max(200).optional(), incomeAccount: z.string().optional()}); 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 roundingType = z.enum(["none", "round_half", "round_whole", "round_down_half", "round_down_whole"]); export const frequency = z.object({interval: z.number().int().gte(1), intervalUnit: z.enum(["DAY", "WEEK", "MONTH"])}); export const invoiceishDraftResult = z.object({draftId: z.number().int().optional(), uuid: z.string().optional(), type: z.enum(["invoice", "cash_invoice", "offer", "order_confirmation", "credit_note", "repeating_invoice"]).optional(), lastModifiedDate: z.string().optional(), issueDate: z.string().optional(), daysUntilDueDate: z.number().int().optional(), invoiceText: z.string().optional(), currency: z.string().optional(), yourReference: z.string().optional(), ourReference: z.string().optional(), orderReference: z.string().optional(), lines: z.array(invoiceishDraftLine).optional(), net: z.number().int().optional(), gross: z.number().int().optional(), bankAccountNumber: z.string().optional(), iban: z.string().optional(), bic: z.string().optional(), paymentAccount: z.string().optional(), customers: z.array(contact).optional(), attachments: z.array(attachment).optional(), createdFromInvoiceId: z.number().int().optional(), projectId: z.number().int().optional(), roundingType: roundingType.optional(), startDate: z.string().optional(), endDate: z.string().optional(), frequency: frequency.optional()}); export const getApiCompaniesCompanySlugOrderConfirmationsDraftsResponse = z.array(invoiceishDraftResult); 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 order confirmation drafts for given company. * * GET /companies/{companySlug}/orderConfirmations/drafts */ export const getApiCompaniesCompanySlugOrderConfirmationsDrafts = async (args: GetApiCompaniesCompanySlugOrderConfirmationsDraftsArgs, options?: GetApiCompaniesCompanySlugOrderConfirmationsDraftsOptions) => { 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))}/orderConfirmations/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(getApiCompaniesCompanySlugOrderConfirmationsDraftsResponse, await res.json()) };