GQLSession.ts 5.0 KB

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