-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-element.d.ts
148 lines (131 loc) · 4.94 KB
/
fetch-element.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/// <reference lib="dom" />
/**
* @returns Promise resolved when updated DOM is rendered by calling [requestIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback)
* @param cb callback invoked on dom rendered, its return value passed back to wait4DomUpdated promise
*/
export function wait4DomUpdated<T>(cb: ()=>T): Promise<T>;
/**
* @returns string lovercased with spaces replaces with dash '-'
* @param s string to convert
*/
export function toKebbabCase(s: string): string;
/**
* webcomponent renders JSON and other content types retrieved by interruptible
* [ fetch() ](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) api
* [more...](https://github.com/sashafirsov/slotted-element#fetch-element)
*/
export class FetchElement extends HTMLElement {
/**
* @see [using custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)
*/
static get observedAttributes(): string[];
/**
* override this map the content type to module loader function
* @see [json remdering module](https://github.com/sashafirsov/slotted-element/blob/main/render/json.js) sample
*/
static get mime2mod(): {
'application/json': () => Promise<typeof import("./render/json.js").default>;
'text/html': () => (data: any, contentType: any, httpCode: any, responseHeaders: any, ...args: any[]) => Promise<void>;
'text/xml': () => (data: any, contentType: any, httpCode: any, responseHeaders: any, ...args: any[]) => Promise<void>;
'application/xml': () => (data: any, contentType: any, httpCode: any, responseHeaders: any, ...args: any[]) => Promise<void>;
'image/svg+xml': () => (data: any, contentType: any, httpCode: any, responseHeaders: any, ...args: any[]) => Promise<void>;
};
/**
* override to override the request headers
*/
get headers(): {[key:string]:string};
/**
* interrupt current request
* @see [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)
*/
abort(): void;
/**
* @see [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
* @param args
*/
fetch( url:Request | string, options ): Promise<any>;
/**
* private
*/
_fetch: (url: any, options: any) => Promise<any>;
/**
* string representing the loading to rendering life cycle
*/
state: 'loading'|'rendering'|'loaded'|'error';
/**
* response.status, set by onResponse() handler, matching http error code
*/
status: string;
/**
* @see [web component lifecycle](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements#using_the_lifecycle_callbacks)
*/
connectedCallback(): void;
/**
* set to true when fetch is initialized
*/
initialized: boolean;
/**
* @see [web component lifecycle](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements#using_the_lifecycle_callbacks)
*/
attributeChangedCallback(name: any, oldValue: any, newValue: any): void;
/**
* callback when `fetch()` is resolved.
* Sets `status`, `contentType`, `responseHeaders` and resolves the method for data
* conversion according to content type.
* @param response
* @returns data promise from `response.json()` or `response.text()`
*/
onResponse(response: any): Promise<any>;
/**
* set by `onResponse()` to 'network error' in case of http code not in 200 range
*/
error: string;
/**
* response.headers.get( 'content-type' )
*/
contentType: string;
/**
* response.headers
*/
responseHeaders: any;
/**
* pre-render callback to massage response data before `render()`
* @param data
*/
setContent(data: any): void;
/**
* callback which check the contentType and invokes renderer from `mime2mod` map
* @param result
*/
onResult(result: any): Promise<any>;
/**
* callback to override the output HTML according to response outcome.
* @param data
* @param contentType
* @param httpCode
* @param responseHeaders
*/
render(data: any, contentType: any, httpCode: any, responseHeaders: any): void;
/**
* default rendering implementation which triggers data and html transformation
* @param data
* @param contentType
* @param httpCode
* @param responseHeaders
* @param args
*/
renderHtml(data: any, contentType: any, httpCode: any, responseHeaders: any, ...args: any[]): Promise<void>;
/**
* callback on `fetch()` failure
* @param error
* @returns value for rejected promise
*/
onError(error: any): any;
/**
* override to limit or define the order of keys on json object to be rendered in table.
* @param obj
* @returns array of keys to be shown in HTML
*/
getKeys(obj: any): string[];
}
export default FetchElement;