Skip to content

Commit 82c3227

Browse files
authored
New Components - imagior (#14303)
* init * new components * pnpm-lock.yaml * add $ variable * improve prop names * return if no new templates * updates * add prefix to customize props labels
1 parent 7fa2913 commit 82c3227

File tree

5 files changed

+259
-59
lines changed

5 files changed

+259
-59
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import imagior from "../../imagior.app.mjs";
2+
3+
export default {
4+
key: "imagior-generate-image",
5+
name: "Generate Image",
6+
description: "Generates a unique and robust image using a provided template. [See the documentation](https://docs.imagior.com/api-reference/image-generate)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
imagior,
11+
templateId: {
12+
propDefinition: [
13+
imagior,
14+
"templateId",
15+
],
16+
reloadProps: true,
17+
},
18+
},
19+
async additionalProps() {
20+
const props = {};
21+
if (!this.templateId) {
22+
return props;
23+
}
24+
const { elements } = await this.imagior.listTemplateElements({
25+
templateId: this.templateId,
26+
});
27+
for (const [
28+
key,
29+
value,
30+
] of Object.entries(elements)) {
31+
props[`customize_${key}`] = {
32+
type: "boolean",
33+
label: `Customize ${key}`,
34+
optional: true,
35+
reloadProps: true,
36+
};
37+
if (this[`customize_${key}`]) {
38+
for (const elementKey of Object.keys(value)) {
39+
props[`${key}_${elementKey}`] = {
40+
type: "string",
41+
label: `${key} - ${elementKey}`,
42+
optional: true,
43+
};
44+
}
45+
}
46+
}
47+
return props;
48+
},
49+
async run({ $ }) {
50+
const elements = {};
51+
const { elements: allElements } = await this.imagior.listTemplateElements({
52+
$,
53+
templateId: this.templateId,
54+
});
55+
for (const [
56+
key,
57+
value,
58+
] of Object.entries(allElements)) {
59+
if (!this[`customize_${key}`]) {
60+
continue;
61+
}
62+
elements[key] = {};
63+
for (const elementKey of Object.keys(value)) {
64+
if (this[`${key}_${elementKey}`]) {
65+
elements[key][elementKey] = this[`${key}_${elementKey}`];
66+
}
67+
}
68+
}
69+
70+
const response = await this.imagior.generateImage({
71+
$,
72+
data: {
73+
templateId: this.templateId,
74+
elements,
75+
},
76+
});
77+
$.export("$summary", `${response.message}`);
78+
return response;
79+
},
80+
};

components/imagior/imagior.app.mjs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,62 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "imagior",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
templateId: {
8+
type: "string",
9+
label: "Template ID",
10+
description: "The unique ID of the design template to use",
11+
async options() {
12+
const templates = await this.listTemplates();
13+
return templates?.map(({
14+
id: value, name: label,
15+
}) => ({
16+
value,
17+
label,
18+
})) || [];
19+
},
20+
},
21+
},
522
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
23+
_baseUrl() {
24+
return "https://api.imagior.com";
25+
},
26+
async _makeRequest(opts = {}) {
27+
const {
28+
$ = this,
29+
path,
30+
...otherOpts
31+
} = opts;
32+
return axios($, {
33+
...otherOpts,
34+
url: `${this._baseUrl()}${path}`,
35+
headers: {
36+
Authorization: `Bearer ${this.$auth.api_key}`,
37+
},
38+
});
39+
},
40+
listTemplates(opts = {}) {
41+
return this._makeRequest({
42+
path: "/templates/all",
43+
...opts,
44+
});
45+
},
46+
listTemplateElements({
47+
templateId, ...opts
48+
}) {
49+
return this._makeRequest({
50+
path: `/templates/${templateId}/elements`,
51+
...opts,
52+
});
53+
},
54+
generateImage(opts = {}) {
55+
return this._makeRequest({
56+
method: "POST",
57+
path: "/image/generate",
58+
...opts,
59+
});
960
},
1061
},
11-
};
62+
};

components/imagior/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/imagior",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Imagior Components",
55
"main": "imagior.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.3"
1417
}
15-
}
18+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import imagior from "../../imagior.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
key: "imagior-new-template",
6+
name: "New Template Created",
7+
description: "Emit new event when a new template is created.",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
imagior,
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+
_getLastTs() {
23+
return this.db.get("lastTs") || 0;
24+
},
25+
_setLastTs(lastTs) {
26+
this.db.set("lastTs", lastTs);
27+
},
28+
generateMeta(template) {
29+
return {
30+
id: template.id,
31+
summary: `New Template: ${template.name}`,
32+
ts: Date.parse(template.createdAt),
33+
};
34+
},
35+
},
36+
async run() {
37+
const lastTs = this._getLastTs();
38+
39+
const templates = await this.imagior.listTemplates({
40+
params: {
41+
sort: "createdAt",
42+
order: "desc",
43+
},
44+
});
45+
46+
if (!templates?.length) {
47+
return;
48+
}
49+
50+
const newTemplates = templates.filter(({ createdAt }) => Date.parse(createdAt) >= lastTs);
51+
52+
if (!newTemplates?.length) {
53+
return;
54+
}
55+
56+
this._setLastTs(Date.parse(newTemplates[0].createdAt));
57+
58+
newTemplates.forEach((template) => {
59+
const meta = this.generateMeta(template);
60+
this.$emit(template, meta);
61+
});
62+
},
63+
};

pnpm-lock.yaml

Lines changed: 55 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)