Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import changesPage from "../../changes_page.app.mjs";

export default {
key: "changes_page-get-latest-post",
name: "Get Latest Post",
description: "Get the latest post from Changes Page. [See the documentation](https://docs.changes.page/docs/api/page#get-latest-post)",
version: "0.0.1",
type: "action",
props: {
changesPage,
},
async run({ $ }) {
const post = await this.changesPage.getLatestPost({
$,
});
$.export("$summary", "Successfully retrieved latest post");
return post;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import changesPage from "../../changes_page.app.mjs";
import { ConfigurationError } from "@pipedream/platform";

export default {
key: "changes_page-get-pinned-post",
name: "Get Pinned Post",
description: "Get the pinned post from Changes Page. [See the documentation](https://docs.changes.page/docs/api/page#get-pinned-post)",
version: "0.0.1",
type: "action",
props: {
changesPage,
},
async run({ $ }) {
try {
const post = await this.changesPage.getPinnedPost({
$,
});
$.export("$summary", "Successfully retrieved pinned post");
return post;
} catch (error) {
if (error.response.status === 404) {
throw new ConfigurationError("No pinned post found");
}
throw error;
}
},
};
26 changes: 26 additions & 0 deletions components/changes_page/actions/get-post/get-post.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import changesPage from "../../changes_page.app.mjs";

export default {
key: "changes_page-get-post",
name: "Get Post",
description: "Get a post by ID from Changes Page. [See the documentation](https://docs.changes.page/docs/api/page#get-a-post-by-id)",
version: "0.0.1",
type: "action",
props: {
changesPage,
postId: {
propDefinition: [
changesPage,
"postId",
],
},
},
async run({ $ }) {
const post = await this.changesPage.getPost({
$,
postId: this.postId,
});
$.export("$summary", `Successfully retrieved post ${this.postId}`);
return post;
},
};
26 changes: 26 additions & 0 deletions components/changes_page/actions/list-posts/list-posts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import changesPage from "../../changes_page.app.mjs";

export default {
key: "changes_page-list-posts",
name: "List Posts",
description: "Retrieve a list of posts from Changes Page. [See the documentation](https://docs.changes.page/docs/api/page#get-all-posts)",
version: "0.0.1",
type: "action",
props: {
changesPage,
maxResults: {
type: "integer",
label: "Max Results",
description: "The maximum number of posts to retrieve",
default: 100,
},
},
async run({ $ }) {
const posts = await this.changesPage.getPaginatedResources({
fn: this.changesPage.listPosts,
max: this.maxResults,
});
$.export("$summary", `Successfully retrieved ${posts.length} posts`);
return posts;
},
};
91 changes: 87 additions & 4 deletions components/changes_page/changes_page.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,94 @@
import { axios } from "@pipedream/platform";
const DEFAULT_LIMIT = 50;

export default {
type: "app",
app: "changes_page",
propDefinitions: {},
propDefinitions: {
postId: {
type: "string",
label: "Post ID",
description: "The ID of the post to retrieve",
async options({ page }) {
const posts = await this.listPosts({
params: {
offset: page * DEFAULT_LIMIT,
},
});
return posts.map((post) => ({
label: post.title,
value: post.id,
}));
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return `https://${this.$auth.subdomain}.changes.page`;
},
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
url: `${this._baseUrl()}${path}`,
...opts,
});
},
getPost({
postId, ...opts
}) {
return this._makeRequest({
path: `/changes.json/${postId}`,
...opts,
});
},
getLatestPost(opts = {}) {
return this._makeRequest({
path: "/latest.json",
...opts,
});
},
getPinnedPost(opts = {}) {
return this._makeRequest({
path: "/pinned.json",
...opts,
});
},
listPosts(opts = {}) {
return this._makeRequest({
path: "/changes.json",
...opts,
});
},
async *paginate({
fn, params = {}, max,
}) {
params = {
...params,
offset: 0,
};
let total, count = 0;
do {
const response = await fn({
params,
});
for (const item of response) {
yield item;
if (max && ++count >= max) {
return;
}
}
total = response.length;
params.offset += DEFAULT_LIMIT;
} while (total === DEFAULT_LIMIT);
},
async getPaginatedResources(opts = {}) {
const resources = this.paginate(opts);
const items = [];
for await (const item of resources) {
items.push(item);
}
return items;
},
},
};
7 changes: 5 additions & 2 deletions components/changes_page/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/changes_page",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream changes.page Components",
"main": "changes_page.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.1.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import changesPage from "../../changes_page.app.mjs";
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";

export default {
key: "changes_page-new-post-created",
name: "New Post Created",
description: "Emit new event when a new post is created. [See the documentation](https://docs.changes.page/docs/api/page#get-all-posts)",
version: "0.0.1",
type: "source",
dedupe: "unique",
props: {
changesPage,
db: "$.service.db",
timer: {
type: "$.interface.timer",
default: {
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},
},
methods: {
_getLastTs() {
return this.db.get("lastTs") || 0;
},
_setLastTs(ts) {
this.db.set("lastTs", ts);
},
generateMeta(post) {
return {
id: post.id,
summary: post.title,
ts: Date.parse(post.created_at),
};
},
async processEvent(max) {
const lastTs = this._getLastTs();
const results = this.changesPage.paginate({
fn: this.changesPage.listPosts,
max,
});
const posts = [];
for await (const post of results) {
if (Date.parse(post.created_at) > lastTs) {
posts.push(post);
} else {
break;
}
}
if (!posts.length) {
return;
}
this._setLastTs(Date.parse(posts[0].created_at));
posts.reverse().forEach((post) => {
const meta = this.generateMeta(post);
this.$emit(post, meta);
});
},
},
hooks: {
async deploy() {
await this.processEvent(25);
},
},
async run() {
await this.processEvent();
},
};
Loading
Loading