mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-18 23:45:26 +02:00
feat(api-keys): Add APIs
This commit is contained in:
parent
bc5063b44c
commit
7cd798a343
24
src/apis/create-api-key.ts
Normal file
24
src/apis/create-api-key.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import {APIKey} from "~/server-types"
|
||||
import {client} from "~/constants/axios-client"
|
||||
import parseAPIKey from "~/apis/helpers/parse-api-key"
|
||||
|
||||
export interface CreateAPIKeyData {
|
||||
label: string
|
||||
scopes: APIKey["scopes"]
|
||||
|
||||
expiresAt?: Date
|
||||
}
|
||||
|
||||
export default async function createAPIKey({
|
||||
label,
|
||||
scopes,
|
||||
expiresAt,
|
||||
}: CreateAPIKeyData): Promise<APIKey> {
|
||||
const {data} = await client.post(`${import.meta.env.VITE_SERVER_BASE_URL}/v1/api-key`, {
|
||||
label,
|
||||
scopes,
|
||||
expiresAt,
|
||||
})
|
||||
|
||||
return parseAPIKey(data)
|
||||
}
|
9
src/apis/delete-api-key.ts
Normal file
9
src/apis/delete-api-key.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import {client} from "~/constants/axios-client"
|
||||
|
||||
export default async function deleteApiKey(id: string): Promise<void> {
|
||||
const {data} = await client.delete(`${import.meta.env.VITE_SERVER_BASE_URL}/v1/api-key/${id}`, {
|
||||
withCredentials: true,
|
||||
})
|
||||
|
||||
return data
|
||||
}
|
22
src/apis/get-api-keys.ts
Normal file
22
src/apis/get-api-keys.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import {APIKey, GetPageData, PaginationResult} from "~/server-types"
|
||||
import {client} from "~/constants/axios-client"
|
||||
import parseAPIKey from "~/apis/helpers/parse-api-key"
|
||||
|
||||
export interface GetAPIKeysData extends GetPageData {}
|
||||
|
||||
export default async function getAPIKeys({size, page}: GetAPIKeysData = {}): Promise<
|
||||
PaginationResult<APIKey>
|
||||
> {
|
||||
const {data} = await client.get(`${import.meta.env.VITE_SERVER_BASE_URL}/v1/api-key/`, {
|
||||
withCredentials: true,
|
||||
params: {
|
||||
size,
|
||||
page,
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
...data,
|
||||
items: data.items.map(parseAPIKey),
|
||||
}
|
||||
}
|
8
src/apis/helpers/parse-api-key.ts
Normal file
8
src/apis/helpers/parse-api-key.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import {APIKey} from "~/server-types"
|
||||
|
||||
export default function parseAPIKey(key: APIKey): APIKey {
|
||||
return {
|
||||
...key,
|
||||
expiresAt: new Date(key.expiresAt),
|
||||
}
|
||||
}
|
@ -68,3 +68,9 @@ export * from "./delete-2fa"
|
||||
export {default as delete2FA} from "./delete-2fa"
|
||||
export * from "./verify-otp"
|
||||
export {default as verifyOTP} from "./verify-otp"
|
||||
export * from "./create-api-key"
|
||||
export {default as createAPIKey} from "./create-api-key"
|
||||
export * from "./get-api-keys"
|
||||
export {default as getAPIKeys} from "./get-api-keys"
|
||||
export * from "./delete-api-key"
|
||||
export {default as deleteAPIKey} from "./delete-api-key"
|
||||
|
Loading…
x
Reference in New Issue
Block a user