You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-plugins-auth.ts 3.3KB

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