Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/coercion/parseDate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from "@jest/globals";
import { InvalidArgumentError } from "commander";
import parseDate from "./parseDate";

describe("Date argument parser", () => {
it("should coerse ISO 8601 strings to objects", () => {
const isoDateFrom = "2023-06-29T12:34:56Z";
const expected = new Date(Date.UTC(2023, 5, 29, 12, 34, 56, 0));

const parsedDate = parseDate(isoDateFrom)

expect(parsedDate).toEqual(expected);
});

it("should raise an error for invalid values", () => {
const malformedDateFrom = "2023-06-29T123456Z";
expect(() => parseDate(malformedDateFrom)).toThrow(InvalidArgumentError);
});
});
16 changes: 16 additions & 0 deletions src/coercion/parseDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { InvalidArgumentError } from "commander";

/**
* @param dateString an ISO 8601 formatted datetime argument
* @returns The argument coerced into a `Date` object
* @throws {InvalidArgumentError} must be a valid ISO 8601 formatted `string`
*/
export default (dateString: string): Date => {
const parsedDate = new Date(dateString);

if (isNaN(parsedDate.getTime())) {
throw new InvalidArgumentError('Invalid datetime format');
}

return parsedDate;
};
4 changes: 2 additions & 2 deletions src/commands/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "../privacy/filter";
import { loadSession } from "../login-session";

export const get = async (moduleName: string, privacyPass?: string) => {
export const get = async (moduleName: string, dateFrom?: Date, privacyPass?: string) => {
const module = getModuleByName(moduleName);

if (module == null) {
Expand Down Expand Up @@ -48,7 +48,7 @@ export const get = async (moduleName: string, privacyPass?: string) => {
);
}

const ocpiObjectStream = fetchDataForModule(session, module);
const ocpiObjectStream = fetchDataForModule(session, module, dateFrom);

const privacyFilteringStream = new Transform({
objectMode: true,
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { stderr } from "node:process";
import { Command } from "commander";
import { INPUT_PARTY_ID_REGEX, login } from "./commands/login";
import { get } from "./commands/get";
import parseDate from "./coercion/parseDate";

// send all console messages to stderr so people can pipe OCPI objects over
// stdout and see the progress messages written to stdout at the same time
Expand Down Expand Up @@ -31,12 +32,17 @@ program
program
.command("get <module>")
.description("Fetch a page of data of a certain OCPI module")
.option<Date>(
"--date-from <ISO 8601 datetime>",
"Limits objects returned to those that have last_updated after or equal to this value",
parseDate
)
.option(
"--privacy-pass <field list>",
"List of fields to exclude from privacy filtering"
)
.action((moduleName: string, options) =>
get(moduleName, options["privacyPass"])
get(moduleName, options["dateFrom"], options["privacyPass"])
);

program.parse();
14 changes: 11 additions & 3 deletions src/ocpi-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function getModuleByName(moduleName: string): OcpiModule<any> | null {
type OcpiPageParameters = {
offset: number;
limit: number;
date_from?: string;
};

export type OcpiResponse<T> = {
Expand Down Expand Up @@ -209,6 +210,7 @@ const ocpiRequestWithLiteralAuthHeaderTokenValue: <T>(
: {
offset: linkToNextPage?.offset,
limit: linkToNextPage?.limit,
date_from: linkToNextPage?.date_from
};

const ocpiResponse = { ...resp.data, nextPage } as OcpiResponse<T>;
Expand Down Expand Up @@ -246,7 +248,8 @@ export type NoSuchEndpoint = "no such endpoint";
*/
export function fetchDataForModule<N extends ModuleID>(
session: LoginSession,
module: OcpiModule<N>
module: OcpiModule<N>,
dateFrom?: Date
): Readable {
let nextPage: OcpiPageParameters | "done" | "notstarted" = "notstarted";

Expand All @@ -264,7 +267,7 @@ export function fetchDataForModule<N extends ModuleID>(

let nextPageData;
try {
const firstPageParameters = { offset: 0, limit: size };
const firstPageParameters = { offset: 0, limit: size, date_from: dateFrom?.toISOString() };
nextPageData = await pullPageOfData(
session,
module,
Expand Down Expand Up @@ -316,10 +319,15 @@ async function pullPageOfData<N extends ModuleID>(
);

if (moduleUrl) {
let queryUrl = `${moduleUrl.url}?offset=${page.offset}&limit=${page.limit}`;
if (page.date_from) {
queryUrl += `&date_from=${page.date_from}`
}

return ocpiRequest(
session,
"get",
`${moduleUrl.url}?offset=${page.offset}&limit=${page.limit}`,
queryUrl,
fromPartyId
);
} else return "no such endpoint";
Expand Down