Skip to content

Commit

Permalink
fix: serialize date to seconds and not milliseconds (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt authored Mar 26, 2020
1 parent 07dc482 commit 4d10f6c
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 34 deletions.
6 changes: 3 additions & 3 deletions e2e/timezone.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { timezone } from "../src/timezone";

test("elevation should call axios correctly", async () => {
test("elevation should get an ok response", async () => {
const params = {
location: { lat: 35, lng: -110 },
timestamp: 0,
location: "30, 50",
timestamp: new Date(),
language: "en" as const,
key: process.env.GOOGLE_MAPS_API_KEY
};
Expand Down
23 changes: 12 additions & 11 deletions src/directions.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import {
DirectionsRoute,
GeocodedWaypoint,
TrafficModel,
TransitRoutingPreference,
TravelRestriction,
LatLng,
Language,
UnitSystem,
LatLng,
RequestParams,
ResponseData,
TrafficModel,
TransitMode,
TransitRoutingPreference,
TravelMode,
ResponseData,
RequestParams
TravelRestriction,
UnitSystem
} from "./common";
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import { latLngToString, serializer, toTimestamp } from "./serialize";

import { defaultAxiosInstance } from "./client";
import { serializer, latLngToString } from "./serialize";

export interface DirectionsRequest extends Partial<AxiosRequestConfig> {
params: {
Expand Down Expand Up @@ -176,8 +177,8 @@ export const defaultParamsSerializer = serializer({
origin: latLngToString,
destination: latLngToString,
waypoints: o => o.map(latLngToString),
arrival_time: Number,
departure_time: Number
arrival_time: toTimestamp,
departure_time: toTimestamp
});

export function directions(
Expand Down
21 changes: 11 additions & 10 deletions src/distance.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import {
DistanceMatrixRow,
TrafficModel,
TransitRoutingPreference,
TravelRestriction,
LatLng,
UnitSystem,
RequestParams,
ResponseData,
TrafficModel,
TransitMode,
TransitRoutingPreference,
TravelMode,
ResponseData,
RequestParams
TravelRestriction,
UnitSystem
} from "./common";
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import { latLngToString, serializer, toTimestamp } from "./serialize";

import { defaultAxiosInstance } from "./client";
import { serializer, latLngToString } from "./serialize";

export interface DistanceMatrixRequest extends Partial<AxiosRequestConfig> {
params: {
Expand Down Expand Up @@ -155,8 +156,8 @@ export const defaultUrl =
export const defaultParamsSerializer = serializer({
origins: o => o.map(latLngToString),
destinations: o => o.map(latLngToString),
arrival_time: Number,
departure_time: Number
arrival_time: toTimestamp,
departure_time: toTimestamp
});

export function distancematrix(
Expand Down
21 changes: 16 additions & 5 deletions src/serialize.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { LatLng, LatLngLiteral } from "./common";
import {
latLngArrayToStringMaybeEncoded,
latLngBoundsToString,
latLngToString,
serializer,
objectToString,
latLngBoundsToString,
latLngArrayToStringMaybeEncoded,
toLatLngLiteral
serializer,
toLatLngLiteral,
toTimestamp
} from "./serialize";
import { LatLngLiteral, LatLng } from "./common";

test("latLngToString is correct", () => {
expect(latLngToString("")).toBe("");
Expand Down Expand Up @@ -78,3 +79,13 @@ test("toLatLngLiteral", () => {
toLatLngLiteral({} as LatLngLiteral);
}).toThrow(TypeError);
});

test("toTimestamp", () => {
expect(toTimestamp(100)).toEqual(100);

const dt = new Date();
const seconds = Number(dt) / 1000
expect(toTimestamp(dt)).toEqual(seconds);

});

10 changes: 9 additions & 1 deletion src/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LatLng, LatLngBounds, LatLngLiteral } from "./common";
import { stringify as qs } from "query-string";

import { encodePath } from "./util";
import { stringify as qs } from "query-string";

const separator = "|";

Expand Down Expand Up @@ -96,3 +97,10 @@ export function serializer(
return qs(params, queryStringOptions);
};
}

export function toTimestamp(o: number | Date) {
if (o instanceof Date) {
return Number(o) / 1000;
}
return o
}
8 changes: 7 additions & 1 deletion src/timezone.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defaultParamsSerializer, defaultUrl, timezone } from "./timezone";

import axios from "axios";
import { timezone, defaultParamsSerializer, defaultUrl } from "./timezone";

jest.mock("axios");

Expand All @@ -25,3 +26,8 @@ test("elevation should call axios correctly", () => {
url: defaultUrl
});
});

test("serializer should handle date object", () => {
const dt = new Date();
expect(defaultParamsSerializer({timestamp: dt})).toEqual(`timestamp=${Number(dt)/1000}`)
});
7 changes: 4 additions & 3 deletions src/timezone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { LatLng, Language, ResponseData, RequestParams } from "./common";
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import { Language, LatLng, RequestParams, ResponseData } from "./common";
import { latLngToString, serializer, toTimestamp } from "./serialize";

import { defaultAxiosInstance } from "./client";
import { serializer, latLngToString } from "./serialize";

export interface TimeZoneRequest extends Partial<AxiosRequestConfig> {
params: {
Expand Down Expand Up @@ -55,7 +56,7 @@ export interface TimeZoneResponse extends AxiosResponse {

export const defaultUrl = "https://maps.googleapis.com/maps/api/timezone/json";
export const defaultParamsSerializer = serializer({
timestamp: Number,
timestamp: toTimestamp,
location: latLngToString
});
export function timezone(
Expand Down

0 comments on commit 4d10f6c

Please sign in to comment.