@larksuiteoapi/api
Version:
larksuite open api sdk
247 lines • 8.58 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.newRequest = exports.newRequestOfTs = exports.newRequestByAuth = exports.Request = exports.setResponseStream = exports.setNeedHelpDeskAuth = exports.setIsResponseStream = exports.setIsNotDataField = exports.setQueryParams = exports.setPathParams = exports.setTenantKey = exports.setUserAccessToken = exports.setTimeoutOfMs = exports.getInfoByCtx = exports.Info = exports.Opt = exports.AccessTokenType = void 0;
const querystring = __importStar(require("querystring"));
const util = __importStar(require("util"));
const constants_1 = require("../constants/constants");
const ctxKeyRequestInfo = "x-request-info";
var AccessTokenType;
(function (AccessTokenType) {
AccessTokenType["None"] = "none_access_token";
AccessTokenType["App"] = "app_access_token";
AccessTokenType["Tenant"] = "tenant_access_token";
AccessTokenType["User"] = "user_access_token";
})(AccessTokenType = exports.AccessTokenType || (exports.AccessTokenType = {}));
class Opt {
}
exports.Opt = Opt;
class Info {
constructor() {
this.isNotDataField = false; // response body is not data field
this.isResponseStream = false;
this.isResponseStreamReal = false;
this.retryable = false;
this.needHelpDeskAuth = false; // need helpdesk token
this.optFns = [];
}
withContext(ctx) {
ctx.set(ctxKeyRequestInfo, this);
}
}
exports.Info = Info;
exports.getInfoByCtx = (ctx) => {
return ctx.get(ctxKeyRequestInfo);
};
exports.setTimeoutOfMs = function (timeoutOfMs) {
return function (opt) {
opt.timeoutOfMs = timeoutOfMs;
};
};
exports.setUserAccessToken = function (userAccessToken) {
return function (opt) {
opt.userAccessToken = userAccessToken;
};
};
exports.setTenantKey = function (tenantKey) {
return function (opt) {
opt.tenantKey = tenantKey;
};
};
exports.setPathParams = function (pathParams) {
return function (opt) {
opt.pathParams = pathParams;
};
};
exports.setQueryParams = function (queryParams) {
return function (opt) {
opt.queryParams = queryParams;
};
};
exports.setIsNotDataField = function () {
return function (opt) {
opt.isNotDataField = true;
};
};
exports.setIsResponseStream = function () {
return function (opt) {
opt.isResponseStream = true;
};
};
exports.setNeedHelpDeskAuth = function () {
return function (opt) {
opt.needHelpDeskAuth = true;
};
};
exports.setResponseStream = function (responseStream) {
return function (opt) {
opt.isResponseStream = true;
opt.responseStream = responseStream;
};
};
class Request extends Info {
url() {
let path = this.httpPath;
if (this.httpPath.indexOf("http") != 0) {
if (this.httpPath.indexOf("/open-apis") == 0) {
path = util.format("%s%s", this.domain, this.httpPath);
}
else {
path = util.format("%s/%s/%s", this.domain, constants_1.OAPIRootPath, this.httpPath);
}
}
if (this.queryParams) {
path = util.format("%s?%s", path, this.queryParams);
}
return path;
}
fullUrl(domain) {
return util.format("%s%s", domain, this.url());
}
setPathParams(pathParams) {
return this.optFns.push(exports.setPathParams(pathParams));
}
setQueryParams(queryParams) {
return this.optFns.push(exports.setQueryParams(queryParams));
}
setTimeoutOfMs(timeoutOfMs) {
return this.optFns.push(exports.setTimeoutOfMs(timeoutOfMs));
}
setTenantKey(tenantKey) {
return this.optFns.push(exports.setTenantKey(tenantKey));
}
setUserAccessToken(userAccessToken) {
return this.optFns.push(exports.setUserAccessToken(userAccessToken));
}
setIsNotDataField() {
return this.optFns.push(exports.setIsNotDataField());
}
setIsResponseStream() {
return this.optFns.push(exports.setIsResponseStream());
}
setResponseStream(responseStream) {
return this.optFns.push(exports.setResponseStream(responseStream));
}
setNeedHelpDeskAuth() {
return this.optFns.push(exports.setNeedHelpDeskAuth());
}
toString() {
return util.format("%s %s %s", this.httpMethod, this.url(), this.accessTokenType);
}
init(domain) {
this.domain = domain;
let opt = new Opt();
for (let v of this.optFns) {
v(opt);
}
this.isNotDataField = opt.isNotDataField;
this.isResponseStream = opt.isResponseStream;
if (opt.responseStream) {
this.isResponseStream = true;
this.output = opt.responseStream;
}
if (opt.tenantKey) {
if (this.accessibleTokenTypeSet.has(AccessTokenType.Tenant)) {
this.accessTokenType = AccessTokenType.Tenant;
this.tenantKey = opt.tenantKey;
}
}
this.tenantKey = opt.tenantKey || "";
if (opt.userAccessToken) {
if (this.accessibleTokenTypeSet.has(AccessTokenType.User)) {
this.accessTokenType = AccessTokenType.User;
this.userAccessToken = opt.userAccessToken || "";
}
}
this.needHelpDeskAuth = opt.needHelpDeskAuth;
this.timeout = opt.timeoutOfMs || 30000;
if (opt.queryParams) {
this.queryParams = querystring.stringify(opt.queryParams);
}
if (opt.pathParams) {
this.httpPath = resolvePath(this.httpPath, opt.pathParams);
}
}
}
exports.Request = Request;
exports.newRequestByAuth = (httpPath, httpMethod, input, output) => {
let r = new Request();
r.httpPath = httpPath;
r.httpMethod = httpMethod;
r.input = input;
r.output = output;
r.accessibleTokenTypeSet = new Set();
r.accessTokenType = AccessTokenType.None;
r.optFns = [exports.setIsNotDataField()];
return r;
};
const resolvePath = (path, pathVar) => {
let tmpPath = path;
let newPath = "";
while (true) {
let i = tmpPath.indexOf(":");
if (i === -1) {
newPath += tmpPath;
break;
}
newPath += tmpPath.substring(0, i);
let subPath = tmpPath.substring(i);
let j = subPath.indexOf("/");
if (j === -1) {
j = subPath.length;
}
let varName = subPath.substring(1, j);
let v = pathVar[varName];
if (v === undefined) {
throw new Error(util.format("path:%s, param name:%s not find value", path, varName));
}
newPath += v;
if (j === subPath.length) {
break;
}
tmpPath = subPath.substring(j);
}
return newPath;
};
exports.newRequestOfTs = (httpPath, httpMethod, accessTokenTypes, input, output, ...optFns) => {
let accessibleTokenTypeSet = new Set();
let accessTokenType = accessTokenTypes[0];
for (let v of accessTokenTypes) {
accessibleTokenTypeSet.add(v);
if (v == AccessTokenType.Tenant) {
accessTokenType = v;
}
}
let r = new Request();
r.httpPath = httpPath;
r.httpMethod = httpMethod;
r.accessibleTokenTypeSet = accessibleTokenTypeSet;
r.input = input;
r.output = output;
r.accessTokenType = accessTokenType;
r.optFns = optFns;
return r;
};
exports.newRequest = (httpPath, httpMethod, accessTokenType, input, ...optFns) => {
let output;
return exports.newRequestOfTs(httpPath, httpMethod, [accessTokenType], input, output, ...optFns);
};
//# sourceMappingURL=request.js.map