import {z} from 'zod' export type DT013EnvironmentTag = 'PRD' | 'SITB' | 'SIT' | 'UIT' | 'DEV'; export type DT020NSubText = string | null; export type DT995SenderUniqueReference = string; export type DT003DateTime = string; export type DT006DIPParticipantID = string; export type DT009DIPParticipantRole = string; export type DT011NCorrelationID = string | null; export type DT006NDIPParticipantID = string | null; export type S1 = {environmentTag: DT013EnvironmentTag, subText: DT020NSubText, senderUniqueReference: DT995SenderUniqueReference, senderTimestamp: DT003DateTime, senderDIPID: DT006DIPParticipantID, senderRoleID: DT009DIPParticipantRole, senderCorrelationID: DT011NCorrelationID, DIPConnectionProviderID: DT006NDIPParticipantID}; export type A0 = {primaryRecipients: Array, secondaryRecipients: Array, always: Array}; export type DT014DIPTransactionID = string; export type DI986PublicationID = string; export type DI988ReplayIndicator = boolean; export type DT018NURL = string | null; export type D0 = {transactionID: DT014DIPTransactionID, transactionTimestamp: DT003DateTime, publicationID: DI986PublicationID, initialCorrelationID: DT011NCorrelationID, replayIndicator: DI988ReplayIndicator, serviceTicketURL: DT018NURL}; export type DT002MPAN = string; export type DI037GSPGroupID = '_A' | '_B' | '_C' | '_D' | '_E' | '_F' | '_G' | '_H' | '_J' | '_K' | '_L' | '_M' | '_N' | '_P'; export type M0 = {MPANCore: DT002MPAN, distributorDIPID: DT006DIPParticipantID, GSPGroupID: DI037GSPGroupID}; export type DT029MeterID = string; export type DT016Decimal93 = string; export type DI094ReadingMethod = 'A' | 'C' | 'E' | 'S'; export type DI096SiteVisitCheckCode = '01' | '02' | '03' | '04' | '05' | '06' | '07' | '08' | '09' | '10' | '11' | '13' | '14' | '15' | '16' | '17' | '18' | '19' | '20' | '21' | '22' | '23' | '24' | '25' | '26' | '27' | '28' | '29' | '30' | '31' | '32' | '33' | '34' | '35' | '36' | '37' | '38' | '39' | '40' | '41' | '42' | '43' | '44' | '50' | '51' | '52' | '53' | '54' | '55' | '56' | '57' | '58' | '88' | '89' | null; export type B064 = {meterID: DT029MeterID, cumulativeRegisterReading: DT016Decimal93, cumulativeRegisterReadingDateTime: DT003DateTime, readingMethod: DI094ReadingMethod, siteVisitCheckCode: DI096SiteVisitCheckCode}; export type DI103ConfigurableRegisterID = string; export type B065 = {meterID: DT029MeterID, configurableRegisterReadingDate: DT003DateTime, configurableRegisterID: DI103ConfigurableRegisterID, configurableRegisterReading: DT016Decimal93}; export type IF041Payload = {CommonBlock: {S0: {interfaceID: 'IF-041', schemaVersion: '011', eventCode: '[ReadingRemote]' | '[ReadingRemv]' | '[ReadingInstl]' | '[ReadingEnergisationChg]' | '[ReadingCOR]' | '[ReadingOnSite]' | '[ReadingCOS]' | '[DSEstimate]' | '[SupplierAgreedCOS]' | '[ReadingOverride]'}, S1: S1, A0?: A0 | undefined, D0?: D0 | undefined, M0: M0}, CustomBlock: {B064List?: Array | undefined, B065List?: Array | undefined}}; export type CreateApiValidateIF041SupplierAgreedCOSArgs = {body: Array<{payload?: IF041Payload | undefined}>}; export type CreateApiValidateIF041SupplierAgreedCOSOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiValidateIF041SupplierAgreedCOSResponse = z.string(); 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 }; /** * Submit IF-041 with Event Code SupplierAgreedCOS message for validation * * POST /validate/IF-041/SupplierAgreedCOS */ export const createApiValidateIF041SupplierAgreedCOS = async (args: CreateApiValidateIF041SupplierAgreedCOSArgs, options?: CreateApiValidateIF041SupplierAgreedCOSOptions) => { const {baseUrl = "https://dip-api-documentation.elexon.co.uk", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/validate/IF-041/SupplierAgreedCOS${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(createApiValidateIF041SupplierAgreedCOSResponse, await res.json()) };