Skip to content

Commit

Permalink
Merge pull request #7114 from TerriaJS/ambient-types
Browse files Browse the repository at this point in the history
Provide ambient types or TSify some js[x] files.
  • Loading branch information
na9da authored Jun 17, 2024
2 parents 07ee722 + cb7ec0f commit ada63cd
Show file tree
Hide file tree
Showing 92 changed files with 659 additions and 472 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### next release (8.7.5)

- TSify some `js` and `jsx` files and provide `.d.ts` ambient type files for a few others. This is so that running `tsc` on an external project that imports Terria code will typecheck successfully.
- [The next improvement]

#### 8.7.4 - 2024-06-07
Expand All @@ -23,6 +24,7 @@

#### 8.7.2 - 2024-05-14

- Add NumberParameterEditor to enable WPS AllowedValues Ranges to be set and use DefaultValue
- Feature info template has access to activeStyle of item having TableTraits.
- Updated a few dependencies to fix security warnings: `underscore`, `visx`, `shpjs`, `resolve-uri-loader`, `svg-sprite-loader`
- Allow related maps UI strings to be translated. Translation support for related maps content is not included.
Expand Down
9 changes: 9 additions & 0 deletions buildprocess/configureWebpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function configureWebpack(
".webpack.js",
".web.js",
".js",
".mjs",
".ts",
".tsx"
];
Expand Down Expand Up @@ -111,6 +112,14 @@ function configureWebpack(
})
});

// Some packages exports an .mjs file for ESM imports.
// This rule instructs webpack to import mjs modules correctly.
config.module.rules.push({
test: /\.mjs$/,
include: /node_modules/,
type: "javascript/auto"
});

const zipJsDir = path.dirname(require.resolve("@zip.js/zip.js/package.json"));

config.module.rules.push({
Expand Down
17 changes: 17 additions & 0 deletions lib/Core/ConsoleAnalytics.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
declare class ConsoleAnalytics {
start: (
configParameters: Partial<{
enableConsoleAnalytics: boolean;
googleAnalyticsKey: any;
googleAnalyticsOptions: any;
}>
) => void;
logEvent: (
category: string,
action: string,
label?: string,
value?: number
) => void;
}

export default ConsoleAnalytics;
4 changes: 1 addition & 3 deletions lib/Core/ConsoleAnalytics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

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

Expand Down Expand Up @@ -49,4 +47,4 @@ ConsoleAnalytics.prototype.logEvent = function (
}
};

module.exports = ConsoleAnalytics;
export default ConsoleAnalytics;
8 changes: 2 additions & 6 deletions lib/Core/DataUri.js → lib/Core/DataUri.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
"use strict";

var DataUri = {
export default {
/**
* Turn a file with the supplied type and stringified data into a data uri that can be set as the href of an anchor tag.
* @param {String} type Data type, eg. 'json' or 'csv'.
* @param {String} dataString The data.
* @return {String} A string that can be used to in an anchor tag's 'href' attribute to represent downloadable data.
*/
make: function (type, dataString) {
make: function (type: string, dataString: string): string | undefined {
if (dataString) {
// Using attachment/* mime type makes safari download as attachment. text/* works on Chrome (as does attachment).
return "data:attachment/" + type + "," + encodeURIComponent(dataString);
}
}
};

module.exports = DataUri;
6 changes: 6 additions & 0 deletions lib/Core/ServerConfig.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare class ServerConfig {
config: unknown;
init(serverConfigUrl: string): Promise<unknown>;
}

export default ServerConfig;
4 changes: 1 addition & 3 deletions lib/Core/ServerConfig.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

const i18next = require("i18next").default;
var defaultValue = require("terriajs-cesium/Source/Core/defaultValue").default;
var loadJson = require("./loadJson").default;
Expand Down Expand Up @@ -49,4 +47,4 @@ ServerConfig.prototype.init = function (serverConfigUrl) {
});
};

module.exports = ServerConfig;
export default ServerConfig;
12 changes: 0 additions & 12 deletions lib/Core/arrayContains.js

This file was deleted.

25 changes: 0 additions & 25 deletions lib/Core/containsAny.js

This file was deleted.

23 changes: 23 additions & 0 deletions lib/Core/containsAny.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import defined from "terriajs-cesium/Source/Core/defined";

/**
* Determins is a given string contains any of a number of possible strings.
*
* @param s The string to test.
* @param possibleStrings The possible strings to test `s` for.
* @return true if `s` contains any of the strings in `possibleStrings`; otherwise, false.
*/
const containsAny = function (s: string, possibleStrings: string[]) {
if (!defined(s)) {
return false;
}

for (let i = 0; i < possibleStrings.length; ++i) {
if (s.indexOf(possibleStrings[i]) >= 0) {
return true;
}
}
return false;
};

export default containsAny;
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";

var linkifyContent = require("./linkifyContent");
import linkifyContent from "./linkifyContent";

/**
* Format the value for the description, used by the Feature Info Panel.
Expand All @@ -9,7 +7,7 @@ var linkifyContent = require("./linkifyContent");
* @param {} value The value to format.
* @param {Object} [options] Number formatting options, passed to Number.toLocaleString().
*/
function formatPropertyValue(value, options) {
function formatPropertyValue(value: any, options?: Intl.NumberFormatOptions) {
if (typeof value === "number") {
// Note we default useGrouping to false (not true) and maximumFractionDigits to 20 (not 3).
return value.toLocaleString(undefined, {
Expand All @@ -27,4 +25,4 @@ function formatPropertyValue(value, options) {
return value;
}

module.exports = formatPropertyValue;
export default formatPropertyValue;
14 changes: 7 additions & 7 deletions lib/Core/linkifyContent.js → lib/Core/linkifyContent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use strict";
import linkifyIt from "linkify-it";

var linkify = require("linkify-it")();
const linkify = linkifyIt();

function linkifyContent(content) {
var matches = linkify.match(content),
result = [],
last;
function linkifyContent(content: string) {
const matches = linkify.match(content),
result = [];
let last: number;

if (matches) {
last = 0;
Expand All @@ -29,4 +29,4 @@ function linkifyContent(content) {
return content;
}

module.exports = linkifyContent;
export default linkifyContent;
2 changes: 0 additions & 2 deletions lib/Core/loadArrayBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ export default function loadArrayBuffer(
): Promise<ArrayBuffer> {
return Resource.fetchArrayBuffer({ url: urlOrResource, headers: headers })!;
}

module.exports = loadArrayBuffer;
10 changes: 0 additions & 10 deletions lib/Core/loadText.js

This file was deleted.

12 changes: 12 additions & 0 deletions lib/Core/loadText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Resource from "terriajs-cesium/Source/Core/Resource";

async function loadText(urlOrResource: string | Resource): Promise<string> {
const resource = (Resource as any).createIfNeeded(urlOrResource) as Resource;
const response = resource.fetchText();
if (response === undefined) {
throw new Error("Request throttled");
}
return response;
}

export default loadText;
13 changes: 13 additions & 0 deletions lib/Core/loadWithXhr.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Resource from "terriajs-cesium/Source/Core/Resource";

interface Options extends Resource.ConstructorOptions {
responseType?: string;
headers?: any;
overrideMimeType?: string;
method?: "GET" | "POST" | "PUT";
data?: any;
}

declare function loadWithXhr(options: Options): Promise<any>;

export default loadWithXhr;
2 changes: 1 addition & 1 deletion lib/Core/loadWithXhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ Object.defineProperties(loadWithXhr, {
}
});

module.exports = loadWithXhr;
export default loadWithXhr;
20 changes: 11 additions & 9 deletions lib/Core/loadXML.js → lib/Core/loadXML.ts
Original file line number Diff line number Diff line change
@@ -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> {
const resource = (Resource as any).createIfNeeded(urlOrResource) as Resource;
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;
10 changes: 4 additions & 6 deletions lib/Core/markdownToHtml.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";

const defined = require("terriajs-cesium/Source/Core/defined").default;
const MarkdownIt = require("markdown-it");
const DOMPurify = require("dompurify/dist/purify");
import injectTerms from "./injectTerms";
import DOMPurify from "dompurify";
import MarkdownIt from "markdown-it";
import defined from "terriajs-cesium/Source/Core/defined";
import { Term } from "../ReactViewModels/defaultTerms";
import injectTerms from "./injectTerms";

const md = new MarkdownIt({
html: true,
Expand Down
32 changes: 0 additions & 32 deletions lib/Core/pollToPromise.js

This file was deleted.

35 changes: 35 additions & 0 deletions lib/Core/pollToPromise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import defaultValue from "terriajs-cesium/Source/Core/defaultValue";
import getTimestamp from "terriajs-cesium/Source/Core/getTimestamp";

interface Options {
pollInterval?: number;
timeout?: number;
}

const pollToPromise = function (f: () => boolean, options: Options) {
options = defaultValue(options, (defaultValue as any).EMPTY_OBJECT);

const pollInterval = defaultValue(options.pollInterval, 1);
const timeout = defaultValue(options.timeout, 5000);

return new Promise<void>((resolve, reject) => {
const startTimestamp = getTimestamp();
const endTimestamp = startTimestamp + timeout;

function poller() {
if (f()) {
resolve();
} else {
if (getTimestamp() > endTimestamp) {
reject();
} else {
setTimeout(poller, pollInterval);
}
}
}

poller();
});
};

export default pollToPromise;
Loading

0 comments on commit ada63cd

Please sign in to comment.