@faybl/client 4.1.0 → 4.1.1
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 +10 -10
- package/package.json +1 -1
- package/src/auth.js +2 -1
- package/src/browser.js +5 -5
- package/src/channel.js +2 -1
- package/src/clientOptions.js +11 -8
- package/src/conf.js +2 -1
- package/src/fetch.js +4 -0
- package/src/index.js +5 -5
package/README.md
CHANGED
|
@@ -19,18 +19,18 @@ const unsubscribe = client.channel.subscribe(ovens[0].channels.temperature, ({ d
|
|
|
19
19
|
|
|
20
20
|
Resolves with a client once its MQTT connection is set up; the connection itself completes in the
|
|
21
21
|
background, which [`connection`](#connection) follows. Rejects when the options name neither a
|
|
22
|
-
valid tenant nor
|
|
22
|
+
valid tenant nor the endpoints needed to connect, when `onError` is not a function, or when the MQTT credentials
|
|
23
23
|
cannot be fetched.
|
|
24
24
|
|
|
25
|
-
| Option | Meaning
|
|
26
|
-
| ---------------------------------------------------------- |
|
|
27
|
-
| `tenantUrl` | Host name of the tenant, such as `atenant.faybl.online`, without scheme or path. Every endpoint defaults to a URL built from it.
|
|
28
|
-
| `confUrl`, `dataHistoryUrl`, `authenticatorUrl`, `mqttUrl` | Override one endpoint. Without a `tenantUrl`,
|
|
29
|
-
| `noMqtt` | Do not connect to MQTT. The bus-facing methods then throw `MQTT is disabled`, and datasets do not load.
|
|
30
|
-
| `noAuth` | Connect to MQTT without credentials. Without it the client never connects anonymously.
|
|
31
|
-
| `onError(error, { source, topic })` | Receives what goes wrong after `createClient` has resolved. See [Errors](#errors).
|
|
32
|
-
| `authConfig` (Node) | `{ issuerUrl, clientId, clientSecret }` authenticates with the OIDC client credentials grant. With only `clientId`, see below.
|
|
33
|
-
| `useWebsocket` (Node) | Connect to MQTT over WebSocket instead of MQTTS.
|
|
25
|
+
| Option | Meaning |
|
|
26
|
+
| ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
27
|
+
| `tenantUrl` | Host name of the tenant, such as `atenant.faybl.online`, without scheme or path. Every endpoint defaults to a URL built from it. |
|
|
28
|
+
| `confUrl`, `dataHistoryUrl`, `authenticatorUrl`, `mqttUrl` | Override one endpoint. Without a `tenantUrl`, only `mqttUrl` and, when the authenticator hands out the MQTT credentials, `authenticatorUrl` are required; a call that needs an endpoint left out rejects. |
|
|
29
|
+
| `noMqtt` | Do not connect to MQTT. The bus-facing methods then throw `MQTT is disabled`, and datasets do not load. |
|
|
30
|
+
| `noAuth` | Connect to MQTT without credentials. Without it the client never connects anonymously. |
|
|
31
|
+
| `onError(error, { source, topic })` | Receives what goes wrong after `createClient` has resolved. See [Errors](#errors). |
|
|
32
|
+
| `authConfig` (Node) | `{ issuerUrl, clientId, clientSecret }` authenticates with the OIDC client credentials grant. With only `clientId`, see below. |
|
|
33
|
+
| `useWebsocket` (Node) | Connect to MQTT over WebSocket instead of MQTTS. |
|
|
34
34
|
|
|
35
35
|
Authentication:
|
|
36
36
|
|
package/package.json
CHANGED
package/src/auth.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { fetchCheckedJson } from './fetch.js';
|
|
1
|
+
import { fetchCheckedJson, missingEndpoint } from './fetch.js';
|
|
2
2
|
|
|
3
3
|
export const getAuthApi = (authenticatorUrl, http) => {
|
|
4
4
|
const getUserInfo = async () => {
|
|
5
|
+
if (!authenticatorUrl) return missingEndpoint('authenticatorUrl');
|
|
5
6
|
return fetchCheckedJson(
|
|
6
7
|
`${authenticatorUrl}/getUserInfo`,
|
|
7
8
|
{
|
package/src/browser.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { clientOptionsError } from './clientOptions.js';
|
|
1
|
+
import { clientOptionsError, tenantServiceUrl } from './clientOptions.js';
|
|
2
2
|
import { createClient as createBrowserClient } from './client.js';
|
|
3
3
|
import { createMqttClient } from './mqtt.js';
|
|
4
4
|
import { createReporter } from './report.js';
|
|
@@ -18,16 +18,16 @@ export const createClient = async (options = {}) => {
|
|
|
18
18
|
? undefined
|
|
19
19
|
: await createMqttClient({
|
|
20
20
|
mqttUrl: mqttUrl || `wss://ws.${tenantUrl}`,
|
|
21
|
-
authenticatorUrl: authenticatorUrl ||
|
|
21
|
+
authenticatorUrl: authenticatorUrl || tenantServiceUrl(tenantUrl, '/authenticator'),
|
|
22
22
|
noAuth,
|
|
23
23
|
report,
|
|
24
24
|
session,
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
return createBrowserClient({
|
|
28
|
-
authenticatorUrl: authenticatorUrl ||
|
|
29
|
-
confUrl: confUrl ||
|
|
30
|
-
dataHistoryUrl: dataHistoryUrl ||
|
|
28
|
+
authenticatorUrl: authenticatorUrl || tenantServiceUrl(tenantUrl, '/authenticator'),
|
|
29
|
+
confUrl: confUrl || tenantServiceUrl(tenantUrl, '/conf'),
|
|
30
|
+
dataHistoryUrl: dataHistoryUrl || tenantServiceUrl(tenantUrl, '/data-history'),
|
|
31
31
|
mqttClient,
|
|
32
32
|
session,
|
|
33
33
|
report,
|
package/src/channel.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { mqttDisabledWarning } from './mqttUtils.js';
|
|
2
2
|
import { assertTopicSegment } from './topicSegment.js';
|
|
3
|
-
import { fetchCheckedJson } from './fetch.js';
|
|
3
|
+
import { fetchCheckedJson, missingEndpoint } from './fetch.js';
|
|
4
4
|
|
|
5
5
|
const baseTopic = 'channel';
|
|
6
6
|
|
|
@@ -23,6 +23,7 @@ export const getChannelApi = (dataHistoryUrl, mqttClient, http) => {
|
|
|
23
23
|
const publish = (rid, pl, other) => mqttClient.publish(topicFor(rid), createPubMsg(pl, other));
|
|
24
24
|
|
|
25
25
|
const read = (args) => {
|
|
26
|
+
if (!dataHistoryUrl) return missingEndpoint('dataHistoryUrl');
|
|
26
27
|
return fetchCheckedJson(
|
|
27
28
|
`${dataHistoryUrl}/read`,
|
|
28
29
|
{
|
package/src/clientOptions.js
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
// scheme or a subdomain, so a scheme or a path in it would produce a broken URL.
|
|
3
3
|
const TENANT_HOST = /^[^\s/:]+(:\d+)?$/;
|
|
4
4
|
|
|
5
|
+
// A service's URL under the tenant, or undefined without one.
|
|
6
|
+
export const tenantServiceUrl = (tenantUrl, path) =>
|
|
7
|
+
tenantUrl === undefined ? undefined : `https://${tenantUrl}${path}`;
|
|
8
|
+
|
|
5
9
|
const tenantUrlError = (tenantUrl) =>
|
|
6
10
|
typeof tenantUrl === 'string' && TENANT_HOST.test(tenantUrl)
|
|
7
11
|
? undefined
|
|
@@ -9,12 +13,11 @@ const tenantUrlError = (tenantUrl) =>
|
|
|
9
13
|
`Invalid tenantUrl ${JSON.stringify(tenantUrl)}: it must be a host name such as 'tenant.faybl.online', without scheme or path`,
|
|
10
14
|
);
|
|
11
15
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
];
|
|
16
|
+
// Only what connecting needs is required up front: the broker, and the authenticator when it hands
|
|
17
|
+
// out the MQTT credentials. Any other endpoint left out fails only the calls that use it, so a
|
|
18
|
+
// service names the endpoints it talks to and no others.
|
|
19
|
+
const requiredEndpoints = ({ noMqtt, noAuth, authConfig }) =>
|
|
20
|
+
noMqtt ? [] : ['mqttUrl', ...(noAuth || authConfig?.issuerUrl ? [] : ['authenticatorUrl'])];
|
|
18
21
|
|
|
19
22
|
const endpointsError = (options) => {
|
|
20
23
|
if (options.tenantUrl !== undefined) return tenantUrlError(options.tenantUrl);
|
|
@@ -30,7 +33,7 @@ const onErrorError = ({ onError }) =>
|
|
|
30
33
|
: Error('Invalid onError: it must be a function');
|
|
31
34
|
|
|
32
35
|
/**
|
|
33
|
-
* Returns the Error createClient rejects with, or undefined when the options name
|
|
34
|
-
*
|
|
36
|
+
* Returns the Error createClient rejects with, or undefined when the options name either a tenant
|
|
37
|
+
* or the endpoints the client needs to connect.
|
|
35
38
|
*/
|
|
36
39
|
export const clientOptionsError = (options) => onErrorError(options) ?? endpointsError(options);
|
package/src/conf.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as R from 'ramda';
|
|
2
2
|
|
|
3
3
|
import { mqttDisabledWarning } from './mqttUtils.js';
|
|
4
|
-
import { fetchCheckedJson } from './fetch.js';
|
|
4
|
+
import { fetchCheckedJson, missingEndpoint } from './fetch.js';
|
|
5
5
|
import { assertTopicSegment, assertTopicSegmentOneOf } from './topicSegment.js';
|
|
6
6
|
|
|
7
7
|
const ENTITIES = [
|
|
@@ -39,6 +39,7 @@ const getConfTopic = (entity, operation, rid) =>
|
|
|
39
39
|
|
|
40
40
|
export const getConfApi = (confUrl, mqttClient, http) => {
|
|
41
41
|
const crudEntity = (entityClass, operation) => (args) => {
|
|
42
|
+
if (!confUrl) return missingEndpoint('confUrl');
|
|
42
43
|
return fetchCheckedJson(
|
|
43
44
|
`${confUrl}/${entityClass}/${operation}`,
|
|
44
45
|
{
|
package/src/fetch.js
CHANGED
|
@@ -9,6 +9,10 @@ const JSON_HEADERS = { 'Content-Type': 'application/json', Accept: 'application/
|
|
|
9
9
|
|
|
10
10
|
const UNAUTHORIZED = 401;
|
|
11
11
|
|
|
12
|
+
// An endpoint createClient was not given fails the call that needs it, not the whole client.
|
|
13
|
+
export const missingEndpoint = (name) =>
|
|
14
|
+
Promise.reject(Error(`createClient was given no ${name} or tenantUrl, which this call needs`));
|
|
15
|
+
|
|
12
16
|
const withToken = async (options, oidcHandler) =>
|
|
13
17
|
oidcHandler
|
|
14
18
|
? R.assocPath(['headers', 'Authorization'])(`Bearer ${await oidcHandler.getToken()}`)(options)
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as R from 'ramda';
|
|
2
2
|
|
|
3
|
-
import { clientOptionsError } from './clientOptions.js';
|
|
3
|
+
import { clientOptionsError, tenantServiceUrl } from './clientOptions.js';
|
|
4
4
|
import { createClient as createServerClient } from './client.js';
|
|
5
5
|
import { createMqttClient } from './mqtt.js';
|
|
6
6
|
import { createOidcHandler } from './oidcHandler.js';
|
|
@@ -36,7 +36,7 @@ export const createClient = async (options = {}) => {
|
|
|
36
36
|
? undefined
|
|
37
37
|
: await createMqttClient({
|
|
38
38
|
mqttUrl: mqttUrl || createMqttUrl({ tenantUrl, useWebsocket }),
|
|
39
|
-
authenticatorUrl: authenticatorUrl ||
|
|
39
|
+
authenticatorUrl: authenticatorUrl || tenantServiceUrl(tenantUrl, '/authenticator'),
|
|
40
40
|
oidcHandler,
|
|
41
41
|
authConfig,
|
|
42
42
|
noAuth,
|
|
@@ -45,9 +45,9 @@ export const createClient = async (options = {}) => {
|
|
|
45
45
|
});
|
|
46
46
|
|
|
47
47
|
return createServerClient({
|
|
48
|
-
authenticatorUrl: authenticatorUrl ||
|
|
49
|
-
confUrl: confUrl ||
|
|
50
|
-
dataHistoryUrl: dataHistoryUrl ||
|
|
48
|
+
authenticatorUrl: authenticatorUrl || tenantServiceUrl(tenantUrl, '/authenticator'),
|
|
49
|
+
confUrl: confUrl || tenantServiceUrl(tenantUrl, '/conf'),
|
|
50
|
+
dataHistoryUrl: dataHistoryUrl || tenantServiceUrl(tenantUrl, '/data-history'),
|
|
51
51
|
mqttClient,
|
|
52
52
|
oidcHandler,
|
|
53
53
|
session,
|