|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | +const DEFAULT_LIMIT = 50; |
| 3 | + |
1 | 4 | export default { |
2 | 5 | type: "app", |
3 | 6 | app: "changes_page", |
4 | | - propDefinitions: {}, |
| 7 | + propDefinitions: { |
| 8 | + postId: { |
| 9 | + type: "string", |
| 10 | + label: "Post ID", |
| 11 | + description: "The ID of the post to retrieve", |
| 12 | + async options({ page }) { |
| 13 | + const posts = await this.listPosts({ |
| 14 | + params: { |
| 15 | + offset: page * DEFAULT_LIMIT, |
| 16 | + }, |
| 17 | + }); |
| 18 | + return posts.map((post) => ({ |
| 19 | + label: post.title, |
| 20 | + value: post.id, |
| 21 | + })); |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
5 | 25 | methods: { |
6 | | - // this.$auth contains connected account data |
7 | | - authKeys() { |
8 | | - console.log(Object.keys(this.$auth)); |
| 26 | + _baseUrl() { |
| 27 | + return `https://${this.$auth.subdomain}.changes.page`; |
| 28 | + }, |
| 29 | + _makeRequest({ |
| 30 | + $ = this, path, ...opts |
| 31 | + }) { |
| 32 | + return axios($, { |
| 33 | + url: `${this._baseUrl()}${path}`, |
| 34 | + ...opts, |
| 35 | + }); |
| 36 | + }, |
| 37 | + getPost({ |
| 38 | + postId, ...opts |
| 39 | + }) { |
| 40 | + return this._makeRequest({ |
| 41 | + path: `/changes.json/${postId}`, |
| 42 | + ...opts, |
| 43 | + }); |
| 44 | + }, |
| 45 | + getLatestPost(opts = {}) { |
| 46 | + return this._makeRequest({ |
| 47 | + path: "/latest.json", |
| 48 | + ...opts, |
| 49 | + }); |
| 50 | + }, |
| 51 | + getPinnedPost(opts = {}) { |
| 52 | + return this._makeRequest({ |
| 53 | + path: "/pinned.json", |
| 54 | + ...opts, |
| 55 | + }); |
| 56 | + }, |
| 57 | + listPosts(opts = {}) { |
| 58 | + return this._makeRequest({ |
| 59 | + path: "/changes.json", |
| 60 | + ...opts, |
| 61 | + }); |
| 62 | + }, |
| 63 | + async *paginate({ |
| 64 | + fn, params = {}, max, |
| 65 | + }) { |
| 66 | + params = { |
| 67 | + ...params, |
| 68 | + offset: 0, |
| 69 | + }; |
| 70 | + let total, count = 0; |
| 71 | + do { |
| 72 | + const response = await fn({ |
| 73 | + params, |
| 74 | + }); |
| 75 | + if (!response || response.length === 0) { |
| 76 | + break; |
| 77 | + } |
| 78 | + for (const item of response) { |
| 79 | + yield item; |
| 80 | + if (max && ++count >= max) { |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + total = response.length; |
| 85 | + params.offset += DEFAULT_LIMIT; |
| 86 | + } while (total === DEFAULT_LIMIT); |
| 87 | + }, |
| 88 | + async getPaginatedResources(opts = {}) { |
| 89 | + const resources = this.paginate(opts); |
| 90 | + const items = []; |
| 91 | + for await (const item of resources) { |
| 92 | + items.push(item); |
| 93 | + } |
| 94 | + return items; |
9 | 95 | }, |
10 | 96 | }, |
11 | 97 | }; |
0 commit comments