import {z} from 'zod' export type TrusthubV1A2pCampaignRegistrationRequest = {a2pBrandRegistrationSid: string, messagingServiceSid?: string | undefined, themeSetId?: string | undefined, useCaseCategories?: Array | undefined, useCaseDescription?: string | undefined, useCaseSampleMessage1?: string | undefined, useCaseSampleMessage2?: string | undefined, useCaseSampleMessage3?: string | undefined, useCaseSampleMessage4?: string | undefined, useCaseSampleMessage5?: string | undefined, useCaseOptInTypes?: Array | undefined, useCaseOptInDescription?: string | undefined, hasEmbeddedLinks?: boolean | undefined, hasEmbeddedPhone?: boolean | undefined, embeddedUrlSample?: string | undefined, directLending?: boolean | undefined, ageGated?: boolean | undefined, privacyPolicyUrl?: string | undefined, termsAndConditionsUrl?: string | undefined, optInKeywords?: Array | undefined, optInMessageSample?: string | undefined, optOutKeywords?: Array | undefined, optOutMessageSample?: string | undefined, helpKeywords?: Array | undefined, helpMessageSample?: string | undefined}; export type CreateApiV1A2PCampaignRegistrationsArgs = {body: TrusthubV1A2pCampaignRegistrationRequest}; export type CreateApiV1A2PCampaignRegistrationsOptions = {baseUrl?: string | undefined, accountSidAuthToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const trusthubV1A2pEmbeddedSession = z.object({id: z.string(), sessionId: z.string(), sessionToken: z.string()}); 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 an A2P Campaign Registration * * POST /v1/A2PCampaignRegistrations */ export const createApiV1A2PCampaignRegistrations = async (args: CreateApiV1A2PCampaignRegistrationsArgs, options?: CreateApiV1A2PCampaignRegistrationsOptions) => { const {baseUrl = "https://trusthub.twilio.com", fetch: fetchFn = fetch, accountSidAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (accountSidAuthToken !== undefined) headers.set('authorization', `Basic ${btoa(`${accountSidAuthToken.username}:${accountSidAuthToken.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/A2PCampaignRegistrations${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(trusthubV1A2pEmbeddedSession, await res.json()) };