Skip to content

Commit

Permalink
fix(usebruno#1910): Use toString method for objects in interpolation
Browse files Browse the repository at this point in the history
For example with Moment the object with JSON.stringify became:
`"Mon Mar 25 2024 22:52:03 GMT+0100"` when using just `toString`
the moment object becomes `Mon Mar 25 2024 22:52:03 GMT+0100`
(without double quotes). I think the should yield more expected
results.
  • Loading branch information
Its-treason committed Mar 25, 2024
1 parent 88c6e99 commit 02c1b69
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/bruno-common/src/interpolate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

import { flattenObject } from '../utils';
import cancelTokens from '@usebruno/app/src/utils/network/cancelTokens';

const interpolate = (str: string, obj: Record<string, any>): string => {
if (!str || typeof str !== 'string' || !obj || typeof obj !== 'object') {
Expand All @@ -27,10 +28,17 @@ const interpolate = (str: string, obj: Record<string, any>): string => {
return match;
}

// When inside json body everything must be encoded so string get double quotes
// Objects must be either JSON encoded or convert to a String via `toString`
if (typeof replacement === 'object') {
// Check if the object has a `toString` method like `Moment`
if (typeof replacement.toString === 'function') {
try {
return replacement.toString();
} catch {}
}
return JSON.stringify(replacement);
}

return replacement;
});
};
Expand Down

0 comments on commit 02c1b69

Please sign in to comment.