Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

refactor: move collection data structures and utility functions to hopp/data

Andrew Bastin5043606701c8
diff --git a/packages/hoppscotch-app/components/collections/ImportExport.vue b/packages/hoppscotch-app/components/collections/ImportExport.vue
index d6d5895d8..1f6d4a05c 100644
--- a/packages/hoppscotch-app/components/collections/ImportExport.vue
+++ b/packages/hoppscotch-app/components/collections/ImportExport.vue
@@ -164,7 +164,7 @@
<script setup lang="ts">
import { computed, ref, watch } from "@nuxtjs/composition-api"
import * as E from "fp-ts/Either"
-import { HoppRESTRequest } from "@hoppscotch/data"
+import { HoppRESTRequest, HoppCollection } from "@hoppscotch/data"
import { apolloClient } from "~/helpers/apollo"
import {
useAxios,
@@ -174,11 +174,7 @@ import {
} from "~/helpers/utils/composables"
import { currentUser$ } from "~/helpers/fb/auth"
import * as teamUtils from "~/helpers/teams/utils"
-import {
- appendRESTCollections,
- Collection,
- restCollections$,
-} from "~/newstore/collections"
+import { appendRESTCollections, restCollections$ } from "~/newstore/collections"
import { RESTCollectionImporters } from "~/helpers/import-export/import/importers"
import { StepReturnValue } from "~/helpers/import-export/steps"
@@ -285,7 +281,7 @@ watch(mySelectedCollectionID, (newValue) => {
const importingMyCollections = ref(false)
-const importToTeams = async (content: Collection<HoppRESTRequest>) => {
+const importToTeams = async (content: HoppCollection<HoppRESTRequest>) => {
importingMyCollections.value = true
if (props.collectionsType.type !== "team-collections") return
await teamUtils
diff --git a/packages/hoppscotch-app/components/collections/graphql/Add.vue b/packages/hoppscotch-app/components/collections/graphql/Add.vue
index 4fbfcc3e6..37a5dd612 100644
--- a/packages/hoppscotch-app/components/collections/graphql/Add.vue
+++ b/packages/hoppscotch-app/components/collections/graphql/Add.vue
@@ -34,8 +34,8 @@
<script lang="ts">
import { defineComponent } from "@nuxtjs/composition-api"
-import { HoppGQLRequest } from "@hoppscotch/data"
-import { addGraphqlCollection, makeCollection } from "~/newstore/collections"
+import { HoppGQLRequest, makeCollection } from "@hoppscotch/data"
+import { addGraphqlCollection } from "~/newstore/collections"
export default defineComponent({
props: {
diff --git a/packages/hoppscotch-app/helpers/fb/collections.ts b/packages/hoppscotch-app/helpers/fb/collections.ts
index 5c4d45f00..b116116b4 100644
--- a/packages/hoppscotch-app/helpers/fb/collections.ts
+++ b/packages/hoppscotch-app/helpers/fb/collections.ts
@@ -5,14 +5,16 @@ import {
onSnapshot,
setDoc,
} from "firebase/firestore"
+import {
+ translateToNewRESTCollection,
+ translateToNewGQLCollection,
+} from "@hoppscotch/data"
import { currentUser$ } from "./auth"
import {
restCollections$,
graphqlCollections$,
setRESTCollections,
setGraphqlCollections,
- translateToNewRESTCollection,
- translateToNewGQLCollection,
} from "~/newstore/collections"
import { settingsStore } from "~/newstore/settings"
diff --git a/packages/hoppscotch-app/helpers/import-export/export/hopp.ts b/packages/hoppscotch-app/helpers/import-export/export/hopp.ts
index 900079412..1e74d6bfe 100644
--- a/packages/hoppscotch-app/helpers/import-export/export/hopp.ts
+++ b/packages/hoppscotch-app/helpers/import-export/export/hopp.ts
@@ -1,10 +1,9 @@
-import { HoppRESTRequest } from "@hoppscotch/data"
+import { HoppRESTRequest, HoppCollection } from "@hoppscotch/data"
import { pipe } from "fp-ts/function"
import * as TE from "fp-ts/TaskEither"
import { HoppExporter } from "."
-import { Collection } from "~/newstore/collections"
-const exporter: HoppExporter<Collection<HoppRESTRequest>> = (content) =>
+const exporter: HoppExporter<HoppCollection<HoppRESTRequest>> = (content) =>
pipe(content, JSON.stringify, TE.right)
export default exporter
diff --git a/packages/hoppscotch-app/helpers/import-export/export/index.ts b/packages/hoppscotch-app/helpers/import-export/export/index.ts
index 058fa57b4..1bf7eb882 100644
--- a/packages/hoppscotch-app/helpers/import-export/export/index.ts
+++ b/packages/hoppscotch-app/helpers/import-export/export/index.ts
@@ -1,6 +1,5 @@
import * as TE from "fp-ts/TaskEither"
-import { HoppRESTRequest } from "@hoppscotch/data"
-import { Collection } from "~/newstore/collections"
+import { HoppRESTRequest, HoppCollection } from "@hoppscotch/data"
export type HoppExporter<T> = (content: T) => TE.TaskEither<string, string>
@@ -10,7 +9,7 @@ export type HoppExporterDefintion<T> = {
}
export const RESTCollectionExporters: HoppExporterDefintion<
- Collection<HoppRESTRequest>
+ HoppCollection<HoppRESTRequest>
>[] = [
{
name: "Hoppscotch REST Collection JSON",
diff --git a/packages/hoppscotch-app/helpers/import-export/import/gist.ts b/packages/hoppscotch-app/helpers/import-export/import/gist.ts
index 36ecf4ddc..709ce45fa 100644
--- a/packages/hoppscotch-app/helpers/import-export/import/gist.ts
+++ b/packages/hoppscotch-app/helpers/import-export/import/gist.ts
@@ -3,13 +3,14 @@ import * as TE from "fp-ts/TaskEither"
import * as TO from "fp-ts/TaskOption"
import * as O from "fp-ts/Option"
import axios from "axios"
-import { HoppRESTRequest } from "@hoppscotch/data"
+import { HoppRESTRequest, HoppCollection } from "@hoppscotch/data"
import { step } from "../steps"
import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "."
-import { Collection } from "~/newstore/collections"
// TODO: Add validation to output
-const fetchGist = (url: string): TO.TaskOption<Collection<HoppRESTRequest>> =>
+const fetchGist = (
+ url: string
+): TO.TaskOption<HoppCollection<HoppRESTRequest>> =>
pipe(
TO.tryCatch(() =>
axios.get(`https://api.github.com/gists/${url.split("/").pop()}`, {
diff --git a/packages/hoppscotch-app/helpers/import-export/import/hopp.ts b/packages/hoppscotch-app/helpers/import-export/import/hopp.ts
index 14f831a53..551799560 100644
--- a/packages/hoppscotch-app/helpers/import-export/import/hopp.ts
+++ b/packages/hoppscotch-app/helpers/import-export/import/hopp.ts
@@ -1,9 +1,9 @@
import { pipe } from "fp-ts/function"
import * as TE from "fp-ts/TaskEither"
import * as E from "fp-ts/Either"
+import { translateToNewRESTCollection } from "@hoppscotch/data"
import { step } from "../steps"
import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "."
-import { translateToNewRESTCollection } from "~/newstore/collections"
export default defineImporter({
name: "import.from_json",
diff --git a/packages/hoppscotch-app/helpers/import-export/import/insomnia.ts b/packages/hoppscotch-app/helpers/import-export/import/insomnia.ts
index 33083a6c2..a5280e4e5 100644
--- a/packages/hoppscotch-app/helpers/import-export/import/insomnia.ts
+++ b/packages/hoppscotch-app/helpers/import-export/import/insomnia.ts
@@ -8,6 +8,8 @@ import {
HoppRESTRequest,
knownContentTypes,
makeRESTRequest,
+ HoppCollection,
+ makeCollection,
} from "@hoppscotch/data"
import * as A from "fp-ts/Array"
import * as S from "fp-ts/string"
@@ -15,7 +17,6 @@ import * as TO from "fp-ts/TaskOption"
import * as TE from "fp-ts/TaskEither"
import { step } from "../steps"
import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "."
-import { Collection, makeCollection } from "~/newstore/collections"
// TODO: Insomnia allows custom prefixes for Bearer token auth, Hoppscotch doesn't. We just ignore the prefix for now
@@ -194,7 +195,7 @@ const getHoppRequest = (req: InsomniaRequestResource): HoppRESTRequest =>
const getHoppFolder = (
folderRes: InsomniaFolderResource,
resources: InsomniaResource[]
-): Collection<HoppRESTRequest> =>
+): HoppCollection<HoppRESTRequest> =>
makeCollection({
name: folderRes.name ?? "",
folders: getFoldersIn(folderRes, resources).map((f) =>
diff --git a/packages/hoppscotch-app/helpers/import-export/import/openapi.ts b/packages/hoppscotch-app/helpers/import-export/import/openapi.ts
index 53721fa80..6d76c4e26 100644
--- a/packages/hoppscotch-app/helpers/import-export/import/openapi.ts
+++ b/packages/hoppscotch-app/helpers/import-export/import/openapi.ts
@@ -15,6 +15,8 @@ import {
HoppRESTRequest,
knownContentTypes,
makeRESTRequest,
+ HoppCollection,
+ makeCollection,
} from "@hoppscotch/data"
import { pipe, flow } from "fp-ts/function"
import * as A from "fp-ts/Array"
@@ -24,7 +26,6 @@ import * as TE from "fp-ts/TaskEither"
import * as RA from "fp-ts/ReadonlyArray"
import { step } from "../steps"
import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "."
-import { Collection, makeCollection } from "~/newstore/collections"
const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const
@@ -558,7 +559,7 @@ const convertPathToHoppReqs = (
const convertOpenApiDocToHopp = (
doc: OpenAPI.Document
-): TE.TaskEither<never, Collection<HoppRESTRequest>[]> => {
+): TE.TaskEither<never, HoppCollection<HoppRESTRequest>[]> => {
const name = doc.info.title
const paths = Object.entries(doc.paths ?? {})
diff --git a/packages/hoppscotch-app/helpers/import-export/import/postman.ts b/packages/hoppscotch-app/helpers/import-export/import/postman.ts
index 2de9659aa..96f465854 100644
--- a/packages/hoppscotch-app/helpers/import-export/import/postman.ts
+++ b/packages/hoppscotch-app/helpers/import-export/import/postman.ts
@@ -14,6 +14,8 @@ import {
HoppRESTReqBody,
HoppRESTRequest,
makeRESTRequest,
+ HoppCollection,
+ makeCollection,
} from "@hoppscotch/data"
import { pipe, flow } from "fp-ts/function"
import * as S from "fp-ts/string"
@@ -22,7 +24,6 @@ import * as O from "fp-ts/Option"
import * as TE from "fp-ts/TaskEither"
import { step } from "../steps"
import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "."
-import { Collection, makeCollection } from "~/newstore/collections"
const safeParseJSON = (jsonStr: string) => O.tryCatch(() => JSON.parse(jsonStr))
@@ -226,7 +227,7 @@ const getHoppRequest = (item: Item): HoppRESTRequest => {
})
}
-const getHoppFolder = (ig: ItemGroup<Item>): Collection<HoppRESTRequest> =>
+const getHoppFolder = (ig: ItemGroup<Item>): HoppCollection<HoppRESTRequest> =>
makeCollection({
name: ig.name,
folders: pipe(
diff --git a/packages/hoppscotch-app/newstore/collections.ts b/packages/hoppscotch-app/newstore/collections.ts
index cb1fe6009..3ed853234 100644
--- a/packages/hoppscotch-app/newstore/collections.ts
+++ b/packages/hoppscotch-app/newstore/collections.ts
@@ -1,22 +1,13 @@
import { pluck } from "rxjs/operators"
import {
HoppGQLRequest,
- translateToGQLRequest,
HoppRESTRequest,
- translateToNewRequest,
+ HoppCollection,
+ makeCollection,
} from "@hoppscotch/data"
import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
import { getRESTSaveContext, setRESTSaveContext } from "./RESTSession"
-export interface Collection<T extends HoppRESTRequest | HoppGQLRequest> {
- v: number
- name: string
- folders: Collection<T>[]
- requests: T[]
-
- id?: string // For Firestore ID
-}
-
const defaultRESTCollectionState = {
state: [
makeCollection<HoppRESTRequest>({
@@ -37,62 +28,11 @@ const defaultGraphqlCollectionState = {
],
}
-export function makeCollection<T extends HoppRESTRequest | HoppGQLRequest>(
- x: Omit<Collection<T>, "v">
-): Collection<T> {
- return {
- v: 1,
- ...x,
- }
-}
-
-export function translateToNewRESTCollection(
- x: any
-): Collection<HoppRESTRequest> {
- if (x.v && x.v === 1) return x
-
- // Legacy
- const name = x.name ?? "Untitled"
- const folders = (x.folders ?? []).map(translateToNewRESTCollection)
- const requests = (x.requests ?? []).map(translateToNewRequest)
-
- const obj = makeCollection<HoppRESTRequest>({
- name,
- folders,
- requests,
- })
-
- if (x.id) obj.id = x.id
-
- return obj
-}
-
-export function translateToNewGQLCollection(
- x: any
-): Collection<HoppGQLRequest> {
- if (x.v && x.v === 1) return x
-
- // Legacy
- const name = x.name ?? "Untitled"
- const folders = (x.folders ?? []).map(translateToNewGQLCollection)
- const requests = (x.requests ?? []).map(translateToGQLRequest)
-
- const obj = makeCollection<HoppGQLRequest>({
- name,
- folders,
- requests,
- })
-
- if (x.id) obj.id = x.id
-
- return obj
-}
-
type RESTCollectionStoreType = typeof defaultRESTCollectionState
type GraphqlCollectionStoreType = typeof defaultGraphqlCollectionState
function navigateToFolderWithIndexPath(
- collections: Collection<HoppRESTRequest | HoppGQLRequest>[],
+ collections: HoppCollection<HoppRESTRequest | HoppGQLRequest>[],
indexPaths: number[]
) {
if (indexPaths.length === 0) return null
@@ -108,7 +48,7 @@ function navigateToFolderWithIndexPath(
const restCollectionDispatchers = defineDispatchers({
setCollections(
_: RESTCollectionStoreType,
- { entries }: { entries: Collection<HoppRESTRequest>[] }
+ { entries }: { entries: HoppCollection<HoppRESTRequest>[] }
) {
return {
state: entries,
@@ -117,7 +57,7 @@ const restCollectionDispatchers = defineDispatchers({
appendCollections(
{ state }: RESTCollectionStoreType,
- { entries }: { entries: Collection<HoppRESTRequest>[] }
+ { entries }: { entries: HoppCollection<HoppRESTRequest>[] }
) {
return {
state: [...state, ...entries],
@@ -126,7 +66,7 @@ const restCollectionDispatchers = defineDispatchers({
addCollection(
{ state }: RESTCollectionStoreType,
- { collection }: { collection: Collection<any> }
+ { collection }: { collection: HoppCollection<any> }
) {
return {
state: [...state, collection],
@@ -149,7 +89,7 @@ const restCollectionDispatchers = defineDispatchers({
{
collectionIndex,
collection,
- }: { collectionIndex: number; collection: Collection<any> }
+ }: { collectionIndex: number; collection: HoppCollection<any> }
) {
return {
state: state.map((col, index) =>
@@ -162,7 +102,7 @@ const restCollectionDispatchers = defineDispatchers({
{ state }: RESTCollectionStoreType,
{ name, path }: { name: string; path: string }
) {
- const newFolder: Collection<HoppRESTRequest> = makeCollection({
+ const newFolder: HoppCollection<HoppRESTRequest> = makeCollection({
name,
folders: [],
requests: [],
@@ -374,7 +314,7 @@ const restCollectionDispatchers = defineDispatchers({
const gqlCollectionDispatchers = defineDispatchers({
setCollections(
_: GraphqlCollectionStoreType,
- { entries }: { entries: Collection<any>[] }
+ { entries }: { entries: HoppCollection<any>[] }
) {
return {
state: entries,
@@ -383,7 +323,7 @@ const gqlCollectionDispatchers = defineDispatchers({
appendCollections(
{ state }: GraphqlCollectionStoreType,
- { entries }: { entries: Collection<any>[] }
+ { entries }: { entries: HoppCollection<any>[] }
) {
return {
state: [...state, ...entries],
@@ -392,7 +332,7 @@ const gqlCollectionDispatchers = defineDispatchers({
addCollection(
{ state }: GraphqlCollectionStoreType,
- { collection }: { collection: Collection<any> }
+ { collection }: { collection: HoppCollection<any> }
) {
return {
state: [...state, collection],
@@ -415,7 +355,7 @@ const gqlCollectionDispatchers = defineDispatchers({
{
collectionIndex,
collection,
- }: { collectionIndex: number; collection: Collection<any> }
+ }: { collectionIndex: number; collection: HoppCollection<any> }
) {
return {
state: state.map((col, index) =>
@@ -428,7 +368,7 @@ const gqlCollectionDispatchers = defineDispatchers({
{ state }: GraphqlCollectionStoreType,
{ name, path }: { name: string; path: string }
) {
- const newFolder: Collection<HoppGQLRequest> = makeCollection({
+ const newFolder: HoppCollection<HoppGQLRequest> = makeCollection({
name,
folders: [],
requests: [],
@@ -650,7 +590,7 @@ export const graphqlCollectionStore = new DispatchingStore(
gqlCollectionDispatchers
)
-export function setRESTCollections(entries: Collection<HoppRESTRequest>[]) {
+export function setRESTCollections(entries: HoppCollection<HoppRESTRequest>[]) {
restCollectionStore.dispatch({
dispatcher: "setCollections",
payload: {
@@ -667,7 +607,9 @@ export const graphqlCollections$ = graphqlCollectionStore.subject$.pipe(
pluck("state")
)
-export function appendRESTCollections(entries: Collection<HoppRESTRequest>[]) {
+export function appendRESTCollections(
+ entries: HoppCollection<HoppRESTRequest>[]
+) {
restCollectionStore.dispatch({
dispatcher: "appendCollections",
payload: {
@@ -676,7 +618,7 @@ export function appendRESTCollections(entries: Collection<HoppRESTRequest>[]) {
})
}
-export function addRESTCollection(collection: Collection<HoppRESTRequest>) {
+export function addRESTCollection(collection: HoppCollection<HoppRESTRequest>) {
restCollectionStore.dispatch({
dispatcher: "addCollection",
payload: {
@@ -700,7 +642,7 @@ export function getRESTCollection(collectionIndex: number) {
export function editRESTCollection(
collectionIndex: number,
- collection: Collection<HoppRESTRequest>
+ collection: HoppCollection<HoppRESTRequest>
) {
restCollectionStore.dispatch({
dispatcher: "editCollection",
@@ -723,7 +665,7 @@ export function addRESTFolder(name: string, path: string) {
export function editRESTFolder(
path: string,
- folder: Collection<HoppRESTRequest>
+ folder: HoppCollection<HoppRESTRequest>
) {
restCollectionStore.dispatch({
dispatcher: "editFolder",
@@ -809,7 +751,9 @@ export function moveRESTRequest(
})
}
-export function setGraphqlCollections(entries: Collection<HoppGQLRequest>[]) {
+export function setGraphqlCollections(
+ entries: HoppCollection<HoppGQLRequest>[]
+) {
graphqlCollectionStore.dispatch({
dispatcher: "setCollections",
payload: {
@@ -819,7 +763,7 @@ export function setGraphqlCollections(entries: Collection<HoppGQLRequest>[]) {
}
export function appendGraphqlCollections(
- entries: Collection<HoppGQLRequest>[]
+ entries: HoppCollection<HoppGQLRequest>[]
) {
graphqlCollectionStore.dispatch({
dispatcher: "appendCollections",
@@ -829,7 +773,9 @@ export function appendGraphqlCollections(
})
}
-export function addGraphqlCollection(collection: Collection<HoppGQLRequest>) {
+export function addGraphqlCollection(
+ collection: HoppCollection<HoppGQLRequest>
+) {
graphqlCollectionStore.dispatch({
dispatcher: "addCollection",
payload: {
@@ -849,7 +795,7 @@ export function removeGraphqlCollection(collectionIndex: number) {
export function editGraphqlCollection(
collectionIndex: number,
- collection: Collection<HoppGQLRequest>
+ collection: HoppCollection<HoppGQLRequest>
) {
graphqlCollectionStore.dispatch({
dispatcher: "editCollection",
@@ -872,7 +818,7 @@ export function addGraphqlFolder(name: string, path: string) {
export function editGraphqlFolder(
path: string,
- folder: Collection<HoppGQLRequest>
+ folder: HoppCollection<HoppGQLRequest>
) {
graphqlCollectionStore.dispatch({
dispatcher: "editFolder",
diff --git a/packages/hoppscotch-app/newstore/localpersistence.ts b/packages/hoppscotch-app/newstore/localpersistence.ts
index 77cfd2e7f..0edcefd81 100644
--- a/packages/hoppscotch-app/newstore/localpersistence.ts
+++ b/packages/hoppscotch-app/newstore/localpersistence.ts
@@ -8,6 +8,8 @@ import { pipe } from "fp-ts/function"
import {
safelyExtractRESTRequest,
translateToNewRequest,
+ translateToNewRESTCollection,
+ translateToNewGQLCollection,
} from "@hoppscotch/data"
import { cloneDeep } from "lodash"
import {
@@ -31,8 +33,6 @@ import {
graphqlCollectionStore,
setGraphqlCollections,
setRESTCollections,
- translateToNewRESTCollection,
- translateToNewGQLCollection,
} from "./collections"
import {
replaceEnvironments,
diff --git a/packages/hoppscotch-app/package.json b/packages/hoppscotch-app/package.json
index d9cb84277..053585b44 100644
--- a/packages/hoppscotch-app/package.json
+++ b/packages/hoppscotch-app/package.json
@@ -57,7 +57,7 @@
"@codemirror/tooltip": "^0.19.12",
"@codemirror/view": "^0.19.39",
"@hoppscotch/codemirror-lang-graphql": "workspace:^0.1.0",
- "@hoppscotch/data": "workspace:^0.2.0",
+ "@hoppscotch/data": "workspace:^0.3.0",
"@hoppscotch/js-sandbox": "workspace:^1.0.0",
"@nuxtjs/axios": "^5.13.6",
"@nuxtjs/composition-api": "^0.31.0",
diff --git a/packages/hoppscotch-data/package.json b/packages/hoppscotch-data/package.json
index bfac5f233..87cd8f2b5 100644
--- a/packages/hoppscotch-data/package.json
+++ b/packages/hoppscotch-data/package.json
@@ -1,6 +1,6 @@
{
"name": "@hoppscotch/data",
- "version": "0.2.0",
+ "version": "0.3.0",
"description": "Data Types, Validations and Migrations for Hoppscotch Public Data Structures",
"main": "dist/index.js",
"module": "true",
diff --git a/packages/hoppscotch-data/src/collection/index.ts b/packages/hoppscotch-data/src/collection/index.ts
new file mode 100644
index 000000000..ed578b70f
--- /dev/null
+++ b/packages/hoppscotch-data/src/collection/index.ts
@@ -0,0 +1,85 @@
+import { HoppGQLRequest, translateToGQLRequest } from "../graphql";
+import { HoppRESTRequest, translateToNewRequest } from "../rest";
+
+const CURRENT_COLL_SCHEMA_VER = 1
+
+type SupportedReqTypes =
+ | HoppRESTRequest
+ | HoppGQLRequest
+
+export type HoppCollection<T extends SupportedReqTypes> = {
+ v: number
+ name: string
+ folders: HoppCollection<T>[]
+ requests: T[]
+
+ id?: string // For Firestore ID data
+}
+
+/**
+ * Generates a Collection object. This ignores the version number object
+ * so it can be incremented independently without updating it everywhere
+ * @param x The Collection Data
+ * @returns The final collection
+ */
+export function makeCollection<T extends SupportedReqTypes>(
+ x: Omit<HoppCollection<T>, "v">
+): HoppCollection<T> {
+ return {
+ v: CURRENT_COLL_SCHEMA_VER,
+ ...x
+ }
+}
+
+/**
+ * Translates an old collection to a new collection
+ * @param x The collection object to load
+ * @returns The proper new collection format
+ */
+export function translateToNewRESTCollection(
+ x: any
+): HoppCollection<HoppRESTRequest> {
+ if (x.v && x.v === 1) return x
+
+ // Legacy
+ const name = x.name ?? "Untitled"
+ const folders = (x.folders ?? []).map(translateToNewRESTCollection)
+ const requests = (x.requests ?? []).map(translateToNewRequest)
+
+ const obj = makeCollection<HoppRESTRequest>({
+ name,
+ folders,
+ requests,
+ })
+
+ if (x.id) obj.id = x.id
+
+ return obj
+}
+
+/**
+ * Translates an old collection to a new collection
+ * @param x The collection object to load
+ * @returns The proper new collection format
+ */
+export function translateToNewGQLCollection(
+ x: any
+): HoppCollection<HoppGQLRequest> {
+ if (x.v && x.v === 1) return x
+
+ // Legacy
+ const name = x.name ?? "Untitled"
+ const folders = (x.folders ?? []).map(translateToNewGQLCollection)
+ const requests = (x.requests ?? []).map(translateToGQLRequest)
+
+ const obj = makeCollection<HoppGQLRequest>({
+ name,
+ folders,
+ requests,
+ })
+
+ if (x.id) obj.id = x.id
+
+ return obj
+}
+
diff --git a/packages/hoppscotch-data/src/index.ts b/packages/hoppscotch-data/src/index.ts
index 0796234a1..539c3ccde 100644
--- a/packages/hoppscotch-data/src/index.ts
+++ b/packages/hoppscotch-data/src/index.ts
@@ -1,2 +1,3 @@
export * from "./rest"
export * from "./graphql"
+export * from "./collection"
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index a81df52b7..e52495288 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -76,7 +76,7 @@ importers:
'@graphql-codegen/urql-introspection': ^2.1.1
'@graphql-typed-document-node/core': ^3.1.1
'@hoppscotch/codemirror-lang-graphql': workspace:^0.1.0
- '@hoppscotch/data': workspace:^0.2.0
+ '@hoppscotch/data': workspace:^0.3.0
'@hoppscotch/js-sandbox': workspace:^1.0.0
'@nuxt/types': ^2.15.8
'@nuxt/typescript-build': ^2.1.0
@@ -3764,14 +3764,14 @@ packages:
ufo: 0.7.9
dev: false
- /@nuxt/kit-edge/3.0.0-27373739.0ed2d4a:
- resolution: {integrity: sha512-xQUBq0nOD8gzJ/j6rvuWYGbjyp/+FShXirVbzP4rChX6UE5dMw+0oRBcmQ8ypA1hmpuXhDZHlw1noU0QHg2R9Q==}
+ /@nuxt/kit-edge/3.0.0-27376999.6abf75d:
+ resolution: {integrity: sha512-9VdJR9s9TXHd+lRRZnZRV+5GwkWKYomIS5NONMVgBDFjxotvqkVsCbpHvUja2I0NlcAxKnFePgqeDC20CBrgHQ==}
engines: {node: ^14.16.0 || ^16.11.0 || ^17.0.0}
dependencies:
- '@nuxt/schema': /@nuxt/schema-edge/3.0.0-27373739.0ed2d4a
+ '@nuxt/schema': /@nuxt/schema-edge/3.0.0-27376999.6abf75d
consola: 2.15.3
defu: 5.0.1
- dotenv: 14.1.0
+ dotenv: 14.2.0
globby: 12.2.0
hash-sum: 2.0.0
jiti: 1.12.9
@@ -3806,8 +3806,8 @@ packages:
node-fetch: 2.6.6
dev: false
- /@nuxt/schema-edge/3.0.0-27373739.0ed2d4a:
- resolution: {integrity: sha512-eMID8ninnb8BwN9b+YpvPbO8tEOrBrvy+2n8wbQe10JhVvf5lLZ+zpGIvlMQbLoFuEEsd444i4m7S5svSr5dTQ==}
+ /@nuxt/schema-edge/3.0.0-27376999.6abf75d:
+ resolution: {integrity: sha512-BdoTPuXoHT3aqQL37g/SPk1jOFO4pHbEClsj/5X6cLKwktTcqSbMkwZJF0tTyhvAwiOSFdlTvI34juPhvLNzPw==}
engines: {node: ^14.16.0 || ^16.11.0 || ^17.0.0}
dependencies:
create-require: 1.1.1
@@ -8374,8 +8374,8 @@ packages:
engines: {node: '>=10'}
dev: true
- /dotenv/14.1.0:
- resolution: {integrity: sha512-h8V+Yfa8m0YSjf3Rgbno51cxWldb4PEixIJVL55VmW7uAfeLQKiaPrEUiBps+ARK9MeqjJgTf269OMmu6lOODQ==}
+ /dotenv/14.2.0:
+ resolution: {integrity: sha512-05POuPJyPpO6jqzTNweQFfAyMSD4qa4lvsMOWyTRTdpHKy6nnnN+IYWaXF+lHivhBH/ufDKlR4IWCAN3oPnHuw==}
engines: {node: '>=12'}
dev: true
@@ -13547,7 +13547,7 @@ packages:
/nuxt-windicss/2.2.2:
resolution: {integrity: sha512-4tvzk9d2TUFxloty187D+wmO8ZNAvpSmRJ5HQO3/AvZdMMhAl4gomXR9fCgFKQe7Fxcj9nIKNInOx8TWowjiqA==}
dependencies:
- '@nuxt/kit': /@nuxt/kit-edge/3.0.0-27373739.0ed2d4a
+ '@nuxt/kit': /@nuxt/kit-edge/3.0.0-27376999.6abf75d
'@windicss/plugin-utils': 1.6.1
consola: 2.15.3
defu: 5.0.0