Skip to content

Commit e0e32f5

Browse files
committed
new components
1 parent acbab99 commit e0e32f5

File tree

4 files changed

+119
-61
lines changed

4 files changed

+119
-61
lines changed

components/imagior/actions/generate-image/generate-image.mjs

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,77 @@ import imagior from "../../imagior.app.mjs";
33
export default {
44
key: "imagior-generate-image",
55
name: "Generate Image",
6-
description: "Generates a unique and robust image using a provided template.",
7-
version: "0.0.{{ts}}",
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",
88
type: "action",
99
props: {
1010
imagior,
11-
template_id: {
11+
templateId: {
1212
propDefinition: [
1313
imagior,
14-
"template_id",
14+
"templateId",
1515
],
16-
},
17-
image_parameters: {
18-
propDefinition: [
19-
imagior,
20-
"image_parameters",
21-
],
22-
optional: true,
16+
reloadProps: true,
2317
},
2418
},
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[elementKey] = {
40+
type: "string",
41+
label: `${elementKey}`,
42+
optional: true,
43+
};
44+
}
45+
}
46+
}
47+
return props;
48+
},
2549
async run({ $ }) {
50+
const elements = {};
51+
const { elements: allElements } = await this.imagior.listTemplateElements({
52+
templateId: this.templateId,
53+
});
54+
for (const [
55+
key,
56+
value,
57+
] of Object.entries(allElements)) {
58+
if (!this[`customize_${key}`]) {
59+
continue;
60+
}
61+
elements[key] = {};
62+
for (const elementKey of Object.keys(value)) {
63+
if (this[elementKey]) {
64+
elements[key][elementKey] = this[elementKey];
65+
}
66+
}
67+
}
68+
2669
const response = await this.imagior.generateImage({
27-
template_id: this.template_id,
28-
image_parameters: this.image_parameters,
70+
$,
71+
data: {
72+
templateId: this.templateId,
73+
elements,
74+
},
2975
});
30-
$.export("$summary", "Successfully generated image");
76+
$.export("$summary", `${response.message}`);
3177
return response;
3278
},
3379
};

components/imagior/imagior.app.mjs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ export default {
44
type: "app",
55
app: "imagior",
66
propDefinitions: {
7-
template_id: {
7+
templateId: {
88
type: "string",
99
label: "Template ID",
10-
description: "The unique ID of the design template you created",
11-
required: true,
12-
},
13-
image_parameters: {
14-
type: "object",
15-
label: "Image Parameters",
16-
description: "Optional parameters to customize the appearance of the image",
17-
optional: true,
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+
},
1820
},
1921
},
2022
methods: {
@@ -23,35 +25,37 @@ export default {
2325
},
2426
async _makeRequest(opts = {}) {
2527
const {
26-
$ = this, method = "GET", path, headers, ...otherOpts
28+
$ = this,
29+
path,
30+
...otherOpts
2731
} = opts;
2832
return axios($, {
2933
...otherOpts,
30-
method,
31-
url: this._baseUrl() + path,
34+
url: `${this._baseUrl()}${path}`,
3235
headers: {
33-
...headers,
34-
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
36+
Authorization: `Bearer ${this.$auth.api_key}`,
3537
},
3638
});
3739
},
38-
async createTemplate(opts = {}) {
40+
listTemplates(opts = {}) {
3941
return this._makeRequest({
40-
method: "POST",
41-
path: "/templates",
42+
path: "/template/all",
4243
...opts,
4344
});
4445
},
45-
async generateImage({
46-
template_id, image_parameters,
46+
listTemplateElements({
47+
templateId, ...opts
4748
}) {
49+
return this._makeRequest({
50+
path: `/template/${templateId}/elements`,
51+
...opts,
52+
});
53+
},
54+
generateImage(opts = {}) {
4855
return this._makeRequest({
4956
method: "POST",
5057
path: "/image/generate",
51-
data: {
52-
templateId: template_id,
53-
elements: image_parameters,
54-
},
58+
...opts,
5559
});
5660
},
5761
},

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: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import imagior from "../../imagior.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
23

34
export default {
45
key: "imagior-new-template",
56
name: "New Template Created",
6-
description: "Emits a new event when a new template is created.",
7+
description: "Emit new event when a new template is created.",
78
version: "0.0.1",
89
type: "source",
910
dedupe: "unique",
@@ -13,42 +14,46 @@ export default {
1314
timer: {
1415
type: "$.interface.timer",
1516
default: {
16-
intervalSeconds: 60 * 15, // 15 minutes
17+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
1718
},
1819
},
1920
},
2021
methods: {
21-
_getPrevTemplateId() {
22-
return this.db.get("prevTemplateId");
22+
_getLastTs() {
23+
return this.db.get("lastTs") || 0;
2324
},
24-
_setPrevTemplateId(id) {
25-
this.db.set("prevTemplateId", id);
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+
};
2634
},
2735
},
2836
async run() {
29-
const { elements: templates } = await this.imagior._makeRequest({
30-
path: "/templates/all",
37+
const lastTs = this._getLastTs();
38+
39+
const templates = await this.imagior.listTemplates({
40+
params: {
41+
sort: "createdAt",
42+
order: "desc",
43+
},
3144
});
3245

33-
if (!templates.length) {
34-
console.log("No templates found");
46+
if (!templates?.length) {
3547
return;
3648
}
3749

38-
const sortedTemplates = templates.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt));
39-
const latestTemplate = sortedTemplates[0];
40-
const prevTemplateId = this._getPrevTemplateId();
50+
const newTemplates = templates.filter(({ createdAt }) => Date.parse(createdAt) >= lastTs);
4151

42-
if (prevTemplateId && latestTemplate.id === prevTemplateId) {
43-
console.log("No new templates found");
44-
return;
45-
}
52+
this._setLastTs(Date.parse(newTemplates[0].createdAt));
4653

47-
this._setPrevTemplateId(latestTemplate.id);
48-
this.$emit(latestTemplate, {
49-
id: latestTemplate.id,
50-
summary: `New template: ${latestTemplate.name}`,
51-
ts: Date.parse(latestTemplate.createdAt),
54+
newTemplates.forEach((template) => {
55+
const meta = this.generateMeta(template);
56+
this.$emit(template, meta);
5257
});
5358
},
5459
};

0 commit comments

Comments
 (0)