Skip to content

Commit

Permalink
Merge pull request #467 from zapier/PDE-2946-fix-normalizeEmptyReques…
Browse files Browse the repository at this point in the history
…tFields

PDE-2946 fix(core): fix `value.replace is not a function` when resolving missing curlies (9.x backport)
  • Loading branch information
eliangcs committed Jan 4, 2022
2 parents 2a11570 + a551b21 commit 76298fa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
22 changes: 11 additions & 11 deletions packages/cli/src/tests/utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ describe('build (runs slowly)', () => {
});

it('should list only required files', () => {
return build.requiredFiles(tmpDir, [entryPoint]).then(smartPaths => {
return build.requiredFiles(tmpDir, [entryPoint]).then((smartPaths) => {
// check that only the required lodash files are grabbed
smartPaths.should.containEql('index.js');
smartPaths.should.containEql('lib/index.js');
smartPaths.should.containEql('lib/resources/recipe.js');

smartPaths.filter(p => p.endsWith('.ts')).length.should.equal(0);
smartPaths.filter((p) => p.endsWith('.ts')).length.should.equal(0);
smartPaths.should.not.containEql('tsconfig.json');

smartPaths.length.should.be.within(200, 300);
});
});

it('should list all the files', () => {
return build.listFiles(tmpDir).then(dumbPaths => {
return build.listFiles(tmpDir).then((dumbPaths) => {
// check that way more than the required package files are grabbed
dumbPaths.should.containEql('index.js');
dumbPaths.should.containEql('lib/index.js');
Expand All @@ -65,7 +65,7 @@ describe('build (runs slowly)', () => {
dumbPaths.should.containEql('src/resources/recipe.ts');
dumbPaths.should.containEql('tsconfig.json');

dumbPaths.length.should.be.within(1500, 2000);
dumbPaths.length.should.be.within(1500, 2500);
});
});

Expand All @@ -77,8 +77,8 @@ describe('build (runs slowly)', () => {
'.env',
'.environment',
'.git/HEAD',
'build/the-build.zip'
].forEach(file => {
'build/the-build.zip',
].forEach((file) => {
const fileDir = file.split(path.sep);
fileDir.pop();
if (fileDir.length > 0) {
Expand All @@ -87,7 +87,7 @@ describe('build (runs slowly)', () => {
fs.outputFileSync(path.join(tmpProjectDir, file), 'the-file');
});

return build.listFiles(tmpProjectDir).then(dumbPaths => {
return build.listFiles(tmpProjectDir).then((dumbPaths) => {
dumbPaths.should.containEql('safe.js');
dumbPaths.should.not.containEql('.env');
dumbPaths.should.not.containEql('build/the-build.zip');
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('build (runs slowly)', () => {
return build
.makeZip(tmpProjectDir, tmpZipPath)
.then(() => decompress(tmpZipPath, tmpUnzipPath))
.then(files => {
.then((files) => {
files.length.should.equal(2);

const indexFile = files.find(
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('build (runs slowly)', () => {
return build
.makeZip(tmpProjectDir, tmpZipPath)
.then(() => decompress(tmpZipPath, tmpUnzipPath))
.then(files => {
.then((files) => {
files.length.should.equal(2);

const indexFile = files.find(
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('build (runs slowly)', () => {
return build
.makeSourceZip(tmpProjectDir, tmpZipPath)
.then(() => decompress(tmpZipPath, tmpUnzipPath))
.then(files => {
.then((files) => {
files.length.should.equal(4);

const indexFile = files.find(
Expand Down Expand Up @@ -261,7 +261,7 @@ describe('build (runs slowly)', () => {
return build
.makeSourceZip(tmpProjectDir, tmpZipPath)
.then(() => decompress(tmpZipPath, tmpUnzipPath))
.then(files => {
.then((files) => {
files.length.should.equal(4);

const indexFile = files.find(
Expand Down
28 changes: 14 additions & 14 deletions packages/core/src/tools/cleaner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ const {
flattenPaths,
getObjectType,
isPlainObj,
recurseReplace
recurseReplace,
} = require('./data');

const DEFAULT_BUNDLE = {
authData: {},
inputData: {},
meta: {},
subscribeData: {},
targetUrl: ''
targetUrl: '',
};

const isCurlies = /{{.*?}}/g;
Expand All @@ -34,7 +34,7 @@ const recurseCleanFuncs = (obj, path) => {
});
} else if (isPlainObj(obj)) {
const newObj = {};
Object.keys(obj).forEach(key => {
Object.keys(obj).forEach((key) => {
const value = obj[key];
newObj[key] = recurseCleanFuncs(value, path.concat([key]));
});
Expand All @@ -45,7 +45,7 @@ const recurseCleanFuncs = (obj, path) => {

// Recurse a nested object replace all instances of keys->vals in the bank.
const recurseReplaceBank = (obj, bank = {}) => {
const replacer = out => {
const replacer = (out) => {
if (!['string', 'number'].includes(typeof out)) {
return out;
}
Expand All @@ -56,7 +56,7 @@ const recurseReplaceBank = (obj, bank = {}) => {
const originalValueStr = String(out);
let maybeChangedString = originalValueStr;

Object.keys(bank).forEach(key => {
Object.keys(bank).forEach((key) => {
// Escape characters (ex. {{foo}} => \\{\\{foo\\}\\} )
const escapedKey = key.replace(/[-[\]/{}()\\*+?.^$|]/g, '\\$&');
const matchesKey = new RegExp(escapedKey, 'g');
Expand Down Expand Up @@ -106,12 +106,12 @@ const finalizeBundle = pipe(
);

// Takes a raw app and bundle and composes a bank of {{key}}->val
const createBundleBank = (appRaw, event = {}, serializeFunc = x => x) => {
const createBundleBank = (appRaw, event = {}, serializeFunc = (x) => x) => {
const bank = {
bundle: finalizeBundle(event.bundle),
process: {
env: _.extend({}, process.env || {})
}
env: _.extend({}, process.env || {}),
},
};

const options = { preserve: { 'bundle.inputData': true } };
Expand All @@ -123,7 +123,7 @@ const createBundleBank = (appRaw, event = {}, serializeFunc = x => x) => {
}, {});
};

const maskOutput = output => _.pick(output, 'results', 'status');
const maskOutput = (output) => _.pick(output, 'results', 'status');

// These normalize functions are called after the initial before middleware that
// cleans the request. The reason is that we need to know why a value is empty
Expand All @@ -132,7 +132,7 @@ const maskOutput = output => _.pick(output, 'results', 'status');
// an earlier Zap step? Or was it a null value? Each has different results depending
// on how the partner has configued their integration.
const normalizeEmptyRequestFields = (shouldCleanup, field, req) => {
const handleEmpty = key => {
const handleEmpty = (key) => {
const value = req[field][key] || '';
const cleaned = value.replace(isCurlies, '');

Expand All @@ -152,11 +152,11 @@ const normalizeEmptyRequestFields = (shouldCleanup, field, req) => {
});
};

const isEmptyQueryParam = value =>
const isEmptyQueryParam = (value) =>
value === '' ||
value === null ||
value === undefined ||
isCurlies.test(value);
(typeof value === 'string' && value.search(isCurlies) >= 0);

const normalizeEmptyParamFields = normalizeEmptyRequestFields.bind(
null,
Expand All @@ -165,7 +165,7 @@ const normalizeEmptyParamFields = normalizeEmptyRequestFields.bind(
);
const normalizeEmptyBodyFields = normalizeEmptyRequestFields.bind(
null,
v => isCurlies.test(v),
(v) => typeof v === 'string' && v.search(isCurlies) >= 0,
'body'
);

Expand All @@ -175,5 +175,5 @@ module.exports = {
normalizeEmptyBodyFields,
normalizeEmptyParamFields,
recurseCleanFuncs,
recurseReplaceBank
recurseReplaceBank,
};
22 changes: 22 additions & 0 deletions packages/core/test/create-request-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,13 +696,35 @@ describe('request client', () => {
empty: '{{bundle.inputData.empty}}',
partial: 'text {{bundle.inputData.partial}}',
value: 'exists',
array: [
'{{bundle.inputData.empty}}',
'foo{{bundle.inputData.noMatch}}',
'bar',
],
obj: {
empty: '{{bundle.inputData.empty}}',
partial: 'text {{bundle.inputData.partial}}',
value: 'exists',
},
},
}).then((response) => {
const { json } = response.json;

should(json.empty).eql('');
should(json.partial).eql('text ');
should(json.value).eql('exists');

// We don't do recursive replacement
should(json.array).eql([
'{{bundle.inputData.empty}}',
'foo{{bundle.inputData.noMatch}}',
'bar',
]);
should(json.obj).eql({
empty: '{{bundle.inputData.empty}}',
partial: 'text {{bundle.inputData.partial}}',
value: 'exists',
});
});
});

Expand Down

0 comments on commit 76298fa

Please sign in to comment.