refactor: implement updated equality heuristics for hopprestrequest struct
This commit is contained in:
committed by
liyasthomas
parent
99148a0a0e
commit
ede27e0600
@@ -1,6 +1,9 @@
|
||||
import cloneDeep from "lodash/cloneDeep"
|
||||
import * as Eq from "fp-ts/Eq"
|
||||
import * as S from "fp-ts/string"
|
||||
import { ValidContentTypes } from "./content-types"
|
||||
import { HoppRESTAuth } from "./HoppRESTAuth"
|
||||
import { lodashIsEqualEq, mapThenEq, undefinedEq } from "../utils/eq"
|
||||
|
||||
export * from "./content-types"
|
||||
export * from "./HoppRESTAuth"
|
||||
@@ -57,6 +60,28 @@ export interface HoppRESTRequest {
|
||||
body: HoppRESTReqBody
|
||||
}
|
||||
|
||||
export const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
|
||||
id: undefinedEq(S.Eq),
|
||||
v: S.Eq,
|
||||
auth: lodashIsEqualEq,
|
||||
body: lodashIsEqualEq,
|
||||
endpoint: S.Eq,
|
||||
headers: mapThenEq(
|
||||
(arr) => arr.filter((h) => h.key !== "" && h.value !== ""),
|
||||
lodashIsEqualEq
|
||||
),
|
||||
params: mapThenEq(
|
||||
(arr) => arr.filter((p) => p.key !== "" && p.value !== ""),
|
||||
lodashIsEqualEq
|
||||
),
|
||||
method: S.Eq,
|
||||
name: S.Eq,
|
||||
preRequestScript: S.Eq,
|
||||
testScript: S.Eq,
|
||||
})
|
||||
|
||||
export const isEqualHoppRESTRequest = HoppRESTRequestEq.equals
|
||||
|
||||
/**
|
||||
* Safely tries to extract REST Request data from an unknown value.
|
||||
* If we fail to detect certain bits, we just resolve it to the default value
|
||||
|
||||
50
packages/hoppscotch-data/src/utils/eq.ts
Normal file
50
packages/hoppscotch-data/src/utils/eq.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import * as Eq from "fp-ts/Eq"
|
||||
import * as S from "fp-ts/string"
|
||||
import isEqual from "lodash/isEqual"
|
||||
|
||||
/*
|
||||
* Eq-s are fp-ts an interface (type class) that defines how the equality
|
||||
* of 2 values of a certain type are matched as equal
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Create an Eq from a non-undefinable value and makes it accept undefined
|
||||
* @param eq The non nullable Eq to add to
|
||||
* @returns The updated Eq which accepts undefined
|
||||
*/
|
||||
export const undefinedEq = <T>(eq: Eq.Eq<T>): Eq.Eq<T | undefined> => ({
|
||||
equals(x: T | undefined, y: T | undefined) {
|
||||
if (x !== undefined && y !== undefined) {
|
||||
return eq.equals(x, y)
|
||||
}
|
||||
|
||||
return x === undefined && y === undefined
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* An Eq which compares by transforming based on a mapping function and then applying the Eq to it
|
||||
* @param map The mapping function to map values to
|
||||
* @param eq The Eq which takes the value which the map returns
|
||||
* @returns An Eq which takes the input of the mapping function
|
||||
*/
|
||||
export const mapThenEq = <A, B>(map: (x: A) => B, eq: Eq.Eq<B>): Eq.Eq<A> => ({
|
||||
equals(x: A, y: A) {
|
||||
return eq.equals(map(x), map(y))
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* An Eq which checks equality of 2 string in a case insensitive way
|
||||
*/
|
||||
export const stringCaseInsensitiveEq: Eq.Eq<string> = mapThenEq(S.toLowerCase, S.Eq)
|
||||
|
||||
/**
|
||||
* An Eq that does equality check with Lodash's isEqual function
|
||||
*/
|
||||
export const lodashIsEqualEq: Eq.Eq<any> = {
|
||||
equals(x: any, y: any) {
|
||||
return isEqual(x, y)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user