Skip to content

Commit

Permalink
More type changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
na9da committed Apr 26, 2024
1 parent 48edd99 commit a9094e8
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 48 deletions.
2 changes: 0 additions & 2 deletions lib/Core/loadArrayBuffer.ts
Expand Up @@ -6,5 +6,3 @@ export default function loadArrayBuffer(
): Promise<ArrayBuffer> {
return Resource.fetchArrayBuffer({ url: urlOrResource, headers: headers })!;
}

module.exports = loadArrayBuffer;
20 changes: 11 additions & 9 deletions lib/Core/loadXML.js → lib/Core/loadXML.ts
@@ -1,8 +1,10 @@
const Resource = require("terriajs-cesium/Source/Core/Resource").default;
import Resource from "terriajs-cesium/Source/Core/Resource";

async function loadXML(urlOrResource) {
var resource = Resource.createIfNeeded(urlOrResource);
const respone = await resource.fetchXML();
async function loadXML(
urlOrResource: string | Resource
): Promise<XMLDocument | undefined> {
var resource = (Resource as any).createIfNeeded(urlOrResource) as Resource;

Check failure on line 6 in lib/Core/loadXML.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected var, use let or const instead

Check failure on line 6 in lib/Core/loadXML.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected var, use let or const instead
const response = await resource.fetchXML();

/**
* Sometimes Cesium's Resource.fetchXML will return an Array Buffer (usually in Node.js env)
Expand All @@ -13,17 +15,17 @@ async function loadXML(urlOrResource) {
* See full license here https://github.com/fengyuanchen/is-array-buffer/blob/main/LICENSE
*/
if (
respone instanceof ArrayBuffer ||
toString.call(respone) === "[object ArrayBuffer]"
response instanceof ArrayBuffer ||
toString.call(response) === "[object ArrayBuffer]"
) {
const enc = new TextDecoder("utf-8");
const xmlString = enc.decode(respone);
const xmlString = enc.decode(response as any);

const parser = new DOMParser();
return parser.parseFromString(xmlString, "text/xml");
}

return respone;
return response;
}

module.exports = loadXML;
export default loadXML;
2 changes: 1 addition & 1 deletion lib/Map/Vector/EarthGravityModel1996.js
Expand Up @@ -2,7 +2,7 @@

var CesiumMath = require("terriajs-cesium/Source/Core/Math").default;
var defined = require("terriajs-cesium/Source/Core/defined").default;
var loadArrayBuffer = require("../../Core/loadArrayBuffer");
var loadArrayBuffer = require("../../Core/loadArrayBuffer").default;

/**
* The Earth Gravity Model 1996 (EGM96) geoid.
Expand Down
20 changes: 12 additions & 8 deletions lib/Map/Vector/Reproject.ts
@@ -1,9 +1,8 @@
import proj4 from "proj4";
import defined from "terriajs-cesium/Source/Core/defined";
import Proj4Definitions from "./Proj4Definitions";
import urijs from "urijs";

const proj4 = require("proj4").default;
const loadText = require("../../Core/loadText");
import loadText from "../../Core/loadText";
import Proj4Definitions from "./Proj4Definitions";

export default {
TERRIA_CRS: "EPSG:4326",
Expand Down Expand Up @@ -60,16 +59,21 @@ export default {
): [number, number] | undefined {
const source =
sourceCode in Proj4Definitions
? new proj4.Proj(Proj4Definitions[sourceCode])
? proj4.Proj(Proj4Definitions[sourceCode])
: undefined;
const dest =
destCode in Proj4Definitions
? new proj4.Proj(Proj4Definitions[destCode])
? proj4.Proj(Proj4Definitions[destCode])
: undefined;
if (!sourceCode || !destCode) {
if (!source || !dest) {
return;
}
return proj4(source, dest, coordinates);
const result = proj4.transform(source, dest, coordinates) ?? {};
if (result) {
const { x, y } = result;
return [x, y];
}
return undefined;
},

/**
Expand Down
5 changes: 2 additions & 3 deletions lib/ModelMixins/XmlRequestMixin.ts
@@ -1,9 +1,8 @@
import URI from "urijs";
import AbstractConstructor from "../Core/AbstractConstructor";
import isDefined from "../Core/isDefined";

const loadXML = require("../Core/loadXML");
const loadWithXhr = require("../Core/loadWithXhr");
import loadWithXhr from "../Core/loadWithXhr";
import loadXML from "../Core/loadXML";

export default function XmlRequestMixin<T extends AbstractConstructor<any>>(
Base: T
Expand Down
2 changes: 1 addition & 1 deletion lib/Models/Catalog/Esri/ArcGisMapServerCatalogItem.ts
Expand Up @@ -88,7 +88,7 @@ class MapServerStratum extends LoadableStratum(
}

static async load(item: ArcGisMapServerCatalogItem) {
if (!isDefined(item.uri)) {
if (!isDefined(item.uri) || !isDefined(item.url)) {
throw new TerriaError({
title: i18next.t("models.arcGisMapServerCatalogItem.invalidUrlTitle"),
message: i18next.t(
Expand Down
2 changes: 1 addition & 1 deletion lib/Models/Catalog/Ows/WebMapServiceCapabilities.ts
Expand Up @@ -163,7 +163,7 @@ export default class WebMapServiceCapabilities {
createTransformer((url: string) => {
return Promise.resolve(loadXML(url)).then(function (capabilitiesXml) {
const json = xml2json(capabilitiesXml);
if (!defined(json.Capability)) {
if (!capabilitiesXml || !defined(json.Capability)) {
throw networkRequestError({
title: "Invalid GetCapabilities",
message:
Expand Down
2 changes: 1 addition & 1 deletion lib/Models/Catalog/Ows/WebMapTileServiceCapabilities.ts
Expand Up @@ -131,7 +131,7 @@ export default class WebMapTileServiceCapabilities {
createTransformer((url: string) => {
return Promise.resolve(loadXML(url)).then(function (capabilitiesXml) {
const json = xml2json(capabilitiesXml);
if (!defined(json.ServiceIdentification)) {
if (!capabilitiesXml || !defined(json.ServiceIdentification)) {
throw networkRequestError({
title: i18next.t(
"models.webMapTileServiceCatalogGroup.invalidCapabilitiesTitle"
Expand Down
4 changes: 2 additions & 2 deletions lib/Models/Catalog/Ows/WebProcessingServiceCapabilities.ts
Expand Up @@ -54,15 +54,15 @@ export interface ServiceProvider {

export default class WebProcessingServiceCapabilities {
constructor(
readonly capabilitiesXml: string,
readonly capabilitiesXml: XMLDocument,
readonly capabilities: Capabilities
) {}

static fromUrl(url: string): Promise<WebProcessingServiceCapabilities> {
return Promise.resolve(loadXML(url)).then(function (capabilitiesXml) {
const capabilities = parseCapabilities(xml2json(capabilitiesXml));

if (capabilities === undefined) {
if (capabilitiesXml === undefined || capabilities === undefined) {
throw networkRequestError({
title: i18next.t(
"models.webProcessingServiceCatalogGroup.invalidCapabilitiesTitle"
Expand Down
@@ -1,19 +1,15 @@
"use strict";

var ImageryLayerFeatureInfo =
require("terriajs-cesium/Source/Scene/ImageryLayerFeatureInfo").default;
var defined = require("terriajs-cesium/Source/Core/defined").default;

var formatPropertyValue = require("../../Core/formatPropertyValue");
import defined from "terriajs-cesium/Source/Core/defined";
import ImageryLayerFeatureInfo from "terriajs-cesium/Source/Scene/ImageryLayerFeatureInfo";
import formatPropertyValue from "../../Core/formatPropertyValue";

/**
* Configures the description of this feature by creating an HTML table of properties and their values.
*
* @param {Object} properties An object literal containing the properties of the feature.
*/
ImageryLayerFeatureInfo.prototype.configureDescriptionFromProperties =
function (properties) {
function describe(properties) {
function (properties: any) {
function describe(properties: any) {
var html = '<table class="cesium-infoBox-defaultTable">';
for (var key in properties) {
if (Object.prototype.hasOwnProperty.call(properties, key)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Models/GlobeOrMap.ts
Expand Up @@ -33,7 +33,7 @@ import createStratumInstance from "./Definition/createStratumInstance";
import TerriaFeature from "./Feature/Feature";
import Terria from "./Terria";

require("./Feature/ImageryLayerFeatureInfo"); // overrides Cesium's prototype.configureDescriptionFromProperties
import "./Feature/ImageryLayerFeatureInfo"; // overrides Cesium's prototype.configureDescriptionFromProperties

export default abstract class GlobeOrMap {
abstract readonly type: string;
Expand Down
5 changes: 2 additions & 3 deletions lib/Models/ItemSearchProviders/TextIndex.ts
@@ -1,8 +1,7 @@
import MiniSearch, { Options as MiniSearchOptions } from "minisearch";
import joinUrl from "./joinUrl";
import loadText from "../../Core/loadText";
import { IndexBase, IndexType } from "./Types";

const loadText = require("../../Core/loadText");
import joinUrl from "./joinUrl";

// Text search query
type TextSearchQuery = string;
Expand Down
15 changes: 8 additions & 7 deletions lib/Models/getToken.js → lib/Models/getToken.ts
@@ -1,12 +1,13 @@
const defined = require("terriajs-cesium/Source/Core/defined").default;
const loadWithXhr = require("../Core/loadWithXhr");
const TerriaError = require("../Core/TerriaError").default;
var i18next = require("i18next").default;
import defined from "terriajs-cesium/Source/Core/defined";
import loadWithXhr from "../Core/loadWithXhr";
import TerriaError from "../Core/TerriaError";
import i18next from "i18next";
import Terria from "./Terria";

function getToken(terria, tokenUrl, url) {
function getToken(terria: Terria, tokenUrl: string, url: string) {
const options = {
url: tokenUrl,
method: "POST",
method: "POST" as const,
headers: { "Content-Type": "application/json" },
data: JSON.stringify({
url: url
Expand Down Expand Up @@ -48,4 +49,4 @@ function getToken(terria, tokenUrl, url) {
});
}

module.exports = getToken;
export default getToken;
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -199,6 +199,7 @@
"@types/linkify-it": "^3.0.5",
"@types/markdown-it": "^14.0.1",
"@types/node": "^18.15.11",
"@types/proj4": "^2.5.5",
"@types/webpack": "4.41.33",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -1867,6 +1867,11 @@
resolved "https://registry.yarnpkg.com/@types/pbf/-/pbf-3.0.2.tgz#8d291ad68b4b8c533e96c174a2e3e6399a59ed61"
integrity sha512-EDrLIPaPXOZqDjrkzxxbX7UlJSeQVgah3i0aA4pOSzmK9zq3BIh7/MZIQxED7slJByvKM4Gc6Hypyu2lJzh3SQ==

"@types/proj4@^2.5.5":
version "2.5.5"
resolved "https://registry.yarnpkg.com/@types/proj4/-/proj4-2.5.5.tgz#044d53782dc75f20335577ca3af2643962a56980"
integrity sha512-y4tHUVVoMEOm2nxRLQ2/ET8upj/pBmoutGxFw2LZJTQWPgWXI+cbxVEUFFmIzr/bpFR83hGDOTSXX6HBeObvZA==

"@types/prop-types@*":
version "15.7.4"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.4.tgz#fcf7205c25dff795ee79af1e30da2c9790808f11"
Expand Down

0 comments on commit a9094e8

Please sign in to comment.