@graphistry/js-upload-api
Version:
Graphistry upload client for reuse by node and browser clients
189 lines • 14.1 kB
JavaScript
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var AbstractClient = /** @class */ (function () {
/**
* @param org The organization to use (optional)
* @param protocol The protocol to use for the server during uploads: 'http' or 'https'.
* @param host The hostname of the server during uploads: defaults to 'hub.graphistry.com'
* @param clientProtocolHostname Base path to use inside the browser and when sharing public URLs: defaults to '{protocol}://{host}'
* @param fetch The fetch implementation to use: defaults to JavaScript fetch function
* @param version The version of the client library
* @param agent The agent name to use when communicating with the server
*/
function AbstractClient(org, protocol, host, clientProtocolHostname, fetchFn, version, agent) {
if (protocol === void 0) { protocol = 'https'; }
if (host === void 0) { host = 'hub.graphistry.com'; }
if (fetchFn === void 0) { fetchFn = fetch; }
if (agent === void 0) { agent = '@graphistry/js-upload-api'; }
this.org = org;
this.protocol = protocol;
this.host = host;
this.fetch = fetchFn;
this.version = version;
this.agent = agent;
this.clientProtocolHostname = clientProtocolHostname || "".concat(protocol, "://").concat(host);
}
/**
*
* See examples at top of file
*
* Set JWT token if already known
*
* @param token The token to use for authentication.
* @returns The client instance.
*/
AbstractClient.prototype.setToken = function (token) {
this._token = token;
return this;
};
/**
*
* @returns Whether the current token is valid
*
*/
AbstractClient.prototype.authTokenValid = function () {
return Boolean(this._token);
};
/**
* @internal
* Internal helper
* @param uri The URI to upload to.
* @param payload The payload to upload.
* @param baseHeaders Optionally override base header object to mix with auth header for the upload.
* @returns The response from the server.
*/
AbstractClient.prototype.post = function (uri, payload, baseHeaders) {
if (baseHeaders === void 0) { baseHeaders = undefined; }
return __awaiter(this, void 0, void 0, function () {
var headers, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
console.debug('post', { uri: uri, payload: payload });
return [4 /*yield*/, this.getSecureHeaders(baseHeaders)];
case 1:
headers = _a.sent();
console.debug('post', { headers: headers });
return [4 /*yield*/, this.postToApi(uri, payload, headers)];
case 2: return [4 /*yield*/, (_a.sent()).json()];
case 3:
response = _a.sent();
console.debug('post response', { uri: uri, payload: payload, response: response });
return [2 /*return*/, response];
}
});
});
};
AbstractClient.isConfigurationValid = function (userId, secret, host) {
console.debug('isConfigurationValid', { userId: userId, secret: secret, host: host });
return (userId || '') !== '' && (secret || '') !== '' && (host || '') !== '';
};
AbstractClient.prototype.getToApi = function (url, headers, baseUrl) {
return __awaiter(this, void 0, void 0, function () {
var resolvedFetch, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
resolvedFetch = this.fetch;
console.debug('getToApi', { url: url, headers: headers });
return [4 /*yield*/, resolvedFetch((baseUrl !== null && baseUrl !== void 0 ? baseUrl : this.getBaseUrl()) + url, {
method: 'GET',
headers: headers,
})];
case 1:
response = _a.sent();
console.debug('getToApi', { url: url, headers: headers });
return [2 /*return*/, response];
}
});
});
};
AbstractClient.prototype.postToApi = function (url, data, headers, baseUrl) {
return __awaiter(this, void 0, void 0, function () {
var resolvedFetch, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
resolvedFetch = this.fetch;
console.debug('postToApi', { url: url, data: data, headers: headers });
return [4 /*yield*/, resolvedFetch((baseUrl !== null && baseUrl !== void 0 ? baseUrl : this.getBaseUrl()) + url, {
method: 'POST',
headers: headers,
body:
//TypedArray
ArrayBuffer.isView(data) && !(data instanceof DataView) ? data
: JSON.stringify(data),
})];
case 1:
response = _a.sent();
console.debug('postToApi', { url: url, data: data, headers: headers, response: response });
return [2 /*return*/, response];
}
});
});
};
AbstractClient.prototype.getBaseHeaders = function () {
return ({
'Accept': 'application/json',
'Content-Type': 'application/json',
});
};
AbstractClient.prototype.getBaseUrl = function () {
return "".concat(this.protocol, "://").concat(this.host, "/");
};
AbstractClient.prototype.getSecureHeaders = function (baseHeaders) {
return __awaiter(this, void 0, void 0, function () {
var headers, tok;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
headers = baseHeaders || this.getBaseHeaders();
return [4 /*yield*/, this.getAuthToken()];
case 1:
tok = _a.sent();
console.debug('getSecureHeaders', { headers: headers, tok: tok });
headers.Authorization = "Bearer ".concat(tok);
console.debug('getSecureHeaders', { headers: headers });
return [2 /*return*/, headers];
}
});
});
};
return AbstractClient;
}());
export { AbstractClient };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQWJzdHJhY3RDbGllbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvQWJzdHJhY3RDbGllbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBQUE7SUFlSTs7Ozs7Ozs7T0FRRztJQUNILHdCQUNJLEdBQVksRUFDWixRQUFrQixFQUFFLElBQTJCLEVBQy9DLHNCQUErQixFQUMvQixPQUFvQixFQUNwQixPQUFnQixFQUNoQixLQUFtQztRQUpuQyx5QkFBQSxFQUFBLGtCQUFrQjtRQUFFLHFCQUFBLEVBQUEsMkJBQTJCO1FBRS9DLHdCQUFBLEVBQUEsZUFBb0I7UUFFcEIsc0JBQUEsRUFBQSxtQ0FBbUM7UUFFbkMsSUFBSSxDQUFDLEdBQUcsR0FBRyxHQUFHLENBQUM7UUFDZixJQUFJLENBQUMsUUFBUSxHQUFHLFFBQVEsQ0FBQztRQUN6QixJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQztRQUNqQixJQUFJLENBQUMsS0FBSyxHQUFHLE9BQU8sQ0FBQztRQUNyQixJQUFJLENBQUMsT0FBTyxHQUFHLE9BQU8sQ0FBQztRQUN2QixJQUFJLENBQUMsS0FBSyxHQUFHLEtBQUssQ0FBQztRQUNuQixJQUFJLENBQUMsc0JBQXNCLEdBQUcsc0JBQXNCLElBQUksVUFBRyxRQUFRLGdCQUFNLElBQUksQ0FBRSxDQUFDO0lBQ3BGLENBQUM7SUFFRDs7Ozs7Ozs7T0FRRztJQUNJLGlDQUFRLEdBQWYsVUFBZ0IsS0FBYTtRQUN6QixJQUFJLENBQUMsTUFBTSxHQUFHLEtBQUssQ0FBQztRQUNwQixPQUFP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