-
Notifications
You must be signed in to change notification settings - Fork 140
/
api-keys.ts
102 lines (94 loc) · 2.8 KB
/
api-keys.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
createAsyncThunk,
createSlice,
SerializedError,
createEntityAdapter,
} from "@reduxjs/toolkit";
import { APIKey } from "pipe/pkg/app/web/model/apikey_pb";
import * as APIKeysAPI from "../api/api-keys";
const MODULE_NAME = "apiKeys";
const apiKeysAdapter = createEntityAdapter<APIKey.AsObject>();
export const generateAPIKey = createAsyncThunk<
string,
{ name: string; role: APIKey.Role }
>(`${MODULE_NAME}/generate`, async ({ name, role }) => {
const res = await APIKeysAPI.generateAPIKey({ name, role });
return res.key;
});
export const fetchAPIKeys = createAsyncThunk<
APIKey.AsObject[],
{ enabled: boolean }
>(`${MODULE_NAME}/getList`, async (options) => {
const res = await APIKeysAPI.getAPIKeys({ options });
return res.keysList;
});
export const disableAPIKey = createAsyncThunk<void, { id: string }>(
`${MODULE_NAME}/disable`,
async ({ id }) => {
await APIKeysAPI.disableAPIKey({ id });
}
);
export const apiKeysSlice = createSlice({
name: "apiKeys",
initialState: apiKeysAdapter.getInitialState<{
generatedKey: string | null;
loading: boolean;
generating: boolean;
disabling: boolean;
error: null | SerializedError;
}>({
generatedKey: null,
loading: false,
generating: false,
disabling: false,
error: null,
}),
reducers: {
clearGeneratedKey(state) {
state.generatedKey = null;
},
},
extraReducers: (builder) => {
builder
// generateAPIKey
.addCase(generateAPIKey.pending, (state) => {
state.generating = true;
state.generatedKey = null;
state.error = null;
})
.addCase(generateAPIKey.rejected, (state, action) => {
state.generating = false;
state.error = action.error;
})
.addCase(generateAPIKey.fulfilled, (state, action) => {
state.generating = false;
state.generatedKey = action.payload;
})
// fetchAPIKeys
.addCase(fetchAPIKeys.pending, (state) => {
state.loading = true;
})
.addCase(fetchAPIKeys.rejected, (state, action) => {
state.loading = false;
state.error = action.error;
})
.addCase(fetchAPIKeys.fulfilled, (state, action) => {
state.loading = false;
apiKeysAdapter.setAll(state, action.payload);
})
// disableAPIKey
.addCase(disableAPIKey.pending, (state) => {
state.disabling = true;
})
.addCase(disableAPIKey.rejected, (state, action) => {
state.disabling = false;
state.error = action.error;
})
.addCase(disableAPIKey.fulfilled, (state) => {
state.disabling = false;
});
},
});
export const { clearGeneratedKey } = apiKeysSlice.actions;
export const { selectAll, selectById } = apiKeysAdapter.getSelectors();
export { APIKey } from "pipe/pkg/app/web/model/apikey_pb";