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:
Anwarul Islam
2024-11-26 16:26:09 +06:00
committed by GitHub
parent f091c1bdc5
commit e8ed938b4c
66 changed files with 3201 additions and 490 deletions

View File

@@ -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
}

View File

@@ -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)),
})

View 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"),
}
},
})

View File

@@ -5,3 +5,4 @@ export * from "./rawKeyValue"
export * from "./environment"
export * from "./global-environment"
export * from "./predefinedVariables"
export * from "./utils/collection"

View 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}`
}