You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
203 lines
6.5 KiB
203 lines
6.5 KiB
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = exports.ApiV2Error = void 0;
|
|
function _axios() {
|
|
const data = _interopRequireDefault(require("axios"));
|
|
_axios = function () {
|
|
return data;
|
|
};
|
|
return data;
|
|
}
|
|
function _concatStream() {
|
|
const data = _interopRequireDefault(require("concat-stream"));
|
|
_concatStream = function () {
|
|
return data;
|
|
};
|
|
return data;
|
|
}
|
|
function _merge() {
|
|
const data = _interopRequireDefault(require("lodash/merge"));
|
|
_merge = function () {
|
|
return data;
|
|
};
|
|
return data;
|
|
}
|
|
function _querystring() {
|
|
const data = _interopRequireDefault(require("querystring"));
|
|
_querystring = function () {
|
|
return data;
|
|
};
|
|
return data;
|
|
}
|
|
function _internal() {
|
|
const data = require("./internal");
|
|
_internal = function () {
|
|
return data;
|
|
};
|
|
return data;
|
|
}
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
const MAX_CONTENT_LENGTH = 100 /* MB */ * 1024 * 1024;
|
|
const MAX_BODY_LENGTH = 100 /* MB */ * 1024 * 1024;
|
|
|
|
// These aren't constants because some commands switch between staging and prod
|
|
function _rootBaseUrl() {
|
|
return `${_internal().Config.api.scheme}://${_internal().Config.api.host}`;
|
|
}
|
|
function _apiBaseUrl() {
|
|
let rootBaseUrl = _rootBaseUrl();
|
|
if (_internal().Config.api.port) {
|
|
rootBaseUrl += ':' + _internal().Config.api.port;
|
|
}
|
|
return rootBaseUrl + '/--/api/v2';
|
|
}
|
|
async function _convertFormDataToBuffer(formData) {
|
|
return new Promise(resolve => {
|
|
formData.pipe((0, _concatStream().default)({
|
|
encoding: 'buffer'
|
|
}, data => resolve({
|
|
data
|
|
})));
|
|
});
|
|
}
|
|
class ApiV2Error extends Error {
|
|
constructor(message, code = 'UNKNOWN') {
|
|
super(message);
|
|
_defineProperty(this, "name", 'ApiV2Error');
|
|
_defineProperty(this, "code", void 0);
|
|
_defineProperty(this, "details", void 0);
|
|
_defineProperty(this, "serverStack", void 0);
|
|
_defineProperty(this, "metadata", void 0);
|
|
_defineProperty(this, "_isApiError", true);
|
|
this.code = code;
|
|
}
|
|
}
|
|
exports.ApiV2Error = ApiV2Error;
|
|
class ApiV2Client {
|
|
static clientForUser(user) {
|
|
if (user) {
|
|
return new ApiV2Client(user);
|
|
}
|
|
return new ApiV2Client();
|
|
}
|
|
static setClientName(name) {
|
|
ApiV2Client.exponentClient = name;
|
|
}
|
|
constructor(options = {}) {
|
|
_defineProperty(this, "sessionSecret", null);
|
|
_defineProperty(this, "accessToken", null);
|
|
if (options.accessToken) {
|
|
this.accessToken = options.accessToken;
|
|
}
|
|
if (options.sessionSecret) {
|
|
this.sessionSecret = options.sessionSecret;
|
|
}
|
|
}
|
|
async getAsync(methodName, args = {}, extraOptions, returnEntireResponse = false) {
|
|
return this._requestAsync(methodName, {
|
|
httpMethod: 'get',
|
|
queryParameters: args
|
|
}, extraOptions, returnEntireResponse);
|
|
}
|
|
async postAsync(methodName, data, extraOptions, returnEntireResponse = false) {
|
|
return this._requestAsync(methodName, {
|
|
httpMethod: 'post',
|
|
body: data
|
|
}, extraOptions, returnEntireResponse);
|
|
}
|
|
async putAsync(methodName, data, extraOptions, returnEntireResponse = false) {
|
|
return this._requestAsync(methodName, {
|
|
httpMethod: 'put',
|
|
body: data
|
|
}, extraOptions, returnEntireResponse);
|
|
}
|
|
async patchAsync(methodName, data, extraOptions, returnEntireResponse = false) {
|
|
return this._requestAsync(methodName, {
|
|
httpMethod: 'patch',
|
|
body: data
|
|
}, extraOptions, returnEntireResponse);
|
|
}
|
|
async deleteAsync(methodName, args = {}, extraOptions, returnEntireResponse = false) {
|
|
return this._requestAsync(methodName, {
|
|
httpMethod: 'delete',
|
|
queryParameters: args
|
|
}, extraOptions, returnEntireResponse);
|
|
}
|
|
async uploadFormDataAsync(methodName, formData) {
|
|
const options = {
|
|
httpMethod: 'put'
|
|
};
|
|
const {
|
|
data
|
|
} = await _convertFormDataToBuffer(formData);
|
|
const uploadOptions = {
|
|
headers: formData.getHeaders(),
|
|
data
|
|
};
|
|
return await this._requestAsync(methodName, options, undefined, false, uploadOptions);
|
|
}
|
|
async _requestAsync(methodName, options, extraRequestOptions = {}, returnEntireResponse = false, uploadOptions) {
|
|
const url = `${_apiBaseUrl()}/${methodName}`;
|
|
let reqOptions = {
|
|
url,
|
|
method: options.httpMethod,
|
|
headers: {
|
|
'Exponent-Client': ApiV2Client.exponentClient
|
|
}
|
|
};
|
|
|
|
// Handle auth method, prioritizing authorization tokens before session secrets
|
|
if (this.accessToken) {
|
|
reqOptions.headers['Authorization'] = `Bearer ${this.accessToken}`;
|
|
} else if (this.sessionSecret) {
|
|
reqOptions.headers['Expo-Session'] = this.sessionSecret;
|
|
}
|
|
|
|
// Handle qs
|
|
if (options.queryParameters) {
|
|
reqOptions.params = options.queryParameters;
|
|
reqOptions.paramsSerializer = _querystring().default.stringify;
|
|
}
|
|
|
|
// Handle body
|
|
if (options.body) {
|
|
reqOptions.data = options.body;
|
|
}
|
|
if (!extraRequestOptions.hasOwnProperty('timeout') && _internal().ConnectionStatus.isOffline()) {
|
|
reqOptions.timeout = 1;
|
|
}
|
|
reqOptions = (0, _merge().default)({}, reqOptions, extraRequestOptions, uploadOptions, {
|
|
maxContentLength: MAX_CONTENT_LENGTH,
|
|
maxBodyLength: MAX_BODY_LENGTH
|
|
});
|
|
let response;
|
|
let result;
|
|
try {
|
|
response = await _axios().default.request(reqOptions);
|
|
result = response.data;
|
|
} catch (e) {
|
|
var _e$response, _e$response$data, _e$response$data$erro;
|
|
if (e !== null && e !== void 0 && (_e$response = e.response) !== null && _e$response !== void 0 && (_e$response$data = _e$response.data) !== null && _e$response$data !== void 0 && (_e$response$data$erro = _e$response$data.errors) !== null && _e$response$data$erro !== void 0 && _e$response$data$erro.length) {
|
|
result = e.response.data;
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
if (result.errors && result.errors.length) {
|
|
const responseError = result.errors[0];
|
|
const error = new ApiV2Error(responseError.message, responseError.code);
|
|
error.serverStack = responseError.stack;
|
|
error.details = responseError.details;
|
|
error.metadata = responseError.metadata;
|
|
throw error;
|
|
}
|
|
return returnEntireResponse ? response : result.data;
|
|
}
|
|
}
|
|
exports.default = ApiV2Client;
|
|
_defineProperty(ApiV2Client, "exponentClient", 'xdl');
|
|
//# sourceMappingURL=ApiV2.js.map
|