fix: use proper values for addTo field when auth type is api-key (#3966)

This commit is contained in:
Akash K
2024-04-16 17:44:28 +05:30
committed by GitHub
parent 787aab650f
commit 0f592d1789
12 changed files with 182 additions and 30 deletions

View File

@@ -3,31 +3,33 @@ import { z } from "zod"
import V1_VERSION from "./v/1"
import V2_VERSION from "./v/2"
import V3_VERSION from "./v/3"
import V4_VERSION from "./v/4"
export { GQLHeader } from "./v/1"
export {
HoppGQLAuthAPIKey,
HoppGQLAuthBasic,
HoppGQLAuthBearer,
HoppGQLAuthNone,
HoppGQLAuthInherit,
} from "./v/2"
export { HoppGQLAuth } from "./v/3"
export { HoppGQLAuth } from "./v/4"
export { HoppGQLAuthOAuth2 } from "./v/3"
export { HoppGQLAuthAPIKey } from "./v/4"
export const GQL_REQ_SCHEMA_VERSION = 3
export const GQL_REQ_SCHEMA_VERSION = 4
const versionedObject = z.object({
v: z.number(),
})
export const HoppGQLRequest = createVersionedEntity({
latestVersion: 3,
latestVersion: 4,
versionMap: {
1: V1_VERSION,
2: V2_VERSION,
3: V3_VERSION,
4: V4_VERSION,
},
getVersion(x) {
const result = versionedObject.safeParse(x)

View File

@@ -0,0 +1,71 @@
import { z } from "zod"
import { defineVersion } from "verzod"
import { HoppRESTAuthOAuth2 } from "../../rest"
import {
HoppGQLAuthAPIKey as HoppGQLAuthAPIKeyOld,
HoppGQLAuthBasic,
HoppGQLAuthBearer,
HoppGQLAuthInherit,
HoppGQLAuthNone,
} from "./2"
import { V3_SCHEMA } from "./3"
export { HoppRESTAuthOAuth2 as HoppGQLAuthOAuth2 } from "../../rest"
export const HoppGQLAuthAPIKey = HoppGQLAuthAPIKeyOld.extend({
addTo: z.enum(["HEADERS", "QUERY_PARAMS"]).catch("HEADERS"),
})
export type HoppGqlAuthOAuth2 = z.infer<typeof HoppRESTAuthOAuth2>
export const HoppGQLAuth = z
.discriminatedUnion("authType", [
HoppGQLAuthNone,
HoppGQLAuthInherit,
HoppGQLAuthBasic,
HoppGQLAuthBearer,
HoppGQLAuthAPIKey,
HoppRESTAuthOAuth2, // both rest and gql have the same auth type for oauth2
])
.and(
z.object({
authActive: z.boolean(),
})
)
export type HoppGQLAuth = z.infer<typeof HoppGQLAuth>
export const V4_SCHEMA = V3_SCHEMA.extend({
v: z.literal(4),
auth: HoppGQLAuth,
})
export default defineVersion({
initial: false,
schema: V4_SCHEMA,
up(old: z.infer<typeof V3_SCHEMA>) {
if (old.auth.authType === "api-key") {
return {
...old,
v: 4 as const,
auth: {
...old.auth,
addTo:
old.auth.addTo === "Query params"
? ("QUERY_PARAMS" as const)
: ("HEADERS" as const),
},
}
}
return {
...old,
v: 4 as const,
auth: {
...old.auth,
},
}
},
})

View File

@@ -5,12 +5,13 @@ import V0_VERSION from "./v/0"
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 { createVersionedEntity, InferredEntity } from "verzod"
import { lodashIsEqualEq, mapThenEq, undefinedEq } from "../utils/eq"
import { HoppRESTReqBody, HoppRESTHeaders, HoppRESTParams } from "./v/1"
import { HoppRESTAuth } from "./v/3"
import { HoppRESTAuth } from "./v/4"
import { HoppRESTRequestVariables } from "./v/2"
import { z } from "zod"
@@ -19,7 +20,6 @@ export * from "./content-types"
export {
FormDataKeyValue,
HoppRESTReqBodyFormData,
HoppRESTAuthAPIKey,
HoppRESTAuthBasic,
HoppRESTAuthInherit,
HoppRESTAuthBearer,
@@ -29,7 +29,6 @@ export {
} from "./v/1"
export {
HoppRESTAuth,
HoppRESTAuthOAuth2,
AuthCodeGrantTypeParams,
ClientCredentialsGrantTypeParams,
@@ -37,6 +36,8 @@ export {
PasswordGrantTypeParams,
} from "./v/3"
export { HoppRESTAuth, HoppRESTAuthAPIKey } from "./v/4"
export { HoppRESTRequestVariables } from "./v/2"
const versionedObject = z.object({
@@ -45,12 +46,13 @@ const versionedObject = z.object({
})
export const HoppRESTRequest = createVersionedEntity({
latestVersion: 3,
latestVersion: 4,
versionMap: {
0: V0_VERSION,
1: V1_VERSION,
2: V2_VERSION,
3: V3_VERSION,
4: V4_VERSION,
},
getVersion(data) {
// For V1 onwards we have the v string storing the number
@@ -92,7 +94,7 @@ const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
),
})
export const RESTReqSchemaVersion = "3"
export const RESTReqSchemaVersion = "4"
export type HoppRESTParam = HoppRESTRequest["params"][number]
export type HoppRESTHeader = HoppRESTRequest["headers"][number]
@@ -187,7 +189,7 @@ export function makeRESTRequest(
export function getDefaultRESTRequest(): HoppRESTRequest {
return {
v: "3",
v: "4",
endpoint: "https://echo.hoppscotch.io",
name: "Untitled",
params: [],

View File

@@ -0,0 +1,69 @@
import { z } from "zod"
import { defineVersion } from "verzod"
import { HoppRESTAuthOAuth2, V3_SCHEMA } from "./3"
import {
HoppRESTAuthAPIKey as HoppRESTAuthAPIKeyOld,
HoppRESTAuthBasic,
HoppRESTAuthBearer,
HoppRESTAuthInherit,
HoppRESTAuthNone,
} from "./1"
// in this new version, we update the old 'Headers' and 'Query params' to be more consistent with OAuth addTo values
// also in the previous version addTo was a string, which prevented some bugs from being caught by the type system
// this version uses an enum, so we get the values as literals in the type system
export const HoppRESTAuthAPIKey = HoppRESTAuthAPIKeyOld.extend({
addTo: z.enum(["HEADERS", "QUERY_PARAMS"]).catch("HEADERS"),
})
export type HoppRESTAuthAPIKey = z.infer<typeof HoppRESTAuthAPIKey>
export const HoppRESTAuth = z
.discriminatedUnion("authType", [
HoppRESTAuthNone,
HoppRESTAuthInherit,
HoppRESTAuthBasic,
HoppRESTAuthBearer,
HoppRESTAuthOAuth2,
HoppRESTAuthAPIKey,
])
.and(
z.object({
authActive: z.boolean(),
})
)
export type HoppRESTAuth = z.infer<typeof HoppRESTAuth>
export const V4_SCHEMA = V3_SCHEMA.extend({
v: z.literal("4"),
auth: HoppRESTAuth,
})
export default defineVersion({
schema: V4_SCHEMA,
initial: false,
up(old: z.infer<typeof V3_SCHEMA>) {
if (old.auth.authType === "api-key") {
return {
...old,
v: "4" as const,
auth: {
...old.auth,
addTo:
old.auth.addTo === "Query params"
? ("QUERY_PARAMS" as const)
: ("HEADERS" as const),
},
}
}
return {
...old,
auth: {
...old.auth,
},
v: "4" as const,
}
},
})