11import { Type } from '@sinclair/typebox'
2- import { validatedFetch } from './validatedFetch.js'
2+ import { ValidationError , validatedFetch } from './validatedFetch.js'
33
44/**
55 * @link https://api.nrfcloud.com/v1/#tag/Account/operation/GetServiceToken
@@ -10,23 +10,25 @@ export const ServiceToken = Type.Object({
1010
1111export const serviceToken =
1212 (
13+ {
14+ apiKey,
15+ endpoint,
16+ } : {
17+ apiKey : string
18+ endpoint : URL
19+ } ,
1320 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 )
2124 const maybeResult = await vf (
2225 { resource : 'account/service-token' } ,
2326 ServiceToken ,
2427 )
2528
2629 if ( 'error' in maybeResult ) {
27- onError ?.( maybeResult . error )
28- throw maybeResult . error
30+ return maybeResult
2931 }
3032
31- return maybeResult . result . token
33+ return maybeResult . result
3234 }
0 commit comments