"Fossies" - the Fresh Open Source Software Archive 
Member "cli-1.1260.0/src/lib/apps/rest-utils.ts" (4 Dec 2023, 3364 Bytes) of package /linux/misc/snyk-cli-1.1260.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /**
2 * Collection of utility function for the
3 * $snyk apps commands
4 */
5 import {
6 EAppsURL,
7 ICreateAppResponse,
8 IGetAppsURLOpts,
9 IRestErrorResponse,
10 SNYK_APP_DEBUG,
11 } from '.';
12 import chalk from 'chalk';
13 import { AuthFailedError, InternalServerError } from '../errors';
14 import * as Debug from 'debug';
15 import config from '../config';
16
17 const debug = Debug(SNYK_APP_DEBUG);
18
19 export function getAppsURL(
20 selection: EAppsURL,
21 opts: IGetAppsURLOpts = {},
22 ): string {
23 // Get the rest URL from user config
24 // Environment variable takes precendence over config
25 const baseURL = config.API_REST_URL;
26 debug(`API rest base URL => ${baseURL}`);
27
28 switch (selection) {
29 case EAppsURL.CREATE_APP:
30 return `${baseURL}/orgs/${opts.orgId}/apps`;
31 default:
32 throw new Error('Invalid selection for URL');
33 }
34 }
35
36 export function handleRestError(error: any): void {
37 if (error.code) {
38 if (error.code === 400) {
39 // Bad request
40 const responseJSON: IRestErrorResponse = error.body;
41 const errString = errorsToDisplayString(responseJSON);
42 throw new Error(errString);
43 } else if (error.code === 401) {
44 // Unauthorized
45 throw AuthFailedError();
46 } else if (error.code === 403) {
47 throw new Error(
48 'Forbidden! the authentication token does not have access to the resource.',
49 );
50 } else if (error.code === 404) {
51 const responseJSON: IRestErrorResponse = error.body;
52 const errString = errorsToDisplayString(responseJSON);
53 throw new Error(errString);
54 } else if (error.code === 500) {
55 throw new InternalServerError('Internal server error');
56 } else {
57 throw new Error(error.message);
58 }
59 } else {
60 throw error;
61 }
62 }
63
64 /**
65 * @param errRes RestError response
66 * @returns {String} Iterates over error and
67 * converts them into a readible string
68 */
69 function errorsToDisplayString(errRes: IRestErrorResponse): string {
70 const resString = `Uh oh! an error occurred while trying to create the Snyk App.
71 Please run the command with '--debug' or '-d' to get more information`;
72 if (!errRes.errors) return resString;
73 errRes.errors.forEach((e) => {
74 let metaString = '',
75 sourceString = '';
76 if (e.meta) {
77 for (const [key, value] of Object.entries(e.meta)) {
78 metaString += `${key}: ${value}\n`;
79 }
80 }
81 if (e.source) {
82 for (const [key, value] of Object.entries(e.source)) {
83 sourceString += `${key}: ${value}\n`;
84 }
85 }
86
87 const meta = metaString || '-';
88 const source = sourceString || '-';
89
90 return `Uh oh! an error occured while trying to create the Snyk App.
91
92 Error Description:\t${e.detail}
93 Request Status:\t${e.status}
94 Source:\t${source}
95 Meta:\t${meta}`;
96 });
97 return resString;
98 }
99
100 export function handleCreateAppRes(res: ICreateAppResponse): string {
101 debug(res);
102 const {
103 name,
104 client_id,
105 redirect_uris,
106 scopes,
107 is_public,
108 client_secret,
109 access_token_ttl_seconds,
110 } = res.data.attributes;
111
112 return `Snyk App created successfully!
113 Please ensure you save the following details:
114
115 App Name: ${name}
116 Client ID: ${client_id}
117 Redirect URIs: ${redirect_uris}
118 Scopes: ${scopes}
119 Is App Public: ${is_public}
120 Access token TTL seconds: ${access_token_ttl_seconds}
121 Client Secret (${chalk.redBright(
122 'keep it safe and protected',
123 )}): ${client_secret}`;
124 }