@deltares/fews-web-oc-utils 0.1.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Deltares
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # fews-web-oc-utils
2
+
3
+ ## Project setup
4
+ ```
5
+ npm install
6
+ ```
7
+
8
+ ### Compiles and hot-reloads for development
9
+ ```
10
+ npm run serve
11
+ ```
12
+
13
+ ### Compiles and minifies for production
14
+ ```
15
+ npm run build
16
+ ```
17
+
18
+ ### Lints and fixes files
19
+ ```
20
+ npm run lint
21
+ ```
22
+
23
+ ### Run Unit tests
24
+ ```
25
+ npm run test
26
+ ```
@@ -0,0 +1 @@
1
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["fews-pi-requests"]=t():e["fews-pi-requests"]=t()}(self,(function(){return(()=>{"use strict";var e={};return(e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})})(e),e})()}));
@@ -0,0 +1,2 @@
1
+ export * from './restservice';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=dataRequestResult.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dataRequestResult.js","sourceRoot":"","sources":["../../../src/restservice/dataRequestResult.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ export * from './dataRequestResult';
2
+ export * from './piRestService';
3
+ export * from './requestOptions';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/restservice/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA"}
@@ -0,0 +1,45 @@
1
+ export default class PiRestService {
2
+ webserviceUrl;
3
+ _oauth2Token = undefined;
4
+ set oauth2Token(value) {
5
+ this._oauth2Token = value;
6
+ }
7
+ constructor(webserviceUrl) {
8
+ this.webserviceUrl = webserviceUrl;
9
+ }
10
+ async getData(url) {
11
+ const dataRequestResult = {};
12
+ const requestParameters = {};
13
+ requestParameters.method = "GET";
14
+ if (this._oauth2Token !== undefined) {
15
+ requestParameters.headers = { "Authorization": "Bearer " + this._oauth2Token };
16
+ }
17
+ const res = await fetch(url, requestParameters);
18
+ return await this.processResponse(dataRequestResult, res, url);
19
+ }
20
+ async processResponse(dataRequestResult, res, url) {
21
+ dataRequestResult.responseCode = res.status;
22
+ if (res.status != 200) {
23
+ dataRequestResult.errorMessage = res.statusText;
24
+ return dataRequestResult;
25
+ }
26
+ try {
27
+ dataRequestResult.data = await res.json();
28
+ }
29
+ catch (e) {
30
+ e.message += `\n When loading ${url}.`;
31
+ throw e;
32
+ }
33
+ return dataRequestResult;
34
+ }
35
+ async getDataWithRequestInit(url, requestInit) {
36
+ const dataRequestResult = {};
37
+ if (this._oauth2Token !== undefined) {
38
+ const authorizationHeader = { "Authorization": "Bearer " + this._oauth2Token };
39
+ requestInit.headers = { ...authorizationHeader, ...requestInit.headers };
40
+ }
41
+ const res = await fetch(url, requestInit);
42
+ return await this.processResponse(dataRequestResult, res, url);
43
+ }
44
+ }
45
+ //# sourceMappingURL=piRestService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"piRestService.js","sourceRoot":"","sources":["../../../src/restservice/piRestService.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,OAAO,aAAa;IACb,aAAa,CAAS;IAC/B,YAAY,GAAuB,SAAS,CAAC;IAErD,IAAI,WAAW,CAAC,KAAa;QACzB,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;IAC9B,CAAC;IAED,YAAY,aAAqB;QAC7B,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACvC,CAAC;IAEM,KAAK,CAAC,OAAO,CAAI,GAAW;QAC/B,MAAM,iBAAiB,GAAG,EAA0B,CAAC;QACrD,MAAM,iBAAiB,GAAG,EAAiB,CAAC;QAC5C,iBAAiB,CAAC,MAAM,GAAG,KAAK,CAAC;QACjC,IAAI,IAAI,CAAC,YAAY,KAAK,SAAS,EAAE;YACjC,iBAAiB,CAAC,OAAO,GAAG,EAAC,eAAe,EAAE,SAAS,GAAG,IAAI,CAAC,YAAY,EAAC,CAAA;SAC/E;QAED,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,EAAC,iBAAiB,CAAC,CAAC;QAC/C,OAAO,MAAM,IAAI,CAAC,eAAe,CAAC,iBAAiB,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IACnE,CAAC;IAEO,KAAK,CAAC,eAAe,CAAI,iBAAuC,EAAE,GAAa,EAAE,GAAW;QAChG,iBAAiB,CAAC,YAAY,GAAG,GAAG,CAAC,MAAM,CAAC;QAC5C,IAAI,GAAG,CAAC,MAAM,IAAI,GAAG,EAAE;YACnB,iBAAiB,CAAC,YAAY,GAAG,GAAG,CAAC,UAAU,CAAC;YAChD,OAAO,iBAAiB,CAAC;SAC5B;QACD,IAAI;YACA,iBAAiB,CAAC,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;SAC7C;QAAC,OAAO,CAAM,EAAE;YACb,CAAC,CAAC,OAAO,IAAI,mBAAmB,GAAG,GAAG,CAAA;YACtC,MAAM,CAAC,CAAC;SACX;QACD,OAAO,iBAAiB,CAAC;IAC7B,CAAC;IAEM,KAAK,CAAC,sBAAsB,CAAI,GAAW,EAAE,WAAwB;QACxE,MAAM,iBAAiB,GAAG,EAA0B,CAAC;QACrD,IAAI,IAAI,CAAC,YAAY,KAAK,SAAS,EAAE;YACjC,MAAM,mBAAmB,GAAG,EAAC,eAAe,EAAE,SAAS,GAAG,IAAI,CAAC,YAAY,EAAC,CAAC;YAC7E,WAAW,CAAC,OAAO,GAAG,EAAC,GAAG,mBAAmB,EAAE,GAAG,WAAW,CAAC,OAAO,EAAC,CAAC;SAC1E;QACD,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;QAC1C,OAAO,MAAM,IAAI,CAAC,eAAe,CAAC,iBAAiB,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IACnE,CAAC;CACJ"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=requestOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"requestOptions.js","sourceRoot":"","sources":["../../../src/restservice/requestOptions.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export * from './restservice';
@@ -0,0 +1,5 @@
1
+ export default interface DataRequestResult<T> {
2
+ responseCode: number;
3
+ errorMessage: string;
4
+ data: T;
5
+ }
@@ -0,0 +1,3 @@
1
+ export * from './dataRequestResult';
2
+ export * from './piRestService';
3
+ export * from './requestOptions';
@@ -0,0 +1,10 @@
1
+ import DataRequestResult from "./dataRequestResult";
2
+ export default class PiRestService {
3
+ private readonly webserviceUrl;
4
+ private _oauth2Token;
5
+ set oauth2Token(value: string);
6
+ constructor(webserviceUrl: string);
7
+ getData<T>(url: string): Promise<DataRequestResult<T>>;
8
+ private processResponse;
9
+ getDataWithRequestInit<T>(url: string, requestInit: RequestInit): Promise<DataRequestResult<T>>;
10
+ }
@@ -0,0 +1,3 @@
1
+ export default interface RequestOptions {
2
+ cache: string;
3
+ }
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@deltares/fews-web-oc-utils",
3
+ "version": "0.1.3",
4
+ "description": "Util Library for common Web OC functionality",
5
+ "author": {
6
+ "name": "Werner Kramer",
7
+ "email": "werner.kramer@deltares.nl"
8
+ },
9
+ "keywords": [
10
+ "FEWS",
11
+ "PI REST Service",
12
+ "Web Operator Client",
13
+ "Web OC"
14
+ ],
15
+ "license": "MIT",
16
+ "main": "./dist/fews-web-oc-utils.umd.js",
17
+ "typings": "./dist/types/index.d.ts",
18
+ "private": false,
19
+ "scripts": {
20
+ "build": "npm run clean && tsc && webpack",
21
+ "clean": "shx rm -rf dist",
22
+ "doc": "typedoc --out doc src",
23
+ "lint": "eslint src/**.ts",
24
+ "lint:fix": "eslint src/**.ts --fix",
25
+ "test:unit": "jest unit --collect-coverage",
26
+ "test:e2e": "jest e2e",
27
+ "test": "jest --coverage"
28
+ },
29
+ "devDependencies": {
30
+ "@types/jest": "^28.0.0",
31
+ "@types/jsdom": "^16.2.14",
32
+ "@typescript-eslint/eslint-plugin": "^2.18.0",
33
+ "@typescript-eslint/parser": "^2.18.0",
34
+ "cross-fetch": "^3.1.5",
35
+ "dotenv": "^16.0.0",
36
+ "eslint": "^6.8.0",
37
+ "fetch-mock": "^9.11.0",
38
+ "jest": "^28.0.1",
39
+ "jest-environment-jsdom": "^29.0.1",
40
+ "jest-junit": "^15.0.0",
41
+ "jest-teamcity-reporter": "^0.9.0",
42
+ "jsdom": "^19.0.0",
43
+ "shx": "^0.3.2",
44
+ "ts-jest": "^28.0.8",
45
+ "ts-loader": "^9.3.1",
46
+ "typedoc": "^0.23.13",
47
+ "typescript": "^4.8.2",
48
+ "webpack": "^5.59.0",
49
+ "webpack-cli": "^4.9.1"
50
+ },
51
+ "jest-junit": {
52
+ "outputDirectory": "reports/",
53
+ "outputName": "junit.xml"
54
+ },
55
+ "engines": {
56
+ "node": ">=0.14"
57
+ },
58
+ "files": [
59
+ "dist/*",
60
+ "src/*"
61
+ ],
62
+ "repository": {
63
+ "type": "git",
64
+ "url": "git+https://github.com/Deltares/fews-web-oc-utils"
65
+ },
66
+ "bugs": {
67
+ "url": "https://github.com/Deltares/fews-web-oc-utils/issues"
68
+ }
69
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './restservice'
@@ -0,0 +1,6 @@
1
+ export default interface DataRequestResult<T> {
2
+ responseCode: number;
3
+ errorMessage: string;
4
+ data: T;
5
+
6
+ }
@@ -0,0 +1,3 @@
1
+ export * from './dataRequestResult'
2
+ export * from './piRestService'
3
+ export * from './requestOptions'
@@ -0,0 +1,51 @@
1
+ import DataRequestResult from "./dataRequestResult";
2
+
3
+ export default class PiRestService {
4
+ private readonly webserviceUrl: string;
5
+ private _oauth2Token: string | undefined = undefined;
6
+
7
+ set oauth2Token(value: string) {
8
+ this._oauth2Token = value;
9
+ }
10
+
11
+ constructor(webserviceUrl: string) {
12
+ this.webserviceUrl = webserviceUrl;
13
+ }
14
+
15
+ public async getData<T>(url: string): Promise<DataRequestResult<T>> {
16
+ const dataRequestResult = {} as DataRequestResult<T>;
17
+ const requestParameters = {} as RequestInit;
18
+ requestParameters.method = "GET";
19
+ if (this._oauth2Token !== undefined) {
20
+ requestParameters.headers = {"Authorization": "Bearer " + this._oauth2Token}
21
+ }
22
+
23
+ const res = await fetch(url,requestParameters);
24
+ return await this.processResponse(dataRequestResult, res, url);
25
+ }
26
+
27
+ private async processResponse<T>(dataRequestResult: DataRequestResult<T>, res: Response, url: string): Promise<DataRequestResult<T>> {
28
+ dataRequestResult.responseCode = res.status;
29
+ if (res.status != 200) {
30
+ dataRequestResult.errorMessage = res.statusText;
31
+ return dataRequestResult;
32
+ }
33
+ try {
34
+ dataRequestResult.data = await res.json();
35
+ } catch (e: any) {
36
+ e.message += `\n When loading ${url}.`
37
+ throw e;
38
+ }
39
+ return dataRequestResult;
40
+ }
41
+
42
+ public async getDataWithRequestInit<T>(url: string, requestInit: RequestInit): Promise<DataRequestResult<T>> {
43
+ const dataRequestResult = {} as DataRequestResult<T>;
44
+ if (this._oauth2Token !== undefined) {
45
+ const authorizationHeader = {"Authorization": "Bearer " + this._oauth2Token};
46
+ requestInit.headers = {...authorizationHeader, ...requestInit.headers};
47
+ }
48
+ const res = await fetch(url, requestInit);
49
+ return await this.processResponse(dataRequestResult, res, url);
50
+ }
51
+ }
@@ -0,0 +1,3 @@
1
+ export default interface RequestOptions {
2
+ cache: string;
3
+ }