import {z} from 'zod' export type InputCharSet = 'numeric' | 'text'; export type TxCode = {input_mode?: InputCharSet | undefined, length?: number | undefined, description?: string | undefined}; export type GrantUrnIetf = {'pre-authorized_code'?: string | undefined, authorization_server?: string | undefined, tx_code?: TxCode | undefined}; export type GrantAuthorizationCode = {issuer_state?: string | undefined, authorization_server?: string | undefined}; export type CredentialOfferRequest = {credential_issuer?: string | undefined, credential_configuration_ids: Array, grants?: ({'urn:ietf:params:oauth:grant-type:pre-authorized_code'?: GrantUrnIetf | undefined, authorization_code?: GrantAuthorizationCode | undefined}) | undefined, client_id?: string | undefined}; export type CreateApiWebappCredentialOffersArgs = {body: CredentialOfferRequest}; export type CreateApiWebappCredentialOffersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiWebappCredentialOffersResponse = z.object({uri: z.string().optional(), userPin: z.string().optional(), txCode: z.object({}).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 }; /** * Create credential offer * * POST /webapp/credential-offers */ export const createApiWebappCredentialOffers = async (args: CreateApiWebappCredentialOffersArgs, options?: CreateApiWebappCredentialOffersOptions) => { const {baseUrl = "https://agent.findynet.demo.sphereon.com/oid4vci", 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(/\/+$/, '')}/webapp/credential-offers${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(createApiWebappCredentialOffersResponse, await res.json()) };