๐Ÿ“ฆ Kong / insomnia

๐Ÿ“„ api-spec.ts ยท 33 lines
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// @ts-expect-error the enquirer types are incomplete https://github.com/enquirer/enquirer/pull/307
import { AutoComplete } from 'enquirer';

import { logger } from '../../logger';
import type { Database } from '../index';
import type { ApiSpec } from './types';
import { ensureSingleOrNone, generateIdIsh, getDbChoice, matchIdIsh } from './util';
const entity = 'api specification';

export const loadApiSpec = (db: Database, identifier: string): ApiSpec | null | undefined => {
  logger.trace('Load %s with identifier `%s` from data store', entity, identifier);
  const items = db.ApiSpec.filter(
    spec => matchIdIsh(spec, identifier) || spec.fileName === identifier || spec.name === identifier,
  );
  logger.trace('Found %d api specification(s).', items.length);
  return ensureSingleOrNone(items, entity);
};

export const promptApiSpec = async (db: Database, ci: boolean): Promise<ApiSpec | null | undefined> => {
  if (ci || !db.ApiSpec.length) {
    return null;
  }

  const prompt = new AutoComplete({
    name: 'apiSpec',
    message: 'Select an API Specification',
    choices: db.ApiSpec.map(s => getDbChoice(generateIdIsh(s), s.fileName)),
  });
  logger.trace('Prompt for %s', entity);
  const [idIsh] = (await prompt.run()).split(' - ').reverse();
  return loadApiSpec(db, idIsh);
};