import {z} from 'zod' export type GetApiV1FreelancersFreelancerIdPurchaseOrdersArgs = {period_start?: string | undefined, period_end?: string | undefined, status?: Array | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiV1FreelancersFreelancerIdPurchaseOrdersOptions = {baseUrl?: string | undefined, cookieSession?: string | undefined, fetch?: typeof fetch | undefined}; export const purchaseOrderSummary = z.object({id: z.string().optional(), internal_id: z.string().nullable().optional(), po_number: z.string().optional(), currency: z.string().optional(), status: z.string().optional(), created_at: z.string().optional(), total_amount: z.number().optional(), jobs_count: z.number().int().optional(), freelancer: z.object({id: z.string().optional(), full_name: z.string().optional()}).optional()}); export const paginationMeta = z.object({total: z.number().int().optional(), page: z.number().int().optional(), per_page: z.number().int().optional()}); export const getApiV1FreelancersFreelancerIdPurchaseOrdersResponse = z.object({data: z.array(purchaseOrderSummary).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 purchase orders for a freelancer * * GET /V1/freelancers/{freelancer_id}/purchase_orders * * Also accepts auth: bearer_auth */ export const getApiV1FreelancersFreelancerIdPurchaseOrders = async (args: GetApiV1FreelancersFreelancerIdPurchaseOrdersArgs, options?: GetApiV1FreelancersFreelancerIdPurchaseOrdersOptions) => { 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.period_start != null) query.append("period_start", String(args.period_start)) if (args.period_end != null) query.append("period_end", String(args.period_end)) for (const item of args.status ?? []) query.append("status", String(item)) 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/freelancers/${encodeURIComponent(String(args.freelancer_id))}/purchase_orders${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(getApiV1FreelancersFreelancerIdPurchaseOrdersResponse, await res.json()) };