Compare commits

...

3 Commits

Author SHA1 Message Date
jamesgeorge007
ce9ea0cbc7 chore: port fix to selfhost-desktop 2024-03-11 22:58:31 +05:30
nivedin
8844d52f5f refactor: update collection import and transform collection update 2024-03-11 22:58:31 +05:30
nivedin
772c1e65e5 fix: requests failed to upgrade version while syncing 2024-03-11 22:58:31 +05:30
5 changed files with 49 additions and 63 deletions

View File

@@ -4,11 +4,12 @@ import * as O from "fp-ts/Option"
import * as RA from "fp-ts/ReadonlyArray" import * as RA from "fp-ts/ReadonlyArray"
import * as A from "fp-ts/Array" import * as A from "fp-ts/Array"
import { translateToNewRESTCollection, HoppCollection } from "@hoppscotch/data" import { translateToNewRESTCollection, HoppCollection } from "@hoppscotch/data"
import { isPlainObject as _isPlainObject } from "lodash-es"
import { IMPORTER_INVALID_FILE_FORMAT } from "." import { IMPORTER_INVALID_FILE_FORMAT } from "."
import { safeParseJSON } from "~/helpers/functional/json" import { safeParseJSON } from "~/helpers/functional/json"
import { translateToNewGQLCollection } from "@hoppscotch/data" import { translateToNewGQLCollection } from "@hoppscotch/data"
import { entityReference } from "verzod"
import { z } from "zod"
export const hoppRESTImporter = (content: string[]) => export const hoppRESTImporter = (content: string[]) =>
pipe( pipe(
@@ -26,26 +27,14 @@ export const hoppRESTImporter = (content: string[]) =>
TE.fromOption(() => IMPORTER_INVALID_FILE_FORMAT) 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. * checks if a collection is a valid hoppscotch collection.
* else translate it into one. * else translate it into one.
*/ */
const validateCollection = (collection: unknown) => { const validateCollection = (collection: unknown) => {
if (isValidCollection(collection)) { const result = entityReference(HoppCollection).safeParse(collection)
return O.some(collection) if (result.success) return O.some(result.data)
}
return O.some(translateToNewRESTCollection(collection)) 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 * @returns the collection if it is valid, else a translated version of the collection
*/ */
export const validateGQLCollection = (collection: unknown) => { export const validateGQLCollection = (collection: unknown) => {
if (isValidCollection(collection)) { const result = z.array(entityReference(HoppCollection)).safeParse(collection)
return O.some(collection)
} if (result.success) return O.some(result.data)
return O.some(translateToNewGQLCollection(collection)) return O.some(translateToNewGQLCollection(collection))
} }

View File

@@ -52,8 +52,6 @@ export function makeCollection(x: Omit<HoppCollection, "v">): HoppCollection {
* @returns The proper new collection format * @returns The proper new collection format
*/ */
export function translateToNewRESTCollection(x: any): HoppCollection { export function translateToNewRESTCollection(x: any): HoppCollection {
if (x.v && x.v === CollectionSchemaVersion) return x
// Legacy // Legacy
const name = x.name ?? "Untitled" const name = x.name ?? "Untitled"
const folders = (x.folders ?? []).map(translateToNewRESTCollection) const folders = (x.folders ?? []).map(translateToNewRESTCollection)
@@ -81,8 +79,6 @@ export function translateToNewRESTCollection(x: any): HoppCollection {
* @returns The proper new collection format * @returns The proper new collection format
*/ */
export function translateToNewGQLCollection(x: any): HoppCollection { export function translateToNewGQLCollection(x: any): HoppCollection {
if (x.v && x.v === CollectionSchemaVersion) return x
// Legacy // Legacy
const name = x.name ?? "Untitled" const name = x.name ?? "Untitled"
const folders = (x.folders ?? []).map(translateToNewGQLCollection) const folders = (x.folders ?? []).map(translateToNewGQLCollection)

View File

@@ -18,22 +18,8 @@ export const HoppRESTRequestVariables = z.array(
export type HoppRESTRequestVariables = z.infer<typeof HoppRESTRequestVariables> export type HoppRESTRequestVariables = z.infer<typeof HoppRESTRequestVariables>
const V2_SCHEMA = z.object({ const V2_SCHEMA = V1_SCHEMA.extend({
v: z.literal("2"), 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, requestVariables: HoppRESTRequestVariables,
}) })

View File

@@ -112,10 +112,15 @@ function exportedCollectionToHoppCollection(
folders: restCollection.folders.map((folder) => folders: restCollection.folders.map((folder) =>
exportedCollectionToHoppCollection(folder, collectionType) exportedCollectionToHoppCollection(folder, collectionType)
), ),
requests: restCollection.requests.map( requests: restCollection.requests.map((request) => {
({ const requestParsedResult = HoppRESTRequest.safeParse(request)
id, if (requestParsedResult.type === "ok") {
return requestParsedResult.value
}
const {
v, v,
id,
auth, auth,
body, body,
endpoint, endpoint,
@@ -125,20 +130,23 @@ function exportedCollectionToHoppCollection(
params, params,
preRequestScript, preRequestScript,
testScript, testScript,
}) => ({ requestVariables,
id, } = request
return {
v, v,
auth, id,
body,
endpoint,
headers,
method,
name, name,
endpoint,
method,
params, params,
requestVariables: requestVariables,
auth,
headers,
body,
preRequestScript, preRequestScript,
testScript, testScript,
}) }
), }),
} }
} else { } else {
const gqlCollection = collection as ExportedUserCollectionGQL const gqlCollection = collection as ExportedUserCollectionGQL

View File

@@ -120,10 +120,15 @@ function exportedCollectionToHoppCollection(
folders: restCollection.folders.map((folder) => folders: restCollection.folders.map((folder) =>
exportedCollectionToHoppCollection(folder, collectionType) exportedCollectionToHoppCollection(folder, collectionType)
), ),
requests: restCollection.requests.map( requests: restCollection.requests.map((request) => {
({ const requestParsedResult = HoppRESTRequest.safeParse(request)
id, if (requestParsedResult.type === "ok") {
return requestParsedResult.value
}
const {
v, v,
id,
auth, auth,
body, body,
endpoint, endpoint,
@@ -134,21 +139,22 @@ function exportedCollectionToHoppCollection(
preRequestScript, preRequestScript,
testScript, testScript,
requestVariables, requestVariables,
}) => ({ } = request
id, return {
v, v,
auth, id,
body,
endpoint,
headers,
method,
name, name,
endpoint,
method,
params, params,
requestVariables: requestVariables,
auth,
headers,
body,
preRequestScript, preRequestScript,
testScript, testScript,
requestVariables, }
}) }),
),
auth: data.auth, auth: data.auth,
headers: data.headers, headers: data.headers,
} }