@venly/venly-core-sdk
Version:
Javascrip/Typescript SDK for Venly's Web3 Services
201 lines • 7.86 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VyRequestData = void 0;
const url_1 = require("url");
const VenlyAPI_1 = require("../VenlyAPI");
const HttpHelper = __importStar(require("../helpers/VyHttpHelper"));
const json_1 = require("../helpers/json");
const models_1 = require("../models");
var VyContentType;
(function (VyContentType) {
VyContentType["None"] = "NONE";
VyContentType["Json"] = "JSON";
VyContentType["Form"] = "FORM";
})(VyContentType || (VyContentType = {}));
class VyRequestData {
constructor(init) {
this.requiredAuth = false;
Object.assign(this, init);
}
static checkWrapRequirement(endpoint) {
switch (endpoint) {
case models_1.VyApiEndpoint.Nft: return false;
case models_1.VyApiEndpoint.Auth: return false;
case models_1.VyApiEndpoint.Extension: return false;
default: return true;
}
}
static create(method, uri, endpoint, environment) {
return new VyRequestData({
method: method,
uri: uri,
endpoint: endpoint,
environment: environment,
isEnveloped: this.checkWrapRequirement(endpoint)
});
}
getUrl(accessToken = null) {
var url;
if (accessToken != null) {
url = HttpHelper.getApiUrl(this.uri, this.endpoint, accessToken.Environment);
}
else {
url = HttpHelper.getApiUrl(this.uri, this.endpoint, this.environment);
}
if (this.queryString != null) {
url += `?${this.queryString}`;
}
return new URL(url);
}
toRequestInit(bearer = null) {
//Headers
var requestHeaders = {};
var defaultHeaders = {
'Accept': 'application/json'
};
//Content-Type
switch (this.contentType) {
case VyContentType.Json:
requestHeaders = Object.assign(Object.assign({}, defaultHeaders), { 'Content-Type': 'application/json' });
break;
case VyContentType.Form:
requestHeaders = Object.assign(Object.assign({}, defaultHeaders), { 'Content-Type': 'application/x-www-form-urlencoded' });
break;
}
//Bearer
if (bearer != null) {
requestHeaders = Object.assign(Object.assign({}, requestHeaders), { 'Authorization': `Bearer ${bearer.Token}` });
}
//User Auth
if (this.authSigningMethod != null) {
requestHeaders = Object.assign(Object.assign({}, requestHeaders), { 'Signing-Method': `${this.authSigningMethod.signingMethodId}:${this.authSigningMethod.signingMethodValue}` });
}
return {
method: this.method,
body: this.getBody(),
headers: requestHeaders
};
}
getBody() {
switch (this.contentType) {
case VyContentType.Json:
return this.content;
case VyContentType.Form:
return new url_1.URLSearchParams(JSON.parse(this.content));
}
return null;
}
static get(uri, endpoint) {
return this.create("GET", uri, endpoint, VenlyAPI_1.Venly.currentEnvironment);
}
static post(uri, endpoint) {
return this.create("POST", uri, endpoint, VenlyAPI_1.Venly.currentEnvironment);
}
static put(uri, endpoint) {
return this.create("PUT", uri, endpoint, VenlyAPI_1.Venly.currentEnvironment);
}
static patch(uri, endpoint) {
return this.create("PATCH", uri, endpoint, VenlyAPI_1.Venly.currentEnvironment);
}
static delete(uri, endpoint) {
return this.create("DELETE", uri, endpoint, VenlyAPI_1.Venly.currentEnvironment);
}
setAuth_OAuth2(required) {
this.requiredAuth = required;
return this;
}
setAuth_SigningMethod(userAuth) {
this.authSigningMethod = userAuth;
return this;
}
setEnveloped(enveloped) {
this.isEnveloped = enveloped;
return this;
}
addJsonContent(content, objectType) {
this.contentType = VyContentType.Json;
this.content = json_1.JsonConvert.serialize(content, objectType);
return this;
}
AddFormContent(content) {
this.contentType = VyContentType.Form;
this.content = JSON.stringify(content);
return this;
}
addQuery(query) {
if (query == null)
return this;
this.queryString = query.toString();
return this;
}
}
exports.VyRequestData = VyRequestData;
__decorate([
(0, json_1.JsonProperty)('uri', String),
__metadata("design:type", String)
], VyRequestData.prototype, "uri", void 0);
__decorate([
(0, json_1.JsonProperty)('method', String),
__metadata("design:type", String)
], VyRequestData.prototype, "method", void 0);
__decorate([
(0, json_1.JsonProperty)('endpoint', models_1.VyApiEndpoint),
__metadata("design:type", String)
], VyRequestData.prototype, "endpoint", void 0);
__decorate([
(0, json_1.JsonProperty)('environment', models_1.VyEnvironment),
__metadata("design:type", String)
], VyRequestData.prototype, "environment", void 0);
__decorate([
(0, json_1.JsonProperty)('requiresWrapping', Boolean),
__metadata("design:type", Boolean)
], VyRequestData.prototype, "isEnveloped", void 0);
__decorate([
(0, json_1.JsonProperty)('selectPropertyName', String),
__metadata("design:type", String)
], VyRequestData.prototype, "selectPropertyName", void 0);
__decorate([
(0, json_1.JsonProperty)('contentStr', String),
__metadata("design:type", String)
], VyRequestData.prototype, "content", void 0);
__decorate([
(0, json_1.JsonProperty)('contentType', VyContentType),
__metadata("design:type", String)
], VyRequestData.prototype, "contentType", void 0);
__decorate([
(0, json_1.JsonProperty)('queryString'),
__metadata("design:type", String)
], VyRequestData.prototype, "queryString", void 0);
//# sourceMappingURL=VyRequestData.js.map