import {z} from 'zod' export type Authenticate = {username?: string | undefined, password?: string | undefined}; export type CreateApiSessionArgs = {body: Authenticate}; export type CreateApiSessionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const functionalityBean = z.object({id: z.string().optional(), description: z.string().optional(), application: z.lazy(() => applicationBean.optional())}); export const applicationBean: z.ZodType = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), roles: z.array(z.lazy(() => roleBean)).optional(), functionalities: z.array(functionalityBean).optional(), users: z.array(z.lazy(() => userBean)).optional()}); export const roleBean: z.ZodType = z.object({id: z.string().optional(), name: z.string().optional(), application: applicationBean.optional(), functionalities: z.array(functionalityBean).optional(), users: z.array(z.lazy(() => userBean)).optional()}); export const userBean: z.ZodType = z.object({username: z.string().optional(), name: z.string().optional(), canOpenCase: z.boolean().optional(), canEditCase: z.boolean().optional(), canViewCase: z.boolean().optional(), password: z.string().optional(), roles: z.array(roleBean).optional()}); export const authenticationResponse = z.object({status: z.enum(["OK", "ERROR", "BAD_REQUEST", "CREATED", "NO_CONTENT", "DELETED", "CANCELLED", "CLOSED", "REMOVED", "NOT_FOUND", "AUTHENTICATION_FAILED", "AUTHENTICATION_OK", "INVALID_SESSION_GUID", "UNAUTHORIZED", "FORBIDDEN", "RECEIVED", "ACCEPTED", "ENQUEUED", "INVALID_PARAMETERS", "FIELD_VALIDATION_ERROR", "ILLEGAL_STATE", "NOT_ENOUGH_DATA", "VALIDATION_ERROR", "IMPORTED", "UPDATED", "IGNORED", "SERVICE_STARTED", "SERVICE_CANCELLED", "SERVICE_SUBMITTED"]).optional(), code: z.number().int().optional(), sessionGuid: z.string().optional(), user: userBean.optional(), token: z.string().optional(), expirationTime: z.number().int().optional(), permissions: z.array(z.string()).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 }; /** * loginRequest * * POST /session */ export const createApiSession = async (args: CreateApiSessionArgs, options?: CreateApiSessionOptions) => { const {baseUrl = "http://:8085/gbds/v2", 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(/\/+$/, '')}/session${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(authenticationResponse, await res.json()) };