feat: collection level headers and authorization (#3505)
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
@@ -65,24 +65,29 @@ import {
|
||||
GetGqlRootUserCollectionsQueryVariables,
|
||||
GetGqlRootUserCollectionsDocument,
|
||||
ReqType,
|
||||
UpdateUserCollectionMutation,
|
||||
UpdateUserCollectionMutationVariables,
|
||||
UpdateUserCollectionDocument,
|
||||
} from "../../api/generated/graphql"
|
||||
|
||||
export const createRESTRootUserCollection = (title: string) =>
|
||||
export const createRESTRootUserCollection = (title: string, data?: string) =>
|
||||
runMutation<
|
||||
CreateRestRootUserCollectionMutation,
|
||||
CreateRestRootUserCollectionMutationVariables,
|
||||
""
|
||||
>(CreateRestRootUserCollectionDocument, {
|
||||
title,
|
||||
data,
|
||||
})()
|
||||
|
||||
export const createGQLRootUserCollection = (title: string) =>
|
||||
export const createGQLRootUserCollection = (title: string, data?: string) =>
|
||||
runMutation<
|
||||
CreateGqlRootUserCollectionMutation,
|
||||
CreateGqlRootUserCollectionMutationVariables,
|
||||
""
|
||||
>(CreateGqlRootUserCollectionDocument, {
|
||||
title,
|
||||
data,
|
||||
})()
|
||||
|
||||
export const createRESTUserRequest = (
|
||||
@@ -117,7 +122,8 @@ export const createGQLUserRequest = (
|
||||
|
||||
export const createRESTChildUserCollection = (
|
||||
title: string,
|
||||
parentUserCollectionID: string
|
||||
parentUserCollectionID: string,
|
||||
data?: string
|
||||
) =>
|
||||
runMutation<
|
||||
CreateRestChildUserCollectionMutation,
|
||||
@@ -126,11 +132,13 @@ export const createRESTChildUserCollection = (
|
||||
>(CreateRestChildUserCollectionDocument, {
|
||||
title,
|
||||
parentUserCollectionID,
|
||||
data,
|
||||
})()
|
||||
|
||||
export const createGQLChildUserCollection = (
|
||||
title: string,
|
||||
parentUserCollectionID: string
|
||||
parentUserCollectionID: string,
|
||||
data?: string
|
||||
) =>
|
||||
runMutation<
|
||||
CreateGqlChildUserCollectionMutation,
|
||||
@@ -139,6 +147,7 @@ export const createGQLChildUserCollection = (
|
||||
>(CreateGqlChildUserCollectionDocument, {
|
||||
title,
|
||||
parentUserCollectionID,
|
||||
data,
|
||||
})()
|
||||
|
||||
export const deleteUserCollection = (userCollectionID: string) =>
|
||||
@@ -160,6 +169,17 @@ export const renameUserCollection = (
|
||||
""
|
||||
>(RenameUserCollectionDocument, { userCollectionID, newTitle })()
|
||||
|
||||
export const updateUserCollection = (
|
||||
userCollectionID: string,
|
||||
newTitle?: string,
|
||||
data?: string
|
||||
) =>
|
||||
runMutation<
|
||||
UpdateUserCollectionMutation,
|
||||
UpdateUserCollectionMutationVariables,
|
||||
""
|
||||
>(UpdateUserCollectionDocument, { userCollectionID, newTitle, data })()
|
||||
|
||||
export const moveUserCollection = (
|
||||
sourceCollectionID: string,
|
||||
destinationCollectionID?: string
|
||||
|
||||
@@ -89,6 +89,7 @@ type ExportedUserCollectionREST = {
|
||||
folders: ExportedUserCollectionREST[]
|
||||
requests: Array<HoppRESTRequest & { id: string }>
|
||||
name: string
|
||||
data: string
|
||||
}
|
||||
|
||||
type ExportedUserCollectionGQL = {
|
||||
@@ -96,18 +97,25 @@ type ExportedUserCollectionGQL = {
|
||||
folders: ExportedUserCollectionGQL[]
|
||||
requests: Array<HoppGQLRequest & { id: string }>
|
||||
name: string
|
||||
data: string
|
||||
}
|
||||
|
||||
function exportedCollectionToHoppCollection(
|
||||
collection: ExportedUserCollectionREST | ExportedUserCollectionGQL,
|
||||
collectionType: "REST" | "GQL"
|
||||
): HoppCollection<HoppRESTRequest | HoppGQLRequest> {
|
||||
): HoppCollection {
|
||||
if (collectionType == "REST") {
|
||||
const restCollection = collection as ExportedUserCollectionREST
|
||||
|
||||
const data =
|
||||
restCollection.data && restCollection.data !== "null"
|
||||
? JSON.parse(restCollection.data)
|
||||
: {
|
||||
auth: { authType: "inherit", authActive: false },
|
||||
headers: [],
|
||||
}
|
||||
return {
|
||||
id: restCollection.id,
|
||||
v: 1,
|
||||
v: 2,
|
||||
name: restCollection.name,
|
||||
folders: restCollection.folders.map((folder) =>
|
||||
exportedCollectionToHoppCollection(folder, collectionType)
|
||||
@@ -139,26 +147,40 @@ function exportedCollectionToHoppCollection(
|
||||
testScript,
|
||||
})
|
||||
),
|
||||
auth: data.auth,
|
||||
headers: data.headers,
|
||||
}
|
||||
} else {
|
||||
const gqlCollection = collection as ExportedUserCollectionGQL
|
||||
const data =
|
||||
gqlCollection.data && gqlCollection.data !== "null"
|
||||
? JSON.parse(gqlCollection.data)
|
||||
: {
|
||||
auth: { authType: "inherit", authActive: false },
|
||||
headers: [],
|
||||
}
|
||||
|
||||
return {
|
||||
id: gqlCollection.id,
|
||||
v: 1,
|
||||
v: 2,
|
||||
name: gqlCollection.name,
|
||||
folders: gqlCollection.folders.map((folder) =>
|
||||
exportedCollectionToHoppCollection(folder, collectionType)
|
||||
),
|
||||
requests: gqlCollection.requests.map(
|
||||
({ v, auth, headers, name, id }) => ({
|
||||
({ v, auth, headers, name, id, query, url, variables }) => ({
|
||||
id,
|
||||
v,
|
||||
auth,
|
||||
headers,
|
||||
name,
|
||||
query,
|
||||
url,
|
||||
variables,
|
||||
})
|
||||
) as HoppGQLRequest[],
|
||||
auth: data.auth,
|
||||
headers: data.headers,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -168,7 +190,6 @@ async function loadUserCollections(collectionType: "REST" | "GQL") {
|
||||
undefined,
|
||||
collectionType == "REST" ? ReqType.Rest : ReqType.Gql
|
||||
)
|
||||
|
||||
if (E.isRight(res)) {
|
||||
const collectionsJSONString =
|
||||
res.right.exportUserCollectionsToJSON.exportedCollection
|
||||
@@ -177,7 +198,6 @@ async function loadUserCollections(collectionType: "REST" | "GQL") {
|
||||
ExportedUserCollectionGQL | ExportedUserCollectionREST
|
||||
>
|
||||
).map((collection) => ({ v: 1, ...collection }))
|
||||
|
||||
runDispatchWithOutSyncing(() => {
|
||||
collectionType == "REST"
|
||||
? setRESTCollections(
|
||||
@@ -186,7 +206,7 @@ async function loadUserCollections(collectionType: "REST" | "GQL") {
|
||||
exportedCollectionToHoppCollection(
|
||||
collection,
|
||||
"REST"
|
||||
) as HoppCollection<HoppRESTRequest>
|
||||
) as HoppCollection
|
||||
)
|
||||
)
|
||||
: setGraphqlCollections(
|
||||
@@ -195,7 +215,7 @@ async function loadUserCollections(collectionType: "REST" | "GQL") {
|
||||
exportedCollectionToHoppCollection(
|
||||
collection,
|
||||
"GQL"
|
||||
) as HoppCollection<HoppGQLRequest>
|
||||
) as HoppCollection
|
||||
)
|
||||
)
|
||||
})
|
||||
@@ -292,19 +312,32 @@ function setupUserCollectionCreatedSubscription() {
|
||||
})
|
||||
} else {
|
||||
// root collections won't have parentCollectionID
|
||||
const data =
|
||||
res.right.userCollectionCreated.data &&
|
||||
res.right.userCollectionCreated.data != "null"
|
||||
? JSON.parse(res.right.userCollectionCreated.data)
|
||||
: {
|
||||
auth: { authType: "inherit", authActive: false },
|
||||
headers: [],
|
||||
}
|
||||
|
||||
runDispatchWithOutSyncing(() => {
|
||||
collectionType == "GQL"
|
||||
? addGraphqlCollection({
|
||||
name: res.right.userCollectionCreated.title,
|
||||
folders: [],
|
||||
requests: [],
|
||||
v: 1,
|
||||
v: 2,
|
||||
auth: data.auth,
|
||||
headers: data.headers,
|
||||
})
|
||||
: addRESTCollection({
|
||||
name: res.right.userCollectionCreated.title,
|
||||
folders: [],
|
||||
requests: [],
|
||||
v: 1,
|
||||
v: 2,
|
||||
auth: data.auth,
|
||||
headers: data?.headers,
|
||||
})
|
||||
|
||||
const localIndex = collectionStore.value.state.length - 1
|
||||
@@ -718,7 +751,7 @@ export const def: CollectionsPlatformDef = {
|
||||
|
||||
function getCollectionPathFromCollectionID(
|
||||
collectionID: string,
|
||||
collections: HoppCollection<HoppRESTRequest | HoppGQLRequest>[],
|
||||
collections: HoppCollection[],
|
||||
parentPath?: string
|
||||
): string | null {
|
||||
for (const collectionIndex in collections) {
|
||||
@@ -742,7 +775,7 @@ function getCollectionPathFromCollectionID(
|
||||
|
||||
function getRequestPathFromRequestID(
|
||||
requestID: string,
|
||||
collections: HoppCollection<HoppRESTRequest | HoppGQLRequest>[],
|
||||
collections: HoppCollection[],
|
||||
parentPath?: string
|
||||
): { collectionPath: string; requestIndex: number } | null {
|
||||
for (const collectionIndex in collections) {
|
||||
@@ -774,7 +807,7 @@ function getRequestPathFromRequestID(
|
||||
function getRequestIndex(
|
||||
requestID: string,
|
||||
parentCollectionPath: string,
|
||||
collections: HoppCollection<HoppRESTRequest | HoppGQLRequest>[]
|
||||
collections: HoppCollection[]
|
||||
) {
|
||||
const collection = navigateToFolderWithIndexPath(
|
||||
collections,
|
||||
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
editUserRequest,
|
||||
moveUserCollection,
|
||||
moveUserRequest,
|
||||
renameUserCollection,
|
||||
updateUserCollection,
|
||||
updateUserCollectionOrder,
|
||||
} from "./collections.api"
|
||||
|
||||
@@ -39,7 +39,7 @@ export const restRequestsMapper = createMapper<string, string>()
|
||||
// temp implementation untill the backend implements an endpoint that accepts an entire collection
|
||||
// TODO: use importCollectionsJSON to do this
|
||||
const recursivelySyncCollections = async (
|
||||
collection: HoppCollection<HoppRESTRequest>,
|
||||
collection: HoppCollection,
|
||||
collectionPath: string,
|
||||
parentUserCollectionID?: string
|
||||
) => {
|
||||
@@ -47,27 +47,69 @@ const recursivelySyncCollections = async (
|
||||
|
||||
// if parentUserCollectionID does not exist, create the collection as a root collection
|
||||
if (!parentUserCollectionID) {
|
||||
const res = await createRESTRootUserCollection(collection.name)
|
||||
|
||||
const data = {
|
||||
auth: collection.auth ?? {
|
||||
authType: "inherit",
|
||||
authActive: true,
|
||||
},
|
||||
headers: collection.headers ?? [],
|
||||
}
|
||||
const res = await createRESTRootUserCollection(
|
||||
collection.name,
|
||||
JSON.stringify(data)
|
||||
)
|
||||
if (E.isRight(res)) {
|
||||
parentCollectionID = res.right.createRESTRootUserCollection.id
|
||||
|
||||
const returnedData = res.right.createRESTRootUserCollection.data
|
||||
? JSON.parse(res.right.createRESTRootUserCollection.data)
|
||||
: {
|
||||
auth: {
|
||||
authType: "inherit",
|
||||
authActive: true,
|
||||
},
|
||||
headers: [],
|
||||
}
|
||||
|
||||
collection.id = parentCollectionID
|
||||
collection.auth = returnedData.auth
|
||||
collection.headers = returnedData.headers
|
||||
removeDuplicateRESTCollectionOrFolder(parentCollectionID, collectionPath)
|
||||
} else {
|
||||
parentCollectionID = undefined
|
||||
}
|
||||
} else {
|
||||
// if parentUserCollectionID exists, create the collection as a child collection
|
||||
const data = {
|
||||
auth: collection.auth ?? {
|
||||
authType: "inherit",
|
||||
authActive: true,
|
||||
},
|
||||
headers: collection.headers ?? [],
|
||||
}
|
||||
|
||||
const res = await createRESTChildUserCollection(
|
||||
collection.name,
|
||||
parentUserCollectionID
|
||||
parentUserCollectionID,
|
||||
JSON.stringify(data)
|
||||
)
|
||||
|
||||
if (E.isRight(res)) {
|
||||
const childCollectionId = res.right.createRESTChildUserCollection.id
|
||||
|
||||
const returnedData = res.right.createRESTChildUserCollection.data
|
||||
? JSON.parse(res.right.createRESTChildUserCollection.data)
|
||||
: {
|
||||
auth: {
|
||||
authType: "inherit",
|
||||
authActive: true,
|
||||
},
|
||||
headers: [],
|
||||
}
|
||||
|
||||
collection.id = childCollectionId
|
||||
collection.auth = returnedData.auth
|
||||
collection.headers = returnedData.headers
|
||||
|
||||
removeDuplicateRESTCollectionOrFolder(
|
||||
childCollectionId,
|
||||
@@ -155,8 +197,13 @@ export const storeSyncDefinition: StoreSyncDefinitionOf<
|
||||
[collectionIndex]
|
||||
)?.id
|
||||
|
||||
if (collectionID && collection.name) {
|
||||
renameUserCollection(collectionID, collection.name)
|
||||
const data = {
|
||||
auth: collection.auth,
|
||||
headers: collection.headers,
|
||||
}
|
||||
|
||||
if (collectionID) {
|
||||
updateUserCollection(collectionID, collection.name, JSON.stringify(data))
|
||||
}
|
||||
},
|
||||
async addFolder({ name, path }) {
|
||||
@@ -195,9 +242,12 @@ export const storeSyncDefinition: StoreSyncDefinitionOf<
|
||||
)?.id
|
||||
|
||||
const folderName = folder.name
|
||||
|
||||
if (folderID && folderName) {
|
||||
renameUserCollection(folderID, folderName)
|
||||
const data = {
|
||||
auth: folder.auth,
|
||||
headers: folder.headers,
|
||||
}
|
||||
if (folderID) {
|
||||
updateUserCollection(folderID, folderName, JSON.stringify(data))
|
||||
}
|
||||
},
|
||||
async removeFolder({ folderID }) {
|
||||
|
||||
@@ -21,7 +21,7 @@ import {
|
||||
deleteUserCollection,
|
||||
deleteUserRequest,
|
||||
editGQLUserRequest,
|
||||
renameUserCollection,
|
||||
updateUserCollection,
|
||||
} from "./collections.api"
|
||||
|
||||
import * as E from "fp-ts/Either"
|
||||
@@ -36,7 +36,7 @@ export const gqlRequestsMapper = createMapper<string, string>()
|
||||
// temp implementation untill the backend implements an endpoint that accepts an entire collection
|
||||
// TODO: use importCollectionsJSON to do this
|
||||
const recursivelySyncCollections = async (
|
||||
collection: HoppCollection<HoppRESTRequest>,
|
||||
collection: HoppCollection,
|
||||
collectionPath: string,
|
||||
parentUserCollectionID?: string
|
||||
) => {
|
||||
@@ -44,12 +44,35 @@ const recursivelySyncCollections = async (
|
||||
|
||||
// if parentUserCollectionID does not exist, create the collection as a root collection
|
||||
if (!parentUserCollectionID) {
|
||||
const res = await createGQLRootUserCollection(collection.name)
|
||||
const data = {
|
||||
auth: collection.auth ?? {
|
||||
authType: "inherit",
|
||||
authActive: true,
|
||||
},
|
||||
headers: collection.headers ?? [],
|
||||
}
|
||||
const res = await createGQLRootUserCollection(
|
||||
collection.name,
|
||||
JSON.stringify(data)
|
||||
)
|
||||
|
||||
if (E.isRight(res)) {
|
||||
parentCollectionID = res.right.createGQLRootUserCollection.id
|
||||
|
||||
const returnedData = res.right.createGQLRootUserCollection.data
|
||||
? JSON.parse(res.right.createGQLRootUserCollection.data)
|
||||
: {
|
||||
auth: {
|
||||
authType: "inherit",
|
||||
authActive: true,
|
||||
},
|
||||
headers: [],
|
||||
}
|
||||
|
||||
collection.id = parentCollectionID
|
||||
collection.auth = returnedData.auth
|
||||
collection.headers = returnedData.headers
|
||||
|
||||
removeDuplicateGraphqlCollectionOrFolder(
|
||||
parentCollectionID,
|
||||
collectionPath
|
||||
@@ -59,15 +82,37 @@ const recursivelySyncCollections = async (
|
||||
}
|
||||
} else {
|
||||
// if parentUserCollectionID exists, create the collection as a child collection
|
||||
|
||||
const data = {
|
||||
auth: collection.auth ?? {
|
||||
authType: "inherit",
|
||||
authActive: true,
|
||||
},
|
||||
headers: collection.headers ?? [],
|
||||
}
|
||||
|
||||
const res = await createGQLChildUserCollection(
|
||||
collection.name,
|
||||
parentUserCollectionID
|
||||
parentUserCollectionID,
|
||||
JSON.stringify(data)
|
||||
)
|
||||
|
||||
if (E.isRight(res)) {
|
||||
const childCollectionId = res.right.createGQLChildUserCollection.id
|
||||
|
||||
const returnedData = res.right.createGQLChildUserCollection.data
|
||||
? JSON.parse(res.right.createGQLChildUserCollection.data)
|
||||
: {
|
||||
auth: {
|
||||
authType: "inherit",
|
||||
authActive: true,
|
||||
},
|
||||
headers: [],
|
||||
}
|
||||
|
||||
collection.id = childCollectionId
|
||||
collection.auth = returnedData.auth
|
||||
collection.headers = returnedData.headers
|
||||
|
||||
removeDuplicateGraphqlCollectionOrFolder(
|
||||
childCollectionId,
|
||||
@@ -158,8 +203,13 @@ export const storeSyncDefinition: StoreSyncDefinitionOf<
|
||||
[collectionIndex]
|
||||
)?.id
|
||||
|
||||
if (collectionID && collection.name) {
|
||||
renameUserCollection(collectionID, collection.name)
|
||||
const data = {
|
||||
auth: collection.auth,
|
||||
headers: collection.headers,
|
||||
}
|
||||
|
||||
if (collectionID) {
|
||||
updateUserCollection(collectionID, collection.name, JSON.stringify(data))
|
||||
}
|
||||
},
|
||||
async addFolder({ name, path }) {
|
||||
@@ -197,8 +247,13 @@ export const storeSyncDefinition: StoreSyncDefinitionOf<
|
||||
path.split("/").map((index) => parseInt(index))
|
||||
)?.id
|
||||
|
||||
if (folderBackendId && folder.name) {
|
||||
renameUserCollection(folderBackendId, folder.name)
|
||||
const data = {
|
||||
auth: folder.auth,
|
||||
headers: folder.headers,
|
||||
}
|
||||
|
||||
if (folderBackendId) {
|
||||
updateUserCollection(folderBackendId, folder.name, JSON.stringify(data))
|
||||
}
|
||||
},
|
||||
async removeFolder({ folderID }) {
|
||||
|
||||
Reference in New Issue
Block a user