@hello.nrfcloud.com/nrfcloud-api-helpers 1.0.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.
Files changed (46) hide show
  1. package/LICENSE +29 -0
  2. package/README.md +15 -0
  3. package/dist/api/DeviceShadow.d.ts +17 -0
  4. package/dist/api/DeviceShadow.js +18 -0
  5. package/dist/api/DeviceShadow.spec.d.ts +1 -0
  6. package/dist/api/DeviceShadow.spec.js +53 -0
  7. package/dist/api/createAccountDevice.d.ts +13 -0
  8. package/dist/api/createAccountDevice.js +20 -0
  9. package/dist/api/deleteAccountDevice.d.ts +4 -0
  10. package/dist/api/deleteAccountDevice.js +9 -0
  11. package/dist/api/devices.d.ts +152 -0
  12. package/dist/api/devices.js +121 -0
  13. package/dist/api/getAccountInfo.d.ts +18 -0
  14. package/dist/api/getAccountInfo.js +21 -0
  15. package/dist/api/getAccountInfo.spec.d.ts +1 -0
  16. package/dist/api/getAccountInfo.spec.js +25 -0
  17. package/dist/api/getCurrentMonthlyCosts.d.ts +9 -0
  18. package/dist/api/getCurrentMonthlyCosts.js +15 -0
  19. package/dist/api/getCurrentMonthlyCosts.spec.d.ts +1 -0
  20. package/dist/api/getCurrentMonthlyCosts.spec.js +17 -0
  21. package/dist/api/getDeviceShadow.d.ts +23 -0
  22. package/dist/api/getDeviceShadow.js +33 -0
  23. package/dist/api/getDeviceShadow.spec.d.ts +1 -0
  24. package/dist/api/getDeviceShadow.spec.js +28 -0
  25. package/dist/api/index.d.ts +9 -0
  26. package/dist/api/index.js +9 -0
  27. package/dist/api/slashless.d.ts +1 -0
  28. package/dist/api/slashless.js +1 -0
  29. package/dist/api/slashless.spec.d.ts +1 -0
  30. package/dist/api/slashless.spec.js +6 -0
  31. package/dist/api/test-data/account.json +63 -0
  32. package/dist/api/validatedFetch.d.ts +30 -0
  33. package/dist/api/validatedFetch.js +54 -0
  34. package/dist/api/validatedFetch.spec.d.ts +1 -0
  35. package/dist/api/validatedFetch.spec.js +101 -0
  36. package/dist/settings/getAllAccounts.d.ts +5 -0
  37. package/dist/settings/getAllAccounts.js +9 -0
  38. package/dist/settings/index.d.ts +4 -0
  39. package/dist/settings/index.js +4 -0
  40. package/dist/settings/initializeAccount.d.ts +10 -0
  41. package/dist/settings/initializeAccount.js +72 -0
  42. package/dist/settings/scope.d.ts +17 -0
  43. package/dist/settings/scope.js +34 -0
  44. package/dist/settings/settings.d.ts +43 -0
  45. package/dist/settings/settings.js +85 -0
  46. package/package.json +95 -0
package/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024, Nordic Semiconductor ASA | nordicsemi.no
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # nRF Cloud API helpers [![npm version](https://img.shields.io/npm/v/@hello.nrfcloud.com/nrfcloud-api-helpers.svg)](https://www.npmjs.com/package/@hello.nrfcloud.com/nrfcloud-api-helpers)
2
+
3
+ [![GitHub Actions](https://github.com/hello-nrfcloud/nrfcloud-api-helpers/workflows/Test%20and%20Release/badge.svg)](https://github.com/hello-nrfcloud/nrfcloud-api-helpers/actions)
4
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
5
+ [![Renovate](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com)
6
+ [![@commitlint/config-conventional](https://img.shields.io/badge/%40commitlint-config--conventional-brightgreen)](https://github.com/conventional-changelog/commitlint/tree/master/@commitlint/config-conventional)
7
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier/)
8
+ [![ESLint: TypeScript](https://img.shields.io/badge/ESLint-TypeScript-blue.svg)](https://github.com/typescript-eslint/typescript-eslint)
9
+
10
+ Helper functions for integrating nRF Cloud APIs in AWS lambdas written in
11
+ TypeScript.
12
+
13
+ ## Installation
14
+
15
+ npm i --save --save-exact @hello.nrfcloud.com/nrfcloud-api-helpers
@@ -0,0 +1,17 @@
1
+ import { type Static } from '@sinclair/typebox';
2
+ /**
3
+ * @link https://api.nrfcloud.com/v1/#tag/All-Devices/operation/ListDevices
4
+ */
5
+ export declare const DeviceShadow: import("@sinclair/typebox").TObject<{
6
+ id: import("@sinclair/typebox").TString;
7
+ state: import("@sinclair/typebox").TObject<{
8
+ reported: import("@sinclair/typebox").TObject<{}>;
9
+ version: import("@sinclair/typebox").TNumber;
10
+ metadata: import("@sinclair/typebox").TObject<{
11
+ reported: import("@sinclair/typebox").TRecord<import("@sinclair/typebox").TString, import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TObject<{
12
+ timestamp: import("@sinclair/typebox").TInteger;
13
+ }>, import("@sinclair/typebox").TRecord<import("@sinclair/typebox").TString, import("@sinclair/typebox").TUnknown>]>>;
14
+ }>;
15
+ }>;
16
+ }>;
17
+ export type DeviceShadowType = Static<typeof DeviceShadow>;
@@ -0,0 +1,18 @@
1
+ import { Type } from '@sinclair/typebox';
2
+ const PropertyMetadata = Type.Union([
3
+ Type.Object({ timestamp: Type.Integer({ minimum: 1, maximum: 9999999999 }) }),
4
+ Type.Record(Type.String({ minLength: 1 }), Type.Unknown()),
5
+ ]);
6
+ /**
7
+ * @link https://api.nrfcloud.com/v1/#tag/All-Devices/operation/ListDevices
8
+ */
9
+ export const DeviceShadow = Type.Object({
10
+ id: Type.String(),
11
+ state: Type.Object({
12
+ reported: Type.Object({}),
13
+ version: Type.Number(),
14
+ metadata: Type.Object({
15
+ reported: Type.Record(Type.String({ minLength: 1 }), PropertyMetadata),
16
+ }),
17
+ }),
18
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,53 @@
1
+ import { describe, it } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { validateWithTypeBox } from '@hello.nrfcloud.com/proto';
4
+ import { DeviceShadow } from './DeviceShadow.js';
5
+ void describe('DeviceShadow type', () => {
6
+ void it('should document the device shadow object', () => {
7
+ const res = validateWithTypeBox(DeviceShadow)({
8
+ id: 'some-device',
9
+ state: {
10
+ version: 42,
11
+ reported: {
12
+ dev: {
13
+ v: {
14
+ imei: '358299840016535',
15
+ iccid: '89450421180216254864',
16
+ modV: 'mfw_nrf91x1_2.0.0-77.beta',
17
+ brdV: 'thingy91x_nrf9161',
18
+ appV: '0.0.0-development',
19
+ },
20
+ ts: 1697102116821,
21
+ },
22
+ },
23
+ metadata: {
24
+ reported: {
25
+ dev: {
26
+ v: {
27
+ imei: {
28
+ timestamp: 1697102122,
29
+ },
30
+ iccid: {
31
+ timestamp: 1697102122,
32
+ },
33
+ modV: {
34
+ timestamp: 1697102122,
35
+ },
36
+ brdV: {
37
+ timestamp: 1697102122,
38
+ },
39
+ appV: {
40
+ timestamp: 1697102122,
41
+ },
42
+ },
43
+ ts: {
44
+ timestamp: 1697102122,
45
+ },
46
+ },
47
+ },
48
+ },
49
+ },
50
+ });
51
+ assert.equal('errors' in res, false);
52
+ });
53
+ });
@@ -0,0 +1,13 @@
1
+ import { type Static } from '@sinclair/typebox';
2
+ /**
3
+ * @link https://api.nrfcloud.com/v1/#tag/Account-Devices/operation/CreateAccountDevice
4
+ */
5
+ declare const CertificateCredentials: import("@sinclair/typebox").TObject<{
6
+ clientCert: import("@sinclair/typebox").TString;
7
+ privateKey: import("@sinclair/typebox").TString;
8
+ }>;
9
+ export declare const createAccountDevice: ({ apiKey, endpoint, }: {
10
+ apiKey: string;
11
+ endpoint: URL;
12
+ }) => Promise<Static<typeof CertificateCredentials>>;
13
+ export {};
@@ -0,0 +1,20 @@
1
+ import { Type } from '@sinclair/typebox';
2
+ import { validatedFetch } from './validatedFetch.js';
3
+ /**
4
+ * @link https://api.nrfcloud.com/v1/#tag/Account-Devices/operation/CreateAccountDevice
5
+ */
6
+ const CertificateCredentials = Type.Object({
7
+ clientCert: Type.String(),
8
+ privateKey: Type.String(),
9
+ });
10
+ export const createAccountDevice = async ({ apiKey, endpoint, }) => {
11
+ const vf = validatedFetch({ endpoint, apiKey });
12
+ const maybeResult = await vf({ resource: 'devices/account', method: 'POST' }, CertificateCredentials);
13
+ if ('error' in maybeResult) {
14
+ throw maybeResult.error;
15
+ }
16
+ return {
17
+ clientCert: maybeResult.result.clientCert,
18
+ privateKey: maybeResult.result.privateKey,
19
+ };
20
+ };
@@ -0,0 +1,4 @@
1
+ export declare const deleteAccountDevice: ({ apiKey, endpoint, }: {
2
+ apiKey: string;
3
+ endpoint: URL;
4
+ }) => Promise<void>;
@@ -0,0 +1,9 @@
1
+ import { slashless } from './slashless.js';
2
+ export const deleteAccountDevice = async ({ apiKey, endpoint, }) => {
3
+ await fetch(`${slashless(endpoint)}/v1/devices/account`, {
4
+ method: 'DELETE',
5
+ headers: {
6
+ Authorization: `Bearer ${apiKey}`,
7
+ },
8
+ });
9
+ };
@@ -0,0 +1,152 @@
1
+ import { type Static } from '@sinclair/typebox';
2
+ import { ValidationError } from './validatedFetch.js';
3
+ type Nullable<T> = {
4
+ [K in keyof T]: T[K] | null;
5
+ };
6
+ export declare const DeviceConfig: import("@sinclair/typebox").TObject<{
7
+ activeMode: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TBoolean>;
8
+ locationTimeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
9
+ activeWaitTime: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
10
+ movementResolution: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
11
+ movementTimeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
12
+ accThreshAct: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
13
+ accThreshInact: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
14
+ accTimeoutInact: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
15
+ nod: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"gnss">, import("@sinclair/typebox").TLiteral<"ncell">, import("@sinclair/typebox").TLiteral<"wifi">]>>>;
16
+ }>;
17
+ declare const Device: import("@sinclair/typebox").TObject<{
18
+ id: import("@sinclair/typebox").TString;
19
+ state: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
20
+ reported: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
21
+ config: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
22
+ activeMode: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TBoolean>;
23
+ locationTimeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
24
+ activeWaitTime: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
25
+ movementResolution: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
26
+ movementTimeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
27
+ accThreshAct: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
28
+ accThreshInact: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
29
+ accTimeoutInact: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
30
+ nod: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"gnss">, import("@sinclair/typebox").TLiteral<"ncell">, import("@sinclair/typebox").TLiteral<"wifi">]>>>;
31
+ }>>;
32
+ connection: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
33
+ status: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"connected">, import("@sinclair/typebox").TLiteral<"disconnected">]>>;
34
+ }>>;
35
+ device: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
36
+ deviceInfo: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
37
+ appVersion: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
38
+ modemFirmware: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
39
+ imei: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
40
+ board: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
41
+ hwVer: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
42
+ }>>;
43
+ }>>;
44
+ }>>;
45
+ desired: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
46
+ config: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
47
+ activeMode: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TBoolean>;
48
+ locationTimeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
49
+ activeWaitTime: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
50
+ movementResolution: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
51
+ movementTimeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
52
+ accThreshAct: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
53
+ accThreshInact: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
54
+ accTimeoutInact: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
55
+ nod: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"gnss">, import("@sinclair/typebox").TLiteral<"ncell">, import("@sinclair/typebox").TLiteral<"wifi">]>>>;
56
+ }>>;
57
+ }>>;
58
+ version: import("@sinclair/typebox").TNumber;
59
+ }>>;
60
+ firmware: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
61
+ app: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
62
+ name: import("@sinclair/typebox").TString;
63
+ version: import("@sinclair/typebox").TString;
64
+ }>>;
65
+ }>>;
66
+ }>;
67
+ declare const Devices: import("@sinclair/typebox").TObject<{
68
+ total: import("@sinclair/typebox").TInteger;
69
+ items: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
70
+ id: import("@sinclair/typebox").TString;
71
+ state: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
72
+ reported: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
73
+ config: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
74
+ activeMode: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TBoolean>;
75
+ locationTimeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
76
+ activeWaitTime: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
77
+ movementResolution: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
78
+ movementTimeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
79
+ accThreshAct: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
80
+ accThreshInact: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
81
+ accTimeoutInact: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
82
+ nod: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"gnss">, import("@sinclair/typebox").TLiteral<"ncell">, import("@sinclair/typebox").TLiteral<"wifi">]>>>;
83
+ }>>;
84
+ connection: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
85
+ status: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"connected">, import("@sinclair/typebox").TLiteral<"disconnected">]>>;
86
+ }>>;
87
+ device: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
88
+ deviceInfo: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
89
+ appVersion: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
90
+ modemFirmware: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
91
+ imei: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
92
+ board: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
93
+ hwVer: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
94
+ }>>;
95
+ }>>;
96
+ }>>;
97
+ desired: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
98
+ config: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
99
+ activeMode: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TBoolean>;
100
+ locationTimeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
101
+ activeWaitTime: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
102
+ movementResolution: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
103
+ movementTimeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
104
+ accThreshAct: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
105
+ accThreshInact: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
106
+ accTimeoutInact: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
107
+ nod: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"gnss">, import("@sinclair/typebox").TLiteral<"ncell">, import("@sinclair/typebox").TLiteral<"wifi">]>>>;
108
+ }>>;
109
+ }>>;
110
+ version: import("@sinclair/typebox").TNumber;
111
+ }>>;
112
+ firmware: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
113
+ app: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
114
+ name: import("@sinclair/typebox").TString;
115
+ version: import("@sinclair/typebox").TString;
116
+ }>>;
117
+ }>>;
118
+ }>>;
119
+ }>;
120
+ type FwType = 'APP' | 'MODEM' | 'BOOT' | 'SOFTDEVICE' | 'BOOTLOADER' | 'MDM_FULL';
121
+ export declare const devices: ({ endpoint, apiKey, }: {
122
+ endpoint: URL;
123
+ apiKey: string;
124
+ }, fetchImplementation?: typeof fetch) => {
125
+ list: () => Promise<{
126
+ error: Error | ValidationError;
127
+ } | {
128
+ result: Static<typeof Devices>;
129
+ }>;
130
+ get: (id: string) => Promise<{
131
+ error: Error | ValidationError;
132
+ } | {
133
+ result: Static<typeof Device>;
134
+ }>;
135
+ updateConfig: (id: string, config: Nullable<Omit<Static<typeof DeviceConfig>, 'nod'>> & Pick<Static<typeof DeviceConfig>, 'nod'>) => Promise<{
136
+ error: Error;
137
+ } | {
138
+ success: boolean;
139
+ }>;
140
+ register: (devices: {
141
+ deviceId: string;
142
+ subType?: string;
143
+ tags?: string[];
144
+ fwTypes?: FwType[];
145
+ certPem: string;
146
+ }[]) => Promise<{
147
+ error: Error;
148
+ } | {
149
+ bulkOpsRequestId: string;
150
+ }>;
151
+ };
152
+ export {};
@@ -0,0 +1,121 @@
1
+ import { Type } from '@sinclair/typebox';
2
+ import { slashless } from './slashless.js';
3
+ import { ValidationError, validatedFetch } from './validatedFetch.js';
4
+ export const DeviceConfig = Type.Partial(Type.Object({
5
+ activeMode: Type.Boolean(), // e.g. false
6
+ locationTimeout: Type.Number(), // e.g. 300
7
+ activeWaitTime: Type.Number(), // e.g. 120
8
+ movementResolution: Type.Number(), // e.g. 120
9
+ movementTimeout: Type.Number(), // e.g. 3600
10
+ accThreshAct: Type.Number(), // e.g. 4
11
+ accThreshInact: Type.Number(), // e.g. 4
12
+ accTimeoutInact: Type.Number(), // e.g. 60
13
+ nod: Type.Array(Type.Union([
14
+ Type.Literal('gnss'),
15
+ Type.Literal('ncell'),
16
+ Type.Literal('wifi'),
17
+ ])), // e.g. ['nod']
18
+ }));
19
+ const Device = Type.Object({
20
+ id: Type.String(),
21
+ state: Type.Optional(Type.Object({
22
+ reported: Type.Optional(Type.Object({
23
+ config: Type.Optional(DeviceConfig),
24
+ connection: Type.Optional(Type.Object({
25
+ status: Type.Optional(Type.Union([
26
+ Type.Literal('connected'),
27
+ Type.Literal('disconnected'),
28
+ ])),
29
+ })),
30
+ device: Type.Optional(Type.Object({
31
+ deviceInfo: Type.Optional(Type.Partial(Type.Object({
32
+ appVersion: Type.String(), // e.g. '1.1.0'
33
+ modemFirmware: Type.String(), // e.g. 'mfw_nrf9160_1.3.4'
34
+ imei: Type.String(), // e.g. '352656108602296'
35
+ board: Type.String(), // e.g. 'thingy91_nrf9160'
36
+ hwVer: Type.String(), // e.g. 'nRF9160 SICA B1A'
37
+ }))),
38
+ })),
39
+ })),
40
+ desired: Type.Optional(Type.Object({
41
+ config: Type.Optional(DeviceConfig),
42
+ })),
43
+ version: Type.Number(),
44
+ })),
45
+ firmware: Type.Optional(Type.Object({
46
+ app: Type.Optional(Type.Object({
47
+ name: Type.String({ minLength: 1 }),
48
+ version: Type.String({ minLength: 1 }),
49
+ })),
50
+ })),
51
+ });
52
+ const Page = (Item) => Type.Object({
53
+ total: Type.Integer(),
54
+ items: Type.Array(Item),
55
+ });
56
+ const Devices = Page(Device);
57
+ /**
58
+ * @link https://api.nrfcloud.com/v1/#tag/IP-Devices/operation/ProvisionDevices
59
+ */
60
+ const ProvisionDevice = Type.Object({
61
+ bulkOpsRequestId: Type.String(),
62
+ });
63
+ export const devices = ({ endpoint, apiKey, }, fetchImplementation) => {
64
+ const headers = {
65
+ Authorization: `Bearer ${apiKey}`,
66
+ Accept: 'application/json; charset=utf-8',
67
+ };
68
+ const vf = validatedFetch({ endpoint, apiKey }, fetchImplementation);
69
+ return {
70
+ list: async () => vf({
71
+ resource: `devices?${new URLSearchParams({
72
+ pageLimit: '100',
73
+ deviceNameFuzzy: 'oob-',
74
+ }).toString()}`,
75
+ }, Devices),
76
+ get: async (id) => vf({ resource: `devices/${encodeURIComponent(id)}` }, Device),
77
+ updateConfig: async (id, config) => fetch(`${slashless(endpoint)}/v1/devices/${encodeURIComponent(id)}/state`, {
78
+ headers: {
79
+ ...headers,
80
+ 'Content-Type': 'application/json',
81
+ },
82
+ method: 'PATCH',
83
+ body: JSON.stringify({
84
+ desired: {
85
+ config,
86
+ },
87
+ }),
88
+ }).then((res) => {
89
+ if (res.status >= 400)
90
+ return { error: new Error(`Update failed: ${res.status}`) };
91
+ return { success: true };
92
+ }),
93
+ register: async (devices) => {
94
+ const bulkRegistrationPayload = devices
95
+ .map(({ deviceId, subType, tags, fwTypes, certPem }) => [
96
+ [
97
+ deviceId,
98
+ subType ?? '',
99
+ (tags ?? []).join('|'),
100
+ (fwTypes ?? []).join('|'),
101
+ `"${certPem}"`,
102
+ ],
103
+ ])
104
+ .map((cols) => cols.join(','))
105
+ .join('\n');
106
+ const maybeResult = await vf({
107
+ resource: 'devices',
108
+ payload: {
109
+ body: bulkRegistrationPayload,
110
+ type: 'application/octet-stream',
111
+ },
112
+ }, ProvisionDevice);
113
+ if ('error' in maybeResult) {
114
+ return {
115
+ error: maybeResult.error,
116
+ };
117
+ }
118
+ return { bulkOpsRequestId: maybeResult.result.bulkOpsRequestId };
119
+ },
120
+ };
121
+ };
@@ -0,0 +1,18 @@
1
+ import { type Static } from '@sinclair/typebox';
2
+ import type { ValidationError } from 'ajv';
3
+ declare const AccountInfoType: import("@sinclair/typebox").TObject<{
4
+ mqttEndpoint: import("@sinclair/typebox").TString;
5
+ mqttTopicPrefix: import("@sinclair/typebox").TString;
6
+ team: import("@sinclair/typebox").TObject<{
7
+ tenantId: import("@sinclair/typebox").TString;
8
+ name: import("@sinclair/typebox").TString;
9
+ }>;
10
+ }>;
11
+ export type AccountInfo = Static<typeof AccountInfoType>;
12
+ export declare const getAccountInfo: ({ apiKey, endpoint, }: {
13
+ apiKey: string;
14
+ endpoint: URL;
15
+ }, fetchImplementation?: typeof fetch) => Promise<{
16
+ error: Error | ValidationError;
17
+ } | AccountInfo>;
18
+ export {};
@@ -0,0 +1,21 @@
1
+ import { Type } from '@sinclair/typebox';
2
+ import { validatedFetch } from './validatedFetch.js';
3
+ const AccountInfoType = Type.Object({
4
+ mqttEndpoint: Type.String(), // e.g. 'mqtt.nrfcloud.com'
5
+ mqttTopicPrefix: Type.String(), // e.g. 'prod/a0673464-e4e1-4b87-bffd-6941a012067b/',
6
+ team: Type.Object({
7
+ tenantId: Type.String(), // e.g. 'bbfe6b73-a46a-43ad-94bd-8e4b4a7847ce',
8
+ name: Type.String(), // e.g. 'hello.nrfcloud.com'
9
+ }),
10
+ });
11
+ export const getAccountInfo = async ({ apiKey, endpoint, }, fetchImplementation) => {
12
+ const maybeAccount = await validatedFetch({
13
+ endpoint,
14
+ apiKey,
15
+ }, fetchImplementation)({
16
+ resource: 'account',
17
+ }, AccountInfoType);
18
+ if ('error' in maybeAccount)
19
+ return maybeAccount;
20
+ return maybeAccount.result;
21
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ import { describe, it } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { getAccountInfo } from './getAccountInfo.js';
4
+ import APIresponse from './test-data/account.json';
5
+ import { check, objectMatching } from 'tsmatchers';
6
+ void describe('getAccountInfo()', () => {
7
+ void it('return the account info', async () => {
8
+ const res = await getAccountInfo({
9
+ endpoint: new URL('https://example.com/'),
10
+ apiKey: 'some-key',
11
+ }, () => Promise.resolve({
12
+ ok: true,
13
+ json: async () => Promise.resolve(APIresponse),
14
+ }));
15
+ assert.equal('error' in res, false);
16
+ check(res).is(objectMatching({
17
+ mqttEndpoint: 'mqtt.nrfcloud.com',
18
+ mqttTopicPrefix: 'prod/b8b26bc5-2814-4063-b4fa-83ecddb2fec7/',
19
+ team: {
20
+ tenantId: 'b8b26bc5-2814-4063-b4fa-83ecddb2fec7',
21
+ name: 'XXX',
22
+ },
23
+ }));
24
+ });
25
+ });
@@ -0,0 +1,9 @@
1
+ import type { ValidationError } from 'ajv';
2
+ export declare const getCurrentMonthlyCosts: ({ apiKey, endpoint, }: {
3
+ apiKey: string;
4
+ endpoint: URL;
5
+ }, fetchImplementation?: typeof fetch) => () => Promise<{
6
+ error: Error | ValidationError;
7
+ } | {
8
+ currentMonthTotalCost: number;
9
+ }>;
@@ -0,0 +1,15 @@
1
+ import { validatedFetch } from './validatedFetch.js';
2
+ import { Type } from '@sinclair/typebox';
3
+ export const getCurrentMonthlyCosts = ({ apiKey, endpoint, }, fetchImplementation) => async () => {
4
+ const vf = validatedFetch({ endpoint, apiKey }, fetchImplementation);
5
+ const maybeResult = await vf({ resource: 'account' }, Type.Object({
6
+ plan: Type.Object({
7
+ currentMonthTotalCost: Type.Number(), // e.g. 2.73
8
+ }),
9
+ }));
10
+ if ('error' in maybeResult) {
11
+ return maybeResult;
12
+ }
13
+ const currentMonthTotalCost = maybeResult.result.plan.currentMonthTotalCost;
14
+ return { currentMonthTotalCost };
15
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ import { describe, it } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { getCurrentMonthlyCosts } from './getCurrentMonthlyCosts.js';
4
+ import APIresponse from './test-data/account.json';
5
+ void describe('getCurrentMonthlyCosts()', () => {
6
+ void it("return the current month's total cost for an account", async () => {
7
+ const res = await getCurrentMonthlyCosts({
8
+ endpoint: new URL('https://example.com/'),
9
+ apiKey: 'some-key',
10
+ }, () => Promise.resolve({
11
+ ok: true,
12
+ json: async () => Promise.resolve(APIresponse),
13
+ }))();
14
+ assert.equal('error' in res, false);
15
+ assert.equal('currentMonthTotalCost' in res && res.currentMonthTotalCost, 24.03);
16
+ });
17
+ });
@@ -0,0 +1,23 @@
1
+ import { type Static } from '@sinclair/typebox';
2
+ import type { ValidationError } from 'ajv';
3
+ declare const DeviceShadows: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
4
+ id: import("@sinclair/typebox").TString;
5
+ state: import("@sinclair/typebox").TObject<{
6
+ reported: import("@sinclair/typebox").TObject<{}>;
7
+ version: import("@sinclair/typebox").TNumber;
8
+ metadata: import("@sinclair/typebox").TObject<{
9
+ reported: import("@sinclair/typebox").TRecord<import("@sinclair/typebox").TString, import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TObject<{
10
+ timestamp: import("@sinclair/typebox").TInteger;
11
+ }>, import("@sinclair/typebox").TRecord<import("@sinclair/typebox").TString, import("@sinclair/typebox").TUnknown>]>>;
12
+ }>;
13
+ }>;
14
+ }>>;
15
+ export declare const getDeviceShadow: ({ endpoint, apiKey, }: {
16
+ endpoint: URL;
17
+ apiKey: string;
18
+ }, fetchImplementation?: typeof fetch) => (devices: string[]) => Promise<{
19
+ shadows: Static<typeof DeviceShadows>;
20
+ } | {
21
+ error: Error | ValidationError;
22
+ }>;
23
+ export {};