@openremote/rest 1.0.2 → 1.2.0-snapshot.20240512154942

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/README.md CHANGED
@@ -1,27 +1,74 @@
1
- # or-core
2
-
3
- > ${DESCRIPTION}
4
-
1
+ # @openremote/rest
5
2
  [![NPM Version][npm-image]][npm-url]
6
3
  [![Linux Build][travis-image]][travis-url]
7
4
  [![Test Coverage][coveralls-image]][coveralls-url]
8
5
 
9
- ## Install
6
+ ES6 modules for connecting to an OpenRemote Manager as well as utilities for performing common tasks.
7
+
8
+ The default export is a singleton of type `RestApi` that can be used to communicate with the OpenRemote Manager REST API.
9
+ It uses an [axios](https://github.com/axios/axios) client to perform the requests and it contains strongly typed
10
+ definitions for each OpenRemote Manager REST API endpoint (JAX-RS resource).
11
+
10
12
 
13
+
14
+ ## Install
11
15
  ```bash
12
- npm i -S @openremote/or-core
16
+ npm i @openremote/rest
17
+ yarn add @openremote/rest
13
18
  ```
14
19
 
15
20
  ## Usage
21
+ For a full list of properties, methods and options refer to the TypeDoc generated [documentation]().
16
22
 
17
- TODO
23
+ If used in conjunction with `@openremote/core` and the `Manager` `init` method has been called then the default export
24
+ will be ready to use, the endpoints can be accessed via the `RestApi` `api` property and each JAX-RS resource defined
25
+ in the OpenRemote Manager is also defined with the same name in the `RestApi` object.
18
26
 
19
- ## License
27
+ ```$typescript
28
+ import openremote from "@openremote/core";
29
+ import rest from "@openremote/rest";
30
+
31
+ openremote.init({
32
+ ...
33
+ }).then((success) => {
34
+ if (success) {
35
+ let assetQuery = ...;
36
+ let response = await rest.api.AssetResource.queryAssets(assetQuery);
37
+ let assets = response.data;
38
+
39
+ // Do something with the assets
40
+ } else {
41
+ // Something has gone wrong
42
+ }
43
+ });
44
+ ```
20
45
 
46
+ It is possible to add additional request interceptors by calling the `addRequestInterceptor` method, it is also possible
47
+ to access the `AxiosInstance` by calling the `axiosInstance` property.
48
+
49
+ It is also possible to instantiate the `RestApi` object on demand but note you will need to ensure the Authorization
50
+ header is correctly set if calling secure endpoints on the OpenRemote Manager REST API.
51
+
52
+ ```$typescript
53
+ import {RestApi} from "@openremote/rest";
54
+
55
+ let rest = new RestApi();
56
+ rest.setTimeout(10000);
57
+ rest.addRequestInterceptor(...);
58
+ rest.initialise();
59
+ ```
60
+
61
+
62
+ ## Supported Browsers
63
+ The last 2 versions of all modern browsers are supported, including Chrome, Safari, Opera, Firefox, Edge. In addition,
64
+ Internet Explorer 11 is also supported.
65
+
66
+
67
+ ## License
21
68
  [GNU AGPL](https://www.gnu.org/licenses/agpl-3.0.en.html)
22
69
 
23
70
  [npm-image]: https://img.shields.io/npm/v/live-xxx.svg
24
- [npm-url]: https://npmjs.org/package/live-xxx
71
+ [npm-url]: https://npmjs.org/package/@openremote/core
25
72
  [travis-image]: https://img.shields.io/travis/live-js/live-xxx/master.svg
26
73
  [travis-url]: https://travis-ci.org/live-js/live-xxx
27
74
  [coveralls-image]: https://img.shields.io/coveralls/live-js/live-xxx/master.svg
package/lib/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { AxiosInstance, AxiosRequestConfig, GenericAxiosResponse, AxiosError } from "axios";
2
+ import { ApiClient, RestResponse } from "./restclient";
3
+ declare const isAxiosError: (payload: any) => payload is AxiosError<any, any>;
4
+ export { RestResponse, GenericAxiosResponse, AxiosError, isAxiosError };
5
+ export declare class RestApi {
6
+ get api(): ApiClient;
7
+ protected _client: ApiClient;
8
+ protected _axiosInstance: AxiosInstance;
9
+ protected _baseUrl: string;
10
+ constructor();
11
+ get axiosInstance(): AxiosInstance;
12
+ get baseUrl(): string;
13
+ setTimeout(timeout: number): void;
14
+ addRequestInterceptor(interceptor: (config: AxiosRequestConfig) => AxiosRequestConfig): void;
15
+ initialise(baseUrl: string): void;
16
+ }
17
+ declare const _default: RestApi;
18
+ export default _default;
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ import axios from"axios";import{ApiClient}from"./restclient";import Qs from"qs";const isAxiosError=axios.isAxiosError;export{isAxiosError};export class RestApi{get api(){return this._client}constructor(){this._axiosInstance=axios.create(),this._axiosInstance.defaults.headers.common["Content-Type"]="application/json",this._axiosInstance.interceptors.request.use((t=>(t.paramsSerializer=t=>Qs.stringify(t,{arrayFormat:"repeat"}),t)))}get axiosInstance(){return this._axiosInstance}get baseUrl(){return this._baseUrl}setTimeout(t){this._axiosInstance.defaults.timeout=t}addRequestInterceptor(t){this._axiosInstance.interceptors.request.use(t)}initialise(t){this._baseUrl=t,this._client=new ApiClient(t,this._axiosInstance)}}export default new RestApi;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAA4E,MAAM,OAAO,CAAC;AACjG,OAAO,EAAC,SAAS,EAAe,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,MAAM,IAAI,CAAC;AAEpB,MAAM,YAAY,GAAG,KAAK,CAAC,YAAY,CAAC;AAExC,OAAO,EAAiD,YAAY,EAAC,CAAC;AAEtE,MAAM,OAAO,OAAO;IAEhB,IAAI,GAAG;QACH,OAAO,IAAI,CAAC,OAAO,CAAC;IACxB,CAAC;IAMD;QACI,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;QACrC,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,GAAG,kBAAkB,CAAC;QACjF,IAAI,CAAC,cAAc,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE;YACpD,MAAM,CAAC,gBAAgB,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,SAAS,CAAC,MAAM,EAAE,EAAC,WAAW,EAAE,QAAQ,EAAC,CAAC,CAAC;YACpF,OAAO,MAAM,CAAC;QAClB,CAAC,CAAC,CAAC;IACP,CAAC;IAED,IAAI,aAAa;QACb,OAAO,IAAI,CA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