import {z} from 'zod' export type ClientBillingDetails = {companyName?: (string | null) | undefined, vatNumber?: (string | null) | undefined, addressLine1?: (string | null) | undefined, addressLine2?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, postcode?: (string | null) | undefined, country?: (string | null) | undefined, contactName?: (string | null) | undefined, contactPhone?: (string | null) | undefined, contactEmail?: (string | null) | undefined, peppolEndpointId?: (string | null) | undefined, peppolEndpointScheme?: (string | null) | undefined, peppolMeansCode?: (string | null) | undefined, bankAccountNumber?: (string | null) | undefined, bankAccountRoutingNumber?: (string | null) | undefined, bankAccountHolderName?: (string | null) | undefined}; export type ClientRequestCreateRequest = {name: string, projects?: Array | undefined, lineItemMask?: string | undefined, defaultTax?: ({id?: number | undefined, rate?: number | undefined}) | undefined, defaultDiscount?: number | undefined, paymentDueDays?: number | undefined, reference?: string | undefined, invoicePublicNotes?: string | undefined, businessDetails?: string | undefined, billingDetails?: ClientBillingDetails | undefined, excludedLabels?: Array | undefined, email?: Array | undefined}; export type CreateApiClientsArgs = {body: ClientRequestCreateRequest}; export type CreateApiClientsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const clientBudget = z.object({type: z.enum(["money", "time", "costs"]), budget: z.number(), period: z.enum(["general", "monthly", "weekly", "daily", "quarterly", "yearly"]), appliedFrom: z.string().nullable().optional(), disallowOverbudget: z.boolean().optional(), excludeUnbillableTime: z.boolean().optional(), excludeExpenses: z.boolean().optional(), threshold: z.number().optional(), progress: z.number().optional()}); export const client = z.object({id: z.number(), name: z.string(), projects: z.array(z.string()), businessDetails: z.string().nullable().optional(), budget: clientBudget.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 }; /** * Create Client * * POST /clients */ export const createApiClients = async (args: CreateApiClientsArgs, options?: CreateApiClientsOptions) => { const {baseUrl = "https://api.everhour.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/clients${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(client, await res.json()) };