Can Actions in Pipedream be Used as Reusable Functions Across Different Workflow Steps?

This topic was automatically generated from Slack. You can find the original thread here.

hey heyy
I’m fairly new to pipedream and I’ve been trying to figure out how I can create a reusable function to use across my workflows but haven’t had much success.
I went through the steps to learn how to create an action, but it looks like actions won’t work as functions, or at least I haven’t been able to understand how to make that work for me. Can anyone help me learn if actions can be used like a reusable function for different steps across my workflows?

An action is definitely the way to go (other than publishing a public npm package). Where are you getting stuck? Did you manage to get the action published?

I went through most of the hello world tutorial and was able to publish a simple action. However, I experienced a couple of hang ups that might prevent me from being able to use actions:

  1. The reusable function I’d like to create would take one argument and would output either a success or an error message. With Pipedream Actions, I was unable to make a function that takes an argument dynamically.
  2. Another issue with actions is if I make a change to the action, I have to update it in every automation I’ve created, this still adds a lot of work and leave a lot of room for human error.
    Do you have any other suggestions or specific examples of how I can create a reusable function with the new acceptance criteria I’ve mentioned here?

Can you elaborate on the specifics of #1?

re: #2, yes that is unfortunately a constraint right now. One workaround could be that if you use Git Sync with your workflows, you could bump the version of the action in any workflow that’s synced to GH locally, then deploy those changes to production.

Unfortunately the company I’m working for is not on the Business subscription (this was too big of a price jump for us unfortunately)

Specifics on #1:
The reusable function I’d like to create takes in an api response. If the object has an ‘error’ then it will return the error message and the location the error occured.

Re: Git Sync, that’s available on the Advanced plan FYI. And re: the reusable function, can you share the code here? On the surface that sounds like a solvable problem.

Oh really?! okay thats good to know.
Here’s the code:

function apiErrorCheck(response, callName) {
  if (response.errors) {
    const errorMessage = response.errors[0].message;
    console.dir({ response, callName });
    throw new Error(`ERROR: ${errorMessage}`);
  } 
  console.log(`SUCCESS: ${callName} api call was successful`);
  console.dir({ response, callName });
}

can you transform this code into a custom action?

function apiErrorCheck(response, callName) {
  if (response.errors) {
    const errorMessage = response.errors[0].message;
    console.dir({ response, callName });
    throw new Error(`ERROR: ${errorMessage}`);
  } 
  console.log(`SUCCESS: ${callName} api call was successful`);
  console.dir({ response, callName });
}

To transform the provided code into a Pipedream custom action, we need to create a Pipedream component using defineComponent. This component will include the logic from your function, and we’ll use Pipedream’s environment to handle the response and errors. Here’s how you can do it: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { response: { type: "object", label: "API Response", description: "The response object from the API call", }, callName: { type: "string", label: "Call Name", description: "The name of the API call", }, }, async run({ steps, $ }) { const { response, callName } = this; if (response.errors) { const errorMessage = response.errors[0].message; console.dir({ response, callName }); throw new Error(`ERROR: ${errorMessage}`); } console.log(`SUCCESS: ${callName} api call was successful`); console.dir({ response, callName }); return { response, callName }; }, }); ### Explanation: - Props: We define two props, response and callName, to pass the API response and the name of the API call to the component. - Error Handling: The logic checks if there are any errors in the response. If errors exist, it throws an error with the message. - Success Log: If there are no errors, it logs a success message. - Return: The component returns the response and callName for further use in the workflow. You can use this component in your Pipedream workflow by configuring the response and callName props with the appropriate data from previous steps.

what about the meta keys for an action, like name, key, version, description, etc

In Pipedream components, metadata keys are essential for defining the properties of an action or trigger. Here’s a breakdown of the required metadata keys for an action component: - key: A unique identifier for the component. This is used internally by Pipedream to distinguish between different components. - name: A human-readable name for the component. This is what users will see in the Pipedream UI. - description: A brief description of what the component does. This helps users understand the purpose of the component. - version: The version of the component. This is useful for tracking changes and updates to the component. - type: For action components, this should be set to "action". This distinguishes it from other types of components, like triggers. Here’s an example of how these metadata keys are used in a component: ```
export default { key: “google_drive-new-shared-drive”, name: “New Shared Drive”, description: “Emits a new event any time a shared drive is created.”, version: “0.0.1”, type: “action”, };

can you add those metadata keys in your code example?

To provide a more accurate response, could you please clarify which code example you’re referring to? If you have a specific code snippet or context in mind, sharing that would help me add the metadata keys appropriately.

Well basically you could write this as your custom action and in the props you can input dynamically the response from the previous step

export default {
  key: "error-handler",
  name: "Error Handler",
  description: "Verifies if there was an error in an api call.",
  version: "0.0.1",
  type: "action",
  props: {
    response: {
      type: "object",
      label: "API Response",
      description: "The response object from the API call",
    },
    callName: {
      type: "string",
      label: "Call Name",
      description: "The name of the API call",
    },
  },
  async run({ steps, $ }) {
    const { response, callName } = this;
    
    if (response.errors) {
      const errorMessage = response.errors[0].message;
      console.dir({ response, callName });
      throw new Error(`ERROR: ${errorMessage}`);
    } 
    
    console.log(`SUCCESS: ${callName} api call was successful`);
    console.dir({ response, callName });
    
    return { response, callName };
  },
}

This is great! Now I’m having a difficult time calling this function in my workflows. Can you help me with this or point me to relevant documentation?

When adding a new step in your workflow, click on My Actions and you should see it there (considering you have published it as a custom action)