import {z} from 'zod' export type GetApiCompaniesCompanySlugOffersOfferIdOptions = {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 dispatchResult = z.object({date: z.string().optional(), dispatchType: z.enum(["email", "sms", "ehf", "letter", "vipps", "efaktura", "unknown"]).optional()}); export const offer = z.object({offerId: z.number().int().optional(), offerDraftUuid: z.string().optional(), date: z.string().optional(), offerNumber: 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(), archived: z.boolean().optional(), accepted: z.string().optional(), dispatches: z.array(dispatchResult).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 }; /** * Returns offer with specified id. * * GET /companies/{companySlug}/offers/{offerId} */ export const getApiCompaniesCompanySlugOffersOfferId = async (options?: GetApiCompaniesCompanySlugOffersOfferIdOptions) => { 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 (fikenApiOauth !== undefined) headers.set('authorization', `Bearer ${fikenApiOauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companySlug))}/offers/${encodeURIComponent(String(args.offerId))}${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(offer, await res.json()) };