Skip to content

Commit f139761

Browse files
authored
New Components - tinyurl (#15456)
* tinyurl init * [Components] tinyurl #15135 Sources - New Link Created Actions - Create Shortened Link - Update Link Metadata - Retrieve Link Analytics * pnpm update
1 parent 65ce00c commit f139761

File tree

8 files changed

+430
-4
lines changed

8 files changed

+430
-4
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import { parseObject } from "../../common/utils.mjs";
3+
import tinyurl from "../../tinyurl.app.mjs";
4+
5+
export default {
6+
key: "tinyurl-create-shortened-link",
7+
name: "Create Shortened Link",
8+
description: "Creates a new shortened link. [See the documentation]()",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
tinyurl,
13+
url: {
14+
propDefinition: [
15+
tinyurl,
16+
"url",
17+
],
18+
},
19+
domain: {
20+
propDefinition: [
21+
tinyurl,
22+
"domain",
23+
],
24+
optional: true,
25+
},
26+
alias: {
27+
propDefinition: [
28+
tinyurl,
29+
"alias",
30+
],
31+
optional: true,
32+
},
33+
tags: {
34+
propDefinition: [
35+
tinyurl,
36+
"tags",
37+
],
38+
optional: true,
39+
},
40+
expiresAt: {
41+
propDefinition: [
42+
tinyurl,
43+
"expiresAt",
44+
],
45+
optional: true,
46+
},
47+
description: {
48+
type: "string",
49+
label: "Description",
50+
description: "The alias description",
51+
optional: true,
52+
},
53+
},
54+
async run({ $ }) {
55+
try {
56+
const response = await this.tinyurl.createTinyURL({
57+
$,
58+
data: {
59+
url: this.url,
60+
domain: this.domain,
61+
alias: this.alias,
62+
tags: parseObject(this.tags)?.join(","),
63+
expires_at: this.expiresAt,
64+
description: this.description,
65+
},
66+
});
67+
$.export("$summary", `Created TinyURL: ${response.data.tiny_url}`);
68+
return response;
69+
} catch ({ response }) {
70+
throw new ConfigurationError(response?.data?.errors[0]);
71+
}
72+
},
73+
};
74+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import tinyurl from "../../tinyurl.app.mjs";
3+
4+
export default {
5+
key: "tinyurl-retrieve-link-analytics",
6+
name: "Retrieve Link Analytics",
7+
description: "Retrieves analytics for a specific TinyURL link, including total clicks, geographic breakdowns, and device types. [See the documentation]()",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
tinyurl,
12+
alert: {
13+
type: "alert",
14+
alertType: "info",
15+
content: "This action is only allowed for paid accounts.",
16+
},
17+
domain: {
18+
propDefinition: [
19+
tinyurl,
20+
"domain",
21+
],
22+
},
23+
urls: {
24+
propDefinition: [
25+
tinyurl,
26+
"urls",
27+
({ domain }) => ({
28+
domain,
29+
}),
30+
],
31+
},
32+
from: {
33+
type: "string",
34+
label: "From",
35+
description: "The start datetime of analitics report",
36+
},
37+
to: {
38+
type: "string",
39+
label: "To",
40+
description: "The end datetime of analitics report. Default is now",
41+
optional: true,
42+
},
43+
tag: {
44+
type: "string",
45+
label: "Tag",
46+
description: "Tag to get analytics for",
47+
optional: true,
48+
},
49+
},
50+
async run({ $ }) {
51+
try {
52+
const analytics = await this.tinyurl.retrieveAnalytics({
53+
$,
54+
params: {
55+
from: this.from,
56+
to: this.to,
57+
alias: this.urls,
58+
tag: this.tag,
59+
},
60+
});
61+
$.export("$summary", `Retrieved analytics for link ${this.urls}`);
62+
return analytics;
63+
} catch ({ response }) {
64+
throw new ConfigurationError(response?.data?.errors[0]);
65+
}
66+
},
67+
};
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import { parseObject } from "../../common/utils.mjs";
3+
import tinyurl from "../../tinyurl.app.mjs";
4+
5+
export default {
6+
key: "tinyurl-update-link-metadata",
7+
name: "Update Link Metadata",
8+
description: "Updates the metadata of an existing TinyURL. [See the documentation]()",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
tinyurl,
13+
domain: {
14+
propDefinition: [
15+
tinyurl,
16+
"domain",
17+
],
18+
},
19+
urls: {
20+
propDefinition: [
21+
tinyurl,
22+
"urls",
23+
({ domain }) => ({
24+
domain,
25+
}),
26+
],
27+
},
28+
newDomain: {
29+
type: "string",
30+
label: "New Domain",
31+
description: "The new domain you would like to switch to",
32+
optional: true,
33+
},
34+
newAlias: {
35+
type: "string",
36+
label: "New Alias",
37+
description: "The new alias you would like to switch to",
38+
optional: true,
39+
},
40+
newStats: {
41+
type: "boolean",
42+
label: "New Stats",
43+
description: "Turns on/off the collection of click analytics",
44+
optional: true,
45+
},
46+
newTags: {
47+
type: "string[]",
48+
label: "New Tags",
49+
description: "Tags you would like this TinyURL to have. Overwrites the existing tags. **Paid accounts only**",
50+
optional: true,
51+
},
52+
newExpiresAt: {
53+
type: "string",
54+
label: "New Expires At",
55+
description: "TinyURL expiration in ISO8601 format. If not set so TinyURL never expires, **Paid accounts only**",
56+
optional: true,
57+
},
58+
newDescription: {
59+
type: "string",
60+
label: "New Description",
61+
description: "The new description",
62+
optional: true,
63+
},
64+
},
65+
async run({ $ }) {
66+
try {
67+
const response = await this.tinyurl.updateTinyURLMetadata({
68+
$,
69+
data: {
70+
domain: this.domain,
71+
alias: this.urls,
72+
new_domain: this.newDomain,
73+
new_alias: this.newAlias,
74+
new_stats: this.newStats,
75+
new_tags: parseObject(this.newTags),
76+
new_expires_at: this.newExpiresAt,
77+
new_description: this.newDescription,
78+
},
79+
});
80+
$.export("$summary", `Updated TinyURL metadata for link: ${response.data.tiny_url}`);
81+
return response;
82+
} catch ({ response }) {
83+
throw new ConfigurationError(response?.data?.errors[0]);
84+
}
85+
},
86+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (Array.isArray(obj)) {
5+
return obj.map((item) => {
6+
if (typeof item === "string") {
7+
try {
8+
return JSON.parse(item);
9+
} catch (e) {
10+
return item;
11+
}
12+
}
13+
return item;
14+
});
15+
}
16+
if (typeof obj === "string") {
17+
try {
18+
return JSON.parse(obj);
19+
} catch (e) {
20+
return obj;
21+
}
22+
}
23+
return obj;
24+
};

components/tinyurl/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@pipedream/tinyurl",
3+
"version": "0.1.0",
4+
"description": "Pipedream TinyURL Components",
5+
"main": "tinyurl.app.mjs",
6+
"keywords": [
7+
"pipedream",
8+
"tinyurl"
9+
],
10+
"homepage": "https://pipedream.com/apps/tinyurl",
11+
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
12+
"publishConfig": {
13+
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
17+
}
18+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import tinyurl from "../../tinyurl.app.mjs";
3+
4+
export default {
5+
key: "tinyurl-new-link-created",
6+
name: "New Shortened URL Created",
7+
description: "Emit new event when a new shortened URL is created.",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
tinyurl,
13+
db: "$.service.db",
14+
timer: {
15+
type: "$.interface.timer",
16+
default: {
17+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
18+
},
19+
},
20+
},
21+
methods: {
22+
_getLastDate() {
23+
return this.db.get("lastDate") || 0;
24+
},
25+
_setLastDate(lastDate) {
26+
this.db.set("lastDate", lastDate);
27+
},
28+
async emitEvent(maxResults = false) {
29+
const lastDate = this._getLastDate();
30+
31+
const { data } = await this.tinyurl.listURLs({
32+
type: "available",
33+
params: {
34+
from: lastDate,
35+
},
36+
});
37+
38+
if (data.length) {
39+
if (maxResults && (data.length > maxResults)) {
40+
data.length = maxResults;
41+
}
42+
this._setLastDate(data[0].created_at);
43+
}
44+
45+
for (const item of data.reverse()) {
46+
this.$emit(item, {
47+
id: item.alias,
48+
summary: `New TinyURL: ${item.tiny_url}`,
49+
ts: Date.parse(item.created_at),
50+
});
51+
}
52+
},
53+
},
54+
hooks: {
55+
async deploy() {
56+
await this.emitEvent(25);
57+
},
58+
},
59+
async run() {
60+
await this.emitEvent();
61+
},
62+
};

0 commit comments

Comments
 (0)