Add type definitions for netlify-identity-widget

This commit is contained in:
Naveen Kumar Sangi
2018-11-20 17:17:35 +05:30
parent daf297918d
commit 9317b400ae
4 changed files with 182 additions and 0 deletions

View File

@@ -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 <https://github.com/nkprince007>
// 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<void> when a user is
* logged in, else returns undefined.
*/
export function logout(): Promise<void> | undefined;

View File

@@ -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<void>
const logoutPromise = NetlifyIdentityWidget.logout();
if (logoutPromise) {
logoutPromise.then(() => {
// You can now do clean up after successful logout
});
}

View File

@@ -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"
]
}

View File

@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}