1717 */
1818
1919import { AuthStrategy } from '../auth' ;
20- import { handleError } from '../utils/http' ;
20+ import { handleError , ensureOk } from '../utils/http' ;
2121import {
2222 AccessKey ,
2323 CreateAccessKey ,
@@ -51,6 +51,23 @@ export interface GetAccessKeysOptions {
5151 limit ?: number ;
5252}
5353
54+ const buildSearchParams = ( options ?: GetAccessKeysOptions ) : string => {
55+ const searchParams = new URLSearchParams ( ) ;
56+ if ( options ?. reference ) {
57+ searchParams . set ( 'reference' , options . reference ) ;
58+ }
59+ if ( options ?. permissions ?. length ) {
60+ searchParams . set ( 'permissions' , options . permissions . join ( ',' ) ) ;
61+ }
62+ if ( options ?. offset ) {
63+ searchParams . set ( 'offset' , String ( options . offset ) ) ;
64+ }
65+ if ( options ?. limit ) {
66+ searchParams . set ( 'limit' , String ( options . limit ) ) ;
67+ }
68+ return searchParams . toString ( ) ;
69+ } ;
70+
5471/**
5572 * Base path for access key resource endpoint.
5673 */
@@ -76,40 +93,29 @@ export class AccessKeyService {
7693 * @returns A promise that resolves with the access keys information.
7794 * @throws {UnauthorizedActionError } If the user is not authorized.
7895 * @throws {PermissionError } If the user is not allowed to request access keys.
96+ * @throws {Error } If an unknown error occurred.
7997 */
8098 public async getAccessKeys (
8199 options ?: GetAccessKeysOptions
82100 ) : Promise < AccessKeysSummary > {
83- const searchParams = new URLSearchParams ( ) ;
84- if ( options ?. reference ) {
85- searchParams . set ( 'reference' , options . reference ) ;
86- }
87- if ( options ?. permissions && options . permissions . length > 0 ) {
88- searchParams . set ( 'permissions' , options . permissions . join ( ',' ) ) ;
89- }
90- if ( options ?. offset ) {
91- searchParams . set ( 'offset' , String ( options . offset ) ) ;
92- }
93- if ( options ?. limit ) {
94- searchParams . set ( 'limit' , String ( options . limit ) ) ;
95- }
96101 try {
97- const response = await fetch ( this . getUrl ( `?${ searchParams . toString ( ) } ` ) , {
98- headers : {
99- ...this . getAuthHeaders ( ) ,
100- Accept : 'application/json' ,
101- } ,
102- } ) ;
103- if ( ! response . ok ) {
104- handleError ( response ) ;
105- }
102+ const response = await fetch (
103+ this . getUrl ( `?${ buildSearchParams ( options ) } ` ) ,
104+ {
105+ headers : {
106+ ...this . getAuthHeaders ( ) ,
107+ Accept : 'application/json' ,
108+ } ,
109+ }
110+ ) ;
111+ ensureOk ( response ) ;
106112 const totalCount = response . headers . get ( 'x-total-count' ) ;
107113 return {
108114 accessKeys : ( await response . json ( ) ) as AccessKey [ ] ,
109115 totalCount : totalCount ? parseInt ( totalCount , 10 ) : 0 ,
110116 } as AccessKeysSummary ;
111- } catch {
112- throw new Error ( 'Failed to get access keys' ) ;
117+ } catch ( error ) {
118+ throw handleError ( error , 'Failed to get access keys' ) ;
113119 }
114120 }
115121
@@ -120,6 +126,7 @@ export class AccessKeyService {
120126 * @throws {UnauthorizedActionError } If the user is not authorized.
121127 * @throws {PermissionError } If the user is not allowed to request access keys.
122128 * @throws {ResourceNotFoundError } If the access key does not exist.
129+ * @throws {Error } If an unknown error occurred.
123130 */
124131 public async getAccessKey ( id : string ) : Promise < AccessKey > {
125132 try {
@@ -129,12 +136,10 @@ export class AccessKeyService {
129136 Accept : 'application/json' ,
130137 } ,
131138 } ) ;
132- if ( ! response . ok ) {
133- handleError ( response ) ;
134- }
139+ ensureOk ( response ) ;
135140 return ( await response . json ( ) ) as AccessKey ;
136- } catch {
137- throw new Error ( 'Failed to get access key' ) ;
141+ } catch ( error ) {
142+ throw handleError ( error , 'Failed to get access key' ) ;
138143 }
139144 }
140145
@@ -144,6 +149,7 @@ export class AccessKeyService {
144149 * @returns A promise that resolves with the ID of the created access key.
145150 * @throws {UnauthorizedActionError } If the user is not authorized.
146151 * @throws {PermissionError } If the user is not allowed to create access key.
152+ * @throws {Error } If an unknown error occurred.
147153 */
148154 public async createAccessKey ( accessKey : CreateAccessKey ) : Promise < string > {
149155 try {
@@ -155,12 +161,10 @@ export class AccessKeyService {
155161 } ,
156162 body : JSON . stringify ( accessKey ) ,
157163 } ) ;
158- if ( ! response . ok ) {
159- handleError ( response ) ;
160- }
164+ ensureOk ( response ) ;
161165 return response . headers . get ( 'location' ) ?. split ( '/' ) . pop ( ) as string ;
162- } catch {
163- throw new Error ( 'Failed to create access key' ) ;
166+ } catch ( error ) {
167+ throw handleError ( error , 'Failed to create access key' ) ;
164168 }
165169 }
166170
@@ -171,6 +175,7 @@ export class AccessKeyService {
171175 * @throws {UnauthorizedActionError } If the user is not authorized.
172176 * @throws {PermissionError } If the user is not allowed to update access key.
173177 * @throws {ResourceNotFoundError } If the access key does not exist.
178+ * @throws {Error } If an unknown error occurred.
174179 */
175180 public async updateAccessKey (
176181 id : string ,
@@ -185,11 +190,9 @@ export class AccessKeyService {
185190 } ,
186191 body : JSON . stringify ( accessKey ) ,
187192 } ) ;
188- if ( ! response . ok ) {
189- handleError ( response ) ;
190- }
191- } catch {
192- throw new Error ( 'Failed to update access key' ) ;
193+ ensureOk ( response ) ;
194+ } catch ( error ) {
195+ throw handleError ( error , 'Failed to update access key' ) ;
193196 }
194197 }
195198
@@ -200,6 +203,7 @@ export class AccessKeyService {
200203 * @throws {UnauthorizedActionError } If the user is not authorized.
201204 * @throws {PermissionError } If the user is not allowed to update access key.
202205 * @throws {ResourceNotFoundError } If the access key does not exist.
206+ * @throws {Error } If an unknown error occurred.
203207 */
204208 public async patchAccessKey (
205209 id : string ,
@@ -214,11 +218,9 @@ export class AccessKeyService {
214218 } ,
215219 body : JSON . stringify ( accessKey ) ,
216220 } ) ;
217- if ( ! response . ok ) {
218- handleError ( response ) ;
219- }
220- } catch {
221- throw new Error ( 'Failed to update access key' ) ;
221+ ensureOk ( response ) ;
222+ } catch ( error ) {
223+ throw handleError ( error , 'Failed to update access key' ) ;
222224 }
223225 }
224226
@@ -228,6 +230,7 @@ export class AccessKeyService {
228230 * @throws {UnauthorizedActionError } If the user is not authorized.
229231 * @throws {PermissionError } If the user is not allowed to delete access key.
230232 * @throws {ResourceNotFoundError } If the access key does not exist.
233+ * @throws {Error } If an unknown error occurred.
231234 */
232235 public async deleteAccessKey ( id : string ) : Promise < void > {
233236 try {
@@ -237,11 +240,9 @@ export class AccessKeyService {
237240 ...this . getAuthHeaders ( ) ,
238241 } ,
239242 } ) ;
240- if ( ! response . ok ) {
241- handleError ( response ) ;
242- }
243- } catch {
244- throw new Error ( 'Failed to delete access key' ) ;
243+ ensureOk ( response ) ;
244+ } catch ( error ) {
245+ throw handleError ( error , 'Failed to delete access key' ) ;
245246 }
246247 }
247248
0 commit comments