import {z} from 'zod' export type GetApiServicesInvitationV1InvitationsArgs = {'inviteeIdentifier.type'?: ('VAT' | 'SCAC' | 'DOT_NUMBER' | 'MC_NUMBER' | 'P44_EU') | undefined, 'inviteeIdentifier.value'?: string | undefined, capability?: 'TRUCKLOAD_ASSET_TRACKING' | undefined, pageNumber?: number | undefined, pageSize?: number | undefined}; export type GetApiServicesInvitationV1InvitationsOptions = {baseUrl?: string | undefined, oAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationInfo = z.object({pageNumber: z.number().int().optional(), pageSize: z.number().int().optional(), total: z.number().int().optional()}); export const invitationContact = z.object({email: z.string().optional(), familyName: z.string().optional(), givenName: z.string().optional(), locale: z.string().optional(), phone: z.string().optional()}); export const inviteeIdentifier = z.object({type: z.enum(["VAT", "SCAC", "DOT_NUMBER", "MC_NUMBER", "P44_EU"]).optional(), value: z.string().optional()}); export const invitationOrganization = z.object({name: z.string().optional()}); export const invitee = z.object({contacts: z.array(invitationContact).optional(), identifiers: z.array(inviteeIdentifier).optional(), organization: invitationOrganization.optional()}); export const invitation = z.object({capability: z.literal("TRUCKLOAD_ASSET_TRACKING").optional(), createdDateTime: z.string().optional(), invitee: invitee.optional(), lastModifiedDateTime: z.string().optional(), status: z.enum(["ACCEPTED", "DECLINED", "IN_PROGRESS", "CANCELLED", "EXPIRED"]).optional()}); export const invitationPage = z.object({paginationInfo: paginationInfo.optional(), results: z.array(invitation).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 }; /** * Get invitations matching a criteria * * GET /services/invitation/v1/invitations */ export const getApiServicesInvitationV1Invitations = async (args: GetApiServicesInvitationV1InvitationsArgs, options?: GetApiServicesInvitationV1InvitationsOptions) => { const {baseUrl = "https://na12.api.project44.com", fetch: fetchFn = fetch, oAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['inviteeIdentifier.type'] != null) query.append("inviteeIdentifier.type", String(args['inviteeIdentifier.type'])) if (args['inviteeIdentifier.value'] != null) query.append("inviteeIdentifier.value", String(args['inviteeIdentifier.value'])) if (args.capability != null) query.append("capability", String(args.capability)) if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (oAuth2 !== undefined) headers.set('authorization', `Bearer ${oAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/services/invitation/v1/invitations${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(invitationPage, await res.json()) };