import {z} from 'zod' export type ApiSmsCampaignStatuses = 'Unsent' | 'Sending' | 'Sent' | 'Cancelled' | 'NotAvailableInThisVersion'; export type ApiSmsCampaign = {message: string, id?: number | undefined, name: string, sendingIdentityId?: number | undefined, sendingIdentity?: (string | null) | undefined, toggleShortLinks?: (boolean | null) | undefined, status?: ApiSmsCampaignStatuses | undefined}; export type UpdateApiV2SmsCampaignsIdArgs = {id: number, body: ApiSmsCampaign}; export type UpdateApiV2SmsCampaignsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiSmsCampaignStatuses = z.enum(["Unsent", "Sending", "Sent", "Cancelled", "NotAvailableInThisVersion"]); export const apiSmsCampaign = z.object({message: z.string().min(1), id: z.number().int().optional(), name: z.string().min(1).max(256), sendingIdentityId: z.number().int().optional(), sendingIdentity: z.string().max(15).nullable().optional(), toggleShortLinks: z.boolean().nullable().optional(), status: apiSmsCampaignStatuses.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 }; /** * Updates an SMS campaign. * * PUT /v2/sms/campaigns/{id} */ export const updateApiV2SmsCampaignsId = async (args: UpdateApiV2SmsCampaignsIdArgs, options?: UpdateApiV2SmsCampaignsIdOptions) => { const {baseUrl = "https://api.dotdigital.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(/\/+$/, '')}/v2/sms/campaigns/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(apiSmsCampaign, await res.json()) };