diff --git a/types/tabris-plugin-firebase/index.d.ts b/types/tabris-plugin-firebase/index.d.ts new file mode 100644 index 0000000000..3a9e7b350f --- /dev/null +++ b/types/tabris-plugin-firebase/index.d.ts @@ -0,0 +1,152 @@ +// Type definitions for tabris-plugin-firebase 1.0 +// Project: https://github.com/eclipsesource/tabris-plugin-firebase/ +// Definitions by: EclipseSource +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +declare global { + namespace firebase { + const Analytics: Analytics; + const Messaging: Messaging; + const MessagingEvents: MessagingEvents; + const MessageEvent: MessageEvent; + type AnalyticsProperties = Partial; + + interface Analytics extends NativeObject, PropertyMixins.Analytics { + logEvent(eventName: string, parameters?: {[key: string]: string}): void; + setUserProperty(propertyName: string, value: string): void; + set(properties: AnalyticsProperties): this; + set(property: string, value: any): this; + } + + interface Messaging extends NativeObject { + readonly instanceId: string; + readonly token: string; + readonly launchData: object; + resetInstanceId(): void; + on(type: string, listener: (event: any) => void, context?: object): this; + on(listeners: MessagingEvents): this; + off(type: string, listener: (event: any) => void, context?: object): this; + off(listeners: MessagingEvents): this; + once(type: string, listener: (event: any) => void, context?: object): this; + once(listeners: MessagingEvents): this; + } + + interface MessagingEvents { + instanceIdChanged?(event: PropertyChangedEvent): void; + tokenChanged?(event: PropertyChangedEvent): void; + message?(event: MessageEvent): void; + } + + interface MessageEvent extends EventObject { + data: any; + } + + namespace PropertyMixins { + interface Analytics { + analyticsCollectionEnabled: boolean; + screenName: string; + userId: string; + } + } + } +} + +// Tabris.js interfaces + +interface EventObject { + readonly target: T; + readonly timeStamp: number; + readonly type: string; +} + +/** + * Base class for all objects with a native implementation. + */ +declare class NativeObject { + protected constructor(properties?: object); + + /** + * Gets the current value of the given *property*. + * @param property + */ + get(property: string): any; + + /** + * Removes all occurrences of *listener* that are bound to *type* and *context* from this widget. + * @param type The type of events to remove listeners for. + * @param listener The listener function to remove. + * @param context The context of the bound listener to remove. + */ + off(type: string, listener: (event: any) => void, context?: object): this; + + /** + * Removes all listeners in the given object from the event type indicated by their key. + * @param listeners A key-value map where the keys are event types and the values are the listeners to deregister from these events, e.g. `{tap: onTap, scroll: onScroll}`. + */ + off(listeners: object): this; + + /** + * Registers a *listener* function to be notified of events of the given *type*. + * @param type The type of events to listen for. + * @param listener The listener function to register. This function will be called with an event object. + * @param context In the listener function, `this` will point to this object. If not present, the listener will be called in the context of this object. + */ + on(type: string, listener: (event: any) => void, context?: object): this; + + /** + * Registers all listeners in the given object for the event type indicated by their key. + * @param listeners A key-value map where the keys are event types and the values are the listeners to register for these events, e.g. `{tap: onTap, scroll: onScroll}`. + */ + on(listeners: object): this; + + /** + * Same as `on`, but removes the listener after it has been invoked by an event. + * @param type The type of the event to listen for. + * @param listener The listener function to register. This function will be called with an event object. + * @param context In the listener function, `this` will point to this object. If not present, the listener will be called in the context of this object. + */ + once(type: string, listener: (event: any) => void, context?: object): this; + + /** + * Same as `on`, but removes the listener after it has been invoked by an event. + * @param listeners A key-value map where the keys are event types and the values are the listeners to register for these events, e.g. `{tap: onTap, scroll: onScroll}`. + */ + once(listeners: object): this; + + /** + * Sets the given property. + * @param property + * @param value + */ + set(property: string, value: any): this; + + /** + * Sets all key-value pairs in the properties object as widget properties. + * @param properties + */ + set(properties: object): this; + + /** + * Notifies all registered listeners for the given *type* and passes the *event* object to the + * listeners. + * @param type The type of event to trigger + * @param event The event object to pass to listener functions. + */ + trigger(type: string, event: EventObject): this; + + /** + * An application-wide unique identifier automatically assigned to all native objects on creation. + * @static + */ + readonly cid: string; +} + +interface PropertyChangedEvent { + readonly target: T; + readonly timeStamp: number; + readonly type: string; + readonly value: U; +} + +export {}; diff --git a/types/tabris-plugin-firebase/tabris-plugin-firebase-tests.ts b/types/tabris-plugin-firebase/tabris-plugin-firebase-tests.ts new file mode 100644 index 0000000000..8dff4f34a8 --- /dev/null +++ b/types/tabris-plugin-firebase/tabris-plugin-firebase-tests.ts @@ -0,0 +1,67 @@ +function testAnalytics() { + // Properties + let analyticsCollectionEnabled: boolean; + let screenName: string; + let userId: string; + + analyticsCollectionEnabled = firebase.Analytics.analyticsCollectionEnabled; + screenName = firebase.Analytics.screenName; + userId = firebase.Analytics.userId; + + firebase.Analytics.analyticsCollectionEnabled = analyticsCollectionEnabled; + firebase.Analytics.screenName = screenName; + firebase.Analytics.userId = userId; + + const properties: firebase.AnalyticsProperties = {analyticsCollectionEnabled, screenName, userId}; + const partialProperties: firebase.AnalyticsProperties = {}; + firebase.Analytics.set(properties); + firebase.Analytics.set(partialProperties); + + // Methods + let thisReturnValue: firebase.Analytics; + const name = ''; + const property = ''; + + thisReturnValue = firebase.Analytics.set({analyticsCollectionEnabled, screenName, userId}); + firebase.Analytics.logEvent(name, {foo: property}); + firebase.Analytics.logEvent(name); + firebase.Analytics.setUserProperty(name, property); +} + +function testMessaging() { + // Properties + let instanceId: string; + let token: string; + let launchData: object; + + instanceId = firebase.Messaging.instanceId; + token = firebase.Messaging.token; + launchData = firebase.Messaging.launchData; + + // Methods + firebase.Messaging.resetInstanceId(); + + // Events + const target: firebase.Messaging = firebase.Messaging; + const timeStamp = 0; + const type = 'foo'; + const value = 'bar'; + const data: any = {}; + + const instanceIdChangedEvent: PropertyChangedEvent = {target, timeStamp, type, value}; + const tokenChangedEvent: PropertyChangedEvent = {target, timeStamp, type, value}; + const messageEvent: firebase.MessageEvent = {target, timeStamp, type, data}; + + firebase.Messaging.on({ + instanceIdChanged: (event: PropertyChangedEvent) => {}, + tokenChanged: (event: PropertyChangedEvent) => {}, + message: (event: firebase.MessageEvent) => {} + }); +} + +interface PropertyChangedEvent { + readonly target: T; + readonly timeStamp: number; + readonly type: string; + readonly value: U; +} diff --git a/types/tabris-plugin-firebase/tsconfig.json b/types/tabris-plugin-firebase/tsconfig.json new file mode 100644 index 0000000000..38a1c8d42c --- /dev/null +++ b/types/tabris-plugin-firebase/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", + "tabris-plugin-firebase-tests.ts" + ] +} diff --git a/types/tabris-plugin-firebase/tslint.json b/types/tabris-plugin-firebase/tslint.json new file mode 100644 index 0000000000..4c4fc86ace --- /dev/null +++ b/types/tabris-plugin-firebase/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "strict-export-declare-modifiers": false + } +}