feat(common): support importing environments individually (#3691)
This commit is contained in:
@@ -1,13 +1,24 @@
|
|||||||
import * as O from "fp-ts/Option"
|
import * as O from "fp-ts/Option"
|
||||||
import { flow } from "fp-ts/function"
|
import { flow } from "fp-ts/function"
|
||||||
|
|
||||||
|
type SafeParseJSON = {
|
||||||
|
(str: string, convertToArray: true): O.Option<Array<unknown>>
|
||||||
|
(str: string, convertToArray?: false): O.Option<Record<string, unknown>>
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks and Parses JSON string
|
* Checks and Parses JSON string
|
||||||
* @param str Raw JSON data to be parsed
|
* @param str Raw JSON data to be parsed
|
||||||
* @returns Option type with some(JSON data) or none
|
* @returns Option type with some(JSON data) or none
|
||||||
*/
|
*/
|
||||||
export const safeParseJSON = (str: string): O.Option<object> =>
|
export const safeParseJSON: SafeParseJSON = (str, convertToArray = false) =>
|
||||||
O.tryCatch(() => JSON.parse(str))
|
O.tryCatch(() => {
|
||||||
|
const data = JSON.parse(str)
|
||||||
|
if (convertToArray) {
|
||||||
|
return Array.isArray(data) ? data : [data]
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if given string is a JSON string
|
* Checks if given string is a JSON string
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ const hoppEnvSchema = z.object({
|
|||||||
})
|
})
|
||||||
|
|
||||||
export const hoppEnvImporter = (content: string) => {
|
export const hoppEnvImporter = (content: string) => {
|
||||||
const parsedContent = safeParseJSON(content)
|
const parsedContent = safeParseJSON(content, true)
|
||||||
|
|
||||||
// parse json from the environments string
|
// parse json from the environments string
|
||||||
if (O.isNone(parsedContent)) {
|
if (O.isNone(parsedContent)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user