diff --git a/types/fbinstant/fbinstant-tests.ts b/types/fbinstant/fbinstant-tests.ts new file mode 100644 index 0000000000..f4a72ea70e --- /dev/null +++ b/types/fbinstant/fbinstant-tests.ts @@ -0,0 +1,60 @@ +declare const FBInstant: FBInstant; + +export class FBInstantTest { + winStreak: number; + + init() { + FBInstant.initializeAsync().then(() => { + FBInstant.setLoadingProgress(100); + FBInstant.startGameAsync().then(() => { + this.startGame(); + }); + }); + } + + startGame() { + const contextId = FBInstant.context.getID(); + const contextType = FBInstant.context.getType(); + + const playerName = FBInstant.player.getName(); + const playerPic = FBInstant.player.getPhoto(); + const playerId = FBInstant.player.getID(); + } + + saveState() { + FBInstant.player.setDataAsync({ + score: this.winStreak + }); + } + + getState() { + FBInstant.player.getDataAsync(['score']) + .then((data) => { + if (typeof data['score'] !== 'undefined') { + this.winStreak = +data['score']; + } + }); + } + + update() { + FBInstant.updateAsync({ + action: 'CUSTOM', + cta: 'Play', + image: '', + text: { + default: 'Edgar played their move', + localizations: { + en_US: 'Edgar played their move', + es_LA: '\u00A1Edgar jug\u00F3 su jugada!' + } + }, + template: 'play_turn', + data: { myReplayData: '...' }, + strategy: 'IMMEDIATE', + notification: 'NO_PUSH' + }).then(() => { + // closes the game after the update is posted. + FBInstant.quit(); + }); + } +} diff --git a/types/fbinstant/index.d.ts b/types/fbinstant/index.d.ts new file mode 100644 index 0000000000..c2237c749c --- /dev/null +++ b/types/fbinstant/index.d.ts @@ -0,0 +1,221 @@ +// Type definitions for FBInstant 6.1 +// Project: https://developers.facebook.com/docs/games/instant-games +// Definitions by: Menushka Weeratunga +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare class FBInstant { + player: Player; + context: Context; + payments: Payments; + + getLocale(): string; + getPlatform(): string; + getSDKVersion(): string; + initializeAsync(): Promise; + setLoadingProgress(progress: number): void; + getSupportedAPIs(): string[]; + getEntryPointData(): any; + getEntryPointAsync(): Promise; + setSessionData(sessionData: any): void; + startGameAsync(): Promise; + shareAsync(payload: SharePayload): Promise; + updateAsync(payload: UpdatePayload): Promise; + switchGameAsync(appID: string, data: string): Promise; + canCreateShortcutAsync(): Promise; + createShortcutAsync(): Promise; + quit(): void; + logEvent(eventName: string, valueToSum?: number, parameter?: any): APIError; + onPause(func: () => void): void; + getInterstitialAdAsync(placementID: string): Promise; + getRewardedVideoAsync(placementID: string): Promise; + matchPlayerAsync(matchTag: string, switchContextWhenMatched?: boolean): Promise; + checkCanPlayerMatchAsync(): Promise; + getLeaderboardAsync(name: string): Promise; +} + +declare class Player { + getID(): string; + getSignedPlayerInfoAsync(requestPayload: string): Promise; + canSubscribeBotAsync(): Promise; + subscribeBotAsync(): Promise; + getName(): string; + getPhoto(): string; + getDataAsync(keys: string[]): Promise; + setDataAsync(data: DataObject): Promise; + flushDataAsync(): Promise; + getStatsAsync(keys: string[]): Promise; + setStatsAsync(stats: StatsObject): Promise; + incrementStatsAsync(increments: IncrementObject): Promise; + getConnectedPlayersAsync(): Promise; +} + +declare class Context { + getID(): string; + getType(): Type; + isSizeBetween(minSize: number, maxSize: number): ContextSizeResponse; + switchAsync(id: string): Promise; + chooseAsync(options: ContextOptions): Promise; + createAsync(playerID: string): Promise; + getPlayersAsync(): Promise; +} + +declare class Leaderboard { + getName(): string; + getContextID(): string; + getEntryCountAsync(): Promise; + setScoreAsync(score: number, extraData: string): Promise; + getPlayerEntryAsync(): Promise; + getEntriesAsync(count: number, offset: number): Promise; +} + +declare class LeaderboardEntry { + getScore(): number; + getFormattedScore(): string; + getTimestamp(): number; + getRank(): number; + getExtraData(): string; + getPlayer(): LeaderboardPlayer; +} + +declare class LeaderboardPlayer { + getName(): string; + getPhoto(): string; + getID(): string; +} + +declare class ConnectedPlayer { + getID(): string; + getName(): string; + getPhoto(): string; +} + +declare class SignedPlayerInfo { + getPlayerID(): string; + getSignature(): string; +} + +declare class ContextPlayer { + getID(): string; + getName(): string; + getPhoto(): string; +} + +declare class AdInstance { + getPlacementID(): string; + loadAsync(): Promise; + showAsync(): Promise; +} + +declare class Payments { + purchaseAsync(purchaseConfig: PurchaseConfig): Promise; + getPurchasesAsync(): Promise; + consumePurchaseAsync(purchaseToken: string): Promise; + onReady(callback: () => void): void; +} + +declare interface Product { + title: string; + productID: string; + description?: string; + imageURI?: string; + price: string; + priceCurrencyCode: string; +} + +declare interface ContextSizeResponse { + answer: boolean; + minSize?: number; + maxSize?: number; +} + +declare interface Purchase { + developerPayload?: string; + paymentID: string; + productID: string; + purchaseTime: string; + purchaseToken: string; + signedRequest: SignedPurchaseRequest; +} + +declare interface ContextOptions { + filters?: ContextFilter[]; + maxSize?: number; + minSize?: number; +} + +declare interface SharePayload { + intent?: Intent; + image?: string; + text?: string; + data?: any; +} + +declare interface APIError { + code: ErrorCodeType; + message: string; +} + +declare interface PurchaseConfig { + productID: string; + developerPayload?: string; +} + +declare interface UpdatePayload { + action?: UpdateAction; + template?: string; + cta?: (string | LocalizableContent); + image?: string; + text?: (string | LocalizableContent); + data?: any; + strategy?: string; + notification?: string; +} + +declare interface LeaderboardUpdatePayload { + action: UpdateAction; + name: string; + text?: string; +} + +declare interface LocalizableContent { + default: string; + localizations: LocalizationsDict; +} + +declare interface DataObject { [ key: string ]: string | number; } + +declare interface StatsObject { [ key: string ]: string | number; } + +declare interface IncrementObject { [ key: string ]: number; } + +declare type LocalizationsDict = any; + +declare type SignedPurchaseRequest = string; + +declare type ContextFilter = "NEW_CONTEXT_ONLY" | "INCLUDE_EXISTING_CHALLENGES" | "NEW_PLAYERS_ONLY"; + +declare type UpdateAction = "CUSTOM" | "LEADERBOARD"; + +declare type Platform = "IOS" | "ANDROID" | "WEB" | "MOBILE_WEB"; + +declare type Type = "POST" | "THREAD" | "GROUP" | "SOLO"; + +declare type Intent = "INVITE" | "REQUEST" | "CHALLENGE" | "SHARE"; + +declare type ErrorCodeType = "ADS_FREQUENT_LOAD" | + "ADS_NO_FILL" | + "ADS_NOT_LOADED" | + "ADS_TOO_MANY_INSTANCES" | + "ANALYTICS_POST_EXCEPTION" | + "CLIENT_REQUIRES_UPDATE" | + "CLIENT_UNSUPPORTED_OPERATION" | + "INVALID_OPERATION" | + "INVALID_PARAM" | + "LEADERBOARD_NOT_FOUND" | + "LEADERBOARD_WRONG_CONTEXT" | + "NETWORK_FAILURE" | + "PENDING_REQUEST" | + "RATE_LIMITED" | + "SAME_CONTEXT" | + "UNKNOWN" | + "USER_INPUT"; diff --git a/types/fbinstant/tsconfig.json b/types/fbinstant/tsconfig.json new file mode 100644 index 0000000000..7cb5ffc8ae --- /dev/null +++ b/types/fbinstant/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "fbinstant-tests.ts" + ] +} diff --git a/types/fbinstant/tslint.json b/types/fbinstant/tslint.json new file mode 100644 index 0000000000..71918edac3 --- /dev/null +++ b/types/fbinstant/tslint.json @@ -0,0 +1,8 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + // Needs to be declares as it cannot be imported in typescript + // using the standard import statement + "strict-export-declare-modifiers": false + } +}