import {z} from 'zod' export type ConsentId = string; export type AuthorisationId = string; export type GetApiV1ConsentsConsentIdAuthorisationsAuthorisationIdArgs = {consentId: ConsentId, authorisationId: AuthorisationId, 'X-Request-ID': string, Digest?: string | undefined, Signature?: string | undefined, 'TPP-Signature-Certificate'?: string | undefined, 'PSU-IP-Address'?: string | undefined, 'PSU-IP-Port'?: string | undefined, 'PSU-Accept'?: string | undefined, 'PSU-Accept-Charset'?: string | undefined, 'PSU-Accept-Encoding'?: string | undefined, 'PSU-Accept-Language'?: string | undefined, 'PSU-User-Agent'?: string | undefined, 'PSU-Http-Method'?: ('GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE') | undefined, 'PSU-Device-ID'?: string | undefined, 'PSU-Geo-Location'?: string | undefined}; export type GetApiV1ConsentsConsentIdAuthorisationsAuthorisationIdOptions = {baseUrl?: string | undefined, BearerAuthOAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const scaStatus = z.enum(["received", "psuIdentified", "psuAuthenticated", "scaMethodSelected", "started", "unconfirmed", "finalised", "failed", "exempted"]); export const psuMessageText = z.string().max(500); export const trustedBeneficiaryFlag = z.boolean(); export const scaStatusResponse = z.object({scaStatus: scaStatus, psuMessage: psuMessageText.optional(), trustedBeneficiaryFlag: trustedBeneficiaryFlag.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 }; /** * Read the SCA status of the consent authorisation * * GET /v1/consents/{consentId}/authorisations/{authorisationId} */ export const getApiV1ConsentsConsentIdAuthorisationsAuthorisationId = async (args: GetApiV1ConsentsConsentIdAuthorisationsAuthorisationIdArgs, options?: GetApiV1ConsentsConsentIdAuthorisationsAuthorisationIdOptions) => { const {baseUrl = "https://api.dev.openbankingproject.ch", fetch: fetchFn = fetch, BearerAuthOAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Request-ID'] != null) headers.set("X-Request-ID", String(args['X-Request-ID'])) if (args.Digest != null) headers.set("Digest", String(args.Digest)) if (args.Signature != null) headers.set("Signature", String(args.Signature)) if (args['TPP-Signature-Certificate'] != null) headers.set("TPP-Signature-Certificate", String(args['TPP-Signature-Certificate'])) if (args['PSU-IP-Address'] != null) headers.set("PSU-IP-Address", String(args['PSU-IP-Address'])) if (args['PSU-IP-Port'] != null) headers.set("PSU-IP-Port", String(args['PSU-IP-Port'])) if (args['PSU-Accept'] != null) headers.set("PSU-Accept", String(args['PSU-Accept'])) if (args['PSU-Accept-Charset'] != null) headers.set("PSU-Accept-Charset", String(args['PSU-Accept-Charset'])) if (args['PSU-Accept-Encoding'] != null) headers.set("PSU-Accept-Encoding", String(args['PSU-Accept-Encoding'])) if (args['PSU-Accept-Language'] != null) headers.set("PSU-Accept-Language", String(args['PSU-Accept-Language'])) if (args['PSU-User-Agent'] != null) headers.set("PSU-User-Agent", String(args['PSU-User-Agent'])) if (args['PSU-Http-Method'] != null) headers.set("PSU-Http-Method", String(args['PSU-Http-Method'])) if (args['PSU-Device-ID'] != null) headers.set("PSU-Device-ID", String(args['PSU-Device-ID'])) if (args['PSU-Geo-Location'] != null) headers.set("PSU-Geo-Location", String(args['PSU-Geo-Location'])) if (BearerAuthOAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuthOAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/consents/${encodeURIComponent(String(args.consentId))}/authorisations/${encodeURIComponent(String(args.authorisationId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(scaStatusResponse, await res.json()) };