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:
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.
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?
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.
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.
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.
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”, };
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.