import {z} from 'zod' export type GetApiCompaniesCompanySlugTransactionsArgs = {page?: number | undefined, pageSize?: number | undefined, lastModified?: string | undefined, lastModifiedLe?: string | undefined, lastModifiedLt?: string | undefined, lastModifiedGe?: string | undefined, lastModifiedGt?: string | undefined, createdDate?: string | undefined, createdDateLe?: string | undefined, createdDateLt?: string | undefined, createdDateGe?: string | undefined, createdDateGt?: string | undefined, type?: string | undefined}; export type GetApiCompaniesCompanySlugTransactionsOptions = {baseUrl?: string | undefined, fikenApiOauth?: string | undefined, fetch?: typeof fetch | undefined}; export const journalEntryLine = z.object({amount: z.number().int(), account: z.string().optional(), vatCode: z.string().optional(), debitAccount: z.string().optional(), debitVatCode: z.number().int().optional(), creditAccount: z.string().optional(), creditVatCode: z.number().int().optional(), projectId: z.array(z.number().int()).optional(), lastModifiedDate: 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 journalEntry = z.object({journalEntryId: z.number().int().optional(), createdDate: z.string().optional(), lastModifiedDate: z.string().optional(), transactionId: z.number().int().optional(), offsetTransactionId: z.number().int().optional(), transactionType: z.string().optional(), journalEntryNumber: z.number().int().optional(), description: z.string().max(200), date: z.string(), lines: z.array(journalEntryLine), attachments: z.array(attachment).optional()}); export const transaction = z.object({transactionId: z.number().int().optional(), createdDate: z.string().optional(), lastModifiedDate: z.string().optional(), description: z.string().optional(), type: z.string().optional(), entries: z.array(journalEntry).optional(), deleted: z.boolean().optional()}); export const getApiCompaniesCompanySlugTransactionsResponse = z.array(transaction); 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 transactions for the specified company * * GET /companies/{companySlug}/transactions */ export const getApiCompaniesCompanySlugTransactions = async (args: GetApiCompaniesCompanySlugTransactionsArgs, options?: GetApiCompaniesCompanySlugTransactionsOptions) => { 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.lastModified != null) query.append("lastModified", String(args.lastModified)) if (args.lastModifiedLe != null) query.append("lastModifiedLe", String(args.lastModifiedLe)) if (args.lastModifiedLt != null) query.append("lastModifiedLt", String(args.lastModifiedLt)) if (args.lastModifiedGe != null) query.append("lastModifiedGe", String(args.lastModifiedGe)) if (args.lastModifiedGt != null) query.append("lastModifiedGt", String(args.lastModifiedGt)) if (args.createdDate != null) query.append("createdDate", String(args.createdDate)) if (args.createdDateLe != null) query.append("createdDateLe", String(args.createdDateLe)) if (args.createdDateLt != null) query.append("createdDateLt", String(args.createdDateLt)) if (args.createdDateGe != null) query.append("createdDateGe", String(args.createdDateGe)) if (args.createdDateGt != null) query.append("createdDateGt", String(args.createdDateGt)) if (args.type != null) query.append("type", String(args.type)) if (fikenApiOauth !== undefined) headers.set('authorization', `Bearer ${fikenApiOauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companySlug))}/transactions${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(getApiCompaniesCompanySlugTransactionsResponse, await res.json()) };