Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type definitions for netlify-identity-widget #30689

Merged
merged 1 commit into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions types/netlify-identity-widget/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
69 changes: 69 additions & 0 deletions types/netlify-identity-widget/netlify-identity-widget-tests.ts
Original file line number Diff line number Diff line change
@@ -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
});
}
24 changes: 24 additions & 0 deletions types/netlify-identity-widget/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
3 changes: 3 additions & 0 deletions types/netlify-identity-widget/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}