From d07b38c7d67842467978e5d59c34f9d7d75f9443 Mon Sep 17 00:00:00 2001 From: "Lin, Hung-Shin" Date: Thu, 23 Nov 2023 23:00:16 +0800 Subject: [PATCH] fix: no callback found error --- packages/container/cli.js | 62 ++++++++++--------- .../src/api/controllers/commandController.js | 2 +- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/packages/container/cli.js b/packages/container/cli.js index 1ca00bc..ba0fb5f 100644 --- a/packages/container/cli.js +++ b/packages/container/cli.js @@ -6,6 +6,23 @@ const axios = require('axios') const { colorize, printResponses, printHints } = require('./print.js') const discoverFiles = require('./discover.js') +const { + API_ENDPOINT, + TOKEN, + PWD, + CMD_NAME, + CMD_OUTPUT_FILE, + CMD_ERROR_FILE, + CMD_EXIT_CODE, +} = process.env + +const api = axios.create({ + baseURL: `${API_ENDPOINT}/api/v1`, + headers: { + Authorization: `Bearer ${TOKEN}`, + }, +}) + async function readOrNone(file) { try { return await fs.readFile(file, 'utf8') @@ -29,24 +46,21 @@ async function handleCommand(command) { }, {}) } -async function main() { - const { - API_ENDPOINT, - TOKEN, - PWD, - CMD_NAME, - CMD_OUTPUT_FILE, - CMD_ERROR_FILE, - CMD_EXIT_CODE, - } = process.env - if (!CMD_NAME) exit(0) +async function handleResponse({ responses, hints, end }) { + if (responses) { + await printResponses(responses, 60) + } + if (hints) { + await printHints(hints, 60) + } + if (end) { + console.log(colorize('Quest completed!', 'blue')) + } + await api.post('/commands/completed') +} - const api = axios.create({ - baseURL: `${API_ENDPOINT}/api/v1`, - headers: { - Authorization: `Bearer ${TOKEN}`, - }, - }) +async function main() { + if (!CMD_NAME) exit(0) const params = await handleCommand(CMD_NAME) @@ -62,18 +76,8 @@ async function main() { } try { const res = await api.post('/commands', payload) - if (!res.data) return - const { responses, hints, end } = res.data - if (responses) { - await printResponses(responses, 60) - } - if (hints) { - await printHints(hints, 60) - } - if (end) { - console.log(colorize('Quest completed!', 'blue')) - } - await api.post('/commands/completed') + if (!res.data || !res.data.responses) return + await handleResponse(res.data) } catch (err) { if (err.response) { console.error(err.response.data) diff --git a/server/src/api/controllers/commandController.js b/server/src/api/controllers/commandController.js index c956117..34a5640 100644 --- a/server/src/api/controllers/commandController.js +++ b/server/src/api/controllers/commandController.js @@ -72,7 +72,7 @@ export const completedCommand = asyncHandler(async (req, res) => { const callback = commandCompleteCallbacks.get(session.id) if (!callback) { - res.status(400).json({ message: 'no callback found' }) + res.status(200).json({ message: 'no callback found' }) return } callback()