import {z} from 'zod' export type RequestDetails = {additionalDetails?: Array<{[key: string]: string}> | undefined, senderIdDetails?: Array<{[key: string]: string}> | undefined}; export type RegistrationCreateBody = {callbackUrl?: string | undefined, notificationContacts?: Array | undefined, policyId?: string | undefined, requestDetails?: Array | undefined, tags?: Array | undefined, servicePlanId?: string | undefined}; export type CreateApiV1ProjectsProjectIdRegistrationsArgs = {projectId: string, body: RegistrationCreateBody}; export type CreateApiV1ProjectsProjectIdRegistrationsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const attachment = z.object({attachmentId: z.string().optional(), createTime: z.string().optional(), fileName: z.string().optional(), mimeType: z.string().optional(), updateTime: z.string().optional(), uploadStatus: z.enum(["COMPLETED", "WAITING", "FAILED"]).optional(), documentType: z.string().optional()}); export const logEntry = z.object({createTime: z.string().optional(), message: z.string().optional(), registrationState: z.string().optional()}); export const pricing = z.object({currency: z.string().optional(), frequency: z.number().int().optional(), mode: z.enum(["SUBSCRIBED", "MANUAL", "NONE"]).optional(), recurrentFeeAmount: z.string().optional(), setupFeeAmount: z.string().optional()}); export const requestDetails = z.object({additionalDetails: z.array(z.record(z.string(), z.string())).optional(), senderIdDetails: z.array(z.record(z.string(), z.string())).optional()}); export const senderId = z.object({value: z.string().optional(), isRejected: z.boolean().optional()}); export const getRegistrationResponse = z.object({attachments: z.array(attachment).optional(), callbackUrl: z.string().optional(), createTime: z.string().optional(), etaDate: z.string().optional(), id: z.string().optional(), logs: z.array(logEntry).optional(), notificationContacts: z.array(z.string()).optional(), owner: z.string().optional(), policyId: z.string().optional(), price: pricing.optional(), registrationType: z.enum(["ALPHANUMERIC_SENDER_ID", "LONG_NUMBER", "SHORT_CODE", "RCS_AGENT"]).optional(), requestDetails: z.array(requestDetails).optional(), senderIds: z.array(z.string()).optional(), senderElements: z.array(senderId).optional(), status: z.enum(["DRAFT", "IN_QUEUE", "IN_PROGRESS", "APPROVED", "REJECTED", "PENDING", "ON_HOLD", "PENDING_APPROVAL", "PENDING_ATTACHMENTS"]).optional(), tags: z.array(z.string()).optional(), updateTime: z.string().optional(), servicePlanId: z.string().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 }; /** * Submit registration * * POST /v1/projects/{projectId}/registrations */ export const createApiV1ProjectsProjectIdRegistrations = async (args: CreateApiV1ProjectsProjectIdRegistrationsArgs, options?: CreateApiV1ProjectsProjectIdRegistrationsOptions) => { const {baseUrl = "https://senderid.registrations.api.sinch.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(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/registrations${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(getRegistrationResponse, await res.json()) };