|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | +import constants from "./common/constants.mjs"; |
| 3 | + |
1 | 4 | export default { |
2 | 5 | type: "app", |
3 | 6 | app: "oto", |
4 | | - propDefinitions: {}, |
| 7 | + propDefinitions: { |
| 8 | + brandName: { |
| 9 | + type: "string", |
| 10 | + label: "Brand Name", |
| 11 | + description: "The brand name associated with the shipment", |
| 12 | + optional: true, |
| 13 | + async options() { |
| 14 | + const { clientStores } = await this.listBrands(); |
| 15 | + return clientStores?.map(({ storeName }) => storeName) || []; |
| 16 | + }, |
| 17 | + }, |
| 18 | + status: { |
| 19 | + type: "string", |
| 20 | + label: "Status", |
| 21 | + description: "The status of an order", |
| 22 | + options: constants.STATUSES, |
| 23 | + optional: true, |
| 24 | + }, |
| 25 | + }, |
5 | 26 | methods: { |
6 | | - // this.$auth contains connected account data |
7 | | - authKeys() { |
8 | | - console.log(Object.keys(this.$auth)); |
| 27 | + _baseUrl() { |
| 28 | + return `${this.$auth.api_url}/rest/v2`; |
| 29 | + }, |
| 30 | + _makeRequest({ |
| 31 | + $ = this, path, ...otherOpts |
| 32 | + }) { |
| 33 | + return axios($, { |
| 34 | + ...otherOpts, |
| 35 | + url: `${this._baseUrl()}${path}`, |
| 36 | + headers: { |
| 37 | + Authorization: `Bearer ${this.$auth.oauth_access_token}`, |
| 38 | + }, |
| 39 | + }); |
| 40 | + }, |
| 41 | + createWebhook(opts = {}) { |
| 42 | + return this._makeRequest({ |
| 43 | + method: "POST", |
| 44 | + path: "/webhook", |
| 45 | + ...opts, |
| 46 | + }); |
| 47 | + }, |
| 48 | + deleteWebhook(opts = {}) { |
| 49 | + return this._makeRequest({ |
| 50 | + method: "DELETE", |
| 51 | + path: "/webhook", |
| 52 | + ...opts, |
| 53 | + }); |
| 54 | + }, |
| 55 | + listOrders(opts = {}) { |
| 56 | + return this._makeRequest({ |
| 57 | + path: "/orders", |
| 58 | + ...opts, |
| 59 | + }); |
| 60 | + }, |
| 61 | + listBrands(opts = {}) { |
| 62 | + return this._makeRequest({ |
| 63 | + path: "/getBrandList", |
| 64 | + ...opts, |
| 65 | + }); |
| 66 | + }, |
| 67 | + createProduct(opts = {}) { |
| 68 | + return this._makeRequest({ |
| 69 | + method: "POST", |
| 70 | + path: "/createProduct", |
| 71 | + ...opts, |
| 72 | + }); |
| 73 | + }, |
| 74 | + trackShipment(opts = {}) { |
| 75 | + return this._makeRequest({ |
| 76 | + method: "POST", |
| 77 | + path: "/trackShipment", |
| 78 | + ...opts, |
| 79 | + }); |
| 80 | + }, |
| 81 | + async *paginate({ |
| 82 | + fn, args, resourceKey, max, |
| 83 | + }) { |
| 84 | + args = { |
| 85 | + ...args, |
| 86 | + params: { |
| 87 | + ...args?.params, |
| 88 | + perPage: constants.DEFAULT_LIMIT, |
| 89 | + page: 1, |
| 90 | + }, |
| 91 | + }; |
| 92 | + let total, count = 0; |
| 93 | + do { |
| 94 | + const response = await fn(args); |
| 95 | + const items = response[resourceKey]; |
| 96 | + for (const item of items) { |
| 97 | + yield item; |
| 98 | + if (max && ++count >= max) { |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + total = items?.length; |
| 103 | + args.params.page++; |
| 104 | + } while (total === args.params.perPage); |
9 | 105 | }, |
10 | 106 | }, |
11 | 107 | }; |
0 commit comments