Skip to content

Commit

Permalink
Merge pull request #176 from lifeomic/reduce-failures-when-on-commonjs
Browse files Browse the repository at this point in the history
Avoid ESM failures when AWS V4 signing is unused
  • Loading branch information
mdlavin authored Aug 21, 2024
2 parents d58fda4 + 913e833 commit 8876494
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/interceptors/aws-v4-signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import {
HttpRequest,
HeaderBag,
} from '@aws-sdk/types';
import buildURL from 'axios/unsafe/helpers/buildURL.js';
import transformData from 'axios/unsafe/core/transformData.js';
import buildFullPath from 'axios/unsafe/core/buildFullPath.js';

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

const combineParams = (url: string, { params, paramsSerializer }: AlphaOptions): HttpRequest['query'] => {
const combineParams = async (url: string, { params, paramsSerializer }: AlphaOptions): Promise<HttpRequest['query']> => {
// buildURL is a ESM module, so we need to import it dynamically since
// we are in a CommonJS environment
const { default: buildURL } = await import('axios/unsafe/helpers/buildURL.js');

const fullUrl: string = buildURL(url, params, paramsSerializer);
const { query } = parseUrl(fullUrl);
return query;
Expand All @@ -54,6 +55,12 @@ const awsV4Signature: AlphaInterceptor = async (config) => {
return config;
}

// transformData and buildFullPath are ESM modules, so we need to import it
// dynamically since we are in a CommonJS environment
const { default: transformData } = await import('axios/unsafe/core/transformData.js');
const { default: buildFullPath } = await import('axios/unsafe/core/buildFullPath.js');


let fullPath: string = buildFullPath(config.baseURL, config.url);
if (isLambdaUrl(fullPath)) {
const lambdaUrl = parseLambdaUrl(fullPath) as LambdaUrl;
Expand Down Expand Up @@ -86,7 +93,7 @@ const awsV4Signature: AlphaInterceptor = async (config) => {
path,
port: port ? Number.parseInt(port) : undefined,
headers: getHeaders(hostname, config),
query: combineParams(fullPath, config),
query: await combineParams(fullPath, config),
body: transformData.call(config, config.data, config.headers, config.transformRequest),
};

Expand Down

0 comments on commit 8876494

Please sign in to comment.