-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
319 lines (319 loc) · 9.45 KB
/
index.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
type TAddScriptOutcome = "added" | "already-added" | "already-added (unmanaged)" | "skipped";
type TAddScriptOptions = {
/**
* Whether the script is an ES module.
*/
isModule?: boolean;
/**
* The loading behaviour of the script.
* - "async": Loaded async, blocks current execution to evaluate.
* - "defer": Loaded async, in-order, evaluated on idle.
* - "blocking": Loaded sync, blocks current execution to evaluate.
*/
loadBehavior?: "async" | "defer" | "blocking";
/**
* The fetch priority of the script.
* - "high": High priority relative to other scripts.
* - "low": Low priority relative to other scripts.
* - "auto": Browser decides priority.
*/
fetchPriority?: "high" | "low" | "auto";
/**
* Whether to skip loading of the script if ES modules are supported.
*/
noopIfModulesSupported?: boolean;
/**
* Multiple requests to the same URL with different query-strings
* will be treated as different scripts when this is set to `false`.
*/
ignoreQueryString?: boolean;
/**
* Security options.
*/
security?: {
crossOrigin?: "anonymous" | "use-credentials";
nonce?: string;
integrity?: string;
referrerPolicy?: "no-referrer" | "no-referrer-when-downgrade" | "origin" | "origin-when-cross-origin" | "same-origin" | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url";
};
/**
* Data attributes.
*/
dataSet?: object;
/**
* A predicate to determine whether the script should be loaded.
*/
skipLoading?: (src: string) => boolean;
};
type TAddScriptCallback = ({ event, removeScriptTag }: {
event: Event;
removeScriptTag: Function;
}) => void;
type TAddScriptCallbacks = {
/**
* A callback that is called when the script is loaded.
*/
onLoad?: TAddScriptCallback;
/**
* A callback that is called when the script fails to load.
*/
onError?: TAddScriptCallback;
};
type TUseScriptOptions = {
/**
* Whether the script should be loaded.
*/
enabled?: boolean;
};
type TLoadScriptResult = {
/**
* The type of outcome.
*/
type: TAddScriptOutcome | "error";
/**
* The event that was fired when the script was loaded.
*/
event?: Event;
/**
* A function that removes the script tag from the DOM. Available if
* the script was successfully added to the DOM in the first place.
*/
removeScriptTag?: Function;
};
declare module "src/Memoize" {
/**
* @template T
* @typedef {(x: T) => T} TIdentity
*/
/**
* @typedef {object} TMemoizeOptions
*
* @property {TIdentity<unknown>} [getKey]
* Function that takes the arguments passed to the memoized function
* and returns a string key to be used for memoization.
*
* Defaults to the identity function, which is just taking the first
* argument and returning it.
*
*/
/**
* Memoize a function.
*
* Offers a means of invalidating the cache, and the key used for
* memoization can be customised with `getKey`.
*
* @example
* const memoized = Memoize(
* (key, invalidate) => {
* setTimeout(invalidate, 1000 * 30)
* return dbLookup(key);
* },
* { getKey: ({ src }) => src }
* );
*
* @template I, O
*
* @param {(input: I, invalidator: () => void) => O} fn
* The function to memoize.
*
* @param {TMemoizeOptions} options
* Options.
*
* @returns {(input: I) => O}
*
*/
export function Memoize<I, O>(fn: (input: I, invalidator: () => void) => O, { getKey }: TMemoizeOptions): (input: I) => O;
export type TIdentity<T> = (x: T) => T;
export type TMemoizeOptions = {
/**
* Function that takes the arguments passed to the memoized function
* and returns a string key to be used for memoization.
*
* Defaults to the identity function, which is just taking the first
* argument and returning it.
*/
getKey?: TIdentity<unknown>;
};
}
declare module "src/validation" {
/**
* Validation against a list of variations.
*
* @template T
* @param {string} name
* @param {T} value
* @param {T[]} variations
* @throws {Error}
* @returns {void}
*/
export function validate<T>(name: string, value: T, variations: T[]): void;
/**
* String validation.
*
* @param {string} name
* @param {unknown} value
* @throws {Error}
* @returns {asserts value is string}
*/
export function validateString(name: string, value: unknown): asserts value is string;
/**
* Function validation.
*
* @param {string} name
* @param {unknown} value
* @throws {Error}
* @returns {asserts value is Function}
*/
export function validateFunction(name: string, value: unknown): asserts value is Function;
/**
* Plain object validation.
*
* @param {string} name
* @param {unknown} value
* @throws {Error}
* @returns {asserts value is object}
*/
export function validatePojo(name: string, value: unknown): asserts value is object;
/**
* Data-set object validation.
*
* @param {string} name
* @param {unknown} value
* @throws {Error}
* @returns {asserts value is object}
*/
export function validateDataSet(name: string, value: unknown): asserts value is object;
/**
* URL validation.
*
* @param {string} name
* @param {unknown} value
* @throws {Error}
* @returns {void}
*/
export function validateURL(name: string, value: unknown): void;
/**
* Check if absolute URL.
*
* @param {unknown} url
* @returns {boolean}
*/
export function isAbsoluteURL(url: unknown): boolean;
export const BOOLEAN: boolean[];
}
declare module "src/core" {
/**
* Detect if a script is already in the document.
*
* By default will not consider the query string, which is often used
* to bust caches. This can be changed by passing an options object
* with `ignoreQueryString` set to `false`.
*
* @example
* scriptAlreadyInDocument("./script.js?bust-cache=1");
*
* scriptAlreadyInDocument("./script.js?bust-cache=2", {
* ignoreQueryString: false,
* });
*
* @param {string} src
* @param {object} [options]
* @param {boolean} [options.ignoreQueryString] Defaults to `true`.
* @returns {boolean}
*/
export function scriptAlreadyInDocument(src: string, options?: {
ignoreQueryString?: boolean;
}): boolean;
/**
* Add a script to the document.
*
* @param {string} src
* The URL of the script to load.
*
* @param {TAddScriptOptions & TAddScriptCallbacks} [options]
* Options.
*
* @throws {Error}
* When an invalid value is passed.
*
* @returns {TAddScriptOutcome}
*/
export function addScript(src: string, options?: TAddScriptOptions & TAddScriptCallbacks): TAddScriptOutcome;
export namespace ADD_SCRIPT_OUTCOME {
let ADDED: string;
let ALREADY_ADDED: string;
let ALREADY_ADDED_UNMANAGED: string;
let SKIPPED: string;
let ERROR: string;
}
}
declare module "src/load-script" {
/**
* Add a script to the document returning a Promise.
*
* @example
* // One script
* await loadScript("https://www.example.com/script.js", {
* loadBehavior: "async"
* })
*
* // Multiple scripts
* await Promise.all(
* [
* "https://www.example.com/script-1.js",
* "https://www.example.com/script-2.js",
* ].map(src => loadScript(src))
* )
*
* @param {string} src
* The URL of the script to load.
*
* @param {TAddScriptOptions} [options]
* Options.
*
* @returns {Promise<TLoadScriptResult>}
*/
export function loadScript(src: string, options?: TAddScriptOptions): Promise<TLoadScriptResult>;
}
declare module "src/make-hook" {
/**
* Create a `useScript` Hook using dependency injection. Compatible with
* React, Preact, Mithril, and SolidJS.
*
* @example
* // React-like API:
* import { useState, useEffect } from "react";
* const useScript = makeHook({ useState, useEffect });
*
* // SolidJS API:
* import { createSignal, onMount } from "solid-js";
* const useScript = makeHook({
* useState: createSignal,
* useEffect: createEffect
* });
*
* function YourComponent() {
* const [state, detail] = useScript("...", options);
* // ...
* }
*
* @param {object} dependencies
* Dependencies.
*
* @param {function} dependencies.useState
* A `useState` Hook from your preferred framework.
*
* @param {function} dependencies.useEffect
* A `useEffect` Hook from your preferred framework.
*
*/
export function makeHook({ useState, useEffect }: {
useState: Function;
useEffect: Function;
}): (src: string, options?: TUseScriptOptions & TAddScriptOptions) => ["pending" | "loading" | "loaded" | "error", TLoadScriptResult];
}
declare module "index" {
export { loadScript } from "./src/load-script.mjs";
export { makeHook } from "./src/make-hook.mjs";
export { addScript, ADD_SCRIPT_OUTCOME } from "./src/core.mjs";
}