1
1
import { Type } from '@sinclair/typebox'
2
- import { validatedFetch } from './validatedFetch.js'
2
+ import { ValidationError , validatedFetch } from './validatedFetch.js'
3
3
4
4
/**
5
5
* @link https://api.nrfcloud.com/v1/#tag/Account/operation/GetServiceToken
@@ -10,23 +10,25 @@ export const ServiceToken = Type.Object({
10
10
11
11
export const serviceToken =
12
12
(
13
+ {
14
+ apiKey,
15
+ endpoint,
16
+ } : {
17
+ apiKey : string
18
+ endpoint : URL
19
+ } ,
13
20
fetchImplementation ?: typeof fetch ,
14
- onError ?: ( error : Error ) => void ,
15
- ) : ( ( args : { apiEndpoint : URL ; apiKey : string } ) => Promise < string > ) =>
16
- async ( { apiEndpoint, apiKey } ) => {
17
- const vf = validatedFetch (
18
- { endpoint : apiEndpoint , apiKey } ,
19
- fetchImplementation ,
20
- )
21
+ ) : ( ( ) => Promise < { error : Error | ValidationError } | { token : string } > ) =>
22
+ async ( ) => {
23
+ const vf = validatedFetch ( { endpoint, apiKey } , fetchImplementation )
21
24
const maybeResult = await vf (
22
25
{ resource : 'account/service-token' } ,
23
26
ServiceToken ,
24
27
)
25
28
26
29
if ( 'error' in maybeResult ) {
27
- onError ?.( maybeResult . error )
28
- throw maybeResult . error
30
+ return maybeResult
29
31
}
30
32
31
- return maybeResult . result . token
33
+ return maybeResult . result
32
34
}
0 commit comments