import {z} from 'zod' export type OrderCreateRequest = {order: {external_id?: string | undefined, customer: {plunet_id?: number | undefined, id?: string | undefined, name?: string | undefined}, project_manager: {email?: string | undefined, id?: string | undefined}, contact_person?: ({plunet_id?: number | undefined, email?: string | undefined}) | undefined, project_name?: string | undefined, subject?: string | undefined, currency?: string | undefined, order_date?: (string | null) | undefined, delivery_deadline?: (string | null) | undefined, rate?: (number | null) | undefined, project_manager_memo?: (string | null) | undefined, template_id?: (number | null) | undefined, items: Array<{language_source?: string | undefined, language_target?: string | undefined, quantity?: number | undefined, description?: (string | null) | undefined, delivery_date?: (string | null) | undefined}>}}; export type CreateApiV1OrdersArgs = {body: OrderCreateRequest}; export type CreateApiV1OrdersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const orderCreateResponse = z.object({id: z.string().nullable().optional(), plunet_id: z.string().nullable().optional(), created: z.boolean().optional(), order_reference: z.string().nullable().optional(), status: z.string().nullable().optional(), external_id: z.string().nullable().optional(), items: z.array(z.object({id: z.string().nullable().optional(), language_source: z.string().nullable().optional(), language_target: z.string().nullable().optional()})).optional(), urls: z.object({worders_admin: z.string().optional(), plunet: z.string().nullable().optional()}).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 a Plunet order with items * * POST /V1/orders */ export const createApiV1Orders = async (args: CreateApiV1OrdersArgs, options?: CreateApiV1OrdersOptions) => { const {baseUrl = "https://api.worders.net", 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/orders${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(orderCreateResponse, await res.json()) };