Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

use-plugins-auth.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {
  2. useMutation,
  3. useQuery,
  4. } from '@tanstack/react-query'
  5. import { del, get, post } from './base'
  6. import { useInvalid } from './use-base'
  7. import type {
  8. Credential,
  9. CredentialTypeEnum,
  10. } from '@/app/components/plugins/plugin-auth/types'
  11. import type { FormSchema } from '@/app/components/base/form/types'
  12. const NAME_SPACE = 'plugins-auth'
  13. export const useGetPluginCredentialInfo = (
  14. url: string,
  15. ) => {
  16. return useQuery({
  17. enabled: !!url,
  18. queryKey: [NAME_SPACE, 'credential-info', url],
  19. queryFn: () => get<{
  20. allow_custom_token?: boolean
  21. supported_credential_types: string[]
  22. credentials: Credential[]
  23. is_oauth_custom_client_enabled: boolean
  24. }>(url),
  25. staleTime: 0,
  26. })
  27. }
  28. export const useInvalidPluginCredentialInfo = (
  29. url: string,
  30. ) => {
  31. return useInvalid([NAME_SPACE, 'credential-info', url])
  32. }
  33. export const useSetPluginDefaultCredential = (
  34. url: string,
  35. ) => {
  36. return useMutation({
  37. mutationFn: (id: string) => {
  38. return post(url, { body: { id } })
  39. },
  40. })
  41. }
  42. export const useGetPluginCredentialList = (
  43. url: string,
  44. ) => {
  45. return useQuery({
  46. queryKey: [NAME_SPACE, 'credential-list', url],
  47. queryFn: () => get(url),
  48. })
  49. }
  50. export const useAddPluginCredential = (
  51. url: string,
  52. ) => {
  53. return useMutation({
  54. mutationFn: (params: {
  55. credentials: Record<string, any>
  56. type: CredentialTypeEnum
  57. name?: string
  58. }) => {
  59. return post(url, { body: params })
  60. },
  61. })
  62. }
  63. export const useUpdatePluginCredential = (
  64. url: string,
  65. ) => {
  66. return useMutation({
  67. mutationFn: (params: {
  68. credential_id: string
  69. credentials?: Record<string, any>
  70. name?: string
  71. }) => {
  72. return post(url, { body: params })
  73. },
  74. })
  75. }
  76. export const useDeletePluginCredential = (
  77. url: string,
  78. ) => {
  79. return useMutation({
  80. mutationFn: (params: { credential_id: string }) => {
  81. return post(url, { body: params })
  82. },
  83. })
  84. }
  85. export const useGetPluginCredentialSchema = (
  86. url: string,
  87. ) => {
  88. return useQuery({
  89. enabled: !!url,
  90. queryKey: [NAME_SPACE, 'credential-schema', url],
  91. queryFn: () => get<FormSchema[]>(url),
  92. })
  93. }
  94. export const useGetPluginOAuthUrl = (
  95. url: string,
  96. ) => {
  97. return useMutation({
  98. mutationKey: [NAME_SPACE, 'oauth-url', url],
  99. mutationFn: () => {
  100. return get<
  101. {
  102. authorization_url: string
  103. state: string
  104. context_id: string
  105. }>(url)
  106. },
  107. })
  108. }
  109. export const useGetPluginOAuthClientSchema = (
  110. url: string,
  111. ) => {
  112. return useQuery({
  113. enabled: !!url,
  114. queryKey: [NAME_SPACE, 'oauth-client-schema', url],
  115. queryFn: () => get<{
  116. schema: FormSchema[]
  117. is_oauth_custom_client_enabled: boolean
  118. is_system_oauth_params_exists?: boolean
  119. client_params?: Record<string, any>
  120. redirect_uri?: string
  121. }>(url),
  122. staleTime: 0,
  123. })
  124. }
  125. export const useInvalidPluginOAuthClientSchema = (
  126. url: string,
  127. ) => {
  128. return useInvalid([NAME_SPACE, 'oauth-client-schema', url])
  129. }
  130. export const useSetPluginOAuthCustomClient = (
  131. url: string,
  132. ) => {
  133. return useMutation({
  134. mutationFn: (params: {
  135. client_params: Record<string, any>
  136. enable_oauth_custom_client: boolean
  137. }) => {
  138. return post<{ result: string }>(url, { body: params })
  139. },
  140. })
  141. }
  142. export const useDeletePluginOAuthCustomClient = (
  143. url: string,
  144. ) => {
  145. return useMutation({
  146. mutationFn: () => {
  147. return del<{ result: string }>(url)
  148. },
  149. })
  150. }