Skip to content

Commit

Permalink
Various type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Jan 10, 2023
1 parent 802cc07 commit e0910b1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 86 deletions.
32 changes: 6 additions & 26 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/generation-forecast.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Query } from "https://deno.land/x/[email protected].7/mod.ts";
import { Query } from "https://deno.land/x/[email protected].9/mod.ts";

// Prepare dates
const
Expand Down
10 changes: 5 additions & 5 deletions examples/generation-per-unit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Query } from "https://deno.land/x/[email protected].7/mod.ts";
import { PsrType } from "https://deno.land/x/[email protected].7/src/parameters/psrtype.js";
import { Query } from "https://deno.land/x/[email protected].9/mod.ts";
import { PsrType } from "https://deno.land/x/[email protected].9/src/parameters/psrtype.js";

// Prepare dates
const
Expand All @@ -17,8 +17,8 @@ const result = await Query(
{
documentType: "A75", // A75 - Actual generation per type
processType: "A16", // A16 - Realised
inDomain: "BZN|DE-LU", // In_Domain
outDomain: "BZN|DE-LU", // Out_Domain
inDomain: "BZN|SE2", // In_Domain
outDomain: "BZN|SE2", // Out_Domain
startDateTime: dateToday, // Start date
endDateTime: dateTomorrow, // End date
}
Expand All @@ -35,7 +35,7 @@ const output = {
};

for (const ts of result.TimeSeries) {
// We expect hourly data, use PT60M and ignore other periods
// We expect hourly data from BZN|SE2, use PT60M and ignore other periods
if (ts.Period.resolution==="PT60M") {
for (const point of ts.Period.Point) {
const psrTypeTranslation = PsrType[ts.MktPSRType.psrType];
Expand Down
2 changes: 1 addition & 1 deletion examples/spot-prices-today.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Query } from "https://deno.land/x/[email protected].7/mod.ts";
import { Query } from "https://deno.land/x/[email protected].9/mod.ts";

// Prepare dates
const
Expand Down
14 changes: 7 additions & 7 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const Query = async (securityToken: string, params: QueryParameters): Promise<Qu
// Validate inDomain, add to parameter list
if (params.inDomain) {
const foundInDomain = Object.entries(Areas).find(([_key, value]) =>
value.includes(params.inDomain)
params.inDomain && value.includes(params.inDomain)
);
if (!foundInDomain) {
throw new Error("inDomain not valid");
Expand All @@ -91,7 +91,7 @@ const Query = async (securityToken: string, params: QueryParameters): Promise<Qu
// Validate inBiddingZoneDomain, add to parameter list
if (params.inBiddingZoneDomain) {
const foundInDomain = Object.entries(Areas).find(([_key, value]) =>
value.includes(params.inBiddingZoneDomain)
params.inBiddingZoneDomain && value.includes(params.inBiddingZoneDomain)
);
if (!foundInDomain) {
throw new Error("inBiddingZoneDomain not valid");
Expand All @@ -103,7 +103,7 @@ const Query = async (securityToken: string, params: QueryParameters): Promise<Qu
// Validate outDomain, add to parameter list
if (params.outDomain) {
const foundOutDomain = Object.entries(Areas).find(([_key, value]) =>
value.includes(params.outDomain)
params.outDomain && value.includes(params.outDomain)
);
if (!foundOutDomain) {
throw new Error("outDomain not valid");
Expand All @@ -114,8 +114,8 @@ const Query = async (securityToken: string, params: QueryParameters): Promise<Qu

// Validate outDomain, add to parameter list
if (params.outBiddingZoneDomain) {
const foundOutDomain = Object.entries(Areas).find(([_key, value]) =>
value.includes(params.outBiddingZoneDomain)
const foundOutDomain = Object.entries(Areas).find(([_key, value]) =>
params.outBiddingZoneDomain && value.includes(params.outBiddingZoneDomain)
);
if (!foundOutDomain) {
throw new Error("outBiddingZoneDomain not valid");
Expand All @@ -125,10 +125,10 @@ const Query = async (securityToken: string, params: QueryParameters): Promise<Qu
}

// Validate startDateTime, endDateTime, custruct timeInterval
if (!(params.startDateTime instanceof Date && !isNaN(params.startDateTime))) {
if (!(params.startDateTime instanceof Date && !isNaN(params.startDateTime.getTime()))) {
throw new Error("startDateTime not valid, should be Date object");
}
if (!(params.endDateTime instanceof Date && !isNaN(params.endDateTime))) {
if (!(params.endDateTime instanceof Date && !isNaN(params.endDateTime.getTime()))) {
throw new Error("endDateTime not valid, should be Date object");
}
const timeInterval = `${params.startDateTime.toISOString()}/${params.endDateTime.toISOString()}`;
Expand Down
110 changes: 64 additions & 46 deletions src/parsedocument.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,79 @@
import { parse } from "https://deno.land/x/xml/mod.ts";

import { parse } from "https://deno.land/x/[email protected]/mod.ts";

interface EntsoeQueryTimeInterval {
start: string;
}

interface EntsoeQueryPoint {
position: string;
"price.amount": string;
}

interface EntsoeQueryPeriod {
timeInterval: EntsoeQueryTimeInterval;
Point: EntsoeQueryPoint[];
}

interface EntsoeQueryEntry {
Period: EntsoeQueryPeriod;
}

interface QueryResult {
TimeSeries: EntsoeQueryEntry[]
}
start: string;
}

interface UnsafeQueryResult {
TimeSeries: EntsoeQueryEntry | EntsoeQueryEntry[]
}
interface EntsoeQueryPoint {
position: number;
"price.amount"?: number;
quantity?: number;
}

const ParseDocument = (xmlDocument: string): QueryResult => {
interface EntsoeQueryPeriod {
timeInterval: EntsoeQueryTimeInterval;
Point: EntsoeQueryPoint[];
resolution: string;
}

// Parse document
const doc = parse(xmlDocument);
interface EntsoeQueryPeriodMktPSRType {
psrType: string;
}
interface EntsoeQueryEntry {
Period: EntsoeQueryPeriod;
"outBiddingZone_Domain.mRID"?: string;
"inBiddingZone_Domain.mRID"?: string;
MktPSRType?: EntsoeQueryPeriodMktPSRType;
}

const rootNode: UnsafeQueryResult = (doc.Publication_MarketDocument || doc.GL_MarketDocument) as unknown as UnsafeQueryResult;
interface QueryResult {
TimeSeries: EntsoeQueryEntry[];
}

// Check that root element exists
if (!rootNode) {
if(doc.Acknowledgement_MarketDocument) {
throw new Error(`Request failed. Code '${doc.Acknowledgement_MarketDocument.Reason.code}', Reason '${doc.Acknowledgement_MarketDocument.Reason.text}'`)
} else {
console.log(doc);
throw new Error("Unknown XML document structure received");
}
}
interface UnsafeQueryResult {
TimeSeries: EntsoeQueryEntry | EntsoeQueryEntry[];
}

interface InvalidQueryResultReason {
code: string;
text: string;
}

interface InvalidQueryResult {
Reason: InvalidQueryResultReason;
}

const ParseDocument = (xmlDocument: string): QueryResult => {
// Parse document
const doc = parse(xmlDocument);

const rootNode: UnsafeQueryResult =
(doc.Publication_MarketDocument || doc.GL_MarketDocument) as unknown as UnsafeQueryResult;

// Check if TimeSeries is a single element or Array
// - If single element, convert to array with one element
if (rootNode.TimeSeries && !Array.isArray(rootNode.TimeSeries)) {
rootNode.TimeSeries = [rootNode.TimeSeries];
// Check that root element exists
if (!rootNode) {
if (doc.Acknowledgement_MarketDocument) {
const invalidRootNode: InvalidQueryResult = doc.Acknowledgement_MarketDocument as unknown as InvalidQueryResult;
throw new Error(
`Request failed. Code '${invalidRootNode.Reason.code}', Reason '${invalidRootNode.Reason.text}'`,
);
} else {
console.log(doc);
throw new Error("Unknown XML document structure received");
}
}

const checkedDocument: QueryResult = rootNode as unknown as QueryResult;
// Check if TimeSeries is a single element or Array
// - If single element, convert to array with one element
if (rootNode.TimeSeries && !Array.isArray(rootNode.TimeSeries)) {
rootNode.TimeSeries = [rootNode.TimeSeries];
}

// Return MarketDocument
return checkedDocument;
const checkedDocument: QueryResult = rootNode as unknown as QueryResult;

// Return MarketDocument
return checkedDocument;
};

export type { QueryResult };
export { ParseDocument };
export { ParseDocument };

0 comments on commit e0910b1

Please sign in to comment.