|
"use strict"; |
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
|
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } |
|
step((generator = generator.apply(thisArg, _arguments || [])).next()); |
|
}); |
|
}; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
const url_1 = require("url"); |
|
var Styles; |
|
(function (Styles) { |
|
Styles["default"] = "default"; |
|
Styles["earth"] = "earth"; |
|
Styles["modernblue"] = "modern-blue"; |
|
Styles["mscgen"] = "mscgen"; |
|
Styles["omegapple"] = "omegapple"; |
|
Styles["qsd"] = "qsd"; |
|
Styles["rose"] = "rose"; |
|
Styles["roundgreen"] = "roundgreen"; |
|
Styles["napkin"] = "napkin"; |
|
Styles["magazine"] = "magazine"; |
|
Styles["vs2010"] = "vs2010"; |
|
Styles["patent"] = "patent"; |
|
})(Styles = exports.Styles || (exports.Styles = {})); |
|
var ImageTypes; |
|
(function (ImageTypes) { |
|
ImageTypes["png"] = "png"; |
|
ImageTypes["pdf"] = "pdf"; |
|
ImageTypes["svg"] = "svg"; |
|
})(ImageTypes = exports.ImageTypes || (exports.ImageTypes = {})); |
|
const DefaultOptions = { |
|
style: Styles.default, |
|
imageType: ImageTypes.png, |
|
}; |
|
class InvalidSequenceDiagramError extends Error { |
|
constructor(errors) { |
|
super(`Errors found in diagram syntax: ${errors.join(',')}`); |
|
Error.captureStackTrace(this, InvalidSequenceDiagramError); |
|
} |
|
} |
|
exports.InvalidSequenceDiagramError = InvalidSequenceDiagramError; |
|
class YouNeedToPayWSDSome$$$Error extends Error { |
|
constructor() { |
|
super('WebSequenceDiagrams rate limited you because you need a pro account.'); |
|
Error.captureStackTrace(this, YouNeedToPayWSDSome$$$Error); |
|
} |
|
} |
|
exports.YouNeedToPayWSDSome$$$Error = YouNeedToPayWSDSome$$$Error; |
|
class ClientError extends Error { |
|
constructor(nestedError) { |
|
super('Client failed to execute call for some reason.'); |
|
this.nestedError = nestedError; |
|
Error.captureStackTrace(this, ClientError); |
|
} |
|
} |
|
exports.ClientError = ClientError; |
|
class RequestError extends Error { |
|
constructor(statusCode, body) { |
|
super('Client failed due to unknown HTTP Request Error.'); |
|
this.statusCode = statusCode; |
|
this.body = body; |
|
Error.captureStackTrace(this, RequestError); |
|
} |
|
} |
|
exports.RequestError = RequestError; |
|
class InvalidResponseError extends Error { |
|
constructor() { |
|
super('WebSequenceDiagrams returned an invalid response.'); |
|
Error.captureStackTrace(this, InvalidResponseError); |
|
} |
|
} |
|
exports.InvalidResponseError = InvalidResponseError; |
|
class WebSequenceDiagramClient { |
|
constructor(client, options = {}) { |
|
this.client = client; |
|
this.options = Object.assign({}, DefaultOptions, options); |
|
} |
|
getContentType() { |
|
switch (this.options.imageType) { |
|
case ImageTypes.pdf: return 'application/pdf'; |
|
case ImageTypes.svg: return 'image/svg+xml'; |
|
default: return 'image/png'; |
|
} |
|
} |
|
// This is just a wrapper around an Axios request to allow reuse of the error handling logic. |
|
static tryRequest(promise) { |
|
return __awaiter(this, void 0, void 0, function* () { |
|
try { |
|
const { data } = yield promise; |
|
return data; |
|
} |
|
catch (error) { |
|
if (error.response) { |
|
// TODO: enumerate other HTTP errors when discovered in the future. |
|
switch (error.response.status) { |
|
case 402: throw new YouNeedToPayWSDSome$$$Error(); |
|
default: throw new RequestError(error.response.status, error.response.data); |
|
} |
|
} |
|
throw new ClientError(error); |
|
} |
|
}); |
|
} |
|
generateDiagram(contents) { |
|
return __awaiter(this, void 0, void 0, function* () { |
|
const params = new url_1.URLSearchParams({ |
|
apiVersion: '1', |
|
message: contents.toString('utf-8'), |
|
style: this.options.style, |
|
format: this.options.imageType, |
|
apikey: this.options.apiKey || undefined, |
|
}); |
|
const { errors, img } = yield WebSequenceDiagramClient.tryRequest(this.client.post('/index.php', params)); |
|
if (errors) { |
|
if (errors.length === 0) { |
|
return yield WebSequenceDiagramClient.tryRequest(this.client.get(`/${img}`, { |
|
responseType: 'arraybuffer', |
|
transformResponse: data => Buffer.from(data), |
|
})); |
|
} |
|
else { |
|
throw new InvalidSequenceDiagramError(errors); |
|
} |
|
} |
|
throw new InvalidResponseError(); |
|
}); |
|
} |
|
} |
|
exports.default = WebSequenceDiagramClient; |
|
//# sourceMappingURL=WebSequenceDiagramClient.js.map |
AMAZING! Thank you!!