|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | + |
1 | 3 | export default { |
2 | 4 | type: "app", |
3 | 5 | app: "growsurf", |
4 | | - propDefinitions: {}, |
| 6 | + propDefinitions: { |
| 7 | + campaignId: { |
| 8 | + type: "string", |
| 9 | + label: "Campaign ID", |
| 10 | + description: "The ID of a campaign", |
| 11 | + async options() { |
| 12 | + const { campaigns } = await this.listCampaigns(); |
| 13 | + return campaigns?.map((campaign) => ({ |
| 14 | + label: campaign.name, |
| 15 | + value: campaign.id, |
| 16 | + })) || []; |
| 17 | + }, |
| 18 | + }, |
| 19 | + }, |
5 | 20 | methods: { |
6 | | - // this.$auth contains connected account data |
7 | | - authKeys() { |
8 | | - console.log(Object.keys(this.$auth)); |
| 21 | + _baseUrl() { |
| 22 | + return "https://api.growsurf.com/v2"; |
| 23 | + }, |
| 24 | + _makeRequest({ |
| 25 | + $ = this, path, ...opts |
| 26 | + }) { |
| 27 | + return axios($, { |
| 28 | + url: `${this._baseUrl()}${path}`, |
| 29 | + headers: { |
| 30 | + "Authorization": `Bearer ${this.$auth.api_key}`, |
| 31 | + }, |
| 32 | + ...opts, |
| 33 | + }); |
| 34 | + }, |
| 35 | + listCampaigns(opts = {}) { |
| 36 | + return this._makeRequest({ |
| 37 | + path: "/campaigns", |
| 38 | + ...opts, |
| 39 | + }); |
| 40 | + }, |
| 41 | + listParticipants({ |
| 42 | + campaignId, ...opts |
| 43 | + }) { |
| 44 | + return this._makeRequest({ |
| 45 | + path: `/campaign/${campaignId}/participants`, |
| 46 | + ...opts, |
| 47 | + }); |
| 48 | + }, |
| 49 | + listReferrals({ |
| 50 | + campaignId, ...opts |
| 51 | + }) { |
| 52 | + return this._makeRequest({ |
| 53 | + path: `/campaign/${campaignId}/referrals`, |
| 54 | + ...opts, |
| 55 | + }); |
| 56 | + }, |
| 57 | + async *paginate({ |
| 58 | + fn, args, resourceKey, max, |
| 59 | + }) { |
| 60 | + args.params = args.params || {}; |
| 61 | + let hasMore = true; |
| 62 | + let count = 0; |
| 63 | + do { |
| 64 | + const response = await fn(args); |
| 65 | + const items = response[resourceKey]; |
| 66 | + if (!items?.length) { |
| 67 | + return; |
| 68 | + } |
| 69 | + for (const item of items) { |
| 70 | + yield item; |
| 71 | + if (max && ++count >= max) { |
| 72 | + return; |
| 73 | + } |
| 74 | + } |
| 75 | + hasMore = response.nextId; |
| 76 | + args.params.nextId = response.nextId; |
| 77 | + } while (hasMore); |
9 | 78 | }, |
10 | 79 | }, |
11 | 80 | }; |
0 commit comments