Added arguments to solid-auth-client methods (#37603)

There are a few optional arguments that can be passed to
the client methods that aren't present in the types currently.
I've added them as they are in the 2.3.0 version of the client
the existing types say they target.
This commit is contained in:
James Durand 2019-08-15 04:33:25 +10:00 committed by Pranav Senthilnathan
parent b0b4b755a9
commit acc0673552
2 changed files with 43 additions and 6 deletions

View File

@ -1,6 +1,7 @@
// Type definitions for solid-auth-client 2.3
// Project: https://github.com/solid/solid-auth-client#readme
// Definitions by: Vincent <https://github.com/Vinnl>
// James <https://github.com/durandj>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
import { EventEmitter } from 'events';
@ -8,13 +9,26 @@ import { EventEmitter } from 'events';
export interface Session {
webId: string;
}
export interface AsyncStorage {
getItem(key: string): Promise<undefined | string>;
setItem(key: string, value: string): Promise<void>;
removeItem(key: string): Promise<void>;
}
interface LoginOptions {
callbackUri?: string;
popupUri?: string;
storage?: Storage | AsyncStorage;
}
export interface SolidAuthClient extends EventEmitter {
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
currentSession(): Promise<Session | undefined>;
trackSession(callback: (session?: Session) => void): void;
login(identityProvider: string): Promise<void>;
logout(): Promise<void>;
popupLogin(params: { popupUri: string }): Promise<Session>;
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
currentSession(storage?: AsyncStorage): Promise<Session | undefined>;
trackSession(callback: (session?: Session) => void): void;
login(identityProvider: string, options?: LoginOptions): Promise<void>;
logout(storage?: AsyncStorage): Promise<void>;
popupLogin(params?: LoginOptions): Promise<Session>;
}
declare const instantiated: SolidAuthClient;

View File

@ -17,6 +17,17 @@ async function login(idp: string) {
alert(`Logged in as ${session.webId}`);
}
async function loginWithOptions(idp: string) {
const session = await auth.currentSession();
if (!session)
await auth.login(idp, {
callbackUri: '/callback',
popupUri: 'https://solid.community/common/popup.html',
storage: localStorage,
});
else alert(`Logged in as ${session.webId}`);
}
async function popupLogin() {
let session = await auth.currentSession();
const popupUri = 'https://solid.community/common/popup.html';
@ -25,6 +36,18 @@ async function popupLogin() {
alert(`Logged in as ${session.webId}`);
}
async function popupLoginWIthOptions() {
let session = await auth.currentSession();
const popupUri = 'https://solid.community/common/popup.html';
if (!session)
session = await auth.popupLogin({
callbackUri: '/callback',
popupUri,
storage: localStorage,
});
alert(`Logged in as ${session.webId}`);
}
auth.logout().then(() => alert('Goodbye!'));
async function greetUser() {