Compare commits
3 Commits
release/20
...
hotfix/req
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce9ea0cbc7 | ||
|
|
8844d52f5f | ||
|
|
772c1e65e5 |
@@ -4,11 +4,12 @@ import * as O from "fp-ts/Option"
|
||||
import * as RA from "fp-ts/ReadonlyArray"
|
||||
import * as A from "fp-ts/Array"
|
||||
import { translateToNewRESTCollection, HoppCollection } from "@hoppscotch/data"
|
||||
import { isPlainObject as _isPlainObject } from "lodash-es"
|
||||
|
||||
import { IMPORTER_INVALID_FILE_FORMAT } from "."
|
||||
import { safeParseJSON } from "~/helpers/functional/json"
|
||||
import { translateToNewGQLCollection } from "@hoppscotch/data"
|
||||
import { entityReference } from "verzod"
|
||||
import { z } from "zod"
|
||||
|
||||
export const hoppRESTImporter = (content: string[]) =>
|
||||
pipe(
|
||||
@@ -26,26 +27,14 @@ export const hoppRESTImporter = (content: string[]) =>
|
||||
TE.fromOption(() => IMPORTER_INVALID_FILE_FORMAT)
|
||||
)
|
||||
|
||||
/**
|
||||
* checks if a value is a plain object
|
||||
*/
|
||||
const isPlainObject = (value: any): value is object => _isPlainObject(value)
|
||||
|
||||
/**
|
||||
* checks if a collection matches the schema for a hoppscotch collection.
|
||||
* here 2 is the latest version of the schema.
|
||||
*/
|
||||
const isValidCollection = (collection: unknown): collection is HoppCollection =>
|
||||
isPlainObject(collection) && "v" in collection && collection.v === 2
|
||||
|
||||
/**
|
||||
* checks if a collection is a valid hoppscotch collection.
|
||||
* else translate it into one.
|
||||
*/
|
||||
const validateCollection = (collection: unknown) => {
|
||||
if (isValidCollection(collection)) {
|
||||
return O.some(collection)
|
||||
}
|
||||
const result = entityReference(HoppCollection).safeParse(collection)
|
||||
if (result.success) return O.some(result.data)
|
||||
|
||||
return O.some(translateToNewRESTCollection(collection))
|
||||
}
|
||||
|
||||
@@ -75,8 +64,9 @@ export const hoppGQLImporter = (content: string) =>
|
||||
* @returns the collection if it is valid, else a translated version of the collection
|
||||
*/
|
||||
export const validateGQLCollection = (collection: unknown) => {
|
||||
if (isValidCollection(collection)) {
|
||||
return O.some(collection)
|
||||
}
|
||||
const result = z.array(entityReference(HoppCollection)).safeParse(collection)
|
||||
|
||||
if (result.success) return O.some(result.data)
|
||||
|
||||
return O.some(translateToNewGQLCollection(collection))
|
||||
}
|
||||
|
||||
@@ -52,8 +52,6 @@ export function makeCollection(x: Omit<HoppCollection, "v">): HoppCollection {
|
||||
* @returns The proper new collection format
|
||||
*/
|
||||
export function translateToNewRESTCollection(x: any): HoppCollection {
|
||||
if (x.v && x.v === CollectionSchemaVersion) return x
|
||||
|
||||
// Legacy
|
||||
const name = x.name ?? "Untitled"
|
||||
const folders = (x.folders ?? []).map(translateToNewRESTCollection)
|
||||
@@ -81,8 +79,6 @@ export function translateToNewRESTCollection(x: any): HoppCollection {
|
||||
* @returns The proper new collection format
|
||||
*/
|
||||
export function translateToNewGQLCollection(x: any): HoppCollection {
|
||||
if (x.v && x.v === CollectionSchemaVersion) return x
|
||||
|
||||
// Legacy
|
||||
const name = x.name ?? "Untitled"
|
||||
const folders = (x.folders ?? []).map(translateToNewGQLCollection)
|
||||
|
||||
@@ -18,22 +18,8 @@ export const HoppRESTRequestVariables = z.array(
|
||||
|
||||
export type HoppRESTRequestVariables = z.infer<typeof HoppRESTRequestVariables>
|
||||
|
||||
const V2_SCHEMA = z.object({
|
||||
const V2_SCHEMA = V1_SCHEMA.extend({
|
||||
v: z.literal("2"),
|
||||
id: z.optional(z.string()), // Firebase Firestore ID
|
||||
|
||||
name: z.string(),
|
||||
method: z.string(),
|
||||
endpoint: z.string(),
|
||||
params: HoppRESTParams,
|
||||
headers: HoppRESTHeaders,
|
||||
preRequestScript: z.string().catch(""),
|
||||
testScript: z.string().catch(""),
|
||||
|
||||
auth: HoppRESTAuth,
|
||||
|
||||
body: HoppRESTReqBody,
|
||||
|
||||
requestVariables: HoppRESTRequestVariables,
|
||||
})
|
||||
|
||||
|
||||
@@ -112,10 +112,15 @@ function exportedCollectionToHoppCollection(
|
||||
folders: restCollection.folders.map((folder) =>
|
||||
exportedCollectionToHoppCollection(folder, collectionType)
|
||||
),
|
||||
requests: restCollection.requests.map(
|
||||
({
|
||||
id,
|
||||
requests: restCollection.requests.map((request) => {
|
||||
const requestParsedResult = HoppRESTRequest.safeParse(request)
|
||||
if (requestParsedResult.type === "ok") {
|
||||
return requestParsedResult.value
|
||||
}
|
||||
|
||||
const {
|
||||
v,
|
||||
id,
|
||||
auth,
|
||||
body,
|
||||
endpoint,
|
||||
@@ -125,20 +130,23 @@ function exportedCollectionToHoppCollection(
|
||||
params,
|
||||
preRequestScript,
|
||||
testScript,
|
||||
}) => ({
|
||||
id,
|
||||
requestVariables,
|
||||
} = request
|
||||
return {
|
||||
v,
|
||||
auth,
|
||||
body,
|
||||
endpoint,
|
||||
headers,
|
||||
method,
|
||||
id,
|
||||
name,
|
||||
endpoint,
|
||||
method,
|
||||
params,
|
||||
requestVariables: requestVariables,
|
||||
auth,
|
||||
headers,
|
||||
body,
|
||||
preRequestScript,
|
||||
testScript,
|
||||
})
|
||||
),
|
||||
}
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
const gqlCollection = collection as ExportedUserCollectionGQL
|
||||
|
||||
@@ -120,10 +120,15 @@ function exportedCollectionToHoppCollection(
|
||||
folders: restCollection.folders.map((folder) =>
|
||||
exportedCollectionToHoppCollection(folder, collectionType)
|
||||
),
|
||||
requests: restCollection.requests.map(
|
||||
({
|
||||
id,
|
||||
requests: restCollection.requests.map((request) => {
|
||||
const requestParsedResult = HoppRESTRequest.safeParse(request)
|
||||
if (requestParsedResult.type === "ok") {
|
||||
return requestParsedResult.value
|
||||
}
|
||||
|
||||
const {
|
||||
v,
|
||||
id,
|
||||
auth,
|
||||
body,
|
||||
endpoint,
|
||||
@@ -134,21 +139,22 @@ function exportedCollectionToHoppCollection(
|
||||
preRequestScript,
|
||||
testScript,
|
||||
requestVariables,
|
||||
}) => ({
|
||||
id,
|
||||
} = request
|
||||
return {
|
||||
v,
|
||||
auth,
|
||||
body,
|
||||
endpoint,
|
||||
headers,
|
||||
method,
|
||||
id,
|
||||
name,
|
||||
endpoint,
|
||||
method,
|
||||
params,
|
||||
requestVariables: requestVariables,
|
||||
auth,
|
||||
headers,
|
||||
body,
|
||||
preRequestScript,
|
||||
testScript,
|
||||
requestVariables,
|
||||
})
|
||||
),
|
||||
}
|
||||
}),
|
||||
auth: data.auth,
|
||||
headers: data.headers,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user