From 2eff7af8765e9e9333948a83067dee5b22d9d9c1 Mon Sep 17 00:00:00 2001 From: Mikko Vuorinen Date: Wed, 9 Aug 2017 14:58:24 +0100 Subject: [PATCH] [hellojs] Update definition for 1.15.1 (#18318) * [hellojs] Add/modify tests for latest version * [hellojs] Update definition to latest version Modify interfaces to reflect version 1.15.1 codebase. Add definition for hello.util. Simplify thenable definition. * [hellojs] Add missing method definition Include api() definition when calling with options object. * [hellojs] Add tslint.json and fix linting errors * [hellojs] Use TypeScript 2.3 Set TypeScript version to 2.3 to avoid errors with object-type. * [hellojs] Switch to PromiseLike https://github.com/DefinitelyTyped/DefinitelyTyped/issues/17251 * [hellojs] Remove unused interface * [hellojs] Rename namespace and add export Add default export for static hello-object. Rename namespace to hello so that the namespace as well as the hello-object will be exported. * [hellojs] Remove redundant 'export' keyword --- types/hellojs/hellojs-tests.ts | 135 +++++++++++++++-- types/hellojs/index.d.ts | 265 ++++++++++++++++++--------------- types/hellojs/tslint.json | 1 + 3 files changed, 269 insertions(+), 132 deletions(-) create mode 100644 types/hellojs/tslint.json diff --git a/types/hellojs/hellojs-tests.ts b/types/hellojs/hellojs-tests.ts index 9883c7a529..478e67fa94 100644 --- a/types/hellojs/hellojs-tests.ts +++ b/types/hellojs/hellojs-tests.ts @@ -1,7 +1,124 @@ +import * as hello from 'hellojs'; +// Test code copied from Azure AD B2C sample app at +// https://github.com/Azure-Samples/active-directory-b2c-javascript-hellojs-singlepageapp/tree/cd5982f09a7bff0a72b7b4e44c4b5190b09e20fa +hello.init({ + serviceName: { + name: 'Test', + oauth: { + version: 2, + auth: '', + grant: '' + }, + refresh: true, + scope_delim: ' ', + logout: () => { + let id_token = hello('networkName').getAuthResponse().id_token; + hello.utils.store('networkName', null); + }, + xhr: (p) => { + let token = p.query.access_token; + delete p.query.access_token; + if (token) { + p.headers = { + Authorization: 'Bearer ' + token, + }; + } + + if (p.method === 'post' || p.method === 'put') { + if (typeof (p.data) === 'object') { + try { + p.data = JSON.stringify(p.data); + p.headers['content-type'] = 'application/json'; + } catch (e) { } + } + } else if (p.method === 'patch') { + hello.utils.extend(p.query, p.data); + p.data = null; + } + return true; + }, + form: false + } +}); + +// Test code copied from hello.js built-in modules at +// https://github.com/MrSwitch/hello.js/blob/v1.15.1/src/modules/joinme.js +hello.init({ + joinme: { + name: 'join.me', + oauth: { + version: 2, + auth: '', + grant: '' + }, + refresh: false, + scope: { + basic: 'user_info', + user: 'user_info', + scheduler: 'scheduler', + start: 'start_meeting', + email: '', + friends: '', + share: '', + publish: '', + photos: '', + publish_files: '', + files: '', + videos: '', + offline_access: '' + }, + scope_delim: ' ', + login: (p) => { + p.options.popup.width = 400; + p.options.popup.height = 700; + }, + base: 'https://api.join.me/v1/', + get: { + me: 'user', + meetings: 'meetings', + 'meetings/info': 'meetings/@{id}' + }, + post: { + 'meetings/start/adhoc': (p, callback) => { + callback('meetings/start'); + }, + }, + patch: { + 'meetings/update': (p, callback) => { + callback('meetings/' + p.data.meetingId); + } + }, + del: { + 'meetings/delete': 'meetings/@{id}' + }, + wrap: { + me: (o, headers) => { + if (!o.email) { + return o; + } + + o.name = o.fullName; + o.first_name = o.name.split(' ')[0]; + o.last_name = o.name.split(' ')[1]; + o.id = o.email; + + return o; + }, + default: (o, headers) => { + return o; + } + }, + xhr: (p, qs) => { + let token = qs.access_token; + delete qs.access_token; + p.headers.Authorization = 'Bearer ' + token; + } + } +}); hello.init({ - 'facebook': '', + facebook: '', }, { redirect_uri: 'hello.html', display: 'page', @@ -19,30 +136,28 @@ hello('facebook').logout(); hello.on('auth.login', auth => { alert('log to ' + auth.network); }).on('auth.logout', auth => { - alert('unlog from ' + auth.network); - }); + alert('unlog from ' + auth.network); +}); hello.getAuthResponse('facebook'); hello.login('facebook', null, () => { - var req = hello.getAuthResponse('facebook'); + let req = hello.getAuthResponse('facebook'); }); hello.logout('facebook'); -var serviceInfo = hello.service('facebook'); - -hello("facebook").api("me").success(function(json) { +hello("facebook").api("me").then((json) => { alert("Your name is " + json.name); -}).error(function () { +}, () => { alert("Whoops!"); }); -var sessionstart = function () { +let sessionstart = () => { alert("Session has started"); }; hello.on("auth.login", sessionstart); hello.off("auth.login", sessionstart); -hello("facebook").login({ scope: "friends,photos,publish" }); \ No newline at end of file +hello("facebook").login({ scope: "friends,photos,publish" }); diff --git a/types/hellojs/index.d.ts b/types/hellojs/index.d.ts index ec07219cc3..16b19afb4a 100644 --- a/types/hellojs/index.d.ts +++ b/types/hellojs/index.d.ts @@ -1,127 +1,148 @@ -// Type definitions for hello.js 0.2 +// Type definitions for hello.js 1.15 // Project: http://adodson.com/hello.js/ // Definitions by: Pavel Zika +// Mikko Vuorinen // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 -interface HelloJSLoginOptions { - redirect_uri?: string; - display?: string; - scope?: string; - response_type?: string; - force?: boolean; - oauth_proxy?: string; - timeout?: number; - default_service?: string; -} - -interface HelloJSLogoutOptions { - force?: boolean; -} - -interface HelloJSImmediateSuccessCB { - (value: T): TP; -} - -interface HelloJSImmediateErrorCB { - (err: any): TP; -} - -interface HelloJSDeferredSuccessCB { - (value: T): HelloJSThenable; -} - -interface HelloJSDeferredErrorCB { - (error: any): HelloJSThenable; -} - -interface HelloJSThenable { - then( - successCB?: HelloJSDeferredSuccessCB, - errorCB?: HelloJSDeferredErrorCB - ): HelloJSThenable; - - then( - successCB?: HelloJSDeferredSuccessCB, - errorCB?: HelloJSImmediateErrorCB - ): HelloJSThenable; - - then( - successCB?: HelloJSImmediateSuccessCB, - errorCB?: HelloJSDeferredErrorCB - ): HelloJSThenable; - - then( - successCB?: HelloJSImmediateSuccessCB, - errorCB?: HelloJSImmediateErrorCB - ): HelloJSThenable; -} - -interface HelloJSEvent extends HelloJSThenable { - on(event: string, callback: (auth: HelloJSEventArgument) => void): HelloJSStatic; - off(event: string, callback: (auth: HelloJSEventArgument) => void): HelloJSStatic; - findEvents(event: string, callback: (name: string, index: number) => void): void; - emit(event: string, data: any): HelloJSStatic; - emitAfter(): HelloJSStatic; - success(callback: (json?: any) => void): HelloJSStatic; - error(callback: (json?: any) => void): HelloJSStatic; - complete(callback: (json?: any) => void): HelloJSStatic; -} - - - -interface HelloJSEventArgument { - network: string; - authResponse?: any; -} - - -interface HelloJSStatic extends HelloJSEvent { - init(serviceAppIds: { [id: string]: string; }, options?: HelloJSLoginOptions): void; - login(network: string, options?: HelloJSLoginOptions, callback?: () => void): HelloJSStatic; - logout(network: string, options?: HelloJSLogoutOptions, callback?: () => void): HelloJSStatic; - getAuthResponse(network: string): any; - service(network: string): HelloJSServiceDef; - settings: HelloJSLoginOptions; - (network: string): HelloJSStaticNamed; - init(servicesDef: { [id: string]: HelloJSServiceDef; }): void; -} - -interface HelloJSStaticNamed { - login(option?: HelloJSLoginOptions, callback?: () => void): void; - logout(callback?: () => void): void; - getAuthResponse(): any; - api(path?: string, method?: string, data?: any, callback?: (json?: any) => void): HelloJSStatic; -} - -interface HelloJSOAuthDef { - version: number; - auth: string; - request: string; - token: string; -} - -interface HelloJSServiceDef { - name: string; - oauth: HelloJSOAuthDef; - scope?: { [id: string]: string; }; - scope_delim?: string; - autorefresh?: boolean; - base?: string; - root?: string; - get?: { [id: string]: any; }; - post?: { [id: string]: any; }; - del?: { [id: string]: string; }; - put?: { [id: string]: any; }; - wrap?: { [id: string]: (par: any) => void; }; - xhr?: (par: any) => void; - jsonp?: (par: any) => void; - form?: (par: any) => void; - api?: (...par: any[]) => void; -} - -declare var hello: HelloJSStatic; - -// Support AMD require -declare module 'hellojs' { - export = hello; +export = hello; + +declare const hello: hello.HelloJSStatic; + +declare namespace hello { + interface HelloJSUtils { + extend(r: object, ...a: any[]): any; + error(code: number, message: string): { code: number, message: string }; + qs(url: string, params?: object, formatFunction?: (param: any) => string): string; + param(o: object, formatFunction?: (param: any) => string): string; + param(s: string, formatFunction?: (param: string) => any): any; + store(name?: string, value?: any): any; + append(node: string | HTMLElement, attr: object | undefined | null, target: string | HTMLElement): HTMLElement; + iframe(src: string, redirectUri?: string): void; + merge(...a: any[]): any; + args(o: object, args: object): any | false; + url(): Location; + url(path: string): URL | HTMLAnchorElement; + diff(a: any[], b: any[]): any[]; + diffKey(a: any[], b: any[]): any[]; + unique(a: any[]): any[]; + isEmpty(obj: any): boolean; + Event: HelloJSEvent; + globalEvent(callback: () => void, guid?: string): string; + popup(url: string, redirectUri?: string, options?: object): Window | any; + responseHandler(window: Window, parent?: any): void; + } + + // API utilities + interface HelloJSUtils { + request(p: object, callback: HelloJSResponseCallback): void; + request_cors(callback: HelloJSResponseCallback): boolean; + domInstance(type: string, data: any): boolean; + clone(obj: T): T; + xhr(method: string, url: string, headers: object, data: any, callback: HelloJSResponseCallback): XMLHttpRequest; + jsonp(url: string, callback: () => void, callbackID?: string, timeout?: number): void; + post(url: string, data: any, options: object, callback: HelloJSResponseCallback, callbackID?: string, timeout?: number): void; + hasBinary(data: any): boolean; + isBinary(data: any): boolean; + toBlob(dataURI: string): Blob | string; + dataToJSON(p: any): any; + nodeListToJSON(nodelist: NodeList): any; + } + + type HelloJSResponseCallback = (r: any, headers: any) => void; + + type HelloJSTokenResponseType = "token" | "code"; + + interface HelloJSLoginOptions { + redirect_uri?: string; + display?: string; + scope?: string; + response_type?: HelloJSTokenResponseType; + force?: boolean | null; + oauth_proxy?: string; + timeout?: number; + default_service?: string; + popup?: HelloJSPopupOptions; + state?: string; + } + + interface HelloJSLogoutOptions { + force?: boolean; + } + + interface HelloJSPopupOptions { + resizable: number; + scrollbars: number; + width: number; + height: number; + } + + interface HelloJSEvent { + on(event: string, callback: (auth: HelloJSEventArgument) => void): HelloJSStatic; + off(event: string, callback: (auth: HelloJSEventArgument) => void): HelloJSStatic; + findEvents(event: string, callback: (name: string, index: number) => void): void; + emit(event: string, data: any): HelloJSStatic; + emitAfter(): HelloJSStatic; + } + + interface HelloJSEventArgument { + network: string; + authResponse?: any; + } + + interface HelloJSStatic extends HelloJSEvent { + init(serviceAppIds: { [id: string]: string; }, options?: HelloJSLoginOptions): void; + init(servicesDef: { [id: string]: HelloJSServiceDef; }): void; + login(callback: () => void): PromiseLike; + login(options?: HelloJSLoginOptions, callback?: () => void): PromiseLike; + login(network?: string, options?: HelloJSLoginOptions, callback?: () => void): PromiseLike; + logout(callback?: () => void): PromiseLike; + logout(options?: HelloJSLogoutOptions, callback?: () => void): PromiseLike; + logout(network?: string, options?: HelloJSLogoutOptions, callback?: () => void): PromiseLike; + getAuthResponse(network?: string): any; + settings: HelloJSLoginOptions; + (network: string): HelloJSStatic; + utils: HelloJSUtils; + api(options: object): PromiseLike; + api(path?: string, method?: string, data?: object, callback?: (json: any) => void): PromiseLike; + api(path?: string, query?: object, method?: string, data?: object, timeout?: number, callback?: (json: any) => void): PromiseLike; + } + + interface HelloJSOAuthDef { + version: string | number; + auth?: string; + } + + interface HelloJSOAuth2Def extends HelloJSOAuthDef { + grant?: string; + response_type?: HelloJSTokenResponseType; + } + + interface HelloJSOAuth1Def extends HelloJSOAuthDef { + request?: string; + token?: string; + } + + type HelloJSUrlMappingFunction = (p: any, callback: (url: string) => void) => void; + + interface HelloJSServiceDef { + name?: string; + oauth: HelloJSOAuth2Def | HelloJSOAuth1Def; + scope?: { [id: string]: string; }; + scope_delim?: string; + refresh?: boolean; + base?: string; + root?: string; + get?: { [id: string]: string | HelloJSUrlMappingFunction }; + post?: { [id: string]: string | HelloJSUrlMappingFunction }; + del?: { [id: string]: string | HelloJSUrlMappingFunction }; + put?: { [id: string]: string | HelloJSUrlMappingFunction }; + patch?: { [id: string]: string | HelloJSUrlMappingFunction }; + wrap?: { [id: string]: (r: any, headers: any, p: any) => void; }; + xhr?(p: any, query: any): void; + jsonp?: ((p: any, query: any) => void) | boolean; + form?: ((p: any, query: any) => void) | boolean; + login?(p: any): void; + logout?: ((callback: () => void | string) => void) | string; + } } diff --git a/types/hellojs/tslint.json b/types/hellojs/tslint.json new file mode 100644 index 0000000000..2750cc0197 --- /dev/null +++ b/types/hellojs/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } \ No newline at end of file