@@ -4,6 +4,7 @@ import useSWRMutation from "swr/mutation";
4
4
import { AuthContext } from "../Context/AuthContext" ;
5
5
import { omitErrorMessageResponse } from "../Utility/utility" ;
6
6
import {
7
+ ApiKey ,
7
8
ApiProductDetails ,
8
9
ApiProductSummary ,
9
10
ApiVersion ,
@@ -19,15 +20,15 @@ import {
19
20
import { fetchJSON , useMultiSwrWithAuth , useSwrWithAuth } from "./utility" ;
20
21
21
22
//
22
- // Queries
23
+ // region Queries
23
24
//
24
25
25
- // User
26
+ // region User
26
27
export function useGetCurrentUser ( ) {
27
28
return useSwrWithAuth < User > ( "/me" ) ;
28
29
}
29
30
30
- // Apps
31
+ // region Apps + API Keys
31
32
export function useListAppsForTeam ( team : Team ) {
32
33
return useSwrWithAuth < App [ ] > ( `/teams/${ team . id } /apps` ) ;
33
34
}
@@ -55,8 +56,11 @@ export function useListFlatAppsForTeamsOmitErrors(teams: Team[]) {
55
56
export function useGetAppDetails ( id ?: string ) {
56
57
return useSwrWithAuth < App > ( `/apps/${ id } ` ) ;
57
58
}
59
+ export function useListApiKeysForApp ( appId : string ) {
60
+ return useSwrWithAuth < ApiKey [ ] > ( `/apps/${ appId } /api-keys` ) ;
61
+ }
58
62
59
- // Teams
63
+ // region Teams
60
64
const TEAMS_SWR_KEY = "teams" ;
61
65
export function useListTeams ( ) {
62
66
return useSwrWithAuth < Team [ ] > ( `/teams` ) ;
@@ -68,7 +72,7 @@ export function useGetTeamDetails(id?: string) {
68
72
return useSwrWithAuth < Team > ( `/teams/${ id } ` ) ;
69
73
}
70
74
71
- // Api Products
75
+ // region API Products
72
76
export function useListApiProducts ( ) {
73
77
return useSwrWithAuth < ApiProductSummary [ ] > ( "/api-products" ) ;
74
78
}
@@ -79,7 +83,7 @@ export function useGetApiProductVersions(id?: string) {
79
83
return useSwrWithAuth < ApiVersion [ ] > ( `/api-products/${ id } /versions` ) ;
80
84
}
81
85
82
- // Subscriptions
86
+ // region Subscriptions
83
87
// this is an admin endpoint
84
88
export function useListSubscriptionsForStatus ( status : SubscriptionStatus ) {
85
89
const swrResponse = useSwrWithAuth < Subscription [ ] | SubscriptionsListError > (
@@ -115,7 +119,7 @@ export function useListSubscriptionsForApps(apps: App[]) {
115
119
}
116
120
117
121
//
118
- // Mutations
122
+ // region Mutations
119
123
//
120
124
121
125
const getLatestAuthHeaders = ( latestAccessToken : string | undefined ) => {
@@ -129,7 +133,7 @@ const getLatestAuthHeaders = (latestAccessToken: string | undefined) => {
129
133
type MutationWithArgs < T > = { arg : T } ;
130
134
131
135
// ------------------------ //
132
- // Create Team
136
+ // region Create Team
133
137
134
138
type CreateTeamParams = MutationWithArgs < { name : string ; description : string } > ;
135
139
@@ -149,7 +153,7 @@ export function useCreateTeamMutation() {
149
153
}
150
154
151
155
// ------------------------ //
152
- // Create Team Member
156
+ // region Create Team Member
153
157
154
158
type AddTeamMemberParams = MutationWithArgs < { email : string ; teamId : string } > ;
155
159
@@ -169,7 +173,7 @@ export function useAddTeamMemberMutation() {
169
173
}
170
174
171
175
// ------------------------ //
172
- // Remove Team Member
176
+ // region Remove Team Member
173
177
174
178
type AdminRemoveTeamMemberParams = MutationWithArgs < {
175
179
teamId : string ;
@@ -194,7 +198,7 @@ export function useRemoveTeamMemberMutation() {
194
198
}
195
199
196
200
// ------------------------ //
197
- // Create App
201
+ // region Create App
198
202
199
203
type CreateAppParams = MutationWithArgs < { name : string ; description : string } > ;
200
204
@@ -220,7 +224,7 @@ export function useCreateAppMutation(teamId: string | undefined) {
220
224
}
221
225
222
226
// ------------------------ //
223
- // Update App
227
+ // region Update App
224
228
225
229
type UpdateAppParams = MutationWithArgs < {
226
230
appId : string ;
@@ -247,7 +251,7 @@ export function useUpdateAppMutation() {
247
251
}
248
252
249
253
// ------------------------ //
250
- // Update Team
254
+ // region Update Team
251
255
252
256
type UpdateTeamParams = MutationWithArgs < {
253
257
teamId : string ;
@@ -272,7 +276,7 @@ export function useUpdateTeamMutation() {
272
276
}
273
277
274
278
// ------------------------ //
275
- // Create App and Subscription
279
+ // region Create App and Subscription
276
280
277
281
type CreateAppAndSubscriptionParams = MutationWithArgs < {
278
282
appName : string ;
@@ -315,7 +319,7 @@ export function useCreateAppAndSubscriptionMutation() {
315
319
}
316
320
317
321
// ------------------------ //
318
- // Create Subscription
322
+ // region Create Subscription
319
323
320
324
type CreateSubscriptionParams = MutationWithArgs < {
321
325
apiProductId : string ;
@@ -344,7 +348,7 @@ export function useCreateSubscriptionMutation(appId: string) {
344
348
}
345
349
346
350
// -------------------------------- //
347
- // (Admin) Approve/Reject Subscription
351
+ // region (Admin) Approve/Reject Subscription
348
352
349
353
type UpdateSubscriptionParams = MutationWithArgs < {
350
354
subscription : Subscription ;
@@ -387,7 +391,7 @@ export function useAdminRejectSubscriptionMutation() {
387
391
}
388
392
389
393
// -------------------------------- //
390
- // Delete Subscription
394
+ // region Delete Subscription
391
395
392
396
export function useDeleteSubscriptionMutation ( ) {
393
397
const { latestAccessToken } = useContext ( AuthContext ) ;
@@ -406,7 +410,7 @@ export function useDeleteSubscriptionMutation() {
406
410
}
407
411
408
412
// -------------------------------- //
409
- // Delete Team
413
+ // region Delete Team
410
414
411
415
type DeleteTeamParams = MutationWithArgs < { teamId : string } > ;
412
416
@@ -424,7 +428,7 @@ export function useDeleteTeamMutation() {
424
428
}
425
429
426
430
// -------------------------------- //
427
- // Delete App
431
+ // region Delete App
428
432
429
433
type DeleteAppParams = MutationWithArgs < { appId : string } > ;
430
434
@@ -440,3 +444,39 @@ export function useDeleteAppMutation() {
440
444
} ;
441
445
return useSWRMutation ( `delete-team` , deleteApp ) ;
442
446
}
447
+
448
+ // -------------------------------- //
449
+ // region Create API Key
450
+
451
+ type CreateApiKeyParams = MutationWithArgs < { apiKeyName : string } > ;
452
+
453
+ export function useCreateApiKeyMutation ( appId : string ) {
454
+ const { latestAccessToken } = useContext ( AuthContext ) ;
455
+ const createApiKey = async ( _ : string , { arg } : CreateApiKeyParams ) => {
456
+ return await fetchJSON ( `/apps/${ appId } /api-keys` , {
457
+ method : "POST" ,
458
+ headers : getLatestAuthHeaders ( latestAccessToken ) ,
459
+ body : JSON . stringify ( arg ) ,
460
+ } ) ;
461
+ } ;
462
+ return useSWRMutation < ApiKey , any , string , CreateApiKeyParams [ "arg" ] > (
463
+ `/apps/${ appId } /api-keys` ,
464
+ createApiKey
465
+ ) ;
466
+ }
467
+
468
+ // -------------------------------- //
469
+ // region Delete API Key
470
+
471
+ type DeleteApiKeyParams = MutationWithArgs < { apiKeyId : string } > ;
472
+
473
+ export function useDeleteApiKeyMutation ( appId : string ) {
474
+ const { latestAccessToken } = useContext ( AuthContext ) ;
475
+ const deleteApiKey = async ( _ : string , { arg } : DeleteApiKeyParams ) => {
476
+ await fetchJSON ( `/api-keys/${ arg . apiKeyId } ` , {
477
+ method : "DELETE" ,
478
+ headers : getLatestAuthHeaders ( latestAccessToken ) ,
479
+ } ) ;
480
+ } ;
481
+ return useSWRMutation ( `/apps/${ appId } /api-keys` , deleteApiKey ) ;
482
+ }
0 commit comments