Skip to content

Commit

Permalink
Serialize endpoint instead of url
Browse files Browse the repository at this point in the history
  • Loading branch information
nalanj committed Dec 3, 2024
1 parent f8504ac commit 50c99d8
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/cli/lib/services/response-saver.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import type { Metadata } from '@nangohq/types';

const FILTER_HEADERS = ['authorization', 'user-agent', 'nango-proxy-user-agent', 'accept-encoding', 'retries', 'host'];

interface CachedRequest {
interface ConfigIdentity {
method: string;
endpoint: string;
requestIdentityHash: string;
requestIdentity: unknown[];
response: unknown;
}

interface ConfigIdentity {
interface CachedRequest {
requestIdentityHash: string;
requestIdentity: unknown[];
response: unknown;
}

export function ensureDirectoryExists(directoryName: string): void {
Expand Down Expand Up @@ -48,7 +50,6 @@ export function onAxiosRequestFulfilled({
return response;
}
const directoryName = `${process.env['NANGO_MOCKS_RESPONSE_DIRECTORY'] ?? ''}${providerConfigKey}`;
const method = response.config.method?.toLowerCase() || 'get';

if (response.request.path.includes(`/connection/${connectionId}?provider_config_key=${providerConfigKey}`)) {
const connection = response.data as Connection;
Expand All @@ -69,9 +70,6 @@ export function onAxiosRequestFulfilled({
return response;
}

const [pathname] = response.request.path.split('?');
const strippedPath = pathname.replace('/', '');

const requestIdentity = computeConfigIdentity(response.config);

saveResponse<CachedRequest>({
Expand All @@ -80,7 +78,7 @@ export function onAxiosRequestFulfilled({
...requestIdentity,
response: response.data
},
customFilePath: `mocks/nango/${method}/${strippedPath}/${syncName}/${requestIdentity.requestIdentityHash}.json`
customFilePath: `mocks/nango/${requestIdentity.method}/${requestIdentity.endpoint}/${syncName}/${requestIdentity.requestIdentityHash}.json`
});

return response;
Expand All @@ -90,9 +88,12 @@ function computeConfigIdentity(config: AxiosRequestConfig): ConfigIdentity {
const method = config.method?.toLowerCase() || 'get';
const params = sortEntries(Object.entries(config.params || {}));

const url = new URL(config.url!);
const endpoint = url.pathname.replace(/^\/proxy\//, '');

const requestIdentity: [string, unknown][] = [
['method', method],
['url', config.url],
['endpoint', endpoint],
['params', params]
];

Expand All @@ -113,6 +114,8 @@ function computeConfigIdentity(config: AxiosRequestConfig): ConfigIdentity {
const requestIdentityHash = crypto.createHash('sha1').update(JSON.stringify(requestIdentity)).digest('hex');

return {
method,
endpoint,
requestIdentityHash,
requestIdentity
};
Expand Down

0 comments on commit 50c99d8

Please sign in to comment.