@magda/typescript-common 1.2.2-alpha.0 → 1.3.1-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ServerError.d.ts +9 -0
- package/dist/ServerError.js +17 -0
- package/dist/ServerError.js.map +1 -0
- package/dist/getAbsoluteUrl.d.ts +5 -4
- package/dist/getAbsoluteUrl.js +21 -17
- package/dist/getAbsoluteUrl.js.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class ServerError extends Error {
|
|
4
|
+
constructor(message = "Unknown Error", statusCode = 500) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.statusCode = statusCode;
|
|
7
|
+
}
|
|
8
|
+
toData() {
|
|
9
|
+
return {
|
|
10
|
+
isError: true,
|
|
11
|
+
errorCode: this.statusCode,
|
|
12
|
+
errorMessage: this.message
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.default = ServerError;
|
|
17
|
+
//# sourceMappingURL=ServerError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServerError.js","sourceRoot":"","sources":["../src/ServerError.ts"],"names":[],"mappings":";;AAAA,MAAqB,WAAY,SAAQ,KAAK;IAG1C,YAAY,UAAkB,eAAe,EAAE,aAAqB,GAAG;QACnE,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IACjC,CAAC;IAED,MAAM;QACF,OAAO;YACH,OAAO,EAAE,IAAI;YACb,SAAS,EAAE,IAAI,CAAC,UAAU;YAC1B,YAAY,EAAE,IAAI,CAAC,OAAO;SAC7B,CAAC;IACN,CAAC;CACJ;AAfD,8BAeC"}
|
package/dist/getAbsoluteUrl.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Join `url` with `baseUrl` if `url` is not an absolute url
|
|
2
|
+
* Join `url` with `baseUrl` if `url` is not an absolute (full) url string
|
|
3
3
|
*
|
|
4
4
|
* @export
|
|
5
|
-
* @param {string} url
|
|
6
|
-
* @param {string} baseUrl
|
|
5
|
+
* @param {string} url A full url string or a url path string (/a/b/c).
|
|
6
|
+
* @param {string} baseUrl A baseUrl used to generate a full url when a url path string is supplied via the `url` parameter.
|
|
7
7
|
* @param {{ [key: string]: string }} [optionalQueries]
|
|
8
|
+
* @param {string[]} [allowedUrlHosts] Optional; when specify, the host of `url` parameter will only be used if it is included by this list.
|
|
8
9
|
* @returns
|
|
9
10
|
*/
|
|
10
11
|
export default function getAbsoluteUrl(url: string, baseUrl: string, optionalQueries?: {
|
|
11
12
|
[key: string]: string;
|
|
12
|
-
}): string;
|
|
13
|
+
}, allowedUrlHosts?: string[]): string;
|
package/dist/getAbsoluteUrl.js
CHANGED
|
@@ -5,31 +5,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const urijs_1 = __importDefault(require("urijs"));
|
|
7
7
|
/**
|
|
8
|
-
* Join `url` with `baseUrl` if `url` is not an absolute url
|
|
8
|
+
* Join `url` with `baseUrl` if `url` is not an absolute (full) url string
|
|
9
9
|
*
|
|
10
10
|
* @export
|
|
11
|
-
* @param {string} url
|
|
12
|
-
* @param {string} baseUrl
|
|
11
|
+
* @param {string} url A full url string or a url path string (/a/b/c).
|
|
12
|
+
* @param {string} baseUrl A baseUrl used to generate a full url when a url path string is supplied via the `url` parameter.
|
|
13
13
|
* @param {{ [key: string]: string }} [optionalQueries]
|
|
14
|
+
* @param {string[]} [allowedUrlHosts] Optional; when specify, the host of `url` parameter will only be used if it is included by this list.
|
|
14
15
|
* @returns
|
|
15
16
|
*/
|
|
16
|
-
function getAbsoluteUrl(url, baseUrl, optionalQueries) {
|
|
17
|
+
function getAbsoluteUrl(url, baseUrl, optionalQueries, allowedUrlHosts) {
|
|
17
18
|
const uri = urijs_1.default(url);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
baseUrl = "";
|
|
19
|
+
const urlHost = uri.host();
|
|
20
|
+
if (urlHost) {
|
|
21
|
+
// --- absolute url, return directly only if the urlHost is included by `allowedUrlHosts` (unless `allowedUrlHosts` is not supplied)
|
|
22
|
+
if (!allowedUrlHosts ||
|
|
23
|
+
allowedUrlHosts.findIndex((item) => item === urlHost) !== -1) {
|
|
24
|
+
return url;
|
|
25
25
|
}
|
|
26
|
-
const baseUri = urijs_1.default(baseUrl);
|
|
27
|
-
const query = uri.search(true);
|
|
28
|
-
const mergedUri = baseUri.segmentCoded(baseUri.segmentCoded().concat(uri.segmentCoded()));
|
|
29
|
-
return mergedUri
|
|
30
|
-
.search(Object.assign(Object.assign({}, (query ? query : {})), (optionalQueries ? optionalQueries : {})))
|
|
31
|
-
.toString();
|
|
32
26
|
}
|
|
27
|
+
// ignore url host of `host` if any and use `baseUrl` to create the final full url string
|
|
28
|
+
if (typeof baseUrl !== "string") {
|
|
29
|
+
baseUrl = "";
|
|
30
|
+
}
|
|
31
|
+
const baseUri = urijs_1.default(baseUrl);
|
|
32
|
+
const query = uri.search(true);
|
|
33
|
+
const mergedUri = baseUri.segmentCoded(baseUri.segmentCoded().concat(uri.segmentCoded()));
|
|
34
|
+
return mergedUri
|
|
35
|
+
.search(Object.assign(Object.assign({}, (query ? query : {})), (optionalQueries ? optionalQueries : {})))
|
|
36
|
+
.toString();
|
|
33
37
|
}
|
|
34
38
|
exports.default = getAbsoluteUrl;
|
|
35
39
|
//# sourceMappingURL=getAbsoluteUrl.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getAbsoluteUrl.js","sourceRoot":"","sources":["../src/getAbsoluteUrl.ts"],"names":[],"mappings":";;;;;AAAA,kDAA0B;AAE1B
|
|
1
|
+
{"version":3,"file":"getAbsoluteUrl.js","sourceRoot":"","sources":["../src/getAbsoluteUrl.ts"],"names":[],"mappings":";;;;;AAAA,kDAA0B;AAE1B;;;;;;;;;GASG;AACH,SAAwB,cAAc,CAClC,GAAW,EACX,OAAe,EACf,eAA2C,EAC3C,eAA0B;IAE1B,MAAM,GAAG,GAAG,eAAK,CAAC,GAAG,CAAC,CAAC;IACvB,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC;IAC3B,IAAI,OAAO,EAAE;QACT,oIAAoI;QACpI,IACI,CAAC,eAAe;YAChB,eAAe,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC,EAC9D;YACE,OAAO,GAAG,CAAC;SACd;KACJ;IACD,yFAAyF;IACzF,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QAC7B,OAAO,GAAG,EAAE,CAAC;KAChB;IACD,MAAM,OAAO,GAAG,eAAK,CAAC,OAAO,CAAC,CAAC;IAC/B,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,SAAS,GAAG,OAAO,CAAC,YAAY,CAClC,OAAO,CAAC,YAAY,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC,CACpD,CAAC;IAEF,OAAO,SAAS;SACX,MAAM,iCACA,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,GACpB,CAAC,eAAe,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,EAC7C;SACD,QAAQ,EAAE,CAAC;AACpB,CAAC;AAjCD,iCAiCC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magda/typescript-common",
|
|
3
3
|
"description": "Common TypeScript code shared between components.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.1-rc.0",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"prebuild": "rimraf dist tsconfig.tsbuildinfo",
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"release": "npm publish || echo \"Skip releasing npm package @magda/typescript-common.\""
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@magda/registry-aspects": "^1.
|
|
21
|
-
"@magda/scripts": "^1.
|
|
20
|
+
"@magda/registry-aspects": "^1.3.1-rc.0",
|
|
21
|
+
"@magda/scripts": "^1.3.1-rc.0",
|
|
22
22
|
"@types/chai": "^4.0.0",
|
|
23
23
|
"@types/chai-as-promised": "^7.1.0",
|
|
24
24
|
"@types/cross-spawn": "^6.0.1",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@types/sinon": "^4.1.3",
|
|
32
32
|
"@types/superagent": "^3.5.5",
|
|
33
33
|
"@types/supertest": "^2.0.3",
|
|
34
|
-
"@types/urijs": "1.19.
|
|
34
|
+
"@types/urijs": "^1.19.19",
|
|
35
35
|
"@types/uuid": "^8.0.0",
|
|
36
36
|
"@types/yargs": "^12.0.8",
|
|
37
37
|
"chai": "^4.1.2",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"request": "^2.88.0",
|
|
64
64
|
"resolve": "^1.15.0",
|
|
65
65
|
"tsmonad": "^0.7.2",
|
|
66
|
-
"urijs": "^1.19.
|
|
66
|
+
"urijs": "^1.19.11",
|
|
67
67
|
"uuid": "^8.2.0",
|
|
68
68
|
"yargs": "^12.0.5"
|
|
69
69
|
},
|