@polarityio/integration-types 1.2.0 → 2.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.
- package/dist/context.d.ts +133 -0
- package/dist/index.d.ts +1 -1
- package/dist/limiter.d.ts +74 -7
- package/package.json +1 -1
package/dist/context.d.ts
CHANGED
|
@@ -118,6 +118,137 @@ export interface PolarityCache {
|
|
|
118
118
|
integration: IntegrationCache;
|
|
119
119
|
user: UserCache;
|
|
120
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Proxy configuration for per-integration HTTP proxy overrides (tier 1).
|
|
123
|
+
*
|
|
124
|
+
* Present only when the Polarity admin has configured a per-integration
|
|
125
|
+
* proxy override. Global proxy settings (tier 2) and host environment
|
|
126
|
+
* proxy variables (tier 3) are **not** surfaced here — they are injected
|
|
127
|
+
* into the worker subprocess environment and picked up by HTTP clients
|
|
128
|
+
* automatically.
|
|
129
|
+
*
|
|
130
|
+
* @example Resolving the proxy URL for a request
|
|
131
|
+
* ```typescript
|
|
132
|
+
* const proxyUrl = context.network.proxy?.https ?? context.network.proxy?.http;
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export interface NetworkProxy {
|
|
136
|
+
/**
|
|
137
|
+
* HTTP proxy URL for plain-text requests.
|
|
138
|
+
* Omitted (not `null`) when unset.
|
|
139
|
+
*
|
|
140
|
+
* @example "http://user:pass@proxy.corp:8080"
|
|
141
|
+
*/
|
|
142
|
+
http?: string;
|
|
143
|
+
/**
|
|
144
|
+
* HTTPS proxy URL for TLS requests.
|
|
145
|
+
* Omitted (not `null`) when unset. Today this is typically the same
|
|
146
|
+
* URL as `http`; both fields exist for future per-scheme support.
|
|
147
|
+
*
|
|
148
|
+
* @example "http://user:pass@proxy.corp:8080"
|
|
149
|
+
*/
|
|
150
|
+
https?: string;
|
|
151
|
+
/**
|
|
152
|
+
* Comma- or whitespace-separated list of hosts/domains that bypass
|
|
153
|
+
* the per-integration proxy.
|
|
154
|
+
*
|
|
155
|
+
* - Omitted when unset (no explicit bypass list configured).
|
|
156
|
+
* - `null` only when the admin explicitly set the bypass list to empty
|
|
157
|
+
* (a valid `NO_PROXY` value meaning "no exceptions — proxy everything").
|
|
158
|
+
*
|
|
159
|
+
* Supports patterns: exact host, `.suffix` domain matching, and `*` wildcard.
|
|
160
|
+
*
|
|
161
|
+
* @example "localhost,.internal,*.corp"
|
|
162
|
+
*/
|
|
163
|
+
noProxy?: string | null;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Per-integration network configuration provided on every dispatch via
|
|
167
|
+
* `context.network`.
|
|
168
|
+
*
|
|
169
|
+
* Contains **tier-1** (per-integration) TLS and proxy overrides configured
|
|
170
|
+
* by the Polarity admin. Global and host proxy settings are handled
|
|
171
|
+
* transparently via the worker environment and do not appear here.
|
|
172
|
+
*
|
|
173
|
+
* ### Precedence
|
|
174
|
+
*
|
|
175
|
+
* 1. **Tier 1 — Per-integration** → surfaces on `context.network`
|
|
176
|
+
* 2. **Tier 2 — Global Proxy Settings** → worker env (`HTTPS_PROXY`, etc.)
|
|
177
|
+
* 3. **Tier 3 — Host env** → inherited by worker subprocess
|
|
178
|
+
*
|
|
179
|
+
* ### Key Rules
|
|
180
|
+
*
|
|
181
|
+
* - Do **not** cache HTTP clients across dispatches — settings may change
|
|
182
|
+
* between dispatches when an admin updates the configuration.
|
|
183
|
+
* - Do **not** set `process.env.HTTP_PROXY` or
|
|
184
|
+
* `process.env.NODE_TLS_REJECT_UNAUTHORIZED` at runtime — this is racy
|
|
185
|
+
* on shared workers and leaks settings between integrations.
|
|
186
|
+
* - When `proxy` is absent, do nothing — the worker env carries
|
|
187
|
+
* tier 2/3 and most HTTP clients pick it up natively.
|
|
188
|
+
*
|
|
189
|
+
* @example Basic usage with any HTTP client
|
|
190
|
+
* ```typescript
|
|
191
|
+
* async function doLookup(entities, options, context) {
|
|
192
|
+
* const proxyUrl = context.network.proxy?.https ?? context.network.proxy?.http;
|
|
193
|
+
* const rejectUnauthorized = context.network.rejectUnauthorized !== false;
|
|
194
|
+
* // Wire proxyUrl and rejectUnauthorized into your HTTP client
|
|
195
|
+
* }
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* @example Usage with postman-request
|
|
199
|
+
* ```typescript
|
|
200
|
+
* async function doLookup(entities, options, context) {
|
|
201
|
+
* const proxy = context.network.proxy?.https ?? context.network.proxy?.http;
|
|
202
|
+
* const client = request.defaults({
|
|
203
|
+
* ...(proxy && { proxy }),
|
|
204
|
+
* ...(context.network.rejectUnauthorized === false && {
|
|
205
|
+
* strictSSL: false,
|
|
206
|
+
* rejectUnauthorized: false,
|
|
207
|
+
* }),
|
|
208
|
+
* });
|
|
209
|
+
* }
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* @example Usage with axios
|
|
213
|
+
* ```typescript
|
|
214
|
+
* async function doLookup(entities, options, context) {
|
|
215
|
+
* const proxyUrl = context.network.proxy?.https ?? context.network.proxy?.http;
|
|
216
|
+
* const config: AxiosRequestConfig = {};
|
|
217
|
+
* if (proxyUrl) {
|
|
218
|
+
* const u = new URL(proxyUrl);
|
|
219
|
+
* config.proxy = {
|
|
220
|
+
* protocol: u.protocol.replace(':', ''),
|
|
221
|
+
* host: u.hostname,
|
|
222
|
+
* port: Number(u.port) || (u.protocol === 'https:' ? 443 : 80),
|
|
223
|
+
* ...(u.username && { auth: { username: u.username, password: u.password } }),
|
|
224
|
+
* };
|
|
225
|
+
* }
|
|
226
|
+
* if (context.network.rejectUnauthorized === false) {
|
|
227
|
+
* config.httpsAgent = new https.Agent({ rejectUnauthorized: false });
|
|
228
|
+
* }
|
|
229
|
+
* }
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
export interface NetworkContext {
|
|
233
|
+
/**
|
|
234
|
+
* Per-integration proxy override (tier 1).
|
|
235
|
+
*
|
|
236
|
+
* **Omitted entirely** when no per-integration proxy is configured.
|
|
237
|
+
* When absent, HTTP clients should use their default behavior
|
|
238
|
+
* (which will pick up tier 2/3 from the worker environment).
|
|
239
|
+
*/
|
|
240
|
+
proxy?: NetworkProxy;
|
|
241
|
+
/**
|
|
242
|
+
* Whether outbound TLS certificates should be verified.
|
|
243
|
+
*
|
|
244
|
+
* - `true` (default) — enforce certificate validation.
|
|
245
|
+
* - `false` — skip TLS certificate verification. Corresponds to the
|
|
246
|
+
* integration's "Allow Unauthorized TLS Certificates" admin setting.
|
|
247
|
+
*
|
|
248
|
+
* **Always present** — TLS strictness is meaningful regardless of proxy config.
|
|
249
|
+
*/
|
|
250
|
+
rejectUnauthorized: boolean;
|
|
251
|
+
}
|
|
121
252
|
/**
|
|
122
253
|
* Integration context provided to integration functions (`doLookup`, `onMessage`,
|
|
123
254
|
* `onDetails`, `validateOptions`).
|
|
@@ -128,6 +259,8 @@ export interface IntegrationContext {
|
|
|
128
259
|
userId: number;
|
|
129
260
|
logger: Logger;
|
|
130
261
|
limiter: Limiter;
|
|
262
|
+
/** Per-integration network (TLS/proxy) configuration provided by the runtime on every dispatch. */
|
|
263
|
+
network: NetworkContext;
|
|
131
264
|
startPolling: (pollName: string) => void;
|
|
132
265
|
stopPolling: (pollName: string) => void;
|
|
133
266
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export type { Channel, StandardEntityType, EntityType, EntityTypeIdentifier, EntityPosition, OnDemandRequestContext, ScreenChangeRequestContext, RequestContext, Entity } from './entity';
|
|
2
2
|
export type { SelectTypeOptionValue, PossibleUserOptionValue, DoLookupUserOptions, ValidateOptionsUserOption, ValidateOptionsUserOptions, ValidationError } from './user-options';
|
|
3
3
|
export type { LookupResult, DoLookupResult } from './result';
|
|
4
|
-
export type { CacheOptions, GlobalCache, IntegrationCache, UserCache, PolarityCache, IntegrationContext } from './context';
|
|
4
|
+
export type { CacheOptions, GlobalCache, IntegrationCache, UserCache, PolarityCache, NetworkProxy, NetworkContext, IntegrationContext } from './context';
|
|
5
5
|
export type { ThrottleStrategy, ScheduleOptions, LimiterCounts, ThrottleSettings, ScopedLimiter, Limiter } from './limiter';
|
|
6
6
|
export type { Logger } from './logger';
|
|
7
7
|
export type { Integration } from './integration';
|
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.
|