diff --git a/components/upstash_redis/actions/list-databases/list-databases.mjs b/components/upstash_redis/actions/list-databases/list-databases.mjs new file mode 100644 index 0000000000000..fd0d237276e7d --- /dev/null +++ b/components/upstash_redis/actions/list-databases/list-databases.mjs @@ -0,0 +1,19 @@ +import app from "../../upstash_redis.app.mjs"; + +export default { + key: "upstash_redis-list-databases", + name: "List Redis Databases", + description: "Lists all Redis databases. [See the documentation](https://upstash.com/docs/devops/developer-api/redis/list_databases)", + version: "0.0.1", + type: "action", + props: { + app, + }, + async run({ $ }) { + const response = await this.app.listDatabases({ + $, + }); + $.export("$summary", `Successfully listed \`${response.length}\` Redis database(s)`); + return response; + }, +}; diff --git a/components/upstash_redis/actions/post-command/post-command.mjs b/components/upstash_redis/actions/post-command/post-command.mjs new file mode 100644 index 0000000000000..234b1b57c809d --- /dev/null +++ b/components/upstash_redis/actions/post-command/post-command.mjs @@ -0,0 +1,49 @@ +import utils from "../../common/utils.mjs"; +import app from "../../upstash_redis.app.mjs"; + +export default { + key: "upstash_redis-post-command", + name: "Post Command", + description: "Post a command to a Redis database. [See the documentation](https://upstash.com/docs/redis/features/restapi#post-command-in-body)", + version: "0.0.1", + type: "action", + props: { + app, + command: { + type: "string", + label: "Command", + description: "The command to post to the database.", + options: [ + "SET", + "GET", + ], + }, + args: { + type: "string[]", + label: "Arguments", + description: "The arguments for the command. Eg. `[\"mykey\", \"myvalue\"]`", + }, + }, + methods: { + postCommand(args = {}) { + return this.app.post(args); + }, + }, + async run({ $ }) { + const { + postCommand, + command, + args, + } = this; + const response = await postCommand({ + $, + data: [ + command, + ...utils.parseArray(args), + ], + }); + + $.export("$summary", "Successfully posted command to the database."); + return response; + }, +}; diff --git a/components/upstash_redis/common/constants.mjs b/components/upstash_redis/common/constants.mjs new file mode 100644 index 0000000000000..079dd467f89b6 --- /dev/null +++ b/components/upstash_redis/common/constants.mjs @@ -0,0 +1,7 @@ +const BASE_URL = "https://api.upstash.com"; +const VERSION_PATH = "/v2/redis"; + +export default { + BASE_URL, + VERSION_PATH, +}; diff --git a/components/upstash_redis/common/utils.mjs b/components/upstash_redis/common/utils.mjs new file mode 100644 index 0000000000000..d9df0aa935aef --- /dev/null +++ b/components/upstash_redis/common/utils.mjs @@ -0,0 +1,28 @@ +import { ConfigurationError } from "@pipedream/platform"; + +function parseArray(value) { + try { + if (!value) { + return []; + } + + if (Array.isArray(value)) { + return value; + } + + const parsedValue = JSON.parse(value); + + if (!Array.isArray(parsedValue)) { + throw new Error("Not an array"); + } + + return parsedValue; + + } catch (e) { + throw new ConfigurationError("Make sure the custom expression contains a valid array object"); + } +} + +export default { + parseArray, +}; diff --git a/components/upstash_redis/package.json b/components/upstash_redis/package.json index 94549794c4e73..d5116d90cb708 100644 --- a/components/upstash_redis/package.json +++ b/components/upstash_redis/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/upstash_redis", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Upstash Redis Components", "main": "upstash_redis.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^1.6.5" } } \ No newline at end of file diff --git a/components/upstash_redis/upstash_redis.app.mjs b/components/upstash_redis/upstash_redis.app.mjs index 73560f10dcf14..8ada2c75aa065 100644 --- a/components/upstash_redis/upstash_redis.app.mjs +++ b/components/upstash_redis/upstash_redis.app.mjs @@ -1,11 +1,70 @@ +import { axios } from "@pipedream/platform"; +import constants from "./common/constants.mjs"; + export default { type: "app", app: "upstash_redis", - propDefinitions: {}, + propDefinitions: { + databaseId: { + type: "string", + label: "Database ID", + description: "The ID of the database.", + async options() { + const databases = await this.listDatabases(); + return databases.map(({ + database_id: value, database_name: label, + }) => ({ + label, + value, + })); + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + getUrl(path, managementApi) { + return managementApi + ? `${constants.BASE_URL}${constants.VERSION_PATH}${path}` + : this.$auth.upstash_redis_rest_url; + }, + getHeaders(headers) { + return { + ...headers, + "Authorization": `Bearer ${this.$auth.uptash_redis_rest_token}`, + }; + }, + getAuth(managementApi) { + if (managementApi) { + const { + email: username, + management_api_key: password, + } = this.$auth; + return { + username, + password, + }; + } + }, + _makeRequest({ + $ = this, path, headers, managementApi, ...args + } = {}) { + return axios($, { + ...args, + url: this.getUrl(path, managementApi), + headers: this.getHeaders(headers), + auth: this.getAuth(managementApi), + }); + }, + post(args = {}) { + return this._makeRequest({ + method: "POST", + ...args, + }); + }, + listDatabases() { + return this._makeRequest({ + managementApi: true, + path: "/databases", + }); }, }, -}; \ No newline at end of file +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 96957d7d10faf..e4c0f412e681c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9141,7 +9141,10 @@ importers: specifiers: {} components/upstash_redis: - specifiers: {} + specifiers: + '@pipedream/platform': ^1.6.5 + dependencies: + '@pipedream/platform': 1.6.5 components/uptimerobot: specifiers: {}