feat: collection runner (#3600)
Co-authored-by: jamesgeorge007 <25279263+jamesgeorge007@users.noreply.github.com> Co-authored-by: nivedin <nivedinp@gmail.com>
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
"homepage": "https://github.com/hoppscotch/hoppscotch#readme",
|
||||
"devDependencies": {
|
||||
"@types/lodash": "4.17.10",
|
||||
"@types/uuid": "10.0.0",
|
||||
"typescript": "5.6.3",
|
||||
"vite": "5.4.9"
|
||||
},
|
||||
@@ -44,6 +45,7 @@
|
||||
"io-ts": "2.2.21",
|
||||
"lodash": "4.17.21",
|
||||
"parser-ts": "0.7.0",
|
||||
"uuid": "10.0.0",
|
||||
"verzod": "0.2.2",
|
||||
"zod": "3.23.8"
|
||||
}
|
||||
|
||||
@@ -4,22 +4,25 @@ import V1_VERSION from "./v/1"
|
||||
import V2_VERSION from "./v/2"
|
||||
import V3_VERSION from "./v/3"
|
||||
import V4_VERSION from "./v/4"
|
||||
import V5_VERSION from "./v/5"
|
||||
|
||||
import { z } from "zod"
|
||||
import { translateToNewRequest } from "../rest"
|
||||
import { translateToGQLRequest } from "../graphql"
|
||||
import { generateUniqueRefId } from "../utils/collection"
|
||||
|
||||
const versionedObject = z.object({
|
||||
v: z.number(),
|
||||
})
|
||||
|
||||
export const HoppCollection = createVersionedEntity({
|
||||
latestVersion: 4,
|
||||
latestVersion: 5,
|
||||
versionMap: {
|
||||
1: V1_VERSION,
|
||||
2: V2_VERSION,
|
||||
3: V3_VERSION,
|
||||
4: V4_VERSION,
|
||||
5: V5_VERSION,
|
||||
},
|
||||
getVersion(data) {
|
||||
const versionCheck = versionedObject.safeParse(data)
|
||||
@@ -35,7 +38,7 @@ export const HoppCollection = createVersionedEntity({
|
||||
|
||||
export type HoppCollection = InferredEntity<typeof HoppCollection>
|
||||
|
||||
export const CollectionSchemaVersion = 4
|
||||
export const CollectionSchemaVersion = 5
|
||||
|
||||
/**
|
||||
* Generates a Collection object. This ignores the version number object
|
||||
@@ -46,6 +49,7 @@ export function makeCollection(x: Omit<HoppCollection, "v">): HoppCollection {
|
||||
return {
|
||||
v: CollectionSchemaVersion,
|
||||
...x,
|
||||
_ref_id: x._ref_id ? x._ref_id : generateUniqueRefId("coll"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +76,7 @@ export function translateToNewRESTCollection(x: any): HoppCollection {
|
||||
})
|
||||
|
||||
if (x.id) obj.id = x.id
|
||||
if (x._ref_id) obj._ref_id = x._ref_id
|
||||
|
||||
return obj
|
||||
}
|
||||
@@ -99,6 +104,7 @@ export function translateToNewGQLCollection(x: any): HoppCollection {
|
||||
})
|
||||
|
||||
if (x.id) obj.id = x.id
|
||||
if (x._ref_id) obj._ref_id = x._ref_id
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { HoppRESTAuth } from "../../rest/v/8"
|
||||
|
||||
import { V3_SCHEMA, v3_baseCollectionSchema } from "./3"
|
||||
|
||||
const v4_baseCollectionSchema = v3_baseCollectionSchema.extend({
|
||||
export const v4_baseCollectionSchema = v3_baseCollectionSchema.extend({
|
||||
v: z.literal(4),
|
||||
auth: z.union([HoppRESTAuth, HoppGQLAuth]),
|
||||
})
|
||||
@@ -19,7 +19,7 @@ type Output = z.output<typeof v4_baseCollectionSchema> & {
|
||||
folders: Output[]
|
||||
}
|
||||
|
||||
const V4_SCHEMA: z.ZodType<Output, z.ZodTypeDef, Input> =
|
||||
export const V4_SCHEMA: z.ZodType<Output, z.ZodTypeDef, Input> =
|
||||
v4_baseCollectionSchema.extend({
|
||||
folders: z.lazy(() => z.array(V4_SCHEMA)),
|
||||
})
|
||||
|
||||
36
packages/hoppscotch-data/src/collection/v/5.ts
Normal file
36
packages/hoppscotch-data/src/collection/v/5.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { defineVersion } from "verzod"
|
||||
import { z } from "zod"
|
||||
|
||||
import { V4_SCHEMA, v4_baseCollectionSchema } from "./4"
|
||||
import { generateUniqueRefId } from "../../utils/collection"
|
||||
|
||||
const v5_baseCollectionSchema = v4_baseCollectionSchema.extend({
|
||||
v: z.literal(5),
|
||||
_ref_id: z.string().optional(),
|
||||
})
|
||||
|
||||
type Input = z.input<typeof v5_baseCollectionSchema> & {
|
||||
folders: Input[]
|
||||
}
|
||||
|
||||
type Output = z.output<typeof v5_baseCollectionSchema> & {
|
||||
folders: Output[]
|
||||
}
|
||||
|
||||
const V5_SCHEMA: z.ZodType<Output, z.ZodTypeDef, Input> =
|
||||
v5_baseCollectionSchema.extend({
|
||||
folders: z.lazy(() => z.array(V5_SCHEMA)),
|
||||
})
|
||||
|
||||
export default defineVersion({
|
||||
initial: false,
|
||||
schema: V5_SCHEMA,
|
||||
// @ts-expect-error
|
||||
up(old: z.infer<typeof V4_SCHEMA>) {
|
||||
return {
|
||||
...old,
|
||||
v: 5 as const,
|
||||
_ref_id: generateUniqueRefId("coll"),
|
||||
}
|
||||
},
|
||||
})
|
||||
@@ -5,3 +5,4 @@ export * from "./rawKeyValue"
|
||||
export * from "./environment"
|
||||
export * from "./global-environment"
|
||||
export * from "./predefinedVariables"
|
||||
export * from "./utils/collection"
|
||||
|
||||
13
packages/hoppscotch-data/src/utils/collection.ts
Normal file
13
packages/hoppscotch-data/src/utils/collection.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { v4 as uuidV4 } from "uuid"
|
||||
|
||||
/**
|
||||
* Generate a unique reference ID
|
||||
* @param prefix Prefix to add to the generated ID
|
||||
* @returns The generated reference ID
|
||||
*/
|
||||
export const generateUniqueRefId = (prefix = "") => {
|
||||
const timestamp = Date.now().toString(36)
|
||||
const randomPart = uuidV4()
|
||||
|
||||
return `${prefix}_${timestamp}_${randomPart}`
|
||||
}
|
||||
Reference in New Issue
Block a user