|
2 | 2 | // Project: https://www.split.io/
|
3 | 3 | // Definitions by: Emiliano Sanchez <https://github.com/EmilianoSanchez/>
|
4 | 4 |
|
| 5 | +import '@splitsoftware/splitio-commons'; |
5 | 6 | import { RequestOptions } from 'http';
|
6 | 7 |
|
7 |
| -export = JsSyncTools; |
8 |
| - |
9 |
| -declare module JsSyncTools { |
| 8 | +/** |
| 9 | + * JavaScript synchronizer tool. |
| 10 | + * |
| 11 | + * @see {@link https://help.split.io/hc/en-us/articles/4421513571469-Split-JavaScript-synchronizer-tools}. |
| 12 | + */ |
| 13 | +export class Synchronizer { |
10 | 14 | /**
|
11 |
| - * JavaScript synchronizer tool. |
| 15 | + * Creates a new Synchronizer instance |
12 | 16 | *
|
13 |
| - * @see {@link https://help.split.io/hc/en-us/articles/4421513571469-Split-JavaScript-synchronizer-tools}. |
| 17 | + * @param config - The synchronizer config object |
14 | 18 | */
|
15 |
| - export class Synchronizer { |
16 |
| - /** |
17 |
| - * Creates a new Synchronizer instance |
18 |
| - * |
19 |
| - * @param config - The synchronizer config object |
20 |
| - */ |
21 |
| - constructor(config: ISynchronizerSettings); |
22 |
| - /** |
23 |
| - * Execute synchronization |
24 |
| - * |
25 |
| - * @param cb - Optional error-first callback to be invoked when the synchronization ends. The callback will be invoked with an error as first argument if the synchronization fails. |
26 |
| - * @returns A promise that resolves when the operation ends, with a boolean indicating if operation succeeded or not. The promise never rejects. |
27 |
| - */ |
28 |
| - execute(cb?: (err?: Error) => void): Promise<boolean>; |
29 |
| - // @TODO expose settings eventually |
30 |
| - // settings: ISettings |
31 |
| - } |
32 |
| - |
| 19 | + constructor(config: ISynchronizerSettings); |
33 | 20 | /**
|
34 |
| - * Log levels. |
| 21 | + * Execute synchronization |
| 22 | + * |
| 23 | + * @param cb - Optional error-first callback to be invoked when the synchronization ends. The callback will be invoked with an error as first argument if the synchronization fails. |
| 24 | + * @returns A promise that resolves when the operation ends, with a boolean indicating if operation succeeded or not. The promise never rejects. |
35 | 25 | */
|
36 |
| - type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE'; |
| 26 | + execute(cb?: (err?: Error) => void): Promise<boolean>; |
| 27 | + // @TODO expose settings eventually |
| 28 | + // settings: ISettings |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Settings interface for Synchronizer instances. |
| 33 | + * |
| 34 | + * @see {@link https://help.split.io/hc/en-us/articles/4421513571469-Split-JavaScript-synchronizer-tools#configuration} |
| 35 | + */ |
| 36 | +export interface ISynchronizerSettings { |
37 | 37 | /**
|
38 |
| - * Available URL settings for the synchronizer. |
| 38 | + * Core settings. |
39 | 39 | */
|
40 |
| - type UrlSettings = { |
41 |
| - /** |
42 |
| - * String property to override the base URL where the synchronizer will get feature flagging related data like a feature flag rollout plan or segments information. |
43 |
| - * |
44 |
| - * @defaultValue `'https://sdk.split.io/api'` |
45 |
| - */ |
46 |
| - sdk?: string |
| 40 | + core: { |
47 | 41 | /**
|
48 |
| - * String property to override the base URL where the synchronizer will post impressions and events. |
| 42 | + * Your SDK key. |
49 | 43 | *
|
50 |
| - * @defaultValue `'https://events.split.io/api'` |
| 44 | + * @see {@link https://help.split.io/hc/en-us/articles/360019916211-API-keys} |
51 | 45 | */
|
52 |
| - events?: string, |
53 |
| - /** |
54 |
| - * String property to override the base URL where the synchronizer will post telemetry data. |
55 |
| - * |
56 |
| - * @defaultValue `'https://telemetry.split.io/api'` |
57 |
| - */ |
58 |
| - telemetry?: string |
59 |
| - }; |
60 |
| - |
61 |
| - /** |
62 |
| - * SplitFilter type. |
63 |
| - */ |
64 |
| - type SplitFilterType = 'byName' | 'bySet'; |
| 46 | + authorizationKey: string |
| 47 | + } |
65 | 48 | /**
|
66 |
| - * Defines a feature flag filter, described by a type and list of values. |
| 49 | + * Defines which kind of storage we should instantiate. |
67 | 50 | */
|
68 |
| - interface SplitFilter { |
| 51 | + storage: { |
| 52 | + /** |
| 53 | + * Storage type. The only possible value is `'PLUGGABLE'`, which is the default. |
| 54 | + */ |
| 55 | + type?: 'PLUGGABLE', |
69 | 56 | /**
|
70 |
| - * Type of the filter. |
| 57 | + * A valid storage instance. |
71 | 58 | */
|
72 |
| - type: SplitFilterType, |
| 59 | + wrapper: Object |
73 | 60 | /**
|
74 |
| - * List of values: feature flag names for 'byName' filter type, and feature flag name prefixes for 'byPrefix' type. |
| 61 | + * Optional prefix added to the storage keys to prevent any kind of data collision between SDK versions. |
| 62 | + * |
| 63 | + * @defaultValue `'SPLITIO'` |
75 | 64 | */
|
76 |
| - values: string[], |
| 65 | + prefix?: string |
77 | 66 | }
|
78 | 67 | /**
|
79 |
| - * ImpressionsMode type. |
| 68 | + * List of URLs that the Synchronizer will use as base for it's synchronization functionalities. |
| 69 | + * Do not change these settings unless you're working an advanced use case, like connecting to a proxy. |
80 | 70 | */
|
81 |
| - type ImpressionsMode = 'OPTIMIZED' | 'DEBUG'; |
| 71 | + urls?: SplitIO.UrlSettings |
82 | 72 | /**
|
83 |
| - * Settings interface for Synchronizer instances. |
| 73 | + * Boolean value to indicate whether the logger should be enabled or disabled by default, or a log level string. |
| 74 | + * |
| 75 | + * Examples: |
| 76 | + * ``` |
| 77 | + * config.debug = true |
| 78 | + * config.debug = 'WARN' |
| 79 | + * ``` |
84 | 80 | *
|
85 |
| - * @see {@link https://help.split.io/hc/en-us/articles/4421513571469-Split-JavaScript-synchronizer-tools#configuration} |
| 81 | + * @defaultValue `false` |
86 | 82 | */
|
87 |
| - interface ISynchronizerSettings { |
88 |
| - /** |
89 |
| - * Core settings. |
90 |
| - */ |
91 |
| - core: { |
92 |
| - /** |
93 |
| - * Your SDK key. |
94 |
| - * |
95 |
| - * @see {@link https://help.split.io/hc/en-us/articles/360019916211-API-keys} |
96 |
| - */ |
97 |
| - authorizationKey: string |
98 |
| - } |
| 83 | + debug?: boolean | SplitIO.LogLevel |
| 84 | + /** |
| 85 | + * Synchronization settings. |
| 86 | + */ |
| 87 | + sync?: { |
99 | 88 | /**
|
100 |
| - * Defines which kind of storage we should instantiate. |
| 89 | + * List of feature flag filters. These filters are used to fetch a subset of the feature flag definitions in your environment. |
| 90 | + * |
| 91 | + * Example: |
| 92 | + * ``` |
| 93 | + * splitFilter: [ |
| 94 | + * { type: 'byName', values: ['my_feature_flag_1', 'my_feature_flag_2'] }, // will fetch feature flags named 'my_feature_flag_1' and 'my_feature_flag_2' |
| 95 | + * ] |
| 96 | + * ``` |
101 | 97 | */
|
102 |
| - storage: { |
103 |
| - /** |
104 |
| - * Storage type. The only possible value is `'PLUGGABLE'`, which is the default. |
105 |
| - */ |
106 |
| - type?: 'PLUGGABLE', |
107 |
| - /** |
108 |
| - * A valid storage instance. |
109 |
| - */ |
110 |
| - wrapper: Object |
111 |
| - /** |
112 |
| - * Optional prefix added to the storage keys to prevent any kind of data collision between SDK versions. |
113 |
| - * |
114 |
| - * @defaultValue `'SPLITIO'` |
115 |
| - */ |
116 |
| - prefix?: string |
117 |
| - } |
| 98 | + splitFilters?: SplitIO.SplitFilter[] |
118 | 99 | /**
|
119 |
| - * List of URLs that the Synchronizer will use as base for it's synchronization functionalities. |
120 |
| - * Do not change these settings unless you're working an advanced use case, like connecting to a proxy. |
| 100 | + * Feature Flag Spec version. Option to determine which version of the feature flag definitions are fetched and stored. |
| 101 | + * Possible values are `'1.0'`, `'1.1'`, `'1.2'`, and `'1.3'`. |
| 102 | + * |
| 103 | + * @defaultValue `'1.3'` |
121 | 104 | */
|
122 |
| - urls?: UrlSettings |
| 105 | + flagSpecVersion?: '1.0' | '1.1' | '1.2' | '1.3' |
123 | 106 | /**
|
124 |
| - * Boolean value to indicate whether the logger should be enabled or disabled by default, or a log level string. |
| 107 | + * Impressions Collection Mode. Option to determine how impressions are going to be sent to Split Servers. |
125 | 108 | *
|
126 |
| - * Examples: |
127 |
| - * ``` |
128 |
| - * config.debug = true |
129 |
| - * config.debug = 'WARN' |
130 |
| - * ``` |
| 109 | + * Possible values are `'DEBUG'` and `'OPTIMIZED'`. |
| 110 | + * - DEBUG: will send all the impressions generated (recommended only for debugging purposes). |
| 111 | + * - OPTIMIZED: will send unique impressions to Split Servers avoiding a considerable amount of traffic that duplicated impressions could generate. |
131 | 112 | *
|
132 |
| - * @defaultValue `false` |
| 113 | + * @defaultValue `'OPTIMIZED'` |
133 | 114 | */
|
134 |
| - debug?: boolean | LogLevel |
| 115 | + impressionsMode?: SplitIO.ImpressionsMode |
135 | 116 | /**
|
136 |
| - * Synchronization settings. |
| 117 | + * Custom options object for HTTP(S) requests in Node.js. |
| 118 | + * If provided, this object is merged with the options object passed for Node-Fetch calls. |
| 119 | + * |
| 120 | + * @see {@link https://www.npmjs.com/package/node-fetch#options} |
137 | 121 | */
|
138 |
| - sync?: { |
| 122 | + requestOptions?: { |
139 | 123 | /**
|
140 |
| - * List of feature flag filters. These filters are used to fetch a subset of the feature flag definitions in your environment. |
| 124 | + * Custom function called before each request, allowing you to add or update headers in Synchronizer HTTP requests. |
| 125 | + * Some headers, such as `SplitSDKVersion`, are required by the Synchronizer and cannot be overridden. |
| 126 | + * To pass multiple headers with the same name, combine their values into a single line, separated by commas. Example: `{ 'Authorization': 'value1, value2' }` |
| 127 | + * Or provide keys with different cases since headers are case-insensitive. Example: `{ 'authorization': 'value1', 'Authorization': 'value2' }` |
| 128 | + * |
| 129 | + * @defaultValue `undefined` |
141 | 130 | *
|
142 |
| - * Example: |
| 131 | + * @param context - The context for the request, which contains the `headers` property object representing the current headers in the request. |
| 132 | + * @returns An object representing a set of headers to be merged with the current headers. |
| 133 | + * |
| 134 | + * @example |
143 | 135 | * ```
|
144 |
| - * splitFilter: [ |
145 |
| - * { type: 'byName', values: ['my_feature_flag_1', 'my_feature_flag_2'] }, // will fetch feature flags named 'my_feature_flag_1' and 'my_feature_flag_2' |
146 |
| - * ] |
| 136 | + * const getHeaderOverrides = (context) => { |
| 137 | + * return { |
| 138 | + * 'Authorization': context.headers['Authorization'] + ', other-value', |
| 139 | + * 'custom-header': 'custom-value' |
| 140 | + * }; |
| 141 | + * }; |
147 | 142 | * ```
|
148 | 143 | */
|
149 |
| - splitFilters?: SplitFilter[] |
| 144 | + getHeaderOverrides?: (context: { headers: Record<string, string> }) => Record<string, string>; |
150 | 145 | /**
|
151 |
| - * Feature Flag Spec version. Option to determine which version of the feature flag definitions are fetched and stored. |
152 |
| - * Possible values are `'1.0'`, `'1.1'`, `'1.2'`, and `'1.3'`. |
| 146 | + * Custom Node.js HTTP(S) Agent used for HTTP(S) requests. |
153 | 147 | *
|
154 |
| - * @defaultValue `'1.3'` |
155 |
| - */ |
156 |
| - flagSpecVersion?: '1.0' | '1.1' | '1.2' | '1.3' |
157 |
| - /** |
158 |
| - * Impressions Collection Mode. Option to determine how impressions are going to be sent to Split Servers. |
| 148 | + * You can use it, for example, for certificate pinning or setting a network proxy: |
159 | 149 | *
|
160 |
| - * Possible values are `'DEBUG'` and `'OPTIMIZED'`. |
161 |
| - * - DEBUG: will send all the impressions generated (recommended only for debugging purposes). |
162 |
| - * - OPTIMIZED: will send unique impressions to Split Servers avoiding a considerable amount of traffic that duplicated impressions could generate. |
163 |
| - * |
164 |
| - * @defaultValue `'OPTIMIZED'` |
165 |
| - */ |
166 |
| - impressionsMode?: ImpressionsMode |
167 |
| - /** |
168 |
| - * Custom options object for HTTP(S) requests in Node.js. |
169 |
| - * If provided, this object is merged with the options object passed for Node-Fetch calls. |
| 150 | + * ``` |
| 151 | + * const { HttpsProxyAgent } = require('https-proxy-agent'); |
170 | 152 | *
|
171 |
| - * @see {@link https://www.npmjs.com/package/node-fetch#options} |
172 |
| - */ |
173 |
| - requestOptions?: { |
174 |
| - /** |
175 |
| - * Custom function called before each request, allowing you to add or update headers in Synchronizer HTTP requests. |
176 |
| - * Some headers, such as `SplitSDKVersion`, are required by the Synchronizer and cannot be overridden. |
177 |
| - * To pass multiple headers with the same name, combine their values into a single line, separated by commas. Example: `{ 'Authorization': 'value1, value2' }` |
178 |
| - * Or provide keys with different cases since headers are case-insensitive. Example: `{ 'authorization': 'value1', 'Authorization': 'value2' }` |
179 |
| - * |
180 |
| - * @defaultValue `undefined` |
181 |
| - * |
182 |
| - * @param context - The context for the request, which contains the `headers` property object representing the current headers in the request. |
183 |
| - * @returns An object representing a set of headers to be merged with the current headers. |
184 |
| - * |
185 |
| - * @example |
186 |
| - * ``` |
187 |
| - * const getHeaderOverrides = (context) => { |
188 |
| - * return { |
189 |
| - * 'Authorization': context.headers['Authorization'] + ', other-value', |
190 |
| - * 'custom-header': 'custom-value' |
191 |
| - * }; |
192 |
| - * }; |
193 |
| - * ``` |
194 |
| - */ |
195 |
| - getHeaderOverrides?: (context: { headers: Record<string, string> }) => Record<string, string>; |
196 |
| - /** |
197 |
| - * Custom Node.js HTTP(S) Agent used for HTTP(S) requests. |
198 |
| - * |
199 |
| - * You can use it, for example, for certificate pinning or setting a network proxy: |
200 |
| - * |
201 |
| - * ``` |
202 |
| - * const { HttpsProxyAgent } = require('https-proxy-agent'); |
203 |
| - * |
204 |
| - * const proxyAgent = new HttpsProxyAgent(process.env.HTTPS_PROXY || 'http://10.10.1.10:1080'); |
205 |
| - * |
206 |
| - * const synchronizer = Synchronizer({ |
207 |
| - * ... |
208 |
| - * sync: { |
209 |
| - * requestOptions: { |
210 |
| - * agent: proxyAgent |
211 |
| - * } |
212 |
| - * } |
213 |
| - * }) |
214 |
| - * ``` |
215 |
| - * |
216 |
| - * @see {@link https://nodejs.org/api/https.html#class-httpsagent} |
217 |
| - * |
218 |
| - * @defaultValue `undefined` |
219 |
| - */ |
220 |
| - agent?: RequestOptions['agent'] |
221 |
| - }, |
222 |
| - } |
223 |
| - /** |
224 |
| - * Scheduler settings. |
225 |
| - */ |
226 |
| - scheduler?: { |
227 |
| - /** |
228 |
| - * Maximum number of impressions to send per POST request. |
| 153 | + * const proxyAgent = new HttpsProxyAgent(process.env.HTTPS_PROXY || 'http://10.10.1.10:1080'); |
229 | 154 | *
|
230 |
| - * @defaultValue `1000` |
231 |
| - */ |
232 |
| - impressionsPerPost?: number |
233 |
| - /** |
234 |
| - * Maximum number of events to send per POST request. |
| 155 | + * const synchronizer = Synchronizer({ |
| 156 | + * ... |
| 157 | + * sync: { |
| 158 | + * requestOptions: { |
| 159 | + * agent: proxyAgent |
| 160 | + * } |
| 161 | + * } |
| 162 | + * }) |
| 163 | + * ``` |
235 | 164 | *
|
236 |
| - * @defaultValue `1000` |
237 |
| - */ |
238 |
| - eventsPerPost?: number |
239 |
| - /** |
240 |
| - * Maximum number of retry attempts for posting impressions and events. |
| 165 | + * @see {@link https://nodejs.org/api/https.html#class-httpsagent} |
241 | 166 | *
|
242 |
| - * @defaultValue `3` |
| 167 | + * @defaultValue `undefined` |
243 | 168 | */
|
244 |
| - maxRetries?: number |
245 |
| - } |
| 169 | + agent?: RequestOptions['agent'] |
| 170 | + }, |
| 171 | + } |
| 172 | + /** |
| 173 | + * Scheduler settings. |
| 174 | + */ |
| 175 | + scheduler?: { |
| 176 | + /** |
| 177 | + * Maximum number of impressions to send per POST request. |
| 178 | + * |
| 179 | + * @defaultValue `1000` |
| 180 | + */ |
| 181 | + impressionsPerPost?: number |
| 182 | + /** |
| 183 | + * Maximum number of events to send per POST request. |
| 184 | + * |
| 185 | + * @defaultValue `1000` |
| 186 | + */ |
| 187 | + eventsPerPost?: number |
| 188 | + /** |
| 189 | + * Maximum number of retry attempts for posting impressions and events. |
| 190 | + * |
| 191 | + * @defaultValue `3` |
| 192 | + */ |
| 193 | + maxRetries?: number |
246 | 194 | }
|
247 | 195 | }
|
0 commit comments