Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

use-plugins-auth.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. queryKey: [NAME_SPACE, 'credential-schema', url],
  90. queryFn: () => get<FormSchema[]>(url),
  91. })
  92. }
  93. export const useGetPluginOAuthUrl = (
  94. url: string,
  95. ) => {
  96. return useMutation({
  97. mutationKey: [NAME_SPACE, 'oauth-url', url],
  98. mutationFn: () => {
  99. return get<
  100. {
  101. authorization_url: string
  102. state: string
  103. context_id: string
  104. }>(url)
  105. },
  106. })
  107. }
  108. export const useGetPluginOAuthClientSchema = (
  109. url: string,
  110. ) => {
  111. return useQuery({
  112. queryKey: [NAME_SPACE, 'oauth-client-schema', url],
  113. queryFn: () => get<{
  114. schema: FormSchema[]
  115. is_oauth_custom_client_enabled: boolean
  116. is_system_oauth_params_exists?: boolean
  117. client_params?: Record<string, any>
  118. redirect_uri?: string
  119. }>(url),
  120. staleTime: 0,
  121. })
  122. }
  123. export const useInvalidPluginOAuthClientSchema = (
  124. url: string,
  125. ) => {
  126. return useInvalid([NAME_SPACE, 'oauth-client-schema', url])
  127. }
  128. export const useSetPluginOAuthCustomClient = (
  129. url: string,
  130. ) => {
  131. return useMutation({
  132. mutationFn: (params: {
  133. client_params: Record<string, any>
  134. enable_oauth_custom_client: boolean
  135. }) => {
  136. return post<{ result: string }>(url, { body: params })
  137. },
  138. })
  139. }
  140. export const useDeletePluginOAuthCustomClient = (
  141. url: string,
  142. ) => {
  143. return useMutation({
  144. mutationFn: () => {
  145. return del<{ result: string }>(url)
  146. },
  147. })
  148. }