Compare commits

...

1 Commits

Author SHA1 Message Date
Julien Fontanet
f8fac876ca WiP 2023-12-22 09:56:44 +01:00
2 changed files with 33 additions and 24 deletions

View File

@@ -157,33 +157,32 @@ function extractFlags(args) {
const noop = Function.prototype
function parseValue(value) {
if (value.startsWith('json:')) {
return JSON.parse(value.slice(5))
}
if (value === 'true') {
return true
}
if (value === 'false') {
return false
}
return value
}
const PARAM_RE = /^([^=]+)=([^]*)$/
function parseParameters(args) {
if (args[0] === '--') {
return args.slice(1).map(parseValue)
}
const params = {}
forEach(args, function (arg) {
let matches
if (!(matches = arg.match(PARAM_RE))) {
throw new Error('invalid arg: ' + arg)
}
const name = matches[1]
let value = matches[2]
if (value.startsWith('json:')) {
value = JSON.parse(value.slice(5))
}
if (name === '@') {
params['@'] = value
return
}
if (value === 'true') {
value = true
} else if (value === 'false') {
value = false
}
params[name] = value
params[matches[1]] = parseValue(matches[2])
})
return params

View File

@@ -251,9 +251,19 @@ export default class Api {
constructor(app) {
this._logger = null
this._methods = { __proto__: null }
this._app = app
const defer =
const seq = async methods => {
for (const method of methods) {
await this.#callApiMethod(method[0], method[1])
}
}
seq.validate = ajv.compile({ type: 'array', minLength: 1, items: { type: ['array', 'string'] } })
const if =
this._methods = { __proto__: null, seq }
this.addApiMethods(methods)
app.hooks.on('start', async () => {
this._logger = await app.getLogger('api')
@@ -367,8 +377,7 @@ export default class Api {
}
async callApiMethod(connection, name, params = {}) {
const method = this._methods[name]
if (!method) {
if (!Object.hasOwn(this._methods, name)) {
throw new MethodNotFound(name)
}
@@ -383,11 +392,12 @@ export default class Api {
apiContext.permission = 'none'
}
return this.#apiContext.run(apiContext, () => this.#callApiMethod(name, method, params))
return this.#apiContext.run(apiContext, () => this.#callApiMethod(name, params))
}
async #callApiMethod(name, method, params) {
async #callApiMethod(name, params) {
const app = this._app
const method = this._methods[name]
const startTime = Date.now()
const { connection, user } = this.apiContext