feat: init request state management
This commit is contained in:
@@ -39,6 +39,9 @@ function recalculateParams(
|
||||
if (!currentParam) {
|
||||
addedKeys.push(key)
|
||||
result.push({ key, value, active: true })
|
||||
} else {
|
||||
addedKeys.push(key)
|
||||
result.push({ key, value, active: currentParam.active })
|
||||
}
|
||||
})
|
||||
|
||||
@@ -47,6 +50,59 @@ function recalculateParams(
|
||||
return result
|
||||
}
|
||||
|
||||
function removeParamFromURL(url: string, param: string): string {
|
||||
try {
|
||||
const urlObj = new URL(url)
|
||||
urlObj.searchParams.delete(param)
|
||||
return urlObj.toString()
|
||||
} catch (e) {
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
||||
function removeAllParamsFromURL(url: string): string {
|
||||
try {
|
||||
const urlObj = new URL(url)
|
||||
const params: string[] = []
|
||||
|
||||
urlObj.searchParams.forEach((_value, key) => params.push(key))
|
||||
|
||||
params.forEach((key) => urlObj.searchParams.delete(key))
|
||||
|
||||
return urlObj.toString()
|
||||
} catch (e) {
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
||||
function updateURLParam(
|
||||
url: string,
|
||||
currKey: string,
|
||||
newKey: string,
|
||||
newValue: string
|
||||
): string {
|
||||
try {
|
||||
const urlObj = new URL(url)
|
||||
|
||||
let params: { key: string; value: string }[] = []
|
||||
|
||||
urlObj.searchParams.forEach((value, key) => params.push({ key, value }))
|
||||
|
||||
params.forEach((x) => urlObj.searchParams.delete(x.key))
|
||||
|
||||
params = params.map((x) => {
|
||||
if (x.key === currKey) return { key: newKey, value: newValue }
|
||||
else return x
|
||||
})
|
||||
|
||||
params.forEach((x) => urlObj.searchParams.append(x.key, x.value))
|
||||
|
||||
return urlObj.toString()
|
||||
} catch (e) {
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
||||
type RESTSession = {
|
||||
request: HoppRESTRequest
|
||||
}
|
||||
@@ -55,6 +111,7 @@ const defaultRESTSession: RESTSession = {
|
||||
request: {
|
||||
endpoint: "https://httpbin.org/",
|
||||
params: [],
|
||||
method: "GET",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -83,6 +140,100 @@ const dispatchers = defineDispatchers({
|
||||
},
|
||||
}
|
||||
},
|
||||
updateParam(
|
||||
curr: RESTSession,
|
||||
{ index, updatedParam }: { index: number; updatedParam: HoppRESTParam }
|
||||
) {
|
||||
const paramsInURL = getParamsInURL(curr.request.endpoint).map((x) => x.key)
|
||||
|
||||
if (paramsInURL.includes(curr.request.params[index].key)) {
|
||||
const updatedURL = updateURLParam(
|
||||
curr.request.endpoint,
|
||||
curr.request.params[index].key,
|
||||
updatedParam.key,
|
||||
updatedParam.value
|
||||
)
|
||||
|
||||
const newParams = curr.request.params.map((param, i) => {
|
||||
if (i === index) return updatedParam
|
||||
else return param
|
||||
})
|
||||
|
||||
return {
|
||||
request: {
|
||||
...curr.request,
|
||||
endpoint: updatedURL,
|
||||
params: newParams,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
const newParams = curr.request.params.map((param, i) => {
|
||||
if (i === index) return updatedParam
|
||||
else return param
|
||||
})
|
||||
|
||||
return {
|
||||
request: {
|
||||
...curr.request,
|
||||
params: newParams,
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
deleteParam(curr: RESTSession, { index }: { index: number }) {
|
||||
const paramsFromURL = getParamsInURL(curr.request.endpoint).map(
|
||||
(x) => x.key
|
||||
)
|
||||
if (paramsFromURL.includes(curr.request.params[index].key)) {
|
||||
const newURL = removeParamFromURL(
|
||||
curr.request.endpoint,
|
||||
curr.request.params[index].key
|
||||
)
|
||||
|
||||
const newParams = getParamsInURL(newURL)
|
||||
|
||||
const recalculatedParams = recalculateParams(
|
||||
curr.request.endpoint,
|
||||
curr.request.params,
|
||||
newParams
|
||||
)
|
||||
return {
|
||||
request: {
|
||||
...curr.request,
|
||||
endpoint: newURL,
|
||||
params: recalculatedParams,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
const newParams = curr.request.params.filter((_x, i) => i !== index)
|
||||
|
||||
return {
|
||||
request: {
|
||||
...curr.request,
|
||||
params: newParams,
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
deleteAllParams(curr: RESTSession) {
|
||||
const newURL = removeAllParamsFromURL(curr.request.endpoint)
|
||||
|
||||
return {
|
||||
request: {
|
||||
...curr.request,
|
||||
endpoint: newURL,
|
||||
params: [],
|
||||
},
|
||||
}
|
||||
},
|
||||
updateMethod(curr: RESTSession, { newMethod }: { newMethod: string }) {
|
||||
return {
|
||||
request: {
|
||||
...curr.request,
|
||||
method: newMethod,
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
const restSessionStore = new DispatchingStore(defaultRESTSession, dispatchers)
|
||||
@@ -105,6 +256,41 @@ export function addRESTParam(newParam: HoppRESTParam) {
|
||||
})
|
||||
}
|
||||
|
||||
export function updateRESTParam(index: number, updatedParam: HoppRESTParam) {
|
||||
restSessionStore.dispatch({
|
||||
dispatcher: "updateParam",
|
||||
payload: {
|
||||
updatedParam,
|
||||
index,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function deleteRESTParam(index: number) {
|
||||
restSessionStore.dispatch({
|
||||
dispatcher: "deleteParam",
|
||||
payload: {
|
||||
index,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function deleteAllRESTParams() {
|
||||
restSessionStore.dispatch({
|
||||
dispatcher: "deleteAllParams",
|
||||
payload: {},
|
||||
})
|
||||
}
|
||||
|
||||
export function updateRESTMethod(newMethod: string) {
|
||||
restSessionStore.dispatch({
|
||||
dispatcher: "updateMethod",
|
||||
payload: {
|
||||
newMethod,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const restRequest$ = restSessionStore.subject$.pipe(
|
||||
pluck("request"),
|
||||
distinctUntilChanged()
|
||||
@@ -119,3 +305,8 @@ export const restParams$ = restSessionStore.subject$.pipe(
|
||||
pluck("request", "params"),
|
||||
distinctUntilChanged()
|
||||
)
|
||||
|
||||
export const restMethod$ = restSessionStore.subject$.pipe(
|
||||
pluck("request", "method"),
|
||||
distinctUntilChanged()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user