From 5de998bdb76c0354d43f5ef0511e08c2aebe96a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Houllier?= Date: Fri, 7 Apr 2017 10:17:05 +0200 Subject: [PATCH 1/2] Add type definitions for zetapush-js --- types/zetapush-js/index.d.ts | 187 +++++++++++++++++++++++++ types/zetapush-js/tsconfig.json | 22 +++ types/zetapush-js/tslint.json | 1 + types/zetapush-js/zetapush-js-tests.ts | 23 +++ 4 files changed, 233 insertions(+) create mode 100644 types/zetapush-js/index.d.ts create mode 100644 types/zetapush-js/tsconfig.json create mode 100644 types/zetapush-js/tslint.json create mode 100644 types/zetapush-js/zetapush-js-tests.ts diff --git a/types/zetapush-js/index.d.ts b/types/zetapush-js/index.d.ts new file mode 100644 index 0000000000..879f5e6f2c --- /dev/null +++ b/types/zetapush-js/index.d.ts @@ -0,0 +1,187 @@ +// Type definitions for zetapush-js 3.1 +// Project: https://github.com/zetapush/zetapush-js +// Definitions by: ghoullier +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface HandshakeFields { + ext: any; +} + +interface AbstractHandshakeOptions { + authType: string; + sandboxId: string; + deploymentId: string; +} + +interface CredentialsHandshakeOptions { + authType: string; + deploymentId: string; + login: string; + password: string; +} + +interface TokenHandshakeOptions { + authType: string; + deploymentId: string; + token: string; +} + +interface AbstractHandshake { + authType: string; + authVersion: string; + sandboxId: string; + deploymentId: string; + getHandshakeFields(client: Client): HandshakeFields; +} + +interface CredentialsAuthData { + login: string; + password: string; +} + +interface CredentialsHandshake extends AbstractHandshake { + login: string; + password: string; + authData: CredentialsAuthData; +} + +interface TokenAuthData { + token: string; +} + +interface TokenHandshake extends AbstractHandshake { + token: string; + authData: TokenAuthData; +} + +type AuthenticationCallback = () => AbstractHandshake; + +type AsyncMacroServicePublisher = (method: string, parameters: any, hardFail?: boolean, debug?: number) => Promise; + +type MacroServicePublisher = (method: string, parameters: any, hardFail?: boolean, debug?: number) => void; + +type ServicePublisher = (method: string, parameters: any) => void; + +interface Options { + apiUrl?: string; + sandboxId: string; + forceHttps?: boolean; + resource?: string; + transports?: any[]; +} + +interface ClientOptions extends Options { + authentication(): AbstractHandshake; +} + +interface WeakClientOptions extends Options { + deploymentId?: string; +} + +interface Services { + Macro: AsyncMacroService; + Messaging: Service; +} + +interface Service { + DEFAULT_DEPLOYMENT_ID: string; + $publish: ServicePublisher; +} + +interface AsyncMacroService { + DEFAULT_DEPLOYMENT_ID: string; + $publish: AsyncMacroServicePublisher; +} + +interface MacroService { + DEFAULT_DEPLOYMENT_ID: string; + $publish: MacroServicePublisher; +} + +interface ServiceDeclaration { + Type: Service; + deploymentId?: string; +} + +interface Token { + token: string; +} + +interface Credentials { + login: string; + password: string; +} + +interface ClientHelper { + authentication: AuthenticationCallback; + servers: Promise; + getUniqRequestId(): string; +} + +type ConnectionStatusHandler = number; + +export class Authentication { + static delegating({ token }: TokenAuthData): TokenHandshake; + static simple({ login, password }: CredentialsAuthData): CredentialsHandshake; + static weak({ token }: TokenAuthData): TokenHandshake; +} + +export interface ConnectionStatusListener { + onConnectionBroken(): void; + onConnectionClosed(): void; + onConnectionEstablished(): void; + onConnectionToServerFail(failure: any): void; + onConnectionWillClose(): void; + onFailedHandshake(failure: any): void; + onMessageLost(): void; + onNoServerUrlAvailable(): void; + onSuccessfulHandshake(authentication: any): void; +} + +export class Client { + helper: ClientHelper; + constructor(options: ClientOptions); + addConnectionStatusListener(listener: ConnectionStatusListener): ConnectionStatusHandler; + connect(): void; + createService(declaration: ServiceDeclaration): Service; + createAsyncMacroService(declaration: ServiceDeclaration): AsyncMacroService; + disconnect(): void; + isConnected(): boolean; + getSandboxId(): string; + getResource(): string; + getUserId(): string; + removeConnectionStatusListener(listener: ConnectionStatusHandler): void; + setAuthentication(authentication: AuthenticationCallback): void; + setLogLevel(level: string): void; + setResource(resource: string): void; + unsubscribe(service: Service): void; + // + onConnectionBroken(handler: () => void): void; + onConnectionClosed(handler: () => void): void; + onConnectionEstablished(handler: () => void): void; + onConnectionToServerFail(handler: (failure: any) => void): void; + onConnectionWillClose(handler: () => void): void; + onFailedHandshake(handler: (failure: any) => void): void; + onMessageLost(handler: () => void): void; + onNoServerUrlAvailable(handler: () => void): void; + onSuccessfulHandshake(handler: (authentication: any) => void): void; +} + +export class SmartClient extends Client { + getCredentials(): any; + getSession(): any; + hasCredentials(): boolean; + isStronglyAuthenticated(session?: any): boolean; + isWeaklyAuthenticated(session?: any): boolean; + setCredentials(credentials: any): void; +} +export class WeakClient extends Client { + constructor(options: WeakClientOptions); + getToken(): Token; +} + +export const services: Services; + +export const VERSION: string; + +export as namespace ZetaPush; diff --git a/types/zetapush-js/tsconfig.json b/types/zetapush-js/tsconfig.json new file mode 100644 index 0000000000..78526186fb --- /dev/null +++ b/types/zetapush-js/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "zetapush-js-tests.ts" + ] +} diff --git a/types/zetapush-js/tslint.json b/types/zetapush-js/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/types/zetapush-js/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" } diff --git a/types/zetapush-js/zetapush-js-tests.ts b/types/zetapush-js/zetapush-js-tests.ts new file mode 100644 index 0000000000..46eb822680 --- /dev/null +++ b/types/zetapush-js/zetapush-js-tests.ts @@ -0,0 +1,23 @@ +import { Authentication, Client, WeakClient } from 'zetapush-js'; + +const weak = new WeakClient({ + sandboxId: '' +}); + +weak.onConnectionEstablished(() => { + // onConnectionEstablished; +}); +weak.connect(); + +const client = new Client({ + apiUrl: '', + sandboxId: '', + authentication: () => Authentication.simple({ + login: 'login', + password: 'password' + }) +}); +client.onConnectionEstablished(() => { + // onConnectionEstablished; +}); +client.connect(); From 34791d816d53c811c8078d2ffe0cbccda746924a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 20 Apr 2017 11:02:49 -0700 Subject: [PATCH 2/2] Fix lint --- types/zetapush-js/index.d.ts | 50 +++++++++++++++++------------------ types/zetapush-js/tslint.json | 2 +- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/types/zetapush-js/index.d.ts b/types/zetapush-js/index.d.ts index 879f5e6f2c..afe0e3c6b0 100644 --- a/types/zetapush-js/index.d.ts +++ b/types/zetapush-js/index.d.ts @@ -3,30 +3,30 @@ // Definitions by: ghoullier // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -interface HandshakeFields { +export interface HandshakeFields { ext: any; } -interface AbstractHandshakeOptions { +export interface AbstractHandshakeOptions { authType: string; sandboxId: string; deploymentId: string; } -interface CredentialsHandshakeOptions { +export interface CredentialsHandshakeOptions { authType: string; deploymentId: string; login: string; password: string; } -interface TokenHandshakeOptions { +export interface TokenHandshakeOptions { authType: string; deploymentId: string; token: string; } -interface AbstractHandshake { +export interface AbstractHandshake { authType: string; authVersion: string; sandboxId: string; @@ -34,35 +34,35 @@ interface AbstractHandshake { getHandshakeFields(client: Client): HandshakeFields; } -interface CredentialsAuthData { +export interface CredentialsAuthData { login: string; password: string; } -interface CredentialsHandshake extends AbstractHandshake { +export interface CredentialsHandshake extends AbstractHandshake { login: string; password: string; authData: CredentialsAuthData; } -interface TokenAuthData { +export interface TokenAuthData { token: string; } -interface TokenHandshake extends AbstractHandshake { +export interface TokenHandshake extends AbstractHandshake { token: string; authData: TokenAuthData; } -type AuthenticationCallback = () => AbstractHandshake; +export type AuthenticationCallback = () => AbstractHandshake; -type AsyncMacroServicePublisher = (method: string, parameters: any, hardFail?: boolean, debug?: number) => Promise; +export type AsyncMacroServicePublisher = (method: string, parameters: any, hardFail?: boolean, debug?: number) => Promise; -type MacroServicePublisher = (method: string, parameters: any, hardFail?: boolean, debug?: number) => void; +export type MacroServicePublisher = (method: string, parameters: any, hardFail?: boolean, debug?: number) => void; -type ServicePublisher = (method: string, parameters: any) => void; +export type ServicePublisher = (method: string, parameters: any) => void; -interface Options { +export interface Options { apiUrl?: string; sandboxId: string; forceHttps?: boolean; @@ -70,55 +70,55 @@ interface Options { transports?: any[]; } -interface ClientOptions extends Options { +export interface ClientOptions extends Options { authentication(): AbstractHandshake; } -interface WeakClientOptions extends Options { +export interface WeakClientOptions extends Options { deploymentId?: string; } -interface Services { +export interface Services { Macro: AsyncMacroService; Messaging: Service; } -interface Service { +export interface Service { DEFAULT_DEPLOYMENT_ID: string; $publish: ServicePublisher; } -interface AsyncMacroService { +export interface AsyncMacroService { DEFAULT_DEPLOYMENT_ID: string; $publish: AsyncMacroServicePublisher; } -interface MacroService { +export interface MacroService { DEFAULT_DEPLOYMENT_ID: string; $publish: MacroServicePublisher; } -interface ServiceDeclaration { +export interface ServiceDeclaration { Type: Service; deploymentId?: string; } -interface Token { +export interface Token { token: string; } -interface Credentials { +export interface Credentials { login: string; password: string; } -interface ClientHelper { +export interface ClientHelper { authentication: AuthenticationCallback; servers: Promise; getUniqRequestId(): string; } -type ConnectionStatusHandler = number; +export type ConnectionStatusHandler = number; export class Authentication { static delegating({ token }: TokenAuthData): TokenHandshake; diff --git a/types/zetapush-js/tslint.json b/types/zetapush-js/tslint.json index 377cc837d4..3db14f85ea 100644 --- a/types/zetapush-js/tslint.json +++ b/types/zetapush-js/tslint.json @@ -1 +1 @@ -{ "extends": "../tslint.json" } +{ "extends": "dtslint/dt.json" }