From de98ea98d93eaf1da3ab027f4cc8c354faa5914a Mon Sep 17 00:00:00 2001 From: Dan Chao Date: Wed, 9 Mar 2016 15:59:25 -0800 Subject: [PATCH] add typings for cross-storage --- cross-storage/cross-storage-tests.ts | 24 ++++++++ cross-storage/cross-storage.d.ts | 88 ++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 cross-storage/cross-storage-tests.ts create mode 100644 cross-storage/cross-storage.d.ts diff --git a/cross-storage/cross-storage-tests.ts b/cross-storage/cross-storage-tests.ts new file mode 100644 index 000000000..9e6b4204a --- /dev/null +++ b/cross-storage/cross-storage-tests.ts @@ -0,0 +1,24 @@ +/// + +import { CrossStorageClient, CrossStorageHub } from "cross-storage"; + + +const client = new CrossStorageClient("http://foo.com", { + timeout: 0, + frameId: "null" +}); + +client.onConnect() + .then(() => client.set("foo", "bar")) + .then(() => client.get("foo")) + .then(() => client.getKeys()) + .then((keys) => client.del.apply(client, keys)) + .then(() => client.clear()) + .then(() => client.close()); + +const hub = CrossStorageHub.init([ + { + origin: /foo/, + allow: ['get', 'set', 'del'] + } +]); \ No newline at end of file diff --git a/cross-storage/cross-storage.d.ts b/cross-storage/cross-storage.d.ts new file mode 100644 index 000000000..d8396e9a6 --- /dev/null +++ b/cross-storage/cross-storage.d.ts @@ -0,0 +1,88 @@ +// Type definitions for cross-storage v0.8.1 +// Project: https://github.com/zendesk/cross-storage +// Definitions by: Daniel Chao +// 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; + + /** + * 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; + /** + * 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; + get (...keys: string[]): Promise; + + /** + * Accepts one or more keys for deletion. Returns a promise that is settled on hub response or timeout. + */ + del (...keys: string[]): Promise; + + /** + * Returns a promise that, when resolved, passes an array of keys currently in storage. + */ + getKeys(): Promise; + + /** + * Returns a promise that, when resolved, passes an array of keys currently in storage. + */ + clear(): Promise; + + /** + * 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; + } + +}