UNPKG

@larksuiteoapi/api

Version:
119 lines 4.19 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BatchAPIReqCall = exports.APIReqCall = exports.APIReqCallResult = exports.BatchReqCall = exports.ReqCall = exports.ReqCallDone = exports.sendRequest = exports.send = void 0; const handlers_1 = require("./core/handlers/handlers"); const error_1 = require("./core/response/error"); const core_1 = require("@larksuiteoapi/core"); exports.send = (ctx, conf, req) => __awaiter(void 0, void 0, void 0, function* () { let response; try { response = yield _sendRequest(ctx, conf, req); } catch (e) { if (error_1.instanceOfError(e)) { throw e; } throw error_1.newErr(e); } if (response.code == error_1.ErrCode.Ok) { return response.data; } throw response; }); exports.sendRequest = (conf, req) => __awaiter(void 0, void 0, void 0, function* () { return _sendRequest(new core_1.Context(), conf, req); }); const _sendRequest = (ctx, conf, req) => __awaiter(void 0, void 0, void 0, function* () { conf.withContext(ctx); req.withContext(ctx); return handlers_1.handle(ctx, req); }); class ReqCallDone { constructor(reqCall) { this.reqCall = reqCall; this.ctx = reqCall.ctx; } } exports.ReqCallDone = ReqCallDone; class ReqCall { constructor(ctx, conf, req) { this.ctx = ctx; this.conf = conf; this.req = req; } do() { return exports.send(this.ctx, this.conf, this.req); } } exports.ReqCall = ReqCall; class BatchReqCall { constructor(errorCallback, ...reqCalls) { this.do = () => __awaiter(this, void 0, void 0, function* () { yield Promise.all(this.reqCalls.map((reqCall, index) => { return reqCall.do().then(result => { this.reqCallDos[index].result = result; }).catch(e => { this.reqCallDos[index].err = e; this.errorCallback(reqCall, e); }); })); return this; }); this.reqCalls = reqCalls; this.reqCallDos = []; for (let v of this.reqCalls) { this.reqCallDos.push(new ReqCallDone(v)); } this.errorCallback = errorCallback; } } exports.BatchReqCall = BatchReqCall; class APIReqCallResult { constructor(reqCall) { this.reqCall = reqCall; } } exports.APIReqCallResult = APIReqCallResult; class APIReqCall { constructor(conf, req) { this.conf = conf; this.req = req; } do() { return exports.sendRequest(this.conf, this.req); } } exports.APIReqCall = APIReqCall; class BatchAPIReqCall { constructor(...reqCalls) { this.do = () => __awaiter(this, void 0, void 0, function* () { if (this.reqCalls.length == 0) { return this; } yield Promise.all(this.reqCalls.map((reqCall, index) => { return reqCall.do().then(result => { this.reqCallResults[index].response = result; }).catch(e => { this.reqCallResults[index].err = e; }); })); return this; }); this.reqCalls = reqCalls; this.reqCallResults = []; for (let v of this.reqCalls) { this.reqCallResults.push(new APIReqCallResult(v)); } } } exports.BatchAPIReqCall = BatchAPIReqCall; //# sourceMappingURL=api.js.map