@pipedream/harvest 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/actions/create-invoice/create-invoice.mjs +116 -0
  2. package/actions/create-project/create-project.mjs +138 -0
  3. package/actions/create-task-assignment/create-task-assignment.mjs +77 -0
  4. package/actions/create-timesheet-entry/create-timesheet-entry.mjs +34 -38
  5. package/actions/create-user/create-user.mjs +109 -0
  6. package/actions/create-user-assignment/create-user-assignment.mjs +85 -0
  7. package/actions/delete-invoice/delete-invoice.mjs +38 -0
  8. package/actions/delete-project/delete-project.mjs +38 -0
  9. package/actions/delete-task-assignment/delete-task-assignment.mjs +45 -0
  10. package/actions/delete-time-entry/delete-time-entry.mjs +38 -0
  11. package/actions/delete-user/delete-user.mjs +39 -0
  12. package/actions/delete-user-assignment/delete-user-assignment.mjs +45 -0
  13. package/actions/get-invoice/get-invoice.mjs +39 -0
  14. package/actions/get-me/get-me.mjs +32 -0
  15. package/actions/get-project/get-project.mjs +39 -0
  16. package/actions/get-projects/get-projects.mjs +15 -12
  17. package/actions/get-task-assignment/get-task-assignment.mjs +46 -0
  18. package/actions/get-time-entry/get-time-entry.mjs +39 -0
  19. package/actions/get-time-report/get-time-report.mjs +80 -0
  20. package/actions/get-user/get-user.mjs +40 -0
  21. package/actions/get-user-assignment/get-user-assignment.mjs +46 -0
  22. package/actions/list-account-id-options/list-account-id-options.mjs +12 -3
  23. package/actions/list-clients/list-clients.mjs +57 -0
  24. package/actions/list-invoices/list-invoices.mjs +86 -0
  25. package/actions/list-task-assignments/list-task-assignments.mjs +66 -0
  26. package/actions/list-tasks/list-tasks.mjs +57 -0
  27. package/actions/list-time-entries/list-time-entries.mjs +150 -0
  28. package/actions/list-user-assignments/list-user-assignments.mjs +74 -0
  29. package/actions/list-users/list-users.mjs +57 -0
  30. package/actions/start-timer/start-timer.mjs +5 -7
  31. package/actions/stop-timer/stop-timer.mjs +5 -7
  32. package/actions/update-invoice/update-invoice.mjs +114 -0
  33. package/actions/update-project/update-project.mjs +140 -0
  34. package/actions/update-task-assignment/update-task-assignment.mjs +77 -0
  35. package/actions/update-time-entry/update-time-entry.mjs +92 -0
  36. package/actions/update-user/update-user.mjs +106 -0
  37. package/actions/update-user-assignment/update-user-assignment.mjs +84 -0
  38. package/common/constants.mjs +51 -1
  39. package/common/utils.mjs +50 -1
  40. package/harvest.app.mjs +683 -110
  41. package/package.json +1 -1
  42. package/sources/new-invoice-entry/new-invoice-entry.mjs +1 -1
  43. package/sources/new-timesheet-entry/new-timesheet-entry.mjs +1 -1
package/common/utils.mjs CHANGED
@@ -1,3 +1,52 @@
1
+ import constants from "./constants.mjs";
2
+
3
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
4
+
5
+ // Caps how long a single Retry-After wait can be, so a large or misbehaving header value
6
+ // (e.g. an intermediate proxy or a monthly-limit response) can't block execution for
7
+ // unpredictably long stretches across maxRetries attempts.
8
+ const MAX_RETRY_AFTER_MS = 30000;
9
+
10
+ /* Retries a single request on 429, honoring the response's Retry-After header (seconds)
11
+ instead of guessing a backoff. Falls through unchanged for any other status/error. */
12
+ const withRetryAfter = async (fn, maxRetries = 3) => {
13
+ for (let attempt = 0; ; attempt++) {
14
+ try {
15
+ return await fn();
16
+ } catch (err) {
17
+ const status = err?.response?.status ?? err?.status;
18
+ if (status !== 429 || attempt >= maxRetries) {
19
+ throw err;
20
+ }
21
+ const retryAfterSeconds = Number(err?.response?.headers?.["retry-after"]);
22
+ const delayMs = Number.isFinite(retryAfterSeconds) && retryAfterSeconds > 0
23
+ ? Math.min(retryAfterSeconds * 1000, MAX_RETRY_AFTER_MS)
24
+ : constants.RATE_LIMIT_BATCH_DELAY_MS;
25
+ await sleep(delayMs);
26
+ }
27
+ }
28
+ };
29
+
30
+ /* Runs fn over items in small concurrent batches, pausing between batches, so callers
31
+ don't blow through Harvest's rate limit with an unbounded Promise.all. Fails fast: the
32
+ first rejection (after any retries inside fn) propagates immediately and no partial
33
+ results are returned. */
34
+ const mapWithRateLimit = async (items, fn, {
35
+ batchSize = constants.RATE_LIMIT_BATCH_SIZE,
36
+ delayMs = constants.RATE_LIMIT_BATCH_DELAY_MS,
37
+ } = {}) => {
38
+ const results = [];
39
+ for (let i = 0; i < items.length; i += batchSize) {
40
+ const batch = items.slice(i, i + batchSize);
41
+ const batchResults = await Promise.all(batch.map(fn));
42
+ results.push(...batchResults);
43
+ if (i + batchSize < items.length) {
44
+ await sleep(delayMs);
45
+ }
46
+ }
47
+ return results;
48
+ };
49
+
1
50
  const removeNullEntries = (obj) =>
2
51
  obj && Object.entries(obj).reduce((acc, [
3
52
  key,
@@ -62,5 +111,5 @@ const isValidTime = (timeString) => {
62
111
  };
63
112
 
64
113
  export {
65
- removeNullEntries, isValidDate, isValidTime,
114
+ removeNullEntries, isValidDate, isValidTime, mapWithRateLimit, withRetryAfter,
66
115
  };