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: streamline duplicated advanced param type definitions for oauth grant types (#5401)

Anwarul Islamf9a1d65ad9a0
diff --git a/packages/hoppscotch-common/src/services/oauth/flows/authCode.ts b/packages/hoppscotch-common/src/services/oauth/flows/authCode.ts
index 5bd4012aa..2558b0dba 100644
--- a/packages/hoppscotch-common/src/services/oauth/flows/authCode.ts
+++ b/packages/hoppscotch-common/src/services/oauth/flows/authCode.ts
@@ -12,26 +12,24 @@ import * as E from "fp-ts/Either"
import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
import { content } from "@hoppscotch/kernel"
import { refreshToken, OAuth2ParamSchema } from "../utils"
+import {
+ AuthCodeGrantTypeParams,
+ OAuth2AuthRequestParam,
+} from "@hoppscotch/data"
const persistenceService = getService(PersistenceService)
const interceptorService = getService(KernelInterceptorService)
-const AuthCodeOauthFlowParamsSchema = z
- .object({
- authEndpoint: z.string(),
- tokenEndpoint: z.string(),
- clientID: z.string(),
- clientSecret: z.string().optional(),
- scopes: z.string().optional(),
- isPKCE: z.boolean(),
- codeVerifierMethod: z.enum(["plain", "S256"]).optional(),
- authRequestParams: z.array(
- OAuth2ParamSchema.omit({
- sendIn: true,
- })
- ),
- refreshRequestParams: z.array(OAuth2ParamSchema),
+// Use the existing schema from hoppscotch-data but ensure required arrays
+const AuthCodeOauthFlowParamsSchema = AuthCodeGrantTypeParams.omit({
+ grantType: true,
+ token: true,
+})
+ .extend({
+ // Override optional arrays to be required for the service layer
+ authRequestParams: z.array(OAuth2AuthRequestParam),
tokenRequestParams: z.array(OAuth2ParamSchema),
+ refreshRequestParams: z.array(OAuth2ParamSchema),
})
.refine(
(params) => {
diff --git a/packages/hoppscotch-common/src/services/oauth/flows/clientCredentials.ts b/packages/hoppscotch-common/src/services/oauth/flows/clientCredentials.ts
index 1803ce795..57604e0d2 100644
--- a/packages/hoppscotch-common/src/services/oauth/flows/clientCredentials.ts
+++ b/packages/hoppscotch-common/src/services/oauth/flows/clientCredentials.ts
@@ -12,16 +12,20 @@ import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
import { RelayRequest, content } from "@hoppscotch/kernel"
import { parseBytesToJSON } from "~/helpers/functional/json"
import { refreshToken, OAuth2ParamSchema } from "../utils"
+import { ClientCredentialsGrantTypeParams } from "@hoppscotch/data"
const interceptorService = getService(KernelInterceptorService)
-const ClientCredentialsFlowParamsSchema = z
- .object({
- authEndpoint: z.string(),
- clientID: z.string(),
- clientSecret: z.string().optional(),
- scopes: z.string().optional(),
+// Use the existing schema from hoppscotch-data but add client authentication mode
+const ClientCredentialsFlowParamsSchema = ClientCredentialsGrantTypeParams.omit(
+ {
+ grantType: true,
+ token: true,
+ }
+)
+ .extend({
clientAuthentication: z.enum(["AS_BASIC_AUTH_HEADERS", "IN_BODY"]),
+ // Override optional arrays to be required for the service layer
tokenRequestParams: z.array(OAuth2ParamSchema),
refreshRequestParams: z.array(OAuth2ParamSchema),
})
diff --git a/packages/hoppscotch-common/src/services/oauth/flows/implicit.ts b/packages/hoppscotch-common/src/services/oauth/flows/implicit.ts
index d1226c109..6d587ea7b 100644
--- a/packages/hoppscotch-common/src/services/oauth/flows/implicit.ts
+++ b/packages/hoppscotch-common/src/services/oauth/flows/implicit.ts
@@ -8,20 +8,22 @@ import {
import { z } from "zod"
import { getService } from "~/modules/dioc"
import * as E from "fp-ts/Either"
+import {
+ ImplicitOauthFlowParams as ImplicitOauthFlowParamsData,
+ OAuth2AuthRequestParam,
+} from "@hoppscotch/data"
import { OAuth2ParamSchema } from "../utils"
const persistenceService = getService(PersistenceService)
-const ImplicitOauthFlowParamsSchema = z
- .object({
- authEndpoint: z.string(),
- clientID: z.string(),
- scopes: z.string().optional(),
- authRequestParams: z.array(
- OAuth2ParamSchema.omit({
- sendIn: true,
- })
- ),
+// Use the existing schema from hoppscotch-data
+const ImplicitOauthFlowParamsSchema = ImplicitOauthFlowParamsData.omit({
+ grantType: true,
+ token: true,
+})
+ .extend({
+ // Override optional arrays to be required for the service layer
+ authRequestParams: z.array(OAuth2AuthRequestParam),
refreshRequestParams: z.array(OAuth2ParamSchema),
})
.refine((params) => {
diff --git a/packages/hoppscotch-common/src/services/oauth/flows/password.ts b/packages/hoppscotch-common/src/services/oauth/flows/password.ts
index 6f9d84978..f48e47486 100644
--- a/packages/hoppscotch-common/src/services/oauth/flows/password.ts
+++ b/packages/hoppscotch-common/src/services/oauth/flows/password.ts
@@ -11,18 +11,18 @@ import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
import { useToast } from "~/composables/toast"
import { content } from "@hoppscotch/kernel"
import { parseBytesToJSON } from "~/helpers/functional/json"
-import { OAuth2ParamSchema, refreshToken } from "../utils"
+import { refreshToken, OAuth2ParamSchema } from "../utils"
+import { PasswordGrantTypeParams } from "@hoppscotch/data"
const interceptorService = getService(KernelInterceptorService)
-const PasswordFlowParamsSchema = z
- .object({
- authEndpoint: z.string(),
- clientID: z.string(),
- clientSecret: z.string().optional(),
- scopes: z.string().optional(),
- username: z.string(),
- password: z.string(),
+// Use the existing schema from hoppscotch-data
+const PasswordFlowParamsSchema = PasswordGrantTypeParams.omit({
+ grantType: true,
+ token: true,
+})
+ .extend({
+ // Override optional arrays to be required for the service layer
tokenRequestParams: z.array(OAuth2ParamSchema),
refreshRequestParams: z.array(OAuth2ParamSchema),
})
diff --git a/packages/hoppscotch-common/src/services/oauth/utils.ts b/packages/hoppscotch-common/src/services/oauth/utils.ts
index 7cb9e54fb..bd4877c9e 100644
--- a/packages/hoppscotch-common/src/services/oauth/utils.ts
+++ b/packages/hoppscotch-common/src/services/oauth/utils.ts
@@ -4,6 +4,7 @@ import { getService } from "~/modules/dioc"
import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
import { content } from "@hoppscotch/kernel"
import { decodeResponseAsJSON } from "./oauth.service"
+import { OAuth2AdvancedParam } from "@hoppscotch/data"
const interceptorService = getService(KernelInterceptorService)
@@ -110,13 +111,9 @@ export const refreshToken = async ({
}
/**
- * Common OAuth2 parameter schema with all possible fields
- * Used as base for both auth requests and advanced token/refresh requests
+ * Common OAuth2 parameter schema
+ * Used for all OAuth parameter types - omit sendIn field where not needed
*/
-export const OAuth2ParamSchema = z.object({
- id: z.number(),
- key: z.string(),
- value: z.string(),
- active: z.boolean(),
+export const OAuth2ParamSchema = OAuth2AdvancedParam.extend({
sendIn: z.enum(["headers", "url", "body"]).optional(),
})
diff --git a/packages/hoppscotch-data/src/rest/index.ts b/packages/hoppscotch-data/src/rest/index.ts
index d20142495..012f7cb4a 100644
--- a/packages/hoppscotch-data/src/rest/index.ts
+++ b/packages/hoppscotch-data/src/rest/index.ts
@@ -62,6 +62,8 @@ export { ImplicitOauthFlowParams } from "./v/15/auth"
export {
HoppRESTAuthOAuth2,
ClientCredentialsGrantTypeParams,
+ OAuth2AdvancedParam,
+ OAuth2AuthRequestParam,
} from "./v/15/auth"
export {
diff --git a/packages/hoppscotch-data/src/rest/v/15/auth.ts b/packages/hoppscotch-data/src/rest/v/15/auth.ts
index eace3b742..b76a4c608 100644
--- a/packages/hoppscotch-data/src/rest/v/15/auth.ts
+++ b/packages/hoppscotch-data/src/rest/v/15/auth.ts
@@ -20,7 +20,7 @@ import { ImplicitOauthFlowParams as ImplicitOauthFlowParamsOld } from "../3"
export { HoppRESTAuthJWT } from "../13/auth"
// Define the OAuth2 advanced parameter structure
-const OAuth2AdvancedParam = z.object({
+export const OAuth2AdvancedParam = z.object({
id: z.number(),
key: z.string(),
value: z.string(),
@@ -29,7 +29,7 @@ const OAuth2AdvancedParam = z.object({
})
// omit sendIn from OAuth2AuthRequestParam
-const OAuth2AuthRequestParam = OAuth2AdvancedParam.omit({ sendIn: true })
+export const OAuth2AuthRequestParam = OAuth2AdvancedParam.omit({ sendIn: true })
export const AuthCodeGrantTypeParams = AuthCodeGrantTypeParamsOld.extend({
authRequestParams: z.array(OAuth2AuthRequestParam).optional().default([]),