microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/appcenter/api/createClient.ts
45lines · modeblame
4b37483dmax-mironov8 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
86b74817ruslan-bikkinin8 years ago | 4 | import AppCenterClient from "../lib/app-center-node-client/index"; |
4b37483dmax-mironov8 years ago | 5 | import { AppCenterClientCredentials } from "./appCenterClientCredentials"; |
| 6 | import * as Q from "q"; | |
| 7 | import { Profile } from "../auth/profile/profile"; | |
| 8 | | |
| 9 | export interface AppCenterClientFactory { | |
| 10 | fromToken(token: string | Q.Promise<string> | {(): Q.Promise<string>}, endpoint: string): AppCenterClient; | |
| 11 | fromProfile(user: Profile, endpoint: string): AppCenterClient | null; | |
| 12 | } | |
| 13 | | |
| 14 | export function createAppCenterClient(): AppCenterClientFactory { | |
| 15 | return { | |
| 16 | fromToken(token: string | Q.Promise<string> | {(): Q.Promise<string>}, endpoint: string): AppCenterClient { | |
| 17 | let tokenFunc: {(): Q.Promise<string>}; | |
| 18 | | |
| 19 | if (typeof token === "string") { | |
| 20 | tokenFunc = () => Q.resolve(<string>token); | |
| 21 | } else if (typeof token === "object") { | |
| 22 | tokenFunc = () => <Q.Promise<string>>token; | |
| 23 | } else { | |
| 24 | tokenFunc = token; | |
| 25 | } | |
| 26 | return new AppCenterClient(new AppCenterClientCredentials(tokenFunc), endpoint); | |
| 27 | }, | |
| 28 | fromProfile(user: Profile, endpoint): AppCenterClient | null { | |
| 29 | if (!user) { | |
| 30 | return null; | |
| 31 | } | |
| 32 | return new AppCenterClient(new AppCenterClientCredentials(() => user.accessToken), endpoint); | |
| 33 | }, | |
| 34 | }; | |
| 35 | } | |
| 36 | | |
bb45fbe6max-mironov8 years ago | 37 | export function getQPromisifiedClientResult<T>(action: Promise<T>): Q.Promise<T> { |
4b37483dmax-mironov8 years ago | 38 | return Q.Promise((resolve, reject) => { |
| 39 | action.then((result: T) => { | |
| 40 | resolve(result); | |
| 41 | }).catch((e) => { | |
| 42 | reject(e); | |
| 43 | }); | |
| 44 | }); | |
| 45 | } |