Skip to content

Commit

Permalink
Tested components (#11991)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcortes committed May 24, 2024
1 parent eaba64b commit 5d3e468
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 8 deletions.
19 changes: 19 additions & 0 deletions components/upstash_redis/actions/list-databases/list-databases.mjs
Original file line number Diff line number Diff line change
@@ -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;
},
};
49 changes: 49 additions & 0 deletions components/upstash_redis/actions/post-command/post-command.mjs
Original file line number Diff line number Diff line change
@@ -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;
},
};
9 changes: 9 additions & 0 deletions components/upstash_redis/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const BASE_URL = "https://api.upstash.com";
const VERSION_PATH = "/v2/redis";
const HTTPS_PROTOCOL = "https://";

export default {
BASE_URL,
VERSION_PATH,
HTTPS_PROTOCOL,
};
28 changes: 28 additions & 0 deletions components/upstash_redis/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -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,
};
7 changes: 5 additions & 2 deletions components/upstash_redis/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^1.6.5"
}
}
}
75 changes: 70 additions & 5 deletions components/upstash_redis/upstash_redis.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,76 @@
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));
getRedisUrl() {
const { upstash_redis_rest_url: url } = this.$auth;
return url.startsWith(constants.HTTPS_PROTOCOL)
? url
: `${constants.HTTPS_PROTOCOL}${url}`;
},
getUrl(path, managementApi) {
return managementApi
? `${constants.BASE_URL}${constants.VERSION_PATH}${path}`
: this.getRedisUrl();
},
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",
});
},
},
};
};
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d3e468

Please sign in to comment.