|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | + |
1 | 3 | export default { |
2 | 4 | type: "app", |
3 | 5 | app: "channable", |
4 | | - propDefinitions: {}, |
| 6 | + propDefinitions: { |
| 7 | + stockUpdateId: { |
| 8 | + type: "string", |
| 9 | + label: "Stock Update ID", |
| 10 | + description: "The ID of a stock update", |
| 11 | + async options({ page }) { |
| 12 | + const { offers } = await this.listStockUpdates({ |
| 13 | + params: { |
| 14 | + limit: 100, |
| 15 | + offset: page * 100, |
| 16 | + }, |
| 17 | + }); |
| 18 | + return offers?.map((offer) => ({ |
| 19 | + label: offer.label, |
| 20 | + value: offer.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://api.channable.com/v1"; |
| 28 | + }, |
| 29 | + _companyId() { |
| 30 | + return this.$auth.company_id; |
| 31 | + }, |
| 32 | + _projectId() { |
| 33 | + return this.$auth.project_id; |
| 34 | + }, |
| 35 | + _makeRequest({ |
| 36 | + $ = this, path, ...opts |
| 37 | + }) { |
| 38 | + return axios($, { |
| 39 | + url: `${this._baseUrl()}${path}`, |
| 40 | + headers: { |
| 41 | + Authorization: `Bearer ${this.$auth.api_token}`, |
| 42 | + }, |
| 43 | + ...opts, |
| 44 | + }); |
| 45 | + }, |
| 46 | + listStockUpdates(opts = {}) { |
| 47 | + return this._makeRequest({ |
| 48 | + path: `/companies/${this._companyId()}/projects/${this._projectId()}/offers`, |
| 49 | + ...opts, |
| 50 | + }); |
| 51 | + }, |
| 52 | + updateStockUpdate(opts = {}) { |
| 53 | + return this._makeRequest({ |
| 54 | + path: `/companies/${this._companyId()}/projects/${this._projectId()}/stock_updates`, |
| 55 | + method: "POST", |
| 56 | + ...opts, |
| 57 | + }); |
| 58 | + }, |
| 59 | + async *paginate({ |
| 60 | + fn, args, resourceKey, max, |
| 61 | + }) { |
| 62 | + args = { |
| 63 | + ...args, |
| 64 | + params: { |
| 65 | + ...args?.params, |
| 66 | + limit: 100, |
| 67 | + offset: 0, |
| 68 | + }, |
| 69 | + }; |
| 70 | + let total, count = 0; |
| 71 | + do { |
| 72 | + const response = await fn(args); |
| 73 | + const items = response[resourceKey]; |
| 74 | + total = items?.length; |
| 75 | + if (!total) { |
| 76 | + return; |
| 77 | + } |
| 78 | + for (const item of items) { |
| 79 | + yield item; |
| 80 | + if (max && ++count >= max) { |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + args.params.offset += args.params.limit; |
| 85 | + } while (total === args.params.limit); |
| 86 | + }, |
| 87 | + async getPaginatedResources(opts) { |
| 88 | + const resources = []; |
| 89 | + for await (const resource of this.paginate(opts)) { |
| 90 | + resources.push(resource); |
| 91 | + } |
| 92 | + return resources; |
9 | 93 | }, |
10 | 94 | }, |
11 | 95 | }; |
0 commit comments