diff --git a/packages/hoppscotch-common/src/components/graphql/Headers.vue b/packages/hoppscotch-common/src/components/graphql/Headers.vue index b808a086f..7b9207b1f 100644 --- a/packages/hoppscotch-common/src/components/graphql/Headers.vue +++ b/packages/hoppscotch-common/src/components/graphql/Headers.vue @@ -156,6 +156,65 @@ + + + + + + + + + + + + + + + + + + + + { ) ) + console.log("not-equal", workingHeaders.value) if (!isEqual(request.value.headers, fixedHeaders)) { request.value.headers = cloneDeep(fixedHeaders) } @@ -429,4 +495,88 @@ const clearContent = () => { bulkHeaders.value = "" } + +const getComputedAuthHeaders = ( + req?: HoppGQLRequest, + auth?: HoppGQLRequest["auth"] +) => { + const request = auth ? { auth: auth ?? { authActive: false } } : req + // If Authorization header is also being user-defined, that takes priority + if (req && req.headers.find((h) => h.key.toLowerCase() === "authorization")) + return [] + + if (!request) return [] + + if (!request.auth || !request.auth.authActive) return [] + + const headers: HoppGQLHeader[] = [] + + // TODO: Support a better b64 implementation than btoa ? + if (request.auth.authType === "basic") { + const username = request.auth.username + const password = request.auth.password + + headers.push({ + active: true, + key: "Authorization", + value: `Basic ${btoa(`${username}:${password}`)}`, + }) + } else if ( + request.auth.authType === "bearer" || + request.auth.authType === "oauth-2" + ) { + headers.push({ + active: true, + key: "Authorization", + value: `Bearer ${request.auth.token}`, + }) + } else if (request.auth.authType === "api-key") { + const { key, value, addTo } = request.auth + + if (addTo === "Headers") { + headers.push({ + active: true, + key, + value, + }) + } + } + + return headers +} + +const getComputedHeaders = (req: HoppGQLRequest) => { + return [ + ...getComputedAuthHeaders(req).map((header) => ({ + source: "auth" as const, + header, + })), + ] +} + +const computedHeaders = computed(() => + getComputedHeaders(request.value).map((header, index) => ({ + id: `header-${index}`, + ...header, + })) +) + +const masking = ref(true) + +const toggleMask = () => { + masking.value = !masking.value +} + +const mask = (header: any) => { + if (header.source === "auth" && masking.value) + return header.header.value.replace(/\S/gi, "*") + return header.header.value +} + +// const changeTab = (tab: ComputedHeader["source"]) => { +// if (tab === "auth") emit("change-tab", "authorization") +// else emit("change-tab", "bodyParams") +// } + +console.log("compoyed-header", computedHeaders.value)