Created
November 15, 2018 09:34
-
-
Save sgr-ksmt/c0ec70a0a130894d1b7949e010dcbb46 to your computer and use it in GitHub Desktop.
Issue Dynamic Link via Cloud Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as functions from 'firebase-functions' | |
| import * as request from 'request-promise-native' | |
| export namespace IssueDynamicLink { | |
| export interface Request { | |
| params: DynamicLinkParameters | |
| } | |
| export interface Response { | |
| url: string | |
| } | |
| } | |
| export interface DynamicLinkParameters { | |
| link: string | |
| iosInfo?: { | |
| iosBundleId?: BundleIdentifier | |
| iosFallbackLink?: string | |
| iosAppStoreId?: AppID | |
| } | |
| socialMetaTagInfo?: { | |
| socialTitle?: string | |
| socialDescription?: string | |
| socialImageLink?: string | |
| } | |
| } | |
| export const onCalledIssueDynamicLink = functions.https.onCall(async (data, context) => { | |
| try { | |
| return issueDynamicLink(data, context) | |
| } catch (error) { | |
| console.error(error) | |
| throw error | |
| } | |
| }) | |
| const issueDynamicLink = async (data: IssueDynamicLink.Request, context: CallableContext) => { | |
| const json = { | |
| 'dynamicLinkInfo': { | |
| 'dynamicLinkDomain': 'example.page.link', | |
| 'link': data.params.link, | |
| 'iosInfo': data.params.iosInfo, | |
| 'socialMetaTagInfo': data.params.socialMetaTagInfo | |
| }, | |
| 'suffix': { | |
| 'option': 'SHORT' | |
| } | |
| } | |
| const options: request.Options = { | |
| uri: `https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=${API_KEY}`, | |
| method: 'POST', | |
| headers: { | |
| 'content-type': 'application/json' | |
| }, | |
| json: JSON.parse(JSON.stringify(json)) | |
| } | |
| const response = await request(options) | |
| return { url: response.shortLink } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment