"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/src/lib/apps/create-app/index.ts" (20 Feb 2024, 2675 Bytes) of package /linux/misc/snyk-cli-1.1280.1.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 import {
    2   AppContext,
    3   AppsErrorMessages,
    4   createAppPrompts,
    5   ICreateAppRequest,
    6   ICreateAppOptions,
    7   SNYK_APP_NAME,
    8   SNYK_APP_REDIRECT_URIS,
    9   SNYK_APP_SCOPES,
   10   SNYK_APP_ORG_ID,
   11   SNYK_APP_CONTEXT,
   12   validateUUID,
   13   validateAllURL,
   14 } from '..';
   15 import * as enquirer from 'enquirer';
   16 import { ValidationError } from '../../errors';
   17 
   18 /**
   19  * Validates and parsed the data required to create app.
   20  * Throws error if option is not provided or is invalid
   21  * @param {ICreateAppOptions} options required to create an app
   22  * @returns {ICreateAppRequest} data that is used to make the request
   23  */
   24 export function createAppDataScriptable(
   25   options: ICreateAppOptions,
   26 ): ICreateAppRequest {
   27   if (!options.org) {
   28     throw new ValidationError(AppsErrorMessages.orgRequired);
   29   } else if (typeof validateUUID(options.org) === 'string') {
   30     // Combines to form "Invalid UUID provided for org id"
   31     throw new ValidationError(`${validateUUID(options.org)} for org id`);
   32   } else if (!options.name) {
   33     throw new ValidationError(AppsErrorMessages.nameRequired);
   34   } else if (!options['redirect-uris']) {
   35     throw new ValidationError(AppsErrorMessages.redirectUrisRequired);
   36   } else if (typeof validateAllURL(options['redirect-uris']) === 'string') {
   37     throw new ValidationError(
   38       validateAllURL(options['redirect-uris']) as string,
   39     );
   40   } else if (!options.scopes) {
   41     throw new ValidationError(AppsErrorMessages.scopesRequired);
   42   } else if (
   43     options.context != null &&
   44     !(options.context == 'user' || options.context == 'tenant')
   45   ) {
   46     throw new ValidationError(AppsErrorMessages.invalidContext);
   47   } else {
   48     return {
   49       orgId: options.org,
   50       snykAppName: options.name,
   51       snykAppRedirectUris: options['redirect-uris']
   52         .replace(/\s+/g, '')
   53         .split(','),
   54       snykAppScopes: options.scopes.replace(/\s+/g, '').split(','),
   55       context: options.context,
   56     };
   57   }
   58 }
   59 
   60 // Interactive format
   61 export async function createAppDataInteractive(): Promise<ICreateAppRequest> {
   62   // Proceed with interactive
   63   const answers = await enquirer.prompt(createAppPrompts);
   64   // Process answers
   65   const snykAppName = answers[SNYK_APP_NAME].trim() as string;
   66   const snykAppRedirectUris = answers[SNYK_APP_REDIRECT_URIS].replace(
   67     /\s+/g,
   68     '',
   69   ).split(',') as string[];
   70   const snykAppScopes = answers[SNYK_APP_SCOPES].replace(/\s+/g, '').split(
   71     ',',
   72   ) as string[];
   73   const orgId = answers[SNYK_APP_ORG_ID].trim() as string;
   74   const context = answers[SNYK_APP_CONTEXT].trim() as AppContext;
   75   // POST: to create an app
   76   return {
   77     orgId,
   78     snykAppName,
   79     snykAppRedirectUris,
   80     snykAppScopes,
   81     context,
   82   };
   83 }