@friggframework/core 2.0.0--canary.657.b14221f.0 → 2.0.0--canary.658.93c8e07.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.
@@ -1,6 +1,27 @@
1
1
  declare module "@friggframework/integrations" {
2
2
  import { Delegate, IFriggDelegate } from "@friggframework/core";
3
3
 
4
+ export type IntegrationMessageAction =
5
+ | { type: "RETRY_WHEN_READY" }
6
+ | { type: "LINK"; label: string; url: string };
7
+
8
+ /**
9
+ * A stored message. The framework stores the keys of an item as given, so a
10
+ * caller can add its own. A rate limit adds `code: "RATE_LIMITED"`,
11
+ * `module`, `reason`, `retryAt` (ISO 8601) and `actions`.
12
+ */
13
+ export interface IntegrationMessage {
14
+ title: string;
15
+ message: string;
16
+ timestamp: number;
17
+ code?: string;
18
+ module?: string;
19
+ reason?: string;
20
+ retryAt?: string;
21
+ actions?: IntegrationMessageAction[];
22
+ [key: string]: unknown;
23
+ }
24
+
4
25
  export interface Integration {
5
26
  entities: any[];
6
27
  userId: string;
@@ -8,10 +29,10 @@ declare module "@friggframework/integrations" {
8
29
  config: any;
9
30
  version: string;
10
31
  messages: {
11
- errors: [];
12
- warnings: [];
13
- info: [];
14
- logs: [];
32
+ errors: IntegrationMessage[];
33
+ warnings: IntegrationMessage[];
34
+ info: IntegrationMessage[];
35
+ logs: IntegrationMessage[];
15
36
  };
16
37
  }
17
38
 
@@ -1,6 +1,6 @@
1
1
  declare module "@friggframework/module-plugin" {
2
2
  import { Delegate, IFriggDelegate } from "@friggframework/core";
3
- import type { RateLimitHint } from "@friggframework/errors";
3
+ import type { RateLimitError, RateLimitHint } from "@friggframework/errors";
4
4
 
5
5
  export interface Credential {
6
6
  id?: string;
@@ -75,6 +75,8 @@ declare module "@friggframework/module-plugin" {
75
75
  static requestTimeoutMs?: number;
76
76
 
77
77
  DLGT_INVALID_AUTH: string;
78
+ /** Notified with the `RateLimitError` right before a wait that is too long throws. */
79
+ DLGT_RATE_LIMITED: string;
78
80
  requestTimeoutMs: number;
79
81
  backOff: number[];
80
82
  fetch: any;
@@ -94,6 +96,12 @@ declare module "@friggframework/module-plugin" {
94
96
  parseBody(response: any): Promise<any>;
95
97
  refreshAuth(): Promise<any>;
96
98
  _adoptNewerCredential(): Promise<boolean>;
99
+ /**
100
+ * Tells the integration about a `RateLimitError` the module throws itself.
101
+ * The Requester calls it for its own errors; a module built on a vendor SDK
102
+ * calls it before its own throw.
103
+ */
104
+ _notifyRateLimited(rateLimitError: RateLimitError): Promise<void>;
97
105
 
98
106
  delegate: any;
99
107
  delegateTypes: any[];
@@ -111,6 +119,7 @@ declare module "@friggframework/module-plugin" {
111
119
  isRefreshable: boolean;
112
120
  refreshCount: number;
113
121
  DLGT_INVALID_AUTH: string;
122
+ DLGT_RATE_LIMITED: string;
114
123
  fetch: any;
115
124
 
116
125
  parseBody(response: any): Promise<any>;
@@ -126,6 +135,7 @@ declare module "@friggframework/module-plugin" {
126
135
  _delete(options: RequestOptions): Promise<any>;
127
136
  refreshAuth(): Promise<any>;
128
137
  _adoptNewerCredential(): Promise<boolean>;
138
+ _notifyRateLimited(rateLimitError: RateLimitError): Promise<void>;
129
139
  }
130
140
 
131
141
  type RequestOptions = {
@@ -1,79 +0,0 @@
1
- const MAX_DEFERRALS = 10;
2
- const MAX_DEFERRED_MS = 24 * 60 * 60 * 1000;
3
- const MAX_DELAY_SECONDS = 900;
4
- const MAX_VISIBILITY_TIMEOUT_SECONDS = 43_200;
5
- const MAX_DEFERRALS_ENV = 'FRIGG_QUEUE_MAX_DEFERRALS';
6
- const MAX_DEFERRED_MS_ENV = 'FRIGG_QUEUE_MAX_DEFERRED_MS';
7
-
8
- const toPositiveInteger = (value) => {
9
- const number = Number(value);
10
- return Number.isInteger(number) && number > 0 ? number : undefined;
11
- };
12
-
13
- /**
14
- * @typedef {Object} DeferralLimits
15
- * @property {number} maxDeferrals Times one message may be put back for a rate limit.
16
- * @property {number} maxDeferredMs Longest total time from the first deferral to the last retry time.
17
- */
18
-
19
- /**
20
- * @param {Object} [env=process.env]
21
- * @returns {DeferralLimits}
22
- */
23
- const readDeferralLimits = (env = process.env) => ({
24
- maxDeferrals: toPositiveInteger(env[MAX_DEFERRALS_ENV]) ?? MAX_DEFERRALS,
25
- maxDeferredMs:
26
- toPositiveInteger(env[MAX_DEFERRED_MS_ENV]) ?? MAX_DEFERRED_MS,
27
- });
28
-
29
- /**
30
- * The deferral counters a message body carries in `_frigg`.
31
- * @returns {{deferrals: number, firstDeferredAt: string|undefined}}
32
- */
33
- const readDeferral = (body) => {
34
- const frigg = body?._frigg;
35
- const deferrals =
36
- Number.isInteger(frigg?.deferrals) && frigg.deferrals > 0
37
- ? frigg.deferrals
38
- : 0;
39
- const firstDeferredAt =
40
- typeof frigg?.firstDeferredAt === 'string' &&
41
- !Number.isNaN(Date.parse(frigg.firstDeferredAt))
42
- ? frigg.firstDeferredAt
43
- : undefined;
44
- return { deferrals, firstDeferredAt };
45
- };
46
-
47
- const nextDeferral = (body, now = Date.now()) => {
48
- const current = readDeferral(body);
49
- return {
50
- deferrals: current.deferrals + 1,
51
- firstDeferredAt: current.firstDeferredAt ?? new Date(now).toISOString(),
52
- };
53
- };
54
-
55
- const withDeferral = (body, deferral) => ({
56
- ...body,
57
- _frigg: { ...body._frigg, ...deferral },
58
- });
59
-
60
- const isDeferralCapped = (
61
- { deferrals, firstDeferredAt, retryAt },
62
- { maxDeferrals, maxDeferredMs }
63
- ) =>
64
- deferrals > maxDeferrals ||
65
- retryAt.getTime() - Date.parse(firstDeferredAt) > maxDeferredMs;
66
-
67
- module.exports = {
68
- MAX_DEFERRALS,
69
- MAX_DEFERRALS_ENV,
70
- MAX_DEFERRED_MS,
71
- MAX_DEFERRED_MS_ENV,
72
- MAX_DELAY_SECONDS,
73
- MAX_VISIBILITY_TIMEOUT_SECONDS,
74
- isDeferralCapped,
75
- nextDeferral,
76
- readDeferral,
77
- readDeferralLimits,
78
- withDeferral,
79
- };