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
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
testEnvironment: "node",
// some dependencies are esm and need to be compiled to work in jest
transformIgnorePatterns: [
"/node_modules/(?!(axios|retry-axios|query-string|decode-uri-component|split-on-first|filter-obj)/)",
"/node_modules/(?!(axios|retry-axios|decode-uri-component|split-on-first|filter-obj)/)",
],
collectCoverageFrom: ["src/**/([a-zA-Z_]*).{js,ts}", "!**/*.test.{js,ts}"],
};
50 changes: 0 additions & 50 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@googlemaps/url-signature": "^1.0.4",
"agentkeepalive": "^4.1.0",
"axios": "^1.5.1",
"query-string": "<8.x",
"retry-axios": "<4"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/serialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ test("createPremiumPlanQueryString", () => {
const queryStringOptions = {
arrayFormat: "separator",
arrayFormatSeparator: "|",
};
} as const;
const baseUrl = "https://test.url/maps/api/directions/json";

expect(
Expand Down
38 changes: 32 additions & 6 deletions src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,40 @@

import { LatLng, LatLngBounds, LatLngLiteral } from "./common";

import { encodePath } from "./util";
import { createSignature } from "@googlemaps/url-signature";
import queryString from "query-string";

const qs = queryString.stringify;
import { encodePath } from "./util";

const separator = "|";

type QueryStringOptions =
| { arrayFormat: "comma" }
| {
arrayFormat: "separator";
arrayFormatSeparator: string;
};
const qs = (params: Record<string, any>, options: QueryStringOptions) => {
const separator =
options.arrayFormat === "comma" ? "," : options.arrayFormatSeparator;

const encode = (value: any) =>
value === null ? "" : encodeURIComponent(value);

return Object.entries(params)
.filter(([, v]) => v !== undefined)
.map(([k, v]) => {
const key = encodeURIComponent(k);
const value = Array.isArray(v)
? v
.filter((v) => v !== undefined)
.map(encode)
.join(separator)
: encode(v);
return value === "" ? key : `${key}=${value}`;
})
.sort()
.join("&");
};

export function latLngToString(o: LatLng) {
if (typeof o === "string") {
return o;
Expand Down Expand Up @@ -113,7 +139,7 @@ export type serializerFormat = { [key: string]: serializerFunction };
export function serializer(
format: serializerFormat,
baseUrl: string,
queryStringOptions: object = {
queryStringOptions: QueryStringOptions = {
arrayFormat: "separator",
arrayFormatSeparator: separator,
}
Expand Down Expand Up @@ -157,7 +183,7 @@ export function toTimestamp(o: "now" | number | Date): number | "now" {

export function createPremiumPlanQueryString(
serializedParams: { [key: string]: string },
queryStringOptions: object,
queryStringOptions: QueryStringOptions,
baseUrl: string
): string {
serializedParams.client = serializedParams.client_id;
Expand Down
Loading