Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated withAPIKey to be synchronous #49

Merged
merged 1 commit into from
Nov 5, 2024
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { GeoPlacesClient, GeocodeCommand } from "@aws-sdk/geo-places-client";
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";

// Create an authentication helper instance using an API key and region
const authHelper = await withAPIKey("<API Key>", "<Region>");
const authHelper = withAPIKey("<API Key>", "<Region>");

// Configures the client to use API keys when making supported requests
const client = new GeoPlacesClient(authHelper.getClientConfig());
Expand All @@ -64,7 +64,7 @@ import { GeoRoutesClient, CalculateRoutesCommand } from "@aws-sdk/geo-routes-cli
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";

// Create an authentication helper instance using an API key and region
const authHelper = await withAPIKey("<API Key>", "<Region>");
const authHelper = withAPIKey("<API Key>", "<Region>");

// Configures the client to use API keys when making supported requests
const client = new GeoRoutesClient(authHelper.getClientConfig());
Expand All @@ -87,7 +87,7 @@ import { LocationClient, ListGeofencesCommand } from "@aws-sdk/client-location";
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";

// Create an authentication helper instance using an API key and region
const authHelper = await withAPIKey("<API Key>", "<Region>");
const authHelper = withAPIKey("<API Key>", "<Region>");

// Configures the client to use API keys when making supported requests
const client = new LocationClient(authHelper.getClientConfig());
Expand Down Expand Up @@ -181,7 +181,7 @@ This example uses the Amazon Location Client to make a request that that authent

```javascript
// Create an authentication helper instance using an API key and region
const authHelper = await amazonLocationClient.withAPIKey("<API Key>", "<Region>");
const authHelper = amazonLocationClient.withAPIKey("<API Key>", "<Region>");

// Configures the client to use API keys when making supported requests
const client = new amazonLocationClient.GeoRoutesClient(authHelper.getClientConfig());
Expand Down Expand Up @@ -289,7 +289,7 @@ npm run typedoc
Creates an auth helper instance using API key (and region, optionally).

```javascript
const authHelper = await withAPIKey(apiKey, region);
const authHelper = withAPIKey(apiKey, region);
```

### `withIdentityPoolId`
Expand Down
30 changes: 29 additions & 1 deletion src/apikey/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("AuthHelper for APIKey", () => {
headers: {},
};
expect(
await signer.sign({
await signer?.sign({
...request,
query,
}),
Expand All @@ -30,6 +30,34 @@ describe("AuthHelper for APIKey", () => {
});
});

it.each([
[{}, { key: API_KEY }],
[{ other: "value" }, { other: "value", key: API_KEY }],
[null, { key: API_KEY }],
])(
"getLocationClientConfig should provide a signer to the query when using synchronous withAPIKey",
async (query, expectedQuery) => {
const authHelper = withAPIKey(API_KEY);
const signer = authHelper.getLocationClientConfig().signer;
const request = {
hostname: "amazonaws.com",
path: "/",
protocol: "https",
method: "GET",
headers: {},
};
expect(
await signer?.sign({
...request,
query,
}),
).toStrictEqual({
...request,
query: expectedQuery,
});
},
);

it("APIKey provided by getLocationClientConfig should be overridden by one set in the command", async () => {
const authHelper = await withAPIKey(API_KEY);
const signer = authHelper.getLocationClientConfig().signer;
Expand Down
2 changes: 1 addition & 1 deletion src/apikey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { LocationClientConfig, SDKAuthHelper } from "../common/types";
*
* @param apiKey APIKey
*/
export async function withAPIKey(apiKey: string, region?: string): Promise<SDKAuthHelper> {
export function withAPIKey(apiKey: string, region?: string): SDKAuthHelper {
const clientConfig: LocationClientConfig = {
signer: {
sign: async (requestToSign) => {
Expand Down
Loading