import {z} from 'zod' export type GetApiV1QuotesArgs = {customer_plunet_id?: number | undefined, status?: string | undefined, updated_since?: string | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiV1QuotesOptions = {baseUrl?: string | undefined, cookieSession?: string | undefined, fetch?: typeof fetch | undefined}; export const quoteCustomer = z.object({plunet_id: z.number().int().nullable().optional(), name: z.string().optional(), address: z.string().nullable().optional()}).nullable(); export const priceLineEntry = z.object({price_unit_id: z.number().int().nullable().optional(), service: z.string().nullable().optional(), unit: z.string().nullable().optional(), amount: z.number().optional(), unit_price: z.number().optional(), total: z.number().optional()}); export const quoteItem = z.object({id: z.string().nullable().optional(), plunet_id: z.string().nullable().optional(), language_source: z.string().nullable().optional(), language_target: z.string().nullable().optional(), client_price: z.number().nullable().optional(), status: z.string().nullable().optional(), price_line: z.array(priceLineEntry).optional()}); export const quote = z.object({id: z.string().optional(), plunet_id: z.string().nullable().optional(), order_reference: z.string().nullable().optional(), created_at: z.string().optional(), valid_until: z.string().nullable().optional(), status: z.string().nullable().optional(), project_name: z.string().nullable().optional(), client_ccy: z.string().nullable().optional(), price_in_client_ccy: z.number().nullable().optional(), customer: quoteCustomer.optional(), contact_person: z.object({full_name: z.string().optional(), email: z.string().optional()}).nullable().optional(), project_manager: z.object({full_name: z.string().optional()}).nullable().optional(), items: z.array(quoteItem).optional()}); export const paginationMeta = z.object({total: z.number().int().optional(), page: z.number().int().optional(), per_page: z.number().int().optional()}); export const getApiV1QuotesResponse = z.object({data: z.array(quote).optional(), meta: paginationMeta.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 }; /** * List quotes * * GET /V1/quotes * * Also accepts auth: bearer_auth */ export const getApiV1Quotes = async (args: GetApiV1QuotesArgs, options?: GetApiV1QuotesOptions) => { const {baseUrl = "https://api.worders.net", fetch: fetchFn = fetch, cookieSession} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customer_plunet_id != null) query.append("customer_plunet_id", String(args.customer_plunet_id)) if (args.status != null) query.append("status", String(args.status)) if (args.updated_since != null) query.append("updated_since", String(args.updated_since)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (cookieSession !== undefined) cookies.push("_worders_session=" + encodeURIComponent(cookieSession)) const url = `${baseUrl.replace(/\/+$/, '')}/V1/quotes${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(getApiV1QuotesResponse, await res.json()) };