|
@ -2,64 +2,65 @@ |
|
|
const request = require('superagent') |
|
|
const request = require('superagent') |
|
|
|
|
|
|
|
|
class paasRequest { |
|
|
class paasRequest { |
|
|
constructor(root) { |
|
|
constructor(root, { query = {} } = {}) { |
|
|
this.root = root; |
|
|
this.root = root; |
|
|
} |
|
|
this.query = query |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
#buildUrl = (url) => { |
|
|
#buildUrl = (url) => { |
|
|
return `${this.root}/${url}`; |
|
|
return `${this.root}/${url}`; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#resultHandler = (resolve, reject) => { |
|
|
#resultHandler = (resolve, reject) => { |
|
|
return (err, res) => { |
|
|
return (err, res) => { |
|
|
if (err) { |
|
|
if (err) { |
|
|
reject(err); |
|
|
reject(err); |
|
|
} else { |
|
|
} else { |
|
|
resolve(res.body); |
|
|
resolve(res.body); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
get = (url, { query = {}, header = {} } = {}) => { |
|
|
get = (url, { query = {}, header = {} } = {}) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
request.get(this.#buildUrl(url)).set(header).query(query).end(this.#resultHandler(resolve, reject)); |
|
|
request.get(this.#buildUrl(url)).set(header).query(Object.assign(query, this.query)).end(this.#resultHandler(resolve, reject)); |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
post = (url, { data = {}, query = {}, header = {} } = {}) => { |
|
|
post = (url, { data = {}, query = {}, header = {} } = {}) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
request.post(this.#buildUrl(url)).set(header).query(query).send(data).end(this.#resultHandler(resolve, reject)); |
|
|
request.post(this.#buildUrl(url)).set(header).query(Object.assign(query, this.query)).send(data).end(this.#resultHandler(resolve, reject)); |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
put = (url, { data = {}, header = {}, query = {}, } = {}) => { |
|
|
put = (url, { data = {}, header = {}, query = {}, } = {}) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
request.put(this.#buildUrl(url)).set(header).query(query).send(data).end(this.#resultHandler(resolve, reject)); |
|
|
request.put(this.#buildUrl(url)).set(header).query(Object.assign(query, this.query)).send(data).end(this.#resultHandler(resolve, reject)); |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
delete = (url, { header = {} } = {}) => { |
|
|
delete = (url, { header = {}, query = {} } = {}) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
request.delete(this.#buildUrl(url)).set(header).end(this.#resultHandler(resolve, reject)); |
|
|
request.delete(this.#buildUrl(url)).set(header).query(Object.assign(query, this.query)).end(this.#resultHandler(resolve, reject)); |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function factory (app, opts) { |
|
|
function factory (app, opts) { |
|
|
if (opts.pssaRequest) { |
|
|
if (opts.pssaRequest) { |
|
|
try { |
|
|
try { |
|
|
for (let r of opts.pssaRequest) { |
|
|
for (let r of opts.pssaRequest) { |
|
|
if (r.name && r.root) { |
|
|
if (r.name && r.root) { |
|
|
app.fs[r.name] = new paasRequest(r.root) |
|
|
app.fs[r.name] = new paasRequest(r.root, { ...(r.params || {}) }) |
|
|
} else { |
|
|
} else { |
|
|
throw 'opts.pssaRequest 参数错误!' |
|
|
throw 'opts.pssaRequest 参数错误!' |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} catch (error) { |
|
|
} |
|
|
console.error(error) |
|
|
} catch (error) { |
|
|
process.exit(-1); |
|
|
console.error(error) |
|
|
} |
|
|
process.exit(-1); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
module.exports = factory; |
|
|
module.exports = factory; |
|
|