1
1
import type { SSMClient } from '@aws-sdk/client-ssm'
2
- import { nrfCloudAccount } from './scope.js'
2
+ import { NRFCLOUD_ACCOUNT_SCOPE , nrfCloudAccount } from './scope.js'
3
3
import {
4
4
get as getSSMSettings ,
5
5
put as putSSMSettings ,
@@ -32,50 +32,10 @@ export const getSettings = ({
32
32
} ) : ( ( ) => Promise < Settings > ) => {
33
33
const settingsReader = getSSMSettings ( ssm ) ( {
34
34
stackName,
35
- ...nrfCloudAccount ( account ) ,
35
+ scope : NRFCLOUD_ACCOUNT_SCOPE ,
36
+ context : nrfCloudAccount ( account ) ,
36
37
} )
37
- return async ( ) : Promise < Settings > => {
38
- const p = await settingsReader ( )
39
- const {
40
- apiEndpoint,
41
- apiKey,
42
- accountDeviceClientCert,
43
- accountDevicePrivateKey,
44
- mqttEndpoint,
45
- accountDeviceClientId,
46
- mqttTopicPrefix,
47
- coapEndpoint,
48
- coapPort,
49
- } = p
50
- if ( apiKey === undefined )
51
- throw new Error ( `No nRF Cloud API key configured!` )
52
- if ( accountDeviceClientCert === undefined )
53
- throw new Error ( `No nRF Cloud account device clientCert configured!` )
54
- if ( accountDevicePrivateKey === undefined )
55
- throw new Error ( `No nRF Cloud account device privateKey configured!` )
56
- if ( accountDeviceClientId === undefined )
57
- throw new Error ( `No nRF Cloud Account Device client ID configured!` )
58
- if ( mqttTopicPrefix === undefined )
59
- throw new Error ( `No nRF Cloud MQTT topic prefix configured!` )
60
- if ( mqttEndpoint === undefined )
61
- throw new Error ( `No nRF Cloud MQTT endpoint configured!` )
62
-
63
- return {
64
- apiEndpoint :
65
- apiEndpoint === undefined ? defaultApiEndpoint : new URL ( apiEndpoint ) ,
66
- apiKey,
67
- mqttEndpoint,
68
- accountDeviceClientCert,
69
- accountDevicePrivateKey,
70
- accountDeviceClientId,
71
- mqttTopicPrefix,
72
- coapEndpoint :
73
- coapEndpoint === undefined
74
- ? defaultCoAPEndpoint
75
- : new URL ( coapEndpoint ) ,
76
- coapPort : parseInt ( coapPort ?? `5684` , 10 ) ,
77
- }
78
- }
38
+ return async ( ) : Promise < Settings > => validateSettings ( await settingsReader ( ) )
79
39
}
80
40
81
41
export const getAPISettings = ( {
@@ -89,7 +49,8 @@ export const getAPISettings = ({
89
49
} ) : ( ( ) => Promise < Pick < Settings , 'apiKey' | 'apiEndpoint' > > ) => {
90
50
const settingsReader = getSSMSettings ( ssm ) ( {
91
51
stackName,
92
- ...nrfCloudAccount ( account ) ,
52
+ scope : NRFCLOUD_ACCOUNT_SCOPE ,
53
+ context : nrfCloudAccount ( account ) ,
93
54
} )
94
55
return async ( ) : Promise < Pick < Settings , 'apiKey' | 'apiEndpoint' > > => {
95
56
const p = await settingsReader ( )
@@ -116,7 +77,8 @@ export const putSettings = ({
116
77
} ) : ( ( settings : Partial < Settings > ) => Promise < void > ) => {
117
78
const settingsWriter = putSSMSettings ( ssm ) ( {
118
79
stackName,
119
- ...nrfCloudAccount ( account ) ,
80
+ scope : NRFCLOUD_ACCOUNT_SCOPE ,
81
+ context : nrfCloudAccount ( account ) ,
120
82
} )
121
83
return async ( settings ) : Promise < void > => {
122
84
await Promise . all (
@@ -145,7 +107,8 @@ export const putSetting = ({
145
107
) => ReturnType < typeof settingsWriter > ) => {
146
108
const settingsWriter = putSSMSettings ( ssm ) ( {
147
109
stackName,
148
- ...nrfCloudAccount ( account ) ,
110
+ scope : NRFCLOUD_ACCOUNT_SCOPE ,
111
+ context : nrfCloudAccount ( account ) ,
149
112
} )
150
113
return async ( property , value , deleteBeforeUpdate ) =>
151
114
settingsWriter ( {
@@ -166,7 +129,47 @@ export const deleteSettings = ({
166
129
} ) : ( ( property : string ) => ReturnType < typeof settingsDeleter > ) => {
167
130
const settingsDeleter = deleteSSMSettings ( ssm ) ( {
168
131
stackName,
169
- ...nrfCloudAccount ( account ) ,
132
+ scope : NRFCLOUD_ACCOUNT_SCOPE ,
133
+ context : nrfCloudAccount ( account ) ,
170
134
} )
171
135
return async ( property ) => settingsDeleter ( { property } )
172
136
}
137
+
138
+ export const validateSettings = ( p : Record < string , string > ) : Settings => {
139
+ const {
140
+ apiEndpoint,
141
+ apiKey,
142
+ accountDeviceClientCert,
143
+ accountDevicePrivateKey,
144
+ mqttEndpoint,
145
+ accountDeviceClientId,
146
+ mqttTopicPrefix,
147
+ coapEndpoint,
148
+ coapPort,
149
+ } = p
150
+ if ( apiKey === undefined ) throw new Error ( `No nRF Cloud API key configured!` )
151
+ if ( accountDeviceClientCert === undefined )
152
+ throw new Error ( `No nRF Cloud account device clientCert configured!` )
153
+ if ( accountDevicePrivateKey === undefined )
154
+ throw new Error ( `No nRF Cloud account device privateKey configured!` )
155
+ if ( accountDeviceClientId === undefined )
156
+ throw new Error ( `No nRF Cloud Account Device client ID configured!` )
157
+ if ( mqttTopicPrefix === undefined )
158
+ throw new Error ( `No nRF Cloud MQTT topic prefix configured!` )
159
+ if ( mqttEndpoint === undefined )
160
+ throw new Error ( `No nRF Cloud MQTT endpoint configured!` )
161
+
162
+ return {
163
+ apiEndpoint :
164
+ apiEndpoint === undefined ? defaultApiEndpoint : new URL ( apiEndpoint ) ,
165
+ apiKey,
166
+ mqttEndpoint,
167
+ accountDeviceClientCert,
168
+ accountDevicePrivateKey,
169
+ accountDeviceClientId,
170
+ mqttTopicPrefix,
171
+ coapEndpoint :
172
+ coapEndpoint === undefined ? defaultCoAPEndpoint : new URL ( coapEndpoint ) ,
173
+ coapPort : parseInt ( coapPort ?? `5684` , 10 ) ,
174
+ }
175
+ }
0 commit comments