|
| 1 | +import 'dotenv/config'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | +import { filterPathsByReqType, getPathParams, validatePath } from './utils.js'; |
| 6 | +import searchSelect from './searchSelect.js'; |
| 7 | +import { checkbox, input } from '@inquirer/prompts'; |
| 8 | +import { makeRequest } from './fetch.js'; |
| 9 | +import { DEFAULT_PARAMS } from './lib/params.js'; |
| 10 | + |
| 11 | +const __filename = fileURLToPath(import.meta.url); |
| 12 | +const __dirname = path.dirname(__filename); |
| 13 | + |
| 14 | +export interface Props { |
| 15 | + outputFile: string | undefined; |
| 16 | + inputFile: string; |
| 17 | + inputPath: string | undefined; |
| 18 | + useDefaults: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +export const main = async ({ outputFile, inputFile, inputPath, useDefaults }: Props) => { |
| 22 | + // Parse The Movie DB's Open API schema |
| 23 | + const json = JSON.parse(fs.readFileSync(`${__dirname}/${inputFile}`, 'utf8')); |
| 24 | + |
| 25 | + // Create path choices |
| 26 | + const getReqPaths = filterPathsByReqType(Object.entries(json.paths), 'get'); |
| 27 | + |
| 28 | + // Get user selected path |
| 29 | + let selectedPath: string; |
| 30 | + if (inputPath) { |
| 31 | + const isValid = validatePath(inputPath, getReqPaths.map((path) => path[0]) as string[]); |
| 32 | + if (!isValid) throw new Error(`Invalid path ${inputPath}!`); |
| 33 | + selectedPath = inputPath; |
| 34 | + } else { |
| 35 | + const pathChoices = getReqPaths.map((path: object) => { |
| 36 | + return { |
| 37 | + name: path[0], |
| 38 | + value: path[0], |
| 39 | + description: path[1]['get'].description, |
| 40 | + }; |
| 41 | + }); |
| 42 | + selectedPath = await searchSelect({ |
| 43 | + message: 'Select a Movie DB API request', |
| 44 | + choices: pathChoices, |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + // Create list of user selected OR default query parameters |
| 49 | + const selectedParams: Array<{ |
| 50 | + param: string; |
| 51 | + value: string; |
| 52 | + path: boolean; |
| 53 | + }> = []; |
| 54 | + const pathParams = getPathParams(selectedPath); |
| 55 | + if (!useDefaults) { |
| 56 | + // Get list of all params for selected path |
| 57 | + const params = json.paths[selectedPath].get.parameters.map((param) => { |
| 58 | + const req = param.required ? ' (required)' : ''; |
| 59 | + return { |
| 60 | + name: param.name + req, |
| 61 | + value: param.name, |
| 62 | + checked: !!req, |
| 63 | + }; |
| 64 | + }); |
| 65 | + |
| 66 | + // Get user selected params |
| 67 | + const selectedParamList: string[] = await checkbox({ |
| 68 | + message: 'Select params to add', |
| 69 | + choices: params, |
| 70 | + loop: true, |
| 71 | + }); |
| 72 | + |
| 73 | + // Prompt user for each selected param |
| 74 | + for (let i = 0; i < selectedParamList.length; i++) { |
| 75 | + const answer = await input({ message: selectedParamList[i] }); |
| 76 | + const isInPath = pathParams.includes(selectedParamList[i]); |
| 77 | + selectedParams.push({ param: selectedParamList[i], value: answer, path: isInPath }); |
| 78 | + } |
| 79 | + } else { |
| 80 | + // Get default path params |
| 81 | + for (let i = 0; i < pathParams.length; i++) { |
| 82 | + const defaultParam = DEFAULT_PARAMS.find((param) => param.name === pathParams[i]); |
| 83 | + if (defaultParam) { |
| 84 | + selectedParams.push({ |
| 85 | + param: pathParams[i], |
| 86 | + value: defaultParam.values[Math.round(Math.random())], |
| 87 | + path: true, |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + await makeRequest({ path: selectedPath, params: selectedParams, outputFile }); |
| 94 | +}; |
0 commit comments