utils.ts (hoppscotch-2.2.1) | : | utils.ts (hoppscotch-3.0.0) | ||
---|---|---|---|---|
import { Either, left, right } from "fp-ts/lib/Either" | import * as O from "fp-ts/Option" | |||
import * as E from "fp-ts/Either" | ||||
import * as QuickJS from "quickjs-emscripten" | import * as QuickJS from "quickjs-emscripten" | |||
import { TestResult } from "./test-runner" | ||||
export function marshalObjectToVM( | export function marshalObjectToVM( | |||
vm: QuickJS.QuickJSVm, | vm: QuickJS.QuickJSVm, | |||
obj: object | obj: object | |||
): Either<string, QuickJS.QuickJSHandle> { | ): E.Either<string, QuickJS.QuickJSHandle> { | |||
let jsonString | let jsonString | |||
try { | try { | |||
jsonString = JSON.stringify(obj) | jsonString = JSON.stringify(obj) | |||
} catch (e) { | } catch (e) { | |||
return left("Marshaling stringification failed") | return E.left("Marshaling stringification failed") | |||
} | } | |||
const vmStringHandle = vm.newString(jsonString) | const vmStringHandle = vm.newString(jsonString) | |||
const jsonHandle = vm.getProp(vm.global, "JSON") | const jsonHandle = vm.getProp(vm.global, "JSON") | |||
const parseFuncHandle = vm.getProp(jsonHandle, "parse") | const parseFuncHandle = vm.getProp(jsonHandle, "parse") | |||
const parseResultHandle = vm.callFunction( | const parseResultHandle = vm.callFunction( | |||
parseFuncHandle, | parseFuncHandle, | |||
vm.undefined, | vm.undefined, | |||
vmStringHandle | vmStringHandle | |||
) | ) | |||
if (parseResultHandle.error) { | if (parseResultHandle.error) { | |||
parseResultHandle.error.dispose() | parseResultHandle.error.dispose() | |||
return left("Marshaling failed") | return E.left("Marshaling failed") | |||
} | } | |||
const resultHandle = vm.unwrapResult(parseResultHandle) | const resultHandle = vm.unwrapResult(parseResultHandle) | |||
vmStringHandle.dispose() | vmStringHandle.dispose() | |||
parseFuncHandle.dispose() | parseFuncHandle.dispose() | |||
jsonHandle.dispose() | jsonHandle.dispose() | |||
return right(resultHandle) | return E.right(resultHandle) | |||
} | ||||
export function getEnv(envName: string, envs: TestResult["envs"]) { | ||||
return O.fromNullable( | ||||
envs.selected.find((x) => x.key === envName) ?? | ||||
envs.global.find((x) => x.key === envName) | ||||
) | ||||
} | ||||
export function setEnv( | ||||
envName: string, | ||||
envValue: string, | ||||
envs: TestResult["envs"] | ||||
): TestResult["envs"] { | ||||
const indexInSelected = envs.selected.findIndex((x) => x.key === envName) | ||||
// Found the match in selected | ||||
if (indexInSelected >= 0) { | ||||
envs.selected[indexInSelected].value = envValue | ||||
return { | ||||
global: envs.global, | ||||
selected: envs.selected, | ||||
} | ||||
} | ||||
const indexInGlobal = envs.global.findIndex((x) => x.key == envName) | ||||
// Found a match in globals | ||||
if (indexInGlobal >= 0) { | ||||
envs.global[indexInGlobal].value = envValue | ||||
return { | ||||
global: envs.global, | ||||
selected: envs.selected, | ||||
} | ||||
} | ||||
// Didn't find in both places, create a new variable in selected | ||||
envs.selected.push({ | ||||
key: envName, | ||||
value: envValue, | ||||
}) | ||||
return { | ||||
global: envs.global, | ||||
selected: envs.selected, | ||||
} | ||||
} | } | |||
End of changes. 6 change blocks. | ||||
5 lines changed or deleted | 55 lines changed or added |