import {z} from 'zod' export type GetApiCompaniesCompanySlugOrderConfirmationsArgs = {page?: number | undefined, pageSize?: number | undefined}; export type GetApiCompaniesCompanySlugOrderConfirmationsOptions = {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 invoiceLineResult = z.object({net: z.number().int().optional(), vat: z.number().int().optional(), vatType: z.string().optional(), gross: z.number().int().optional(), netInNok: z.number().int().optional(), vatInNok: z.number().int().optional(), grossInNok: z.number().int().optional(), vatInPercent: z.number().optional(), unitPrice: z.number().int().optional(), quantity: z.number().optional(), discount: z.number().optional(), productId: z.number().int().optional(), productName: z.string().optional(), description: z.string().max(200).optional(), comment: z.string().max(200).optional(), incomeAccount: z.string().optional()}); export const orderConfirmation = z.object({confirmationId: z.number().int().optional(), confirmationDraftUuid: z.string().optional(), date: z.string().optional(), confirmationNumber: z.number().int().optional(), net: z.number().int().optional(), vat: z.number().int().optional(), gross: z.number().int().optional(), comment: z.string().optional(), yourReference: z.string().optional(), ourReference: z.string().optional(), orderReference: z.string().optional(), discount: z.number().optional(), address: address.optional(), lines: z.array(invoiceLineResult).optional(), currency: z.string().optional(), contactId: z.number().int().optional(), contactPersonId: z.number().int().optional(), projectId: z.number().int().optional(), createdInvoice: z.number().int().optional(), archived: z.boolean().optional(), internalComment: z.string().optional()}); export const getApiCompaniesCompanySlugOrderConfirmationsResponse = z.array(orderConfirmation); 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 confirmations for given company * * GET /companies/{companySlug}/orderConfirmations */ export const getApiCompaniesCompanySlugOrderConfirmations = async (args: GetApiCompaniesCompanySlugOrderConfirmationsArgs, options?: GetApiCompaniesCompanySlugOrderConfirmationsOptions) => { 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${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(getApiCompaniesCompanySlugOrderConfirmationsResponse, await res.json()) };