Skip to content

Commit

Permalink
fix code execution params name clobbering, and make variable access s…
Browse files Browse the repository at this point in the history
…lightly safer
  • Loading branch information
soorria committed May 16, 2023
1 parent 9017863 commit 082d25d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { UnwrapVariable, Variable, createVariable, toPath } from "./variable";
import {
UnwrapVariable,
Variable,
createVariable,
toOptionalPath,
toPath,
} from "./variable";
import { API, APIAuthDetails } from "./api";
import {
AllowedTransformationId,
Expand Down Expand Up @@ -133,7 +139,7 @@ export class Chain<

public code<
CodeParams extends Record<string, Variable<any>>,
Fn extends (params: CodeParams) => any
Fn extends (params: UnwrapVariable<CodeParams>) => any
>(
params: CodeParams,
fn: Fn
Expand All @@ -142,16 +148,16 @@ export class Chain<
} {
const paramsObjectContent = Object.entries(params)
.map(([key, variable]) => {
return `${JSON.stringify(key)}: ${toPath(variable)}`;
return `${JSON.stringify(key)}: ${toOptionalPath(variable)}`;
})
.join(",");

const fnString = fn.toString();

const codeAsIIFE = `
return (() => {
const params = { ${paramsObjectContent} };
return (${fnString})(params);
const _$$params = { ${paramsObjectContent} };
return (${fnString})(_$$params);
})();`.trim();

return this.step("js_code_transformation", {
Expand Down
4 changes: 4 additions & 0 deletions src/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ export const isVariable = (value: any): value is Variable<any> => {
export const toPath = (variable: Variable<unknown>): string => {
return variable[VARIABLE_INTERNAL].path;
};

export const toOptionalPath = (variable: Variable<unknown>): string => {
return toPath(variable).split(".").join("?.");
};

0 comments on commit 082d25d

Please sign in to comment.