backward compatible functions

This commit is contained in:
shamsmosowi
2022-03-04 21:51:35 +08:00
parent 7b80691f4a
commit 8f281809cc
5 changed files with 109 additions and 73 deletions

View File

@@ -15,6 +15,7 @@ import { WIKI_LINKS } from "@src/constants/externalLinks";
import { name } from "@root/package.json";
/* eslint-disable import/no-webpack-loader-syntax */
import defaultValueDefs from "!!raw-loader!./defaultValue.d.ts";
import { useProjectContext } from "@src/contexts/ProjectContext";
const _CodeEditor = lazy(
() =>
import("@src/components/CodeEditor" /* webpackChunkName: "CodeEditor" */)
@@ -31,14 +32,19 @@ export interface IDefaultValueInputProps extends IMenuModalProps {
}
const CodeEditor = ({ type, config, handleChange }) => {
const { compatibleRowyRunVersion } = useProjectContext();
const functionBodyOnly = compatibleRowyRunVersion!({ maxVersion: "1.4.0" });
const returnType = getFieldProp("dataType", type) ?? "any";
const defaultValueFn = config.defaultValue?.defaultValueFn
? config.defaultValue?.defaultValueFn
const dynamicValueFn = functionBodyOnly
? config.defaultValue?.script
: config.defaultValue?.dynamicValueFn
? config.defaultValue?.dynamicValueFn
: config.defaultValue?.script
? `const defaultValueFn : DefaultValue = async ({row,ref,db,storage,auth})=>{
? `const dynamicValueFn : DefaultValue = async ({row,ref,db,storage,auth})=>{
${config.defaultValue.script}
}`
: `const defaultValueFn: DefaultValue = async ({row,ref,db,storage,auth})=>{
: `const dynamicValueFn : DefaultValue = async ({row,ref,db,storage,auth})=>{
// Write your default value code here
// for example:
// generate random hex color
@@ -48,15 +54,17 @@ const CodeEditor = ({ type, config, handleChange }) => {
}`;
return (
<_CodeEditor
value={defaultValueFn}
diagnosticsOptions={diagnosticsOptions}
value={dynamicValueFn}
diagnosticsOptions={functionBodyOnly ? undefined : diagnosticsOptions}
extraLibs={[
defaultValueDefs.replace(
`"PLACEHOLDER_OUTPUT_TYPE"`,
`${returnType} | Promise<${returnType}>`
),
]}
onChange={handleChange("defaultValue.defaultValueFn")}
onChange={handleChange(
functionBodyOnly ? "defaultValue.script" : "defaultValue.dynamicValueFn"
)}
/>
);
};

View File

@@ -34,6 +34,7 @@ import { WIKI_LINKS } from "@src/constants/externalLinks";
import { useAppContext } from "@src/contexts/AppContext";
/* eslint-disable import/no-webpack-loader-syntax */
import actionDefs from "!!raw-loader!./action.d.ts";
import { RUN_ACTION_TEMPLATE, UNDO_ACTION_TEMPLATE } from "./templates";
const diagnosticsOptions = {
noSemanticValidation: false,
@@ -47,11 +48,12 @@ const CodeEditor = lazy(
);
const Settings = ({ config, onChange }) => {
const { tableState, roles } = useProjectContext();
const { tableState, roles, compatibleRowyRunVersion } = useProjectContext();
const { projectId } = useAppContext();
const [activeStep, setActiveStep] = useState<
"requirements" | "friction" | "action" | "undo" | "customization"
>("requirements");
const functionBodyOnly = compatibleRowyRunVersion!({ maxVersion: "1.4.0" });
const steps =
config.isActionScript && _get(config, "undo.enabled")
? ["requirements", "friction", "action", "undo", "customization"]
@@ -95,72 +97,25 @@ const Settings = ({ config, onChange }) => {
typeof config.confirmation === "string" &&
config.confirmation !== "");
const runFn = config.runFn
const runFn = functionBodyOnly
? config?.script
: config.runFn
? config.derivativeFn
: config?.script
? `const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => {
${config.script.replace(/utilFns.getSecret/g, "rowy.secrets.getSecret")}
}`
: `const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => {
// Write your action code here
// for example:
// const authToken = await rowy.secrets.getSecret("service")
// try {
// const resp = await fetch('https://example.com/api/v1/users/'+ref.id,{
// method: 'PUT',
// headers: {
// 'Content-Type': 'application/json',
// 'Authorization': authToken
// },
// body: JSON.stringify(row)
// })
//
// return {
// success: true,
// message: 'User updated successfully on example service',
// status: "upto date"
// }
// } catch (error) {
// return {
// success: false,
// message: 'User update failed on example service',
// }
// }
// checkout the documentation for more info: https://docs.rowy.io/field-types/action#script
}`;
: RUN_ACTION_TEMPLATE;
const undoFn = config.undoFn
const undoFn = functionBodyOnly
? _get(config, "undo.script")
: config.undoFn
? config.undoFn
: _get(config, "undo.script")
? `const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => {
? `const action : Action = async ({row,ref,db,storage,auth,actionParams,user}) => {
${_get(config, "undo.script")}
}`
: `const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => {
// Write your undo code here
// for example:
// const authToken = await rowy.secrets.getSecret("service")
// try {
// const resp = await fetch('https://example.com/api/v1/users/'+ref.id,{
// method: 'DELETE',
// headers: {
// 'Content-Type': 'application/json',
// 'Authorization': authToken
// },
// body: JSON.stringify(row)
// })
//
// return {
// success: true,
// message: 'User deleted successfully on example service',
// status: null
// }
// } catch (error) {
// return {
// success: false,
// message: 'User delete failed on example service',
// }
// }
}`;
: UNDO_ACTION_TEMPLATE;
return (
<SteppedAccordion
steps={[
@@ -414,9 +369,15 @@ const Settings = ({ config, onChange }) => {
<CodeEditor
minHeight={200}
value={runFn}
onChange={onChange("runFn")}
onChange={
functionBodyOnly
? onChange("script")
: onChange("runFn")
}
extraLibs={scriptExtraLibs}
diagnosticsOptions={diagnosticsOptions}
diagnosticsOptions={
functionBodyOnly ? undefined : diagnosticsOptions
}
/>
</Suspense>
<CodeEditorHelper
@@ -524,8 +485,15 @@ const Settings = ({ config, onChange }) => {
<Suspense fallback={<FieldSkeleton height={300} />}>
<CodeEditor
value={undoFn}
onChange={onChange("undoFn")}
onChange={
functionBodyOnly
? onChange("undo.script")
: onChange("undoFn")
}
extraLibs={scriptExtraLibs}
diagnosticsOptions={
functionBodyOnly ? undefined : diagnosticsOptions
}
/>
</Suspense>
<CodeEditorHelper

View File

@@ -0,0 +1,54 @@
export const RUN_ACTION_TEMPLATE = `const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => {
// Write your action code here
// for example:
// const authToken = await rowy.secrets.getSecret("service")
// try {
// const resp = await fetch('https://example.com/api/v1/users/'+ref.id,{
// method: 'PUT',
// headers: {
// 'Content-Type': 'application/json',
// 'Authorization': authToken
// },
// body: JSON.stringify(row)
// })
//
// return {
// success: true,
// message: 'User updated successfully on example service',
// status: "upto date"
// }
// } catch (error) {
// return {
// success: false,
// message: 'User update failed on example service',
// }
// }
// checkout the documentation for more info: https://docs.rowy.io/field-types/action#script
}`;
export const UNDO_ACTION_TEMPLATE = `const action : Action = async ({row,ref,db,storage,auth,actionParams,user}) => {
// Write your undo code here
// for example:
// const authToken = await rowy.secrets.getSecret("service")
// try {
// const resp = await fetch('https://example.com/api/v1/users/'+ref.id,{
// method: 'DELETE',
// headers: {
// 'Content-Type': 'application/json',
// 'Authorization': authToken
// },
// body: JSON.stringify(row)
// })
//
// return {
// success: true,
// message: 'User deleted successfully on example service',
// status: null
// }
// } catch (error) {
// return {
// success: false,
// message: 'User delete failed on example service',
// }
// }
}`;

View File

@@ -33,16 +33,20 @@ export default function Settings({
onBlur,
errors,
}: ISettingsProps) {
const { tableState } = useProjectContext();
const { tableState, compatibleRowyRunVersion } = useProjectContext();
if (!tableState?.columns) return <></>;
const columns = Object.values(tableState.columns);
const returnType = getFieldProp("dataType", config.renderFieldType) ?? "any";
console.log({ returnType, r: config.renderFieldType });
const columnOptions = columns
.filter((column) => column.fieldName !== fieldName)
.filter((column) => column.type !== FieldType.subTable)
.map((c) => ({ label: c.name, value: c.key }));
const code = config.derivativeFn
const functionBodyOnly = compatibleRowyRunVersion!({ maxVersion: "1.4.0" });
const code = functionBodyOnly
? config?.script
: config.derivativeFn
? config.derivativeFn
: config?.script
? `const derivative:Derivative = async ({row,ref,db,storage,auth})=>{
@@ -118,7 +122,9 @@ export default function Settings({
<CodeEditorHelper docLink={WIKI_LINKS.fieldTypesDerivative} />
<Suspense fallback={<FieldSkeleton height={200} />}>
<CodeEditor
diagnosticsOptions={diagnosticsOptions}
diagnosticsOptions={
functionBodyOnly ? undefined : diagnosticsOptions
}
value={code}
extraLibs={[
derivativeDefs.replace(
@@ -126,7 +132,7 @@ export default function Settings({
`${returnType} | Promise<${returnType}>`
),
]}
onChange={onChange("code")}
onChange={onChange(functionBodyOnly ? "script" : "code")}
/>
</Suspense>
</div>

View File

@@ -17,7 +17,7 @@ export const rowyRun = async ({
route,
body,
params,
localhost = true,
localhost = false,
json = true,
signal,
}: IRowyRunRequestProps) => {