import {z} from 'zod' export type InvoiceSeriesListParams = {search?: string | undefined, sort?: string | undefined, order?: string | undefined, count?: number | undefined, page?: number | undefined, limit?: number | undefined, organizationId?: string | undefined, status?: string | undefined, offset?: number | undefined}; export type CreateApiV1InvoiceSeriesSearchArgs = {body: InvoiceSeriesListParams}; export type CreateApiV1InvoiceSeriesSearchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceSeriesDto = z.object({id: z.string().optional(), user: z.string().optional(), lastUpdate: z.number().int().optional(), created: z.number().int().optional(), deleted: z.boolean().optional(), organizationId: z.string().optional(), name: z.string().optional(), pattern: z.string().optional(), prefix: z.string().optional(), currentCounter: z.number().int().optional(), resetBehavior: z.string().optional(), lastResetYear: z.number().int().optional(), lastResetMonth: z.number().int().optional(), active: z.boolean().optional(), lastInvoiceNumber: z.string().optional(), lastUsedTimestamp: z.number().int().optional()}); export const listParams = z.object({search: z.string().optional(), sort: z.string().optional(), order: z.string().optional(), count: z.number().int().optional(), page: z.number().int().optional(), limit: z.number().int().optional(), offset: z.number().int().optional()}); export const invoiceSeriesList = z.object({items: z.array(invoiceSeriesDto).optional(), params: listParams.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 }; /** * Search invoice series * * POST /v1/invoice-series/search * * Also accepts auth: apiKeyAuth */ export const createApiV1InvoiceSeriesSearch = async (args: CreateApiV1InvoiceSeriesSearchArgs, options?: CreateApiV1InvoiceSeriesSearchOptions) => { const {baseUrl = "https://api.timesheet.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/invoice-series/search${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(invoiceSeriesList, await res.json()) };