@polarityio/integration-types 1.2.0 → 1.2.1
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.
- package/dist/limiter.d.ts +74 -7
- package/package.json +1 -1
package/dist/limiter.d.ts
CHANGED
|
@@ -103,25 +103,62 @@ export interface ScopedLimiter {
|
|
|
103
103
|
* Schedule an async job within this scope's rate limit.
|
|
104
104
|
*
|
|
105
105
|
* @param fn - The async function to execute when a slot is available.
|
|
106
|
+
* Any additional arguments after `fn` are passed through as arguments to `fn`.
|
|
107
|
+
* @param args - Optional arguments forwarded to `fn` when it is invoked.
|
|
106
108
|
* @returns A promise that resolves with the return value of `fn`.
|
|
107
109
|
*
|
|
108
|
-
* @example
|
|
110
|
+
* @example Closure style — capture variables from surrounding scope
|
|
109
111
|
* ```ts
|
|
110
112
|
* const scoped = context.limiter.scope(userId);
|
|
111
113
|
* const result = await scoped.schedule(async () => {
|
|
112
114
|
* return await fetchUserData(userId);
|
|
113
115
|
* });
|
|
114
116
|
* ```
|
|
117
|
+
*
|
|
118
|
+
* @example Function reference style — pass arguments through to fn
|
|
119
|
+
* ```ts
|
|
120
|
+
* async function fetchUserPage(id: string, page: number) {
|
|
121
|
+
* return await apiClient.get(`/users/${id}/page/${page}`);
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* const scoped = context.limiter.scope(userId);
|
|
125
|
+
* const result = await scoped.schedule(fetchUserPage, userId, 1);
|
|
126
|
+
* ```
|
|
115
127
|
*/
|
|
116
|
-
schedule<T>(fn: () =>
|
|
128
|
+
schedule<T, A extends unknown[]>(fn: (...args: A) => PromiseLike<T>, ...args: A): Promise<T>;
|
|
117
129
|
/**
|
|
118
130
|
* Schedule an async job with explicit scheduling options.
|
|
119
131
|
*
|
|
120
132
|
* @param options - Priority, id, weight, and expiration settings.
|
|
121
133
|
* @param fn - The async function to execute when a slot is available.
|
|
134
|
+
* Any additional arguments after `fn` are passed through as arguments to `fn`.
|
|
135
|
+
* @param args - Optional arguments forwarded to `fn` when it is invoked.
|
|
122
136
|
* @returns A promise that resolves with the return value of `fn`.
|
|
137
|
+
*
|
|
138
|
+
* @example Closure style — capture variables from surrounding scope
|
|
139
|
+
* ```ts
|
|
140
|
+
* const scoped = context.limiter.scope(userId);
|
|
141
|
+
* const result = await scoped.schedule(
|
|
142
|
+
* { priority: 1 },
|
|
143
|
+
* async () => await fetchPriorityData(userId)
|
|
144
|
+
* );
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* @example Function reference style — pass arguments through to fn
|
|
148
|
+
* ```ts
|
|
149
|
+
* async function fetchPriorityData(id: string) {
|
|
150
|
+
* return await apiClient.get(`/users/${id}/priority`);
|
|
151
|
+
* }
|
|
152
|
+
*
|
|
153
|
+
* const scoped = context.limiter.scope(userId);
|
|
154
|
+
* const result = await scoped.schedule(
|
|
155
|
+
* { priority: 1 },
|
|
156
|
+
* fetchPriorityData,
|
|
157
|
+
* userId
|
|
158
|
+
* );
|
|
159
|
+
* ```
|
|
123
160
|
*/
|
|
124
|
-
schedule<T>(options: ScheduleOptions, fn: () =>
|
|
161
|
+
schedule<T, A extends unknown[]>(options: ScheduleOptions, fn: (...args: A) => PromiseLike<T>, ...args: A): Promise<T>;
|
|
125
162
|
}
|
|
126
163
|
/**
|
|
127
164
|
* Distributed rate limiter available on the integration context as `context.limiter`.
|
|
@@ -153,32 +190,62 @@ export interface Limiter {
|
|
|
153
190
|
* minimum time between jobs has elapsed.
|
|
154
191
|
*
|
|
155
192
|
* @param fn - The async function to execute when a slot is available.
|
|
193
|
+
* Any additional arguments after `fn` are passed through as arguments to `fn`.
|
|
194
|
+
* @param args - Optional arguments forwarded to `fn` when it is invoked.
|
|
156
195
|
* @returns A promise that resolves with the return value of `fn`.
|
|
157
196
|
*
|
|
158
|
-
* @example
|
|
197
|
+
* @example Closure style — capture variables from surrounding scope
|
|
159
198
|
* ```ts
|
|
160
199
|
* const data = await context.limiter.schedule(async () => {
|
|
161
200
|
* return await fetch('https://api.example.com/data');
|
|
162
201
|
* });
|
|
163
202
|
* ```
|
|
203
|
+
*
|
|
204
|
+
* @example Function reference style — pass arguments through to fn
|
|
205
|
+
* ```ts
|
|
206
|
+
* async function fetchData(url: string, headers: Record<string, string>) {
|
|
207
|
+
* return await fetch(url, { headers });
|
|
208
|
+
* }
|
|
209
|
+
*
|
|
210
|
+
* const data = await context.limiter.schedule(
|
|
211
|
+
* fetchData,
|
|
212
|
+
* 'https://api.example.com/data',
|
|
213
|
+
* { Authorization: 'Bearer token' }
|
|
214
|
+
* );
|
|
215
|
+
* ```
|
|
164
216
|
*/
|
|
165
|
-
schedule<T>(fn: () =>
|
|
217
|
+
schedule<T, A extends unknown[]>(fn: (...args: A) => PromiseLike<T>, ...args: A): Promise<T>;
|
|
166
218
|
/**
|
|
167
219
|
* Schedule an async job with explicit scheduling options (priority, id, etc.).
|
|
168
220
|
*
|
|
169
221
|
* @param options - Options controlling priority, weight, and expiration.
|
|
170
222
|
* @param fn - The async function to execute when a slot is available.
|
|
223
|
+
* Any additional arguments after `fn` are passed through as arguments to `fn`.
|
|
224
|
+
* @param args - Optional arguments forwarded to `fn` when it is invoked.
|
|
171
225
|
* @returns A promise that resolves with the return value of `fn`.
|
|
172
226
|
*
|
|
173
|
-
* @example
|
|
227
|
+
* @example Closure style — capture variables from surrounding scope
|
|
174
228
|
* ```ts
|
|
175
229
|
* const result = await context.limiter.schedule(
|
|
176
230
|
* { priority: 1, id: 'critical-job' },
|
|
177
231
|
* async () => await criticalApiCall()
|
|
178
232
|
* );
|
|
179
233
|
* ```
|
|
234
|
+
*
|
|
235
|
+
* @example Function reference style — pass arguments through to fn
|
|
236
|
+
* ```ts
|
|
237
|
+
* async function fetchEndpoint(endpoint: string) {
|
|
238
|
+
* return await apiClient.get(endpoint);
|
|
239
|
+
* }
|
|
240
|
+
*
|
|
241
|
+
* const result = await context.limiter.schedule(
|
|
242
|
+
* { priority: 1, id: 'critical-job' },
|
|
243
|
+
* fetchEndpoint,
|
|
244
|
+
* '/critical-resource'
|
|
245
|
+
* );
|
|
246
|
+
* ```
|
|
180
247
|
*/
|
|
181
|
-
schedule<T>(options: ScheduleOptions, fn: () =>
|
|
248
|
+
schedule<T, A extends unknown[]>(options: ScheduleOptions, fn: (...args: A) => PromiseLike<T>, ...args: A): Promise<T>;
|
|
182
249
|
/**
|
|
183
250
|
* Dynamically update the limiter's throttle settings at runtime.
|
|
184
251
|
* Updates propagate to all scoped (per-key) limiters as well.
|