terriajs
Version:
Geospatial data visualization platform.
90 lines • 3.23 kB
JavaScript
import i18next from "i18next";
import { isJsonObject } from "../Core/Json";
import loadJson from "../Core/loadJson";
import loadWithXhr from "../Core/loadWithXhr";
import TerriaError from "../Core/TerriaError";
export const DEFAULT_MAX_SHARE_SIZE = 200 * 1024; // 200 KB
/**
* Interface to the terriajs-server service for creating short share links.
* @param {*} options
*
* @alias ShareDataService
* @constructor
*/
export default class ShareDataService {
terria;
url;
_serverConfig;
constructor(options) {
this.terria = options.terria;
this.url = options.url;
}
init(serverConfig) {
this.url = this.url ?? this.terria.configParameters.shareUrl ?? "share";
this._serverConfig = serverConfig;
}
get isUsable() {
return ((this.url !== undefined &&
typeof this._serverConfig === "object" &&
typeof this._serverConfig.newShareUrlPrefix === "string") ||
this.url !== "share");
}
// get the raw string from serverConfig.shareMaxRequestSize
get shareMaxRequestSize() {
return this._serverConfig.shareMaxRequestSize;
}
// get the parsed value in bytes from the server
get shareMaxRequestSizeBytes() {
return this._serverConfig.shareMaxRequestSizeBytes;
}
/**
* Allocates a share token using Terria Server, storing the provided data there.
* @param shareData JSON to store.
* @return A promise for the token (which can later be resolved at /share/TOKEN).
*/
async getShareToken(shareData) {
if (!this.isUsable) {
throw TerriaError.from("`ShareDataService` is not usable");
}
try {
const result = await loadWithXhr({
url: this.url,
method: "POST",
data: JSON.stringify(shareData),
headers: { "Content-Type": "application/json" },
responseType: "json"
});
const json = typeof result === "string" ? JSON.parse(result) : result;
return json.id;
}
catch (error) {
throw TerriaError.from(error, {
title: i18next.t("models.shareData.generateErrorTitle"),
message: i18next.t("models.shareData.generateErrorMessage"),
importance: 1
});
}
}
async resolveData(token) {
if (!this.isUsable) {
throw TerriaError.from("`ShareDataService` is not usable");
}
try {
const shareJson = await loadJson(this.url + "/" + token);
if (!isJsonObject(shareJson, false)) {
throw TerriaError.from(`Invalid server response for share ${this.url + "/" + token}\n\`${JSON.stringify(shareJson)}\``);
}
return shareJson;
}
catch (error) {
throw TerriaError.from(error, {
title: i18next.t("models.shareData.expandErrorTitle"),
message: i18next.t("models.shareData.expandErrorMessage", {
appName: this.terria.appName
}),
importance: 1
});
}
}
}
//# sourceMappingURL=ShareDataService.js.map