forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcross-storage.d.ts
86 lines (73 loc) · 3.39 KB
/
cross-storage.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
// Type definitions for cross-storage v0.8.1
// Project: https://github.com/zendesk/cross-storage
// Definitions by: Daniel Chao <http://dchao.co/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module "cross-storage" {
interface CrossStorageClientOptions {
timeout?: number;
promise?: any;
frameId?: string;
}
type CrossStorageMethod = "get" | "set" | "del" | "getKeys" | "clear";
interface SubDomain {
origin: RegExp;
allow: CrossStorageMethod[];
}
export class CrossStorageClient {
/**
* Constructs a new cross storage client given the url to a hub. By default, an iframe is created
* within the document body that points to the url. It also accepts an options object, which may include
* a timeout, frameId, and promise. The timeout, in milliseconds, is applied to each request and defaults
* to 5000ms. The options object may also include a frameId, identifying an existing frame on which to
* install its listeners. If the promise key is supplied the constructor for a Promise, that Promise
* library will be used instead of the default window.Promise.
*/
constructor (hubUrl: string, opts: CrossStorageClientOptions)
/**
* Returns a promise that is fulfilled when a connection has been established with the cross storage
* hub. Its use is required to avoid sending any requests prior to initialization being complete.
*/
onConnect (): Promise<void>;
/**
* Sets a key to the specified value, optionally accepting a ttl to passively expire the key after a
* number of milliseconds. Returns a promise that is fulfilled on success, or rejected if any errors
* setting the key occurred, or the request timed out.
*/
set (key: string, value: any, ttl?: number): Promise<void>;
/**
* Accepts one or more keys for which to retrieve their values. Returns a promise that is settled on
* hub response or timeout. On success, it is fulfilled with the value of the key if only passed a
* single argument. Otherwise it's resolved with an array of values. On failure, it is rejected with
* the corresponding error message.
*/
get (key: string): Promise<any>;
get (...keys: string[]): Promise<any[]>;
/**
* Accepts one or more keys for deletion. Returns a promise that is settled on hub response or timeout.
*/
del (...keys: string[]): Promise<void>;
/**
* Returns a promise that, when resolved, passes an array of keys currently in storage.
*/
getKeys(): Promise<string[]>;
/**
* Returns a promise that, when resolved, passes an array of keys currently in storage.
*/
clear(): Promise<void>;
/**
* Deletes the iframe and sets the connected state to false. The client can no longer be used after
* being invoked.
*/
close(): void;
}
export class CrossStorageHub {
/**
* Accepts an array of objects with two keys: origin and allow. The value of origin is expected to be
* a RegExp, and allow, an array of strings. The cross storage hub is then initialized to accept requests
* from any of the matching origins, allowing access to the associated lists of methods. Methods may
* include any of: get, set, del, getKeys and clear. A 'ready' message is sent to the parent window once
* complete.
*/
static init (subdomains: SubDomain[]): void;
}
}