From 9317b400aec9c70318d32bfc3848a90ce57b7486 Mon Sep 17 00:00:00 2001 From: Naveen Kumar Sangi Date: Tue, 20 Nov 2018 17:17:35 +0530 Subject: [PATCH] Add type definitions for netlify-identity-widget --- types/netlify-identity-widget/index.d.ts | 86 +++++++++++++++++++ .../netlify-identity-widget-tests.ts | 69 +++++++++++++++ types/netlify-identity-widget/tsconfig.json | 24 ++++++ types/netlify-identity-widget/tslint.json | 3 + 4 files changed, 182 insertions(+) create mode 100644 types/netlify-identity-widget/index.d.ts create mode 100644 types/netlify-identity-widget/netlify-identity-widget-tests.ts create mode 100644 types/netlify-identity-widget/tsconfig.json create mode 100644 types/netlify-identity-widget/tslint.json diff --git a/types/netlify-identity-widget/index.d.ts b/types/netlify-identity-widget/index.d.ts new file mode 100644 index 0000000000..a231be53d2 --- /dev/null +++ b/types/netlify-identity-widget/index.d.ts @@ -0,0 +1,86 @@ +// Type definitions for netlify-identity-widget 1.4 +// Project: https://github.com/netlify/netlify-identity-widget +// Definitions by: Naveen Kumar Sangi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export interface InitOptions { + // The container to attach to. e.g.: '#some-query-selector' + container?: string; + + // Absolute url to endpoint. ONLY USE IN SPECIAL CASES! + // e.g. https://www.example.com/.netlify/functions/identity + // Generally avoid setting the APIUrl. You should only set this when + // your app is served from a domain that differs from where the + // identity endpoint is served.This is common for Cordova or Electron + // apps where you host from localhost or a file. + APIUrl?: string; +} + +export interface Token { + access_token: string; + expires_at: string | number; + expires_in: string | number; + refresh_token: string; + token_type: string; +} + +export interface User { + api: { + _sameOrigin?: boolean; + apiURL: string; + defaultHeaders: { + [header: string]: string | string[] | undefined; + }; + }; + app_metadata: { + provider: string; + }; + aud: string; + audience?: any; + confirmed_at: string; + created_at: string; + updated_at: string; + email: string; + id: string; + role: string; + token?: Token; + url: string; + user_metadata: { + avatar_url: string; + full_name: string; + }; +} + +/** + * Initialises the netlify identity widget. + */ +export function init(opts?: InitOptions): void; + +/** + * Opens the netlify login modal to the corresponding tab. + */ +export function open(tabName?: "signup" | "login"): void; + +/** + * Closes the netlify login modal. + */ +export function close(): void; + +/** + * Retrieves the current logged in user information. + */ +export function currentUser(): User | null; + +/** + * Registers callbacks to corresponding events on the widget. + */ +export function on(event: 'init', cb: (user: User | null) => void): void; +export function on(event: 'login', cb: (user: User) => void): void; +export function on(event: 'logout' | 'open' | 'close', cb: () => void): void; +export function on(event: 'error', cb: (err: Error) => void): void; + +/** + * Logs out the current user. Returns a Promise when a user is + * logged in, else returns undefined. + */ +export function logout(): Promise | undefined; diff --git a/types/netlify-identity-widget/netlify-identity-widget-tests.ts b/types/netlify-identity-widget/netlify-identity-widget-tests.ts new file mode 100644 index 0000000000..f44daaec37 --- /dev/null +++ b/types/netlify-identity-widget/netlify-identity-widget-tests.ts @@ -0,0 +1,69 @@ +import es6styleimport from 'netlify-identity-widget'; + +import NetlifyIdentityWidget = require('netlify-identity-widget'); + +// Type 0: es6styleimport test +es6styleimport.init(); + +// Type 1: Initialize without options +NetlifyIdentityWidget.init(); +NetlifyIdentityWidget.init({}); + +// Type 2: Initialize with container option +NetlifyIdentityWidget.init({ container: 'body' }); + +// Type 3: Initialize with a specific APIUrl +NetlifyIdentityWidget.init({ APIUrl: 'https://www.example.com/.netlify/functions/identity' }); + +// Type 4: Initialize with both the options specified +NetlifyIdentityWidget.init({ + APIUrl: 'https://www.example.com/.netlify/functions/identity', + container: 'body', +}); + +// Open widget modal to let users login +NetlifyIdentityWidget.open(); +NetlifyIdentityWidget.on('open', () => { + // Widget is open and ready to login +}); + +// Open wigdet modal with signup tab selected +NetlifyIdentityWidget.open('signup'); + +// Close the widget programmatically +NetlifyIdentityWidget.close(); +NetlifyIdentityWidget.on('close', () => { + // Widget is closed +}); + +// Event handling after login +NetlifyIdentityWidget.on('login', (user) => { + // You can now use User info after a successful login +}); + +// Event handling after logout +NetlifyIdentityWidget.on('logout', () => { + // You can now notify that the logout was successful +}); + +// Event handling after login on page refresh +NetlifyIdentityWidget.on('init', (user) => { + // Now the widget is ready to use + // If a user was already logged in, the value is returned else null is passed via callback +}); + +// Event handling on errors +NetlifyIdentityWidget.on('error', (err) => { + // The error occured during operation is passed in via callback +}); + +// Use the current logged in user +const user = NetlifyIdentityWidget.currentUser(); + +// If a user is logged in, logout returns a Promise +const logoutPromise = NetlifyIdentityWidget.logout(); +if (logoutPromise) { + logoutPromise.then(() => { + // You can now do clean up after successful logout + }); +} diff --git a/types/netlify-identity-widget/tsconfig.json b/types/netlify-identity-widget/tsconfig.json new file mode 100644 index 0000000000..8ded135467 --- /dev/null +++ b/types/netlify-identity-widget/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "allowSyntheticDefaultImports": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "netlify-identity-widget-tests.ts" + ] +} diff --git a/types/netlify-identity-widget/tslint.json b/types/netlify-identity-widget/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/netlify-identity-widget/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}