Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

ci7lus/ironpipe

Repository files navigation

ironpipe

CI npm version

TypeScript typed helpers for pipedream.

Install

yarn add -D ironpipe

Usage

defineComponent

defineComponent is a function for defining type-safe component.

import { defineComponent } from "ironpipe"

module.exports = defineComponent({
  name: "componentname",
  version: "0.0.1",
  props: {
    url: {
      type: "string",
      label: "something url",
    },
    timer: {
      type: "$.interface.timer",
      default: {
        intervalSeconds: 60 * 15,
      },
    },
    db: "$.service.db",
    http: "$.interface.http",
  },
  methods: {
    random(n: number) {
      return Math.random() * n
    },
  },
  dedupe: "unique",
  async run() {
    // `this` will be typed whenever possible.
    this.db.set("random-value", this.random(100))
    this.db.set("something-url", this.url)

    this.http.respond({
      status: 200,
      headers: {},
      body: this.db.get<number>("random-value")?.toString() || this.url,
    })
  },
})

defineAction

defineAction is a function for defining type-safe action component.
Ref: Quickstart: Action Development

import { defineAction } from "ironpipe"

module.exports = defineAction({
  type: "action",
  name: "example-action",
  key: "example_action",
  version: "0.0.1",
  props: {
    url: {
      type: "string",
      label: "something url",
    },
    target: {
      type: "string",
      label: "replace target string",
      optional: true,
    },
  },
  async run() {
    return this.url.replace(this.target || "http", "https")
  },
})

Example Usage

Example: Use twitter extension
import { defineComponent } from "ironpipe"
const twitter = require("https://github.com/PipedreamHQ/pipedream/components/twitter/twitter.app.js")
import { AxiosResponse, AxiosRequestConfig } from "axios"
import { FullUser, Status } from "twitter-d"

module.exports = defineComponent({
  name: "tweet",
  version: "0.0.1",
  props: {
    url: {
      type: "string",
      label: "something url",
      optional: true,
    },
    timer: {
      type: "$.interface.timer",
      default: {
        intervalSeconds: 60 * 15,
      },
    },
    db: "$.service.db",
    twitter,
  },
  methods: {
    async random(n: number) {
      return Math.random() * n
    },
  },
  dedupe: "unique",
  async run(): Promise<any> {
    // this any to avoid unexplained break of this.methods type when `run` return something
    const rand = this.random(100)
    this.db.set("random-value", rand)
    this.db.set("something-url", this.url)

    const twit = this.twitter as TwitterAppJS

    const r = await twit._makeRequest({
      url: `https://api.twitter.com/1.1/statuses/update.json?status=${rand}`,
      method: "POST",
    })
    return r.data
  },
})

type TwitterAppJS = {
  _makeRequest: (
    opts: AxiosRequestConfig
  ) => Promise<AxiosResponse<{ statuses: (Status & { text: string })[] }>>
  verifyCredentials: () => Promise<FullUser>
  search: (opts: {
    q: string
    since_id?: string | number
    tweet_mode?: "extended"
    count?: number
    result_type?: "recent" | "polular" | "mixed"
    lang?: string
    locale?: string
    geocode?: string
    max_id?: string | number
  }) => Promise<AxiosResponse<{ statuses: (Status & { text: string })[] }>>
}