apikey.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { Result } from '@/request/Result'
  2. import { get, post, del, put } from '@/request/index'
  3. import { type Ref } from 'vue'
  4. export interface ApiKey {
  5. id: string
  6. api_key_prefix: string
  7. name: string
  8. status: 'active' | 'disabled'
  9. last_used_at: string | null
  10. create_time: string
  11. }
  12. export interface CreateApiKeyResponse {
  13. id: string
  14. api_key: string
  15. api_key_prefix: string
  16. name: string
  17. status: string
  18. create_time: string
  19. }
  20. const api_key_prefix = '/platform/api-keys'
  21. /**
  22. * 获取 API Key 列表
  23. */
  24. const getApiKeyList: (loading?: Ref<boolean>) => Promise<Result<Array<ApiKey>>> = (loading) => {
  25. return get(api_key_prefix, undefined, loading)
  26. }
  27. /**
  28. * 创建 API Key(完整密钥仅返回一次)
  29. */
  30. const createApiKey: (
  31. data: { name?: string },
  32. loading?: Ref<boolean>,
  33. ) => Promise<Result<CreateApiKeyResponse>> = (data, loading) => {
  34. return post(api_key_prefix, data, loading)
  35. }
  36. /**
  37. * 更新 API Key 状态
  38. */
  39. const updateApiKeyStatus: (
  40. keyId: string,
  41. data: { status: string },
  42. loading?: Ref<boolean>,
  43. ) => Promise<Result<null>> = (keyId, data, loading) => {
  44. return put(`${api_key_prefix}/${keyId}`, data, loading)
  45. }
  46. /**
  47. * 删除 API Key
  48. */
  49. const deleteApiKey: (
  50. keyId: string,
  51. loading?: Ref<boolean>,
  52. ) => Promise<Result<null>> = (keyId, loading) => {
  53. return del(`${api_key_prefix}/${keyId}`, undefined, loading)
  54. }
  55. export { getApiKeyList, createApiKey, updateApiKeyStatus, deleteApiKey }