import {z} from 'zod' export type Header = {requestId: string, service: string, issuerCode: string, subIssuerCode: string, keyTag?: string | undefined, iv?: (string | string) | undefined}; export type EchoBody = {timestamp: unknown}; export type Signature = {algorithm?: string | undefined, keyInfo?: string | undefined, value?: string | undefined}; export type Footer = {signature?: Signature | undefined}; export type EchoMessage = {header: Header, body: EchoBody, footer?: Footer | undefined}; export type CreateApiEchoArgs = {body: EchoMessage}; export type CreateApiEchoOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const header = z.object({requestId: z.string(), service: z.string().min(1).max(255), issuerCode: z.string().min(5).max(5), subIssuerCode: z.string().min(5).max(5), keyTag: z.string().min(2).max(2).optional(), iv: z.union([z.string().min(32).max(32), z.string().min(24).max(24)]).optional()}); export const echoBody = z.object({timestamp: z.string()}); export const signature = z.object({algorithm: z.string().optional(), keyInfo: z.string().optional(), value: z.string().optional()}); export const footer = z.object({signature: signature.optional()}); export const echoMessage = z.object({header: header, body: echoBody, footer: footer.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 }; /** * Echo function to test the availability of the web service. `header`'s `keyTag` and `iv` are not used in this operation. * * POST /echo */ export const createApiEcho = async (args: CreateApiEchoArgs, options: CreateApiEchoOptions) => { const {baseUrl, 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(/\/+$/, '')}/echo${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(echoMessage, await res.json()) };