import {z} from 'zod' export type InvitationContact = {email?: string | undefined, familyName?: string | undefined, givenName?: string | undefined, locale?: string | undefined, phone?: string | undefined}; export type InviteeIdentifier = {type?: ('VAT' | 'SCAC' | 'DOT_NUMBER' | 'MC_NUMBER' | 'P44_EU') | undefined, value?: string | undefined}; export type InvitationOrganization = {name?: string | undefined}; export type Invitee = {contacts?: Array | undefined, identifiers?: Array | undefined, organization?: InvitationOrganization | undefined}; export type Invitation = {capability?: 'TRUCKLOAD_ASSET_TRACKING' | undefined, createdDateTime?: string | undefined, invitee?: Invitee | undefined, lastModifiedDateTime?: string | undefined, status?: ('ACCEPTED' | 'DECLINED' | 'IN_PROGRESS' | 'CANCELLED' | 'EXPIRED') | undefined}; export type CreateApiServicesInvitationV1InvitationsArgs = {body: Invitation}; export type CreateApiServicesInvitationV1InvitationsOptions = {baseUrl?: string | undefined, oAuth2?: string | undefined, fetch?: typeof fetch | undefined}; 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 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 }; /** * Invite an entity to join your network. * * POST /services/invitation/v1/invitations */ export const createApiServicesInvitationV1Invitations = async (args: CreateApiServicesInvitationV1InvitationsArgs, options?: CreateApiServicesInvitationV1InvitationsOptions) => { const {baseUrl = "https://na12.api.project44.com", fetch: fetchFn = fetch, oAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(invitation, await res.json()) };