import {z} from 'zod' export type GetApiV1EntitiesNameArgs = {name: string, keys?: Array | undefined, values?: Array | undefined, schemaOnly?: boolean | undefined, includeInactive?: boolean | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiV1EntitiesNameOptions = {baseUrl?: string | undefined, oAuthKeycloak?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1EntitiesNameResponse = z.object({status: z.string().optional(), code: z.number().int().optional(), entityName: z.string().optional(), entityLabel: z.string().optional(), entitySchema: z.object({}).optional(), offset: z.number().int().optional(), limit: z.number().int().optional(), data: z.array(z.unknown()).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 }; /** * Gets data items for an entity (supports pagination and filters/queries) * * GET /v1/entities/{name} */ export const getApiV1EntitiesName = async (args: GetApiV1EntitiesNameArgs, options?: GetApiV1EntitiesNameOptions) => { const {baseUrl = "https://virtserver.swaggerhub.com/Viable-Data/Reference-Data-Service-API/0.0.1", fetch: fetchFn = fetch, oAuthKeycloak} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.keys != null) query.append("keys", args.keys.join(',')) if (args.values != null) query.append("values", args.values.join(',')) if (args.schemaOnly != null) query.append("schemaOnly", String(args.schemaOnly)) if (args.includeInactive != null) query.append("includeInactive", String(args.includeInactive)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (oAuthKeycloak !== undefined) headers.set('authorization', `Bearer ${oAuthKeycloak}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/entities/${encodeURIComponent(String(args.name))}${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(getApiV1EntitiesNameResponse, await res.json()) };