From ffc891f38efc45f48b1ed10c3e8642c1a3b77cd0 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Mon, 12 Jul 2021 20:17:13 -0400 Subject: [PATCH] feat: add header session store fields and dispatchers --- helpers/types/HoppRESTRequest.ts | 7 ++++ newstore/RESTSession.ts | 65 +++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/helpers/types/HoppRESTRequest.ts b/helpers/types/HoppRESTRequest.ts index f0ceced5f..cc7148005 100644 --- a/helpers/types/HoppRESTRequest.ts +++ b/helpers/types/HoppRESTRequest.ts @@ -4,8 +4,15 @@ export type HoppRESTParam = { active: boolean } +export type HoppRESTHeader = { + key: string + value: string + active: boolean +} + export interface HoppRESTRequest { method: string endpoint: string params: HoppRESTParam[] + headers: HoppRESTHeader[] } diff --git a/newstore/RESTSession.ts b/newstore/RESTSession.ts index bd33e4f38..6593579dc 100644 --- a/newstore/RESTSession.ts +++ b/newstore/RESTSession.ts @@ -1,6 +1,10 @@ import { pluck, distinctUntilChanged } from "rxjs/operators" import DispatchingStore, { defineDispatchers } from "./DispatchingStore" -import { HoppRESTParam, HoppRESTRequest } from "~/helpers/types/HoppRESTRequest" +import { + HoppRESTHeader, + HoppRESTParam, + HoppRESTRequest, +} from "~/helpers/types/HoppRESTRequest" function getParamsInURL(url: string): { key: string; value: string }[] { const result: { key: string; value: string }[] = [] @@ -111,6 +115,7 @@ const defaultRESTSession: RESTSession = { request: { endpoint: "https://httpbin.org/", params: [], + headers: [], method: "GET", }, } @@ -234,6 +239,36 @@ const dispatchers = defineDispatchers({ }, } }, + addHeader(curr: RESTSession, { entry }: { entry: HoppRESTHeader }) { + return { + request: { + ...curr.request, + headers: [...curr.request.headers, entry], + }, + } + }, + updateHeader( + curr: RESTSession, + { index, updatedEntry }: { index: number; updatedEntry: HoppRESTHeader } + ) { + return { + request: { + ...curr.request, + headers: curr.request.headers.map((header, i) => { + if (i === index) return updatedEntry + else return header + }), + }, + } + }, + deleteHeader(curr: RESTSession, { index }: { index: number }) { + return { + request: { + ...curr.request, + headers: curr.request.headers.filter((_, i) => i !== index), + }, + } + }, }) const restSessionStore = new DispatchingStore(defaultRESTSession, dispatchers) @@ -291,6 +326,34 @@ export function updateRESTMethod(newMethod: string) { }) } +export function addRESTHeader(entry: HoppRESTHeader) { + restSessionStore.dispatch({ + dispatcher: "addHeader", + payload: { + entry, + }, + }) +} + +export function updateRESTHeader(index: number, updatedEntry: HoppRESTHeader) { + restSessionStore.dispatch({ + dispatcher: "updateHeader", + payload: { + index, + updatedEntry, + }, + }) +} + +export function deleteRESTHeader(index: number) { + restSessionStore.dispatch({ + dispatcher: "deleteHeader", + payload: { + index, + }, + }) +} + export const restRequest$ = restSessionStore.subject$.pipe( pluck("request"), distinctUntilChanged()