import {z} from 'zod' export type RefundRequestType = 'arrived_late' | 'delivered_early' | 'delivered_to_wrong_address' | 'missing_or_incorrect_items' | 'never_delivered' | 'poor_delivery_experience' | 'other'; export type DeliveryIncidentInputSerializer = {raiseIncident: boolean, incidentNotes: string, incidentType?: ('REFUND_REQUEST' | 'COURIER_NOTES') | undefined, incidentSubtype?: (RefundRequestType | null) | undefined, incidentFileUrl?: (string | null) | undefined, incidentFileUrls?: (Array | null) | undefined, requestAmountDeliveryFeeCents?: (number | null) | undefined, requestAmountOrderValueCents?: (number | null) | undefined, requestAmountTipCents?: (number | null) | undefined}; export type CreateApiV1JobsJobIdTasksTaskIdDeliveryIncidentArgs = {jobId: string, taskId: string, body: DeliveryIncidentInputSerializer}; export type CreateApiV1JobsJobIdTasksTaskIdDeliveryIncidentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const deliveryIncidentOutputSerializer = z.object({jobId: z.string(), taskId: z.string(), raiseIncident: z.boolean(), incidentNotes: z.string(), incidentType: z.string().nullable(), incidentSubtype: z.string().nullable(), incidentFileUrl: z.string().nullable(), incidentFileUrls: z.array(z.string()).nullable(), refundRequestId: z.string().nullable().optional(), requestAmountDeliveryFeeCents: z.number().int().nullable().optional(), requestAmountOrderValueCents: z.number().int().nullable().optional(), requestAmountTipCents: z.number().int().nullable().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 delivery incident * * POST /v1/jobs/{jobId}/tasks/{taskId}/delivery_incident */ export const createApiV1JobsJobIdTasksTaskIdDeliveryIncident = async (args: CreateApiV1JobsJobIdTasksTaskIdDeliveryIncidentArgs, options?: CreateApiV1JobsJobIdTasksTaskIdDeliveryIncidentOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/jobs/${encodeURIComponent(String(args.jobId))}/tasks/${encodeURIComponent(String(args.taskId))}/delivery_incident${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(deliveryIncidentOutputSerializer, await res.json()) };