Skip to content

Commit 8876494

Browse files
authored
Merge pull request #176 from lifeomic/reduce-failures-when-on-commonjs
Avoid ESM failures when AWS V4 signing is unused
2 parents d58fda4 + 913e833 commit 8876494

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/interceptors/aws-v4-signature.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import {
66
HttpRequest,
77
HeaderBag,
88
} from '@aws-sdk/types';
9-
import buildURL from 'axios/unsafe/helpers/buildURL.js';
10-
import transformData from 'axios/unsafe/core/transformData.js';
11-
import buildFullPath from 'axios/unsafe/core/buildFullPath.js';
129

1310
import type { Alpha } from '../alpha';
1411
import type { AlphaInterceptor, AlphaOptions } from '../types';
@@ -35,7 +32,11 @@ const unsignableHeaders = new Set([
3532
'range',
3633
]);
3734

38-
const combineParams = (url: string, { params, paramsSerializer }: AlphaOptions): HttpRequest['query'] => {
35+
const combineParams = async (url: string, { params, paramsSerializer }: AlphaOptions): Promise<HttpRequest['query']> => {
36+
// buildURL is a ESM module, so we need to import it dynamically since
37+
// we are in a CommonJS environment
38+
const { default: buildURL } = await import('axios/unsafe/helpers/buildURL.js');
39+
3940
const fullUrl: string = buildURL(url, params, paramsSerializer);
4041
const { query } = parseUrl(fullUrl);
4142
return query;
@@ -54,6 +55,12 @@ const awsV4Signature: AlphaInterceptor = async (config) => {
5455
return config;
5556
}
5657

58+
// transformData and buildFullPath are ESM modules, so we need to import it
59+
// dynamically since we are in a CommonJS environment
60+
const { default: transformData } = await import('axios/unsafe/core/transformData.js');
61+
const { default: buildFullPath } = await import('axios/unsafe/core/buildFullPath.js');
62+
63+
5764
let fullPath: string = buildFullPath(config.baseURL, config.url);
5865
if (isLambdaUrl(fullPath)) {
5966
const lambdaUrl = parseLambdaUrl(fullPath) as LambdaUrl;
@@ -86,7 +93,7 @@ const awsV4Signature: AlphaInterceptor = async (config) => {
8693
path,
8794
port: port ? Number.parseInt(port) : undefined,
8895
headers: getHeaders(hostname, config),
89-
query: combineParams(fullPath, config),
96+
query: await combineParams(fullPath, config),
9097
body: transformData.call(config, config.data, config.headers, config.transformRequest),
9198
};
9299

0 commit comments

Comments
 (0)