import {z} from 'zod' export type CreateApiV2AuthenticationRegistrationIdArgs = {'x-tenant-id': string, registrationId: string, 'x-challengecode': string}; export type CreateApiV2AuthenticationRegistrationIdOptions = {baseUrl?: string | undefined, oauth2AuthorizationCode?: string | undefined, fetch?: typeof fetch | undefined}; export const jTLWawiRestContractsModelsV1AuthenticationRegistrationStatus = z.union([z.literal(0), z.literal(1), z.literal(2)]); export const jTLWawiRestContractsModelsV1AuthenticationRegistrationRequestStatusInfo = z.object({appId: z.string().optional(), registrationRequestId: z.string().optional(), status: jTLWawiRestContractsModelsV1AuthenticationRegistrationStatus.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 }; /** * Register Multi Instance App * * POST /v2/authentication/{registrationId} * * Also accepts auth: oauth2-client-credentials */ export const createApiV2AuthenticationRegistrationId = async (args: CreateApiV2AuthenticationRegistrationIdArgs, options?: CreateApiV2AuthenticationRegistrationIdOptions) => { const {baseUrl = "https://scx-sbx.api.jtl-software.com", fetch: fetchFn = fetch, oauth2AuthorizationCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-tenant-id'] != null) headers.set("x-tenant-id", String(args['x-tenant-id'])) if (args['x-challengecode'] != null) headers.set("x-challengecode", String(args['x-challengecode'])) if (oauth2AuthorizationCode !== undefined) headers.set('authorization', `Bearer ${oauth2AuthorizationCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/authentication/${encodeURIComponent(String(args.registrationId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(jTLWawiRestContractsModelsV1AuthenticationRegistrationRequestStatusInfo, await res.json()) };