UNPKG

15.9 kBTypeScriptView Raw
1declare namespace FomanticUI {
2 interface API {
3 settings: APISettings;
4
5 /**
6 * Execute query using existing API settings.
7 */
8 (behavior: 'query'): JQuery;
9
10 /**
11 * Adds data to existing templated url and returns full url string.
12 */
13 (behavior: 'add url data', url: string, data: any): JQuery;
14
15 /**
16 * Gets promise for current API request.
17 */
18 (behavior: 'get request'): JQuery;
19
20 /**
21 * Aborts current API request.
22 */
23 (behavior: 'abort'): JQuery;
24
25 /**
26 * Returns whether last request was cancelled.
27 */
28 (behavior: 'was cancelled'): boolean;
29
30 /**
31 * Returns whether last request was failure.
32 */
33 (behavior: 'was failure'): boolean;
34
35 /**
36 * Returns whether last request was successful.
37 */
38 (behavior: 'was successful'): boolean;
39
40 /**
41 * Returns whether last request was completed.
42 */
43 (behavior: 'was complete'): boolean;
44
45 /**
46 * Returns whether element is disabled.
47 */
48 (behavior: 'is disabled'): boolean;
49
50 /**
51 * Returns whether element response is mocked.
52 */
53 (behavior: 'is mocked'): boolean;
54
55 /**
56 * Returns whether element is loading.
57 */
58 (behavior: 'is loading'): boolean;
59
60 /**
61 * Sets loading state to element.
62 */
63 (behavior: 'set loading'): void;
64
65 /**
66 * Sets error state to element.
67 */
68 (behavior: 'set error'): void;
69
70 /**
71 * Removes loading state to element.
72 */
73 (behavior: 'remove loading'): void;
74
75 /**
76 * Removes error state to element.
77 */
78 (behavior: 'remove error'): void;
79
80 /**
81 * Gets event that API request will occur on.
82 */
83 (behavior: 'get event'): string;
84
85 /**
86 * Returns 'encodeURIComponent' value only if value passed is not already encoded.
87 */
88 (behavior: 'get url encoded value', value: any): string;
89
90 /**
91 * Reads a locally cached response for a URL.
92 */
93 (behavior: 'read cached response', url: string): any;
94
95 /**
96 * Writes a cached response for a URL.
97 */
98 (behavior: 'write cached response', url: string, response: any): void;
99
100 /**
101 * Creates new cache, removing all locally cached URLs.
102 */
103 (behavior: 'create cache'): void;
104
105 /**
106 * Removes API settings from the page and all events.
107 */
108 (behavior: 'destroy'): JQuery;
109
110 <K extends keyof APISettings | JQueryAjaxSettings>(behavior: 'setting', name: K, value?: undefined, ): Partial<Pick<APISettings, keyof APISettings>> | Partial<Pick<JQueryAjaxSettings, keyof JQueryAjaxSettings>>;
111 <K extends keyof APISettings>(behavior: 'setting', name: K, value: APISettings[K]): JQuery;
112 (behavior: 'setting', value: Partial<Pick<APISettings, keyof APISettings>> | Partial<Pick<JQueryAjaxSettings, keyof JQueryAjaxSettings>>): JQuery;
113 (settings?: Partial<Pick<APISettings, keyof APISettings>> | Partial<Pick<JQueryAjaxSettings, keyof JQueryAjaxSettings>>): JQuery;
114 }
115
116 /**
117 * @see {@link https://fomantic-ui.com/behaviors/api.html#/settings}
118 */
119 interface APISettings {
120 // region API Settings
121
122 /**
123 * When API event should occur.
124 * @default 'auto'
125 */
126 on: string;
127
128 /**
129 * Can be set to 'local' to cache successful returned AJAX responses when using a JSON API.
130 * This helps avoid server roundtrips when API endpoints will return the same results when accessed repeatedly.
131 * Setting to 'false', will add cache busting parameters to the URL.
132 * @default true
133 */
134 cache: boolean | 'local';
135
136 /**
137 * UI state will be applied to this element, defaults to triggering element.
138 * @default false
139 */
140 stateContext: false | JQuery;
141
142 /**
143 * Whether to encode parameters with 'encodeURIComponent' before adding into url string.
144 * @default true
145 */
146 encodeParameters: boolean;
147
148 /**
149 * Whether to automatically include default data like {value} and {text}.
150 * @default true
151 */
152 defaultData: boolean;
153
154 /**
155 * Whether to serialize closest form and include in request.
156 * Use `true` to convert complex named keys like `a[b][1][c][]` into a nested object
157 * Use `formdata` for formdata web api
158 * @default false
159 */
160 serializeForm: boolean;
161
162 /**
163 * How long to wait when a request is made before triggering request, useful for rate limiting 'oninput'.
164 * @default 0
165 */
166 throttle: number;
167
168 /**
169 * When set to false will not delay the first request made, when no others are queued.
170 * @default true
171 */
172 throttleFirstRequest: boolean;
173
174 /**
175 * Whether an API request can occur while another request is still pending.
176 * @default false
177 */
178 interruptRequests: boolean;
179
180 /**
181 * Minimum duration to show loading indication.
182 * @default 0
183 */
184 loadingDuration: number;
185
186 /**
187 * The default 'auto' will automatically remove error state after error duration, unless the element is a 'form'.
188 * @default 'auto'
189 */
190 hideError: 'auto' | boolean;
191
192 /**
193 * Setting to 'true', will not remove error. Setting to a duration in milliseconds to show error state after request error.
194 * @default 2000
195 */
196 errorDuration: true | number;
197
198 // endregion
199
200 // region API Settings
201
202 /**
203 * Named API action for query, originally specified in '$.fn.settings.api'.
204 * @default false
205 */
206 action: string | false;
207
208 /**
209 * Templated URL for query, will override specified action.
210 * @default false
211 */
212 url: string | false;
213
214 /**
215 * base URL to apply to all endpoints. Will be prepended to each given url.
216 * @default ''
217 */
218 base: string;
219
220 /**
221 * Variables to use for replacement.
222 * @default false
223 */
224 urlData: any;
225
226 /**
227 * Can be set to a Javascript object which will be returned automatically instead of requesting JSON from server.
228 * @default false
229 */
230 response: any;
231
232 /**
233 * When specified, this function can be used to retrieve content from a server and return it asynchronously instead of a standard AJAX call.
234 * The callback function should return the server response.
235 * @default false
236 */
237 responseAsync: ((settings: APISettings, callback: (response: any) => void) => void) | false;
238
239 /**
240 * Alias of 'response'.
241 * @default false
242 */
243 mockResponse: any;
244
245 /**
246 * Alias of 'responseAsync'.
247 * @default false
248 */
249 mockResponseAsync: ((settings: APISettings, callback: (response: any) => void) => void) | false;
250
251 /**
252 * If set, the onResponse event handler is able to handle an raw Array Object, which is probably returned by the requested source.
253 * Even if the datatype is json, it won't be force converted into an object anymore then.
254 * @default true
255 */
256 rawResponse: boolean;
257
258 /**
259 * Method for transmitting request to server.
260 * @default 'get'
261 */
262 method: 'get' | 'post' | 'put' | 'delete' | 'head' | 'options' | 'patch';
263
264 /**
265 * Expected data type of response.
266 * @default 'json'
267 */
268 dataType: 'xml' | 'json' | 'jsonp' | 'script' | 'html' | 'text';
269
270 /**
271 * Expected data type of response.
272 * @default {}
273 */
274 data: any;
275
276 // endregion
277
278 // region Callbacks
279
280 /**
281 * Allows modifying settings before request, or cancelling request.
282 */
283 beforeSend(settings: APISettings): any;
284
285 /**
286 * Allows modifying XHR object for request.
287 */
288 beforeXHR(xhrObject: JQuery.jqXHR): any;
289
290 /**
291 * Callback that occurs when request is made. Receives both the API success promise and the XHR request promise.
292 */
293 onRequest(promise: JQuery.Deferred<any>, xhr: JQuery.jqXHR): void;
294
295 /**
296 * Allows modifying the server's response before parsed by other callbacks to determine API event success.
297 */
298 onResponse(response: any): void;
299
300 /**
301 * Determines whether completed JSON response should be treated as successful.
302 *
303 * @see {@link http://fomantic-ui.com/behaviors/api.html#determining-json-success}
304 */
305 successTest(response: any): boolean;
306
307 /**
308 * Callback after successful response, JSON response must pass 'successTest'.
309 */
310 onSuccess(response: any, element: JQuery, xhr: JQuery.jqXHR): void;
311
312 /**
313 * Callback on request complete regardless of conditions.
314 */
315 onComplete(response: any, element: JQuery, xhr: JQuery.jqXHR): void;
316
317 /**
318 * Callback on failed response, or JSON response that fails 'successTest'.
319 */
320 onFailure(response: any, element: JQuery): void;
321
322 /**
323 * Callback on server error from returned status code, or XHR failure.
324 */
325 onError(errorMessage: string, element: JQuery, xhr: JQuery.jqXHR): void;
326
327 /**
328 * Callback on abort caused by user clicking a link or manually cancelling request.
329 */
330 onAbort(errorMessage: string, element: JQuery, xhr: JQuery.jqXHR): void;
331
332 // endregion
333
334 // region DOM Settings
335
336 /**
337 * Selectors used to find parts of a module.
338 */
339 selector: API.SelectorSettings;
340
341 /**
342 * Class names used to determine element state.
343 */
344 className: API.ClassNameSettings;
345
346 /**
347 * Regular expressions used for template matching.
348 */
349 regExp: API.RegExpSettings;
350
351 /**
352 * Metadata used to store XHR and response promise.
353 */
354 metadata: API.MetadataSettings;
355
356 // endregion
357
358 // region Debug Settings
359
360 /**
361 * Name used in log statements.
362 * @default 'API'
363 */
364 name: string;
365
366 /**
367 * Event namespace. Makes sure module teardown does not effect other events attached to an element.
368 * @default 'api'
369 */
370 namespace: string;
371
372 /**
373 * Silences all console output including error messages, regardless of other debug settings.
374 * @default false
375 */
376 silent: boolean;
377
378 /**
379 * Debug output to console.
380 * @default false
381 */
382 debug: boolean;
383
384 /**
385 * Show 'console.table' output with performance metrics.
386 * @default true
387 */
388 performance: boolean;
389
390 /**
391 * Debug output includes all internal behaviors.
392 * @default false
393 */
394 verbose: boolean;
395
396 errors: API.ErrorSettings;
397
398 // endregion
399 }
400
401 namespace API {
402 type SelectorSettings = Partial<Pick<Settings.Selectors, keyof Settings.Selectors>>;
403 type ClassNameSettings = Partial<Pick<Settings.ClassNames, keyof Settings.ClassNames>>;
404 type RegExpSettings = Partial<Pick<Settings.RegExps, keyof Settings.RegExps>>;
405 type MetadataSettings = Partial<Pick<Settings.Metadatas, keyof Settings.Metadatas>>;
406 type ErrorSettings = Partial<Pick<Settings.Errors, keyof Settings.Errors>>;
407
408 namespace Settings {
409 interface Selectors {
410 /**
411 * @default '.disabled'
412 */
413 disabled: string;
414
415 /**
416 * @default 'form'
417 */
418 form: string;
419 }
420
421 interface ClassNames {
422 /**
423 * @default 'loading'
424 */
425 loading: string;
426
427 /**
428 * @default 'error'
429 */
430 error: string;
431 }
432
433 interface RegExps {
434 /**
435 * @default /{\$*[\da-z]+}/gi
436 */
437 required: RegExp;
438
439 /**
440 * @default /{\/\$*[\da-z]+}/gi
441 */
442 optional: RegExp;
443
444 /**
445 * @default /^[_a-z][\w-]*(?:\[[\w-]*])*$/i
446 */
447 validate: RegExp;
448
449 /**
450 * /[\w-]+|(?=\[])/gi
451 */
452 key: RegExp;
453
454 /**
455 * @default /^$/
456 */
457 push: RegExp;
458
459 /**
460 * @default /^\d+$/
461 */
462 fixed: RegExp;
463
464 /**
465 * @default /^[\w-]+$/i
466 */
467 named: RegExp;
468 }
469
470 interface Metadatas {
471 /**
472 * @default 'action'
473 */
474 action: string;
475
476 /**
477 * @default 'url'
478 */
479 url: string;
480 }
481
482 interface Errors {
483 /**
484 * @default 'The before send function has aborted the request.'
485 */
486 beforeSend: string;
487
488 /**
489 * @default 'There was an error with your request.'
490 */
491 error: string;
492
493 /**
494 * @default 'API Request Aborted. Exit conditions met.'
495 */
496 exitConditions: string;
497
498 /**
499 * @default 'JSON could not be parsed during error handling.'
500 */
501 JSONParse: string;
502
503 /**
504 * @default 'You are using legacy API success callback names.'
505 */
506 legacyParameters: string;
507
508 /**
509 * @default 'The method you called is not defined.'
510 */
511 method: string;
512
513 /**
514 * @default 'API action used but no url was defined.'
515 */
516 missingAction: string;
517
518 /**
519 * @default 'No URL specified for API event.'
520 */
521 missingURL: string;
522
523 /**
524 * @default 'The beforeSend callback must return a settings object, beforeSend ignored.'
525 */
526 noReturnedValue: string;
527
528 /**
529 * @default 'There was an error with your request.'
530 */
531 parseError: string;
532
533 /**
534 * @default 'Caching responses locally requires session storage.'
535 */
536 noStorage: string;
537
538 /**
539 * @default 'Missing a required URL parameter: '
540 */
541 requiredParameter: string;
542
543 /**
544 * @default 'Server gave an error: '
545 */
546 statusMessage: string;
547
548 /**
549 * @default 'Your request timed out.'
550 */
551 timeout: string;
552 }
553 }
554 }
555}
556
\No newline at end of file