refactor: import-export team coll in new schema
This commit is contained in:
@@ -108,6 +108,27 @@ const importToPersonalWorkspace = (collections: HoppCollection[]) => {
|
||||
})
|
||||
}
|
||||
|
||||
function translateToTeamCollectionFormat(x: HoppCollection) {
|
||||
const folders: HoppCollection[] = (x.folders ?? []).map(
|
||||
translateToTeamCollectionFormat
|
||||
)
|
||||
|
||||
const data = {
|
||||
auth: x.auth,
|
||||
headers: x.headers,
|
||||
}
|
||||
|
||||
const obj = {
|
||||
...x,
|
||||
folders,
|
||||
data,
|
||||
}
|
||||
|
||||
if (x.id) obj.id = x.id
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
const importToTeamsWorkspace = async (collections: HoppCollection[]) => {
|
||||
if (!hasTeamWriteAccess.value || !selectedTeamID.value) {
|
||||
return E.left({
|
||||
@@ -115,8 +136,12 @@ const importToTeamsWorkspace = async (collections: HoppCollection[]) => {
|
||||
})
|
||||
}
|
||||
|
||||
const transformedCollection = collections.map((collection) =>
|
||||
translateToTeamCollectionFormat(collection)
|
||||
)
|
||||
|
||||
const res = await toTeamsImporter(
|
||||
JSON.stringify(collections),
|
||||
JSON.stringify(transformedCollection),
|
||||
selectedTeamID.value
|
||||
)()
|
||||
|
||||
@@ -400,7 +425,6 @@ const HoppTeamCollectionsExporter: ImporterOrExporter = {
|
||||
},
|
||||
action: async () => {
|
||||
isHoppTeamCollectionExporterInProgress.value = true
|
||||
|
||||
if (
|
||||
props.collectionsType.type === "team-collections" &&
|
||||
props.collectionsType.selectedTeam
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
query GetCollectionTitle($collectionID: ID!) {
|
||||
collection(collectionID: $collectionID) {
|
||||
title
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
query GetCollectionTitleAndData($collectionID: ID!) {
|
||||
collection(collectionID: $collectionID) {
|
||||
title
|
||||
data
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ query GetSingleCollection($collectionID: ID!) {
|
||||
collection(collectionID: $collectionID) {
|
||||
id
|
||||
title
|
||||
data
|
||||
parent {
|
||||
id
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ subscription TeamCollectionAdded($teamID: ID!) {
|
||||
teamCollectionAdded(teamID: $teamID) {
|
||||
id
|
||||
title
|
||||
data
|
||||
parent {
|
||||
id
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
ExportAsJsonDocument,
|
||||
GetCollectionChildrenIDsDocument,
|
||||
GetCollectionRequestsDocument,
|
||||
GetCollectionTitleDocument,
|
||||
GetCollectionTitleAndDataDocument,
|
||||
} from "./graphql"
|
||||
|
||||
export const BACKEND_PAGE_SIZE = 10
|
||||
@@ -84,16 +84,19 @@ export const getCompleteCollectionTree = (
|
||||
pipe(
|
||||
TE.Do,
|
||||
|
||||
TE.bind("title", () =>
|
||||
TE.bind("titleAndData", () =>
|
||||
pipe(
|
||||
() =>
|
||||
runGQLQuery({
|
||||
query: GetCollectionTitleDocument,
|
||||
query: GetCollectionTitleAndDataDocument,
|
||||
variables: {
|
||||
collectionID: collID,
|
||||
},
|
||||
}),
|
||||
TE.map((x) => x.collection!.title)
|
||||
TE.map((result) => ({
|
||||
title: result.collection!.title,
|
||||
data: result.collection!.data,
|
||||
}))
|
||||
)
|
||||
),
|
||||
TE.bind("children", () =>
|
||||
@@ -107,22 +110,36 @@ export const getCompleteCollectionTree = (
|
||||
TE.bind("requests", () => () => getCollectionRequests(collID)),
|
||||
|
||||
TE.map(
|
||||
({ title, children, requests }) =>
|
||||
({ titleAndData, children, requests }) =>
|
||||
<TeamCollection>{
|
||||
id: collID,
|
||||
children,
|
||||
requests,
|
||||
title,
|
||||
title: titleAndData.title,
|
||||
data: titleAndData.data,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
export const teamCollToHoppRESTColl = (coll: TeamCollection): HoppCollection =>
|
||||
makeCollection({
|
||||
export const teamCollToHoppRESTColl = (
|
||||
coll: TeamCollection
|
||||
): HoppCollection => {
|
||||
const data =
|
||||
coll.data && coll.data !== "null"
|
||||
? JSON.parse(coll.data)
|
||||
: {
|
||||
auth: { authType: "inherit", authActive: true },
|
||||
headers: [],
|
||||
}
|
||||
|
||||
return makeCollection({
|
||||
name: coll.title,
|
||||
folders: coll.children?.map(teamCollToHoppRESTColl) ?? [],
|
||||
requests: coll.requests?.map((x) => x.request) ?? [],
|
||||
auth: data.auth ?? { authType: "inherit", authActive: true },
|
||||
headers: data.headers ?? [],
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JSON string of all the collection of the specified team
|
||||
|
||||
@@ -29,6 +29,7 @@ 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
|
||||
|
||||
@@ -699,7 +699,7 @@ export default class NewTeamCollectionAdapter {
|
||||
children: null,
|
||||
requests: null,
|
||||
title: result.right.teamCollectionAdded.title,
|
||||
data: null,
|
||||
data: result.right.teamCollectionAdded.data ?? null,
|
||||
},
|
||||
result.right.teamCollectionAdded.parent?.id ?? null
|
||||
)
|
||||
|
||||
@@ -107,7 +107,7 @@ function exportedCollectionToHoppCollection(
|
||||
if (collectionType == "REST") {
|
||||
const restCollection = collection as ExportedUserCollectionREST
|
||||
const data =
|
||||
restCollection.data && restCollection.data != "null"
|
||||
restCollection.data && restCollection.data !== "null"
|
||||
? JSON.parse(restCollection.data)
|
||||
: {
|
||||
auth: { authType: "inherit", authActive: false },
|
||||
@@ -115,7 +115,7 @@ function exportedCollectionToHoppCollection(
|
||||
}
|
||||
return {
|
||||
id: restCollection.id,
|
||||
v: 1,
|
||||
v: 2,
|
||||
name: restCollection.name,
|
||||
folders: restCollection.folders.map((folder) =>
|
||||
exportedCollectionToHoppCollection(folder, collectionType)
|
||||
@@ -162,7 +162,7 @@ function exportedCollectionToHoppCollection(
|
||||
|
||||
return {
|
||||
id: gqlCollection.id,
|
||||
v: 1,
|
||||
v: 2,
|
||||
name: gqlCollection.name,
|
||||
folders: gqlCollection.folders.map((folder) =>
|
||||
exportedCollectionToHoppCollection(folder, collectionType)
|
||||
@@ -324,7 +324,7 @@ function setupUserCollectionCreatedSubscription() {
|
||||
name: res.right.userCollectionCreated.title,
|
||||
folders: [],
|
||||
requests: [],
|
||||
v: 1,
|
||||
v: 2,
|
||||
auth: data.auth,
|
||||
headers: data.headers,
|
||||
})
|
||||
@@ -332,7 +332,7 @@ function setupUserCollectionCreatedSubscription() {
|
||||
name: res.right.userCollectionCreated.title,
|
||||
folders: [],
|
||||
requests: [],
|
||||
v: 1,
|
||||
v: 2,
|
||||
auth: data.auth,
|
||||
headers: data?.headers,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user