/
create-app.ts
57 lines (54 loc) · 1.35 KB
/
create-app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as Debug from 'debug';
import {
EAppsURL,
getAppsURL,
handleCreateAppRes,
handleV3Error,
ICreateAppRequest,
ICreateAppResponse,
SNYK_APP_DEBUG,
} from '../../../lib/apps';
import { makeRequestV3 } from '../../../lib/request/promise';
import { spinner } from '../../../lib/spinner';
const debug = Debug(SNYK_APP_DEBUG);
/**
* Function to process the app creation request and
* handle any errors that are request error and print
* in a formatted string. It throws is error is unknown
* or cannot be handled.
* @param {ICreateAppRequest} data to create the app
* @returns {String} response formatted string
*/
export async function createApp(
data: ICreateAppRequest,
): Promise<string | void> {
debug('App data', data);
const {
orgId,
snykAppName: name,
snykAppRedirectUris: redirectUris,
snykAppScopes: scopes,
} = data;
const payload = {
method: 'POST',
url: getAppsURL(EAppsURL.CREATE_APP, { orgId }),
body: {
name,
redirectUris,
scopes,
},
qs: {
version: '2021-08-11~experimental',
},
};
try {
await spinner('Creating your Snyk App');
const response = await makeRequestV3<ICreateAppResponse>(payload);
debug(response);
spinner.clearAll();
return handleCreateAppRes(response);
} catch (error) {
spinner.clearAll();
handleV3Error(error);
}
}