GQLSession.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import { distinctUntilChanged, pluck } from "rxjs/operators"
  2. import {
  3. GQLHeader,
  4. HoppGQLRequest,
  5. makeGQLRequest,
  6. HoppGQLAuth,
  7. } from "@hoppscotch/data"
  8. import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
  9. import { useStream } from "~/helpers/utils/composables"
  10. type GQLSession = {
  11. request: HoppGQLRequest
  12. schema: string
  13. response: string
  14. }
  15. export const defaultGQLSession: GQLSession = {
  16. request: makeGQLRequest({
  17. name: "Untitled request",
  18. url: "https://echo.hoppscotch.io/graphql",
  19. headers: [],
  20. variables: `{
  21. "id": "1"
  22. }`,
  23. query: `query Request {
  24. method
  25. url
  26. headers {
  27. key
  28. value
  29. }
  30. }
  31. `,
  32. auth: {
  33. authType: "none",
  34. authActive: true,
  35. },
  36. }),
  37. schema: "",
  38. response: "",
  39. }
  40. const dispatchers = defineDispatchers({
  41. setSession(_: GQLSession, { session }: { session: GQLSession }) {
  42. return session
  43. },
  44. setName(curr: GQLSession, { newName }: { newName: string }) {
  45. return {
  46. request: {
  47. ...curr.request,
  48. name: newName,
  49. },
  50. }
  51. },
  52. setURL(curr: GQLSession, { newURL }: { newURL: string }) {
  53. return {
  54. request: {
  55. ...curr.request,
  56. url: newURL,
  57. },
  58. }
  59. },
  60. setHeaders(curr: GQLSession, { headers }: { headers: GQLHeader[] }) {
  61. return {
  62. request: {
  63. ...curr.request,
  64. headers,
  65. },
  66. }
  67. },
  68. addHeader(curr: GQLSession, { header }: { header: GQLHeader }) {
  69. return {
  70. request: {
  71. ...curr.request,
  72. headers: [...curr.request.headers, header],
  73. },
  74. }
  75. },
  76. removeHeader(curr: GQLSession, { headerIndex }: { headerIndex: number }) {
  77. return {
  78. request: {
  79. ...curr.request,
  80. headers: curr.request.headers.filter((_x, i) => i !== headerIndex),
  81. },
  82. }
  83. },
  84. updateHeader(
  85. curr: GQLSession,
  86. {
  87. headerIndex,
  88. updatedHeader,
  89. }: { headerIndex: number; updatedHeader: GQLHeader }
  90. ) {
  91. return {
  92. request: {
  93. ...curr.request,
  94. headers: curr.request.headers.map((x, i) =>
  95. i === headerIndex ? updatedHeader : x
  96. ),
  97. },
  98. }
  99. },
  100. setQuery(curr: GQLSession, { newQuery }: { newQuery: string }) {
  101. return {
  102. request: {
  103. ...curr.request,
  104. query: newQuery,
  105. },
  106. }
  107. },
  108. setVariables(curr: GQLSession, { newVariables }: { newVariables: string }) {
  109. return {
  110. request: {
  111. ...curr.request,
  112. variables: newVariables,
  113. },
  114. }
  115. },
  116. setResponse(_: GQLSession, { newResponse }: { newResponse: string }) {
  117. return {
  118. response: newResponse,
  119. }
  120. },
  121. setAuth(curr: GQLSession, { newAuth }: { newAuth: HoppGQLAuth }) {
  122. return {
  123. request: {
  124. ...curr.request,
  125. auth: newAuth,
  126. },
  127. }
  128. },
  129. })
  130. export const gqlSessionStore = new DispatchingStore(
  131. defaultGQLSession,
  132. dispatchers
  133. )
  134. export function setGQLURL(newURL: string) {
  135. gqlSessionStore.dispatch({
  136. dispatcher: "setURL",
  137. payload: {
  138. newURL,
  139. },
  140. })
  141. }
  142. export function setGQLHeaders(headers: GQLHeader[]) {
  143. gqlSessionStore.dispatch({
  144. dispatcher: "setHeaders",
  145. payload: {
  146. headers,
  147. },
  148. })
  149. }
  150. export function addGQLHeader(header: GQLHeader) {
  151. gqlSessionStore.dispatch({
  152. dispatcher: "addHeader",
  153. payload: {
  154. header,
  155. },
  156. })
  157. }
  158. export function updateGQLHeader(headerIndex: number, updatedHeader: GQLHeader) {
  159. gqlSessionStore.dispatch({
  160. dispatcher: "updateHeader",
  161. payload: {
  162. headerIndex,
  163. updatedHeader,
  164. },
  165. })
  166. }
  167. export function removeGQLHeader(headerIndex: number) {
  168. gqlSessionStore.dispatch({
  169. dispatcher: "removeHeader",
  170. payload: {
  171. headerIndex,
  172. },
  173. })
  174. }
  175. export function clearGQLHeaders() {
  176. gqlSessionStore.dispatch({
  177. dispatcher: "setHeaders",
  178. payload: {
  179. headers: [],
  180. },
  181. })
  182. }
  183. export function setGQLQuery(newQuery: string) {
  184. gqlSessionStore.dispatch({
  185. dispatcher: "setQuery",
  186. payload: {
  187. newQuery,
  188. },
  189. })
  190. }
  191. export function setGQLVariables(newVariables: string) {
  192. gqlSessionStore.dispatch({
  193. dispatcher: "setVariables",
  194. payload: {
  195. newVariables,
  196. },
  197. })
  198. }
  199. export function setGQLResponse(newResponse: string) {
  200. gqlSessionStore.dispatch({
  201. dispatcher: "setResponse",
  202. payload: {
  203. newResponse,
  204. },
  205. })
  206. }
  207. export function getGQLSession() {
  208. return gqlSessionStore.value
  209. }
  210. export function setGQLSession(session: GQLSession) {
  211. gqlSessionStore.dispatch({
  212. dispatcher: "setSession",
  213. payload: {
  214. session,
  215. },
  216. })
  217. }
  218. export function useGQLRequestName() {
  219. return useStream(gqlName$, gqlSessionStore.value.request.name, (newName) => {
  220. gqlSessionStore.dispatch({
  221. dispatcher: "setName",
  222. payload: { newName },
  223. })
  224. })
  225. }
  226. export function setGQLAuth(newAuth: HoppGQLAuth) {
  227. gqlSessionStore.dispatch({
  228. dispatcher: "setAuth",
  229. payload: {
  230. newAuth,
  231. },
  232. })
  233. }
  234. export const gqlName$ = gqlSessionStore.subject$.pipe(
  235. pluck("request", "name"),
  236. distinctUntilChanged()
  237. )
  238. export const gqlURL$ = gqlSessionStore.subject$.pipe(
  239. pluck("request", "url"),
  240. distinctUntilChanged()
  241. )
  242. export const gqlQuery$ = gqlSessionStore.subject$.pipe(
  243. pluck("request", "query"),
  244. distinctUntilChanged()
  245. )
  246. export const gqlVariables$ = gqlSessionStore.subject$.pipe(
  247. pluck("request", "variables"),
  248. distinctUntilChanged()
  249. )
  250. export const gqlHeaders$ = gqlSessionStore.subject$.pipe(
  251. pluck("request", "headers"),
  252. distinctUntilChanged()
  253. )
  254. export const gqlResponse$ = gqlSessionStore.subject$.pipe(
  255. pluck("response"),
  256. distinctUntilChanged()
  257. )
  258. export const gqlAuth$ = gqlSessionStore.subject$.pipe(
  259. pluck("request", "auth"),
  260. distinctUntilChanged()
  261. )