Skip to content

Commit 2aae9a9

Browse files
committed
[Components] l2s #14107
Actions - Create Shortened URL
1 parent 2af634d commit 2aae9a9

File tree

4 files changed

+97
-134
lines changed

4 files changed

+97
-134
lines changed
Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,89 @@
1+
import { parseObject } from "../../common/utils.mjs";
12
import l2s from "../../l2s.app.mjs";
2-
import { axios } from "@pipedream/platform";
33

44
export default {
55
key: "l2s-create-shortened-url",
66
name: "Create Shortened URL",
77
description: "Generates a shortened URL utilizing L2S capabilities. [See the documentation](https://docs.l2s.is/)",
8-
version: "0.0.{{ts}}",
8+
version: "0.0.1",
99
type: "action",
1010
props: {
1111
l2s,
1212
url: {
13-
propDefinition: [
14-
l2s,
15-
"url",
16-
],
13+
type: "string",
14+
label: "URL",
15+
description: "The URL to be shortened",
1716
},
1817
customKey: {
19-
propDefinition: [
20-
l2s,
21-
"customKey",
22-
],
18+
type: "string",
19+
label: "Custom Key",
20+
description: "Custom key for the shortened URL",
21+
optional: true,
2322
},
2423
utmSource: {
25-
propDefinition: [
26-
l2s,
27-
"utmSource",
28-
],
24+
type: "string",
25+
label: "UTM Source",
26+
description: "UTM source parameter",
27+
optional: true,
2928
},
3029
utmMedium: {
31-
propDefinition: [
32-
l2s,
33-
"utmMedium",
34-
],
30+
type: "string",
31+
label: "UTM Medium",
32+
description: "UTM medium parameter",
33+
optional: true,
3534
},
3635
utmCampaign: {
37-
propDefinition: [
38-
l2s,
39-
"utmCampaign",
40-
],
36+
type: "string",
37+
label: "UTM Campaign",
38+
description: "UTM campaign parameter",
39+
optional: true,
4140
},
4241
utmTerm: {
43-
propDefinition: [
44-
l2s,
45-
"utmTerm",
46-
],
42+
type: "string",
43+
label: "UTM Term",
44+
description: "UTM term parameter",
45+
optional: true,
4746
},
4847
utmContent: {
49-
propDefinition: [
50-
l2s,
51-
"utmContent",
52-
],
48+
type: "string",
49+
label: "UTM Content",
50+
description: "UTM content parameter",
51+
optional: true,
5352
},
5453
title: {
55-
propDefinition: [
56-
l2s,
57-
"title",
58-
],
54+
type: "string",
55+
label: "Title",
56+
description: "Title for the shortened URL",
57+
optional: true,
5958
},
6059
tags: {
61-
propDefinition: [
62-
l2s,
63-
"tags",
64-
],
60+
type: "string[]",
61+
label: "Tags",
62+
description: "Tags associated with the URL",
63+
optional: true,
6564
},
6665
},
6766
async run({ $ }) {
68-
const response = await this.l2s.shortenUrl({
69-
url: this.url,
70-
customKey: this.customKey,
71-
utmSource: this.utmSource,
72-
utmMedium: this.utmMedium,
73-
utmCampaign: this.utmCampaign,
74-
utmTerm: this.utmTerm,
75-
utmContent: this.utmContent,
76-
title: this.title,
77-
tags: this.tags,
67+
const {
68+
l2s,
69+
tags,
70+
...data
71+
} = this;
72+
73+
if (tags) {
74+
data.tags = parseObject(tags);
75+
}
76+
77+
const { response: { data: response } } = await l2s.shortenUrl({
78+
$,
79+
data,
7880
});
7981

82+
const shortUrl = `https://l2s.is/${response.key}`;
8083
$.export("$summary", "URL shortened successfully");
81-
return response;
84+
return {
85+
short_url: shortUrl,
86+
...response,
87+
};
8288
},
8389
};

components/l2s/common/utils.mjs

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/l2s/l2s.app.mjs

Lines changed: 14 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,99 +3,29 @@ import { axios } from "@pipedream/platform";
33
export default {
44
type: "app",
55
app: "l2s",
6-
propDefinitions: {
7-
url: {
8-
type: "string",
9-
label: "URL",
10-
description: "The URL to be shortened",
11-
},
12-
customKey: {
13-
type: "string",
14-
label: "Custom Key",
15-
description: "Custom key for the shortened URL",
16-
optional: true,
17-
},
18-
utmSource: {
19-
type: "string",
20-
label: "UTM Source",
21-
description: "UTM source parameter",
22-
optional: true,
23-
},
24-
utmMedium: {
25-
type: "string",
26-
label: "UTM Medium",
27-
description: "UTM medium parameter",
28-
optional: true,
29-
},
30-
utmCampaign: {
31-
type: "string",
32-
label: "UTM Campaign",
33-
description: "UTM campaign parameter",
34-
optional: true,
35-
},
36-
utmTerm: {
37-
type: "string",
38-
label: "UTM Term",
39-
description: "UTM term parameter",
40-
optional: true,
41-
},
42-
utmContent: {
43-
type: "string",
44-
label: "UTM Content",
45-
description: "UTM content parameter",
46-
optional: true,
47-
},
48-
title: {
49-
type: "string",
50-
label: "Title",
51-
description: "Title for the shortened URL",
52-
optional: true,
53-
},
54-
tags: {
55-
type: "string[]",
56-
label: "Tags",
57-
description: "Tags associated with the URL",
58-
optional: true,
59-
},
60-
},
616
methods: {
62-
authKeys() {
63-
console.log(Object.keys(this.$auth));
64-
},
657
_baseUrl() {
668
return "https://api.l2s.is";
679
},
68-
async _makeRequest(opts = {}) {
69-
const {
70-
$ = this, method = "POST", path = "/url", headers, data,
71-
} = opts;
10+
_headers() {
11+
return {
12+
Authorization: `Bearer ${this.$auth.api_key}`,
13+
};
14+
},
15+
_makeRequest({
16+
$ = this, path, ...opts
17+
}) {
7218
return axios($, {
73-
method,
7419
url: this._baseUrl() + path,
75-
headers: {
76-
...headers,
77-
Authorization: `Bearer ${this.$auth.api_token}`,
78-
},
79-
data,
20+
headers: this._headers(),
21+
...opts,
8022
});
8123
},
82-
async shortenUrl(opts = {}) {
83-
const {
84-
url, customKey, utmSource, utmMedium, utmCampaign, utmTerm, utmContent, title, tags,
85-
} = opts;
86-
const data = {
87-
url,
88-
customKey,
89-
utmSource,
90-
utmMedium,
91-
utmCampaign,
92-
utmTerm,
93-
utmContent,
94-
title,
95-
tags,
96-
};
24+
shortenUrl(opts = {}) {
9725
return this._makeRequest({
98-
data,
26+
method: "POST",
27+
path: "/url",
28+
...opts,
9929
});
10030
},
10131
},

components/l2s/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/l2s",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream L2S Components",
55
"main": "l2s.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.1"
1417
}
1518
}

0 commit comments

Comments
 (0)