Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Type definitions for FBInstant v2.1 (deprecated)
// Type definitions for FBInstant v2.1
// Api documentation : https://developers.facebook.com/docs/games/fbinstant-api
//
/**
* Top level namespace for the Instant Games SDK.
*/
declare var FBInstant:fbinstant.IFBInstant;
declare namespace fbinstant
{
interface IPlayer
{
/**
* A unique identifier for the player. This should remain the same for the same Facebook user. The same Facebook user won't have the same identifier when in different game apps. The value will always be null until FBInstant.initializeAsync() resolves.
* @return string A unique identifier for the player.
*/
getID(): string;
/**
* The player's localized display name. The value will always be null until FBInstant.initializeAsync() resolves.
* @return string The player's localized display name.
*/
getName(): string;
/**
* A url to the player's public profile photo. The photo will always be a square, and with dimensions of at least 200x200. When rendering it in the game, the exact dimensions should never be assumed to be constant. It's recommended to always scale the image to a desired size before rendering. The value will always be null until FBInstant.initializeAsync() resolves.
* WARNING: Due to CORS, using these photos in the game canvas can cause it to be tainted, which will prevent the canvas data from being extracted. To prevent this, set the cross-origin attribute of the images you use to 'anonymous'.
* @return string Url to the player's public profile photo.
*/
getPhoto(): string;
/**
* Retrieve data from the designated cloud storage of the current player.
* @param keys string[] An array of unique keys to retrieve data for.
* @return PromiseLike<Object> A promise that resolves with an object which contains the current key-value pairs for each key specified in the input array, if they exist.
*/
getDataAsync(keys: string[]): PromiseLike<Object>;
/**
* Save data to the designated cloud storage of the current player. You can save a maximum of 1MB data for each player.
* @param data Object An object containing a set of key-value pairs that should be persisted to cloud storage. The object must contain only serializable values - any non-serializable values will cause the entire modification to be rejected.
* @return PromiseLike<void> A promise that resolves when the input values are set.
*/
setDataAsync(data: Object): PromiseLike<void>;
}
interface IContext
{
/**
* A unique identifier for the current game context.
* This represents a specific context that the game is being played in.
* For example a particular messenger conversation or facebook post.
* This identifier will be populated only after FBInstant.startGameAsync resolves, and may be updated
* when FBInstant.endGameAsync resolves. It will return null if the current mobile app version doesn't
* support context id, or when the game is being played in a solo context.
* @return string A unique identifier for the current game context.
*/
getID(): string;
/**
* Type of the current game context. Could be 'post', 'thread', 'group', or 'solo'.
* @return string Returns string Type of the current game context.
*/
getType(): string;
}
interface IFBInstant
{
/**
* The current locale. See https://www.facebook.com/translations/FacebookLocales.xml for a complete list of supported locale values.
* Use this to determine what language the current game should be localized with. The value will always be null until FBInstant.initializeAsync() resolves.
* @return string The current locale.
*/
getLocale(): string;
/**
* The platform on which the game is currently running. Possible values are: 'iOS', 'android' and 'web'. The value will always be null until FBInstant.initializeAsync() resolves.
* @return string The platform on which the game is currently running.
*/
getPlatform(): string;
/**
* A string indicating the version of the sdk.
* @return string Version of the sdk.
*/
getSDKVersion(): string;
/**
* Initializes the SDK library. This should be called before any other SDK functions.
* @return PromiseLike<void> A promise that resolves when the sdk is ready to use.
*/
initializeAsync(): PromiseLike<void>;
/**
* Report the progress of initial resource loading.
* @param percentage number A number between 0 and 100.
*/
setLoadingProgress(percentage: number):void;
/**
* This indicates the game has finished loading resources and is ready to start.
* FBInstant.context.id will be set to its expected value after the returned promise resolves.
* @return PromiseLike<void> A promise that resolves when the game should be started. This signal indicates that the user has explicitly expressed the intention to start playing, and the game should not implement another "tap to start" step after this.
*/
startGameAsync(): PromiseLike<void>;
/**
* Contains functions and properties related to the current player.
*/
player: IPlayer;
/**
* Contains functions and properties related to the current game context.
*/
context: IContext;
/**
* Update the score. This function should be called as frequently as possible whenever the player's score changes in the game. These score updates will be used by the platform to generate real time updates on user experience outside of the game. When endGameAsync is called, the latest score reported via this API will be used for the leaderboards.
* @param score number The user's score in the game
*/
setScore(score: number):void;
/**
* This yields control back to the player, which shows the end screen.
* FBInstant.context.id may be updated when the returned promise resolves.
* @return PromiseLike<void> A promise that resolves when the game is restarted.
*/
endGameAsync(): PromiseLike<void>;
/**
* Attempt to take a screenshot which can be shared by the user later.
* @return PromiseLike<void> A promise that resolves when the screenshot has been recorded successfully, or rejects if the recording fails, returning the relevant error message.
*/
takeScreenshotAsync(): PromiseLike<void>;
/**
* Send a screenshot image which can be shared by the user later.
* @param base64picture string with the picture to be send in a base64 encoding.
* @return A promise that resolves if the screenshot is accepted. The promise will reject if we failed to process the image, in which case the error message will explain the exact reason
*/
sendScreenshotAsync(base64picture: string):PromiseLike<void>;
/**
* Abort the game. This should only be called if the game gets into an unrecoverable state.
* @param e Error An Error containing information about what went wrong.
*/
abort(e: Error):void;
/**
* Log an app event with FB Analytics. See https://developers.facebook.com/docs/javascript/reference/v2.8#app_events for more details about FB Analytics.
* @param eventName string Name of the event. Must be 2 to 40 characters, and can only contain '_', '-', ' ', and alphanumeric characters.
* @param valueToSum number An optional numeric value that FB Analytics can calculate a sum with.
* @param parameters Object An optional object that can contain up to 25 key-value pairs to be logged with the event. Keys must be 2 to 40 characters, and can only contain '_', '-', ' ', and alphanumeric characters. Values must be less than 100 characters in length.
*/
logEvent(eventName: string, valueToSum:number, parameters:Object):void;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.