feat: add initial task on session creation

This commit is contained in:
wancat committed 2023-09-01 17:16:32 +08:00
1 parent 97b0fb0f1d
commit 0ea44e1f71
7 files changed
+59 -28

No files matched your search

+20 -14
View File
@@ -1,8 +1,11 @@
/* eslint-disable no-underscore-dangle */
import axios from 'axios'
import { stdin, stdout, exit } from 'process'
import { io } from 'socket.io-client'
import { program } from 'commander'
let api = null
function get(key, defaultValue) {
const value = process.env[key]
if (value != null) {
@@ -20,26 +23,23 @@ function debug(...args) {
}
}
async function createdSession() {
async function createSession() {
console.log('Creating a new session...')
const res = await fetch(`${program.opts().host}/api/v1/sessions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
quest_id: 'helloworld',
}),
const res = await api.post('/sessions', {
quest_id: 'helloworld',
})
const data = await res.json()
const { data } = await res
if (!data._id) {
console.error(data)
exit(1)
}
console.log('Created Session ID:', data._id)
return data
}
async function getSessionList() {
const res = await fetch(`${program.opts().host}/api/v1/sessions`)
const data = await res.json()
return data
const res = await api.get('/sessions')
return res.data
}
async function lastSession() {
@@ -102,12 +102,18 @@ async function connect(sessionId) {
}
async function main() {
api = axios.create({
baseURL: `${program.opts().host}/api/v1`,
headers: {
'Content-Type': 'application/json',
},
})
console.log(program.opts().session)
// main()
const sessionId =
program.opts().session ||
(program.opts().create ? (await createdSession())._id : null) ||
(program.opts().create ? (await createSession())._id : null) ||
(await lastSession())._id
await connect(sessionId)
+1
View File
@@ -8,6 +8,7 @@
"start": "node index.js"
},
"dependencies": {
"axios": "^1.5.0",
"commander": "^11.0.0",
"socket.io-client": "^4.7.2"
}