Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { jest } from "@jest/globals";
import type { bootstrap } from "./bootstrap.js";
import type { APIOptions } from "./index.js";

type ImportLibraryMock = jest.Mock<typeof google.maps.importLibrary>;

Expand Down Expand Up @@ -81,6 +82,17 @@ describe("importLibrary(): basic operation", () => {
expect(mockBootstrap).toHaveBeenCalledWith(options);
});

it("should log a warning when apiKey is used and use it as key", async () => {
const { setOptions } = await import("./index.js");
const { logDevWarning, MSG_API_KEY_USED } = await import("./messages.js");

const options = { apiKey: "foo" } as unknown as APIOptions;
setOptions(options);

expect(logDevWarning).toHaveBeenCalledWith(MSG_API_KEY_USED);
expect(options.key).toBe("foo");
});

it("should return the value from importLibrary", async () => {
const { setOptions, importLibrary } = await import("./index.js");

Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
MSG_SCRIPT_ELEMENT_EXISTS,
MSG_REPEATED_SET_OPTIONS,
MSG_SET_OPTIONS_NOT_CALLED,
MSG_API_KEY_USED,
} from "./messages.js";

export type APIOptions = {
Expand Down Expand Up @@ -82,6 +83,14 @@ export function setOptions(options: APIOptions) {
return;
}

if ((options as Record<string, unknown>).apiKey) {
logDevWarning(MSG_API_KEY_USED);

if (!options.key) {
options.key = (options as Record<string, unknown>).apiKey as string;
}
}

installImportLibrary_(options);
setOptionsWasCalled_ = true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export const MSG_SCRIPT_ELEMENT_EXISTS =
"problems using the API. Make sure to remove the script " +
"loading the API.";

export const MSG_API_KEY_USED =
"The 'apiKey' parameter was used in setOptions(), but 'key' is the correct " +
"parameter name. Please update your configuration.";

// Development mode check - bundlers will replace process.env.NODE_ENV at build time
declare const process: { env: { NODE_ENV?: string } };
const __DEV__ = process.env.NODE_ENV !== 'production';
Expand Down
Loading