microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.8

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/appcenter/api/createClient.ts

45lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import AppCenterClient from "../lib/app-center-node-client/index";
5import { AppCenterClientCredentials } from "./appCenterClientCredentials";
6import * as Q from "q";
7import { Profile } from "../auth/profile/profile";
8
9export 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
14export 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
37export function getQPromisifiedClientResult<T>(action: Promise<T>): Q.Promise<T> {
38 return Q.Promise((resolve, reject) => {
39 action.then((result: T) => {
40 resolve(result);
41 }).catch((e) => {
42 reject(e);
43 });
44 });
45}