|
|
@ -2,8 +2,9 @@ |
|
|
|
const request = require('superagent') |
|
|
|
|
|
|
|
class paasRequest { |
|
|
|
constructor(root) { |
|
|
|
constructor(root, { query = {} } = {}) { |
|
|
|
this.root = root; |
|
|
|
this.query = query |
|
|
|
} |
|
|
|
|
|
|
|
#buildUrl = (url) => { |
|
|
@ -22,25 +23,25 @@ class paasRequest { |
|
|
|
|
|
|
|
get = (url, { query = {}, header = {} } = {}) => { |
|
|
|
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 = {} } = {}) => { |
|
|
|
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 = {}, } = {}) => { |
|
|
|
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) => { |
|
|
|
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)); |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -50,7 +51,7 @@ function factory (app, opts) { |
|
|
|
try { |
|
|
|
for (let r of opts.pssaRequest) { |
|
|
|
if (r.name && r.root) { |
|
|
|
app.fs[r.name] = new paasRequest(r.root) |
|
|
|
app.fs[r.name] = new paasRequest(r.root, { ...(r.params || {}) }) |
|
|
|
} else { |
|
|
|
throw 'opts.pssaRequest 参数错误!' |
|
|
|
} |
|
|
|