import {z} from 'zod' export type RefRecProgram = string; export type RefCountry = string; export type RefFuelType = string; export type RefStateProvince = string; export type CreateApiGeneratorsActionSearchArgs = {page?: number | undefined, size?: number | undefined, include?: string | undefined, body: {upn?: string | undefined, name?: string | undefined, program?: RefRecProgram | undefined, registryAssignedId?: string | undefined, registryAssignedIds?: Array | undefined, hostCountry?: RefCountry | undefined, fuelType?: RefFuelType | undefined, state?: RefStateProvince | undefined}}; export type CreateApiGeneratorsActionSearchOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const refRecProgram = z.string(); export const refGeneratorStatus = z.string(); export const refStateProvince = z.string(); export const refCountry = z.string(); export const refFuelType = z.string(); export const fuelType = z.object({name: z.string().optional(), type: refFuelType.optional(), fuelSource: z.string().optional()}); export const refStateEligibilityType = z.string(); export const stateEligibility = z.object({name: z.string().optional(), type: refStateEligibilityType.optional(), stateIdentifier: z.string().optional()}); export const refVoluntaryEligibilityType = z.string(); export const voluntaryEligibility = z.object({name: z.string().optional(), type: refVoluntaryEligibilityType.optional(), effectiveDate: z.string().optional(), expirationDate: z.string().optional()}); export const generator = z.object({id: z.number().int().optional(), upn: z.string().optional(), name: z.string(), program: refRecProgram.optional(), registryAssignedId: z.string().optional(), status: refGeneratorStatus.optional(), siteCode: z.string().optional(), unitName: z.string().optional(), controlArea: z.string().optional(), parentCompanyName: z.string().optional(), powerCompanyName: z.string().optional(), powerCompanyCode: z.string().optional(), ownerName: z.string().optional(), ownershipType: z.string().optional(), countyName: z.string().optional(), stateProvince: refStateProvince.optional(), country: refCountry.optional(), contactInfo: z.string().optional(), registryAcctHolderName: z.string().optional(), commencedDt: z.string().optional(), nameplateCapacity: z.number().optional(), facNoncompetitiveCertData: z.string().optional(), fuelTypes: z.array(z.object({fuelType: fuelType, stateEligibilities: z.array(stateEligibility).optional(), voluntaryEligibilities: z.array(voluntaryEligibility).optional()})).optional()}); export const createApiGeneratorsActionSearchResponse = z.object({page: z.number().int(), size: z.number().int(), totalPages: z.number().int(), totalElements: z.number().int(), content: z.array(generator).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 }; /** * Generators search * * POST /generators/action/search */ export const createApiGeneratorsActionSearch = async (args: CreateApiGeneratorsActionSearchArgs, options?: CreateApiGeneratorsActionSearchOptions) => { const {baseUrl = "https://sandbox.preprod.connect.xpansiv.com/app/api/v1", fetch: fetchFn = fetch, bearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.include != null) query.append("include", String(args.include)) if (bearerToken !== undefined) headers.set('authorization', `Bearer ${bearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/generators/action/search${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(createApiGeneratorsActionSearchResponse, await res.json()) };