From d32606365942a157a3d778a90d065d63a9c666b3 Mon Sep 17 00:00:00 2001 From: nivedin Date: Tue, 28 Nov 2023 23:23:36 +0530 Subject: [PATCH] chore: update types --- .../src/helpers/rest/default.ts | 2 +- .../src/helpers/rest/document.ts | 7 ++ .../helpers/types/HoppInheritedProperties.ts | 8 ++ .../src/newstore/collections.ts | 25 ++++- .../hoppscotch-data/src/collection/index.ts | 28 +++-- packages/hoppscotch-data/src/rest/index.ts | 1 + packages/hoppscotch-data/src/rest/v/1.ts | 100 +++++++++++------- 7 files changed, 124 insertions(+), 47 deletions(-) create mode 100644 packages/hoppscotch-common/src/helpers/types/HoppInheritedProperties.ts diff --git a/packages/hoppscotch-common/src/helpers/rest/default.ts b/packages/hoppscotch-common/src/helpers/rest/default.ts index 873ee2d7d..d5bd45e9f 100644 --- a/packages/hoppscotch-common/src/helpers/rest/default.ts +++ b/packages/hoppscotch-common/src/helpers/rest/default.ts @@ -8,7 +8,7 @@ export const getDefaultRESTRequest = (): HoppRESTRequest => ({ headers: [], method: "GET", auth: { - authType: "none", + authType: "inherit", authActive: true, }, preRequestScript: "", diff --git a/packages/hoppscotch-common/src/helpers/rest/document.ts b/packages/hoppscotch-common/src/helpers/rest/document.ts index 93272dbc3..afc8e4ff4 100644 --- a/packages/hoppscotch-common/src/helpers/rest/document.ts +++ b/packages/hoppscotch-common/src/helpers/rest/document.ts @@ -2,6 +2,7 @@ import { HoppRESTRequest } from "@hoppscotch/data" import { HoppRESTResponse } from "../types/HoppRESTResponse" import { HoppTestResult } from "../types/HoppTestResult" import { RESTOptionTabs } from "~/components/http/RequestOptions.vue" +import { HoppInheritedProperty } from "../types/HoppInheritedProperties" export type HoppRESTSaveContext = | { @@ -80,4 +81,10 @@ export type HoppRESTDocument = { * Options tab preference for the current tab's document */ optionTabPreference?: RESTOptionTabs + + /** + * The inherited properties from the parent collection + * (if any) + */ + inheritedProperties?: HoppInheritedProperty } diff --git a/packages/hoppscotch-common/src/helpers/types/HoppInheritedProperties.ts b/packages/hoppscotch-common/src/helpers/types/HoppInheritedProperties.ts new file mode 100644 index 000000000..98df1fbb3 --- /dev/null +++ b/packages/hoppscotch-common/src/helpers/types/HoppInheritedProperties.ts @@ -0,0 +1,8 @@ +import { HoppRESTRequest } from "@hoppscotch/data" + +export type HoppInheritedProperty = { + parentId: string + parentName: string + auth?: HoppRESTRequest["auth"] + headers?: HoppRESTRequest["headers"] +} diff --git a/packages/hoppscotch-common/src/newstore/collections.ts b/packages/hoppscotch-common/src/newstore/collections.ts index 0a25ee5ad..317b43751 100644 --- a/packages/hoppscotch-common/src/newstore/collections.ts +++ b/packages/hoppscotch-common/src/newstore/collections.ts @@ -17,6 +17,11 @@ const defaultRESTCollectionState = { name: "My Collection", folders: [], requests: [], + auth: { + authType: "inherit", + authActive: false, + }, + headers: [], }), ], } @@ -27,6 +32,11 @@ const defaultGraphqlCollectionState = { name: "My GraphQL Collection", folders: [], requests: [], + auth: { + authType: "inherit", + authActive: false, + }, + headers: [], }), ], } @@ -132,6 +142,15 @@ const restCollectionDispatchers = defineDispatchers({ name, folders: [], requests: [], + auth: { + authType: "inherit", + authActive: false, + auth: { + authType: "inherit", + authActive: false, + }, + }, + headers: [], }) const newState = state @@ -690,8 +709,12 @@ const gqlCollectionDispatchers = defineDispatchers({ name, folders: [], requests: [], + auth: { + authType: "inherit", + authActive: false, + }, + headers: [], }) - const newState = state const indexPaths = path.split("/").map((x) => parseInt(x)) diff --git a/packages/hoppscotch-data/src/collection/index.ts b/packages/hoppscotch-data/src/collection/index.ts index e34c58ddc..870d09654 100644 --- a/packages/hoppscotch-data/src/collection/index.ts +++ b/packages/hoppscotch-data/src/collection/index.ts @@ -1,11 +1,13 @@ -import { GQL_REQ_SCHEMA_VERSION, HoppGQLRequest, translateToGQLRequest } from "../graphql"; -import { HoppRESTRequest, translateToNewRequest } from "../rest"; +import { + GQL_REQ_SCHEMA_VERSION, + HoppGQLRequest, + translateToGQLRequest, +} from "../graphql" +import { HoppRESTRequest, translateToNewRequest } from "../rest" const CURRENT_COLL_SCHEMA_VER = 1 -type SupportedReqTypes = - | HoppRESTRequest - | HoppGQLRequest +type SupportedReqTypes = HoppRESTRequest | HoppGQLRequest export type HoppCollection = { v: number @@ -13,6 +15,9 @@ export type HoppCollection = { folders: HoppCollection[] requests: T[] + auth: T["auth"] + headers: T["headers"] + id?: string // For Firestore ID data } @@ -27,7 +32,7 @@ export function makeCollection( ): HoppCollection { return { v: CURRENT_COLL_SCHEMA_VER, - ...x + ...x, } } @@ -46,10 +51,15 @@ export function translateToNewRESTCollection( const folders = (x.folders ?? []).map(translateToNewRESTCollection) const requests = (x.requests ?? []).map(translateToNewRequest) + const auth = x.auth ?? "None" + const headers = x.headers ?? [] + const obj = makeCollection({ name, folders, requests, + auth, + headers, }) if (x.id) obj.id = x.id @@ -72,14 +82,18 @@ export function translateToNewGQLCollection( const folders = (x.folders ?? []).map(translateToNewGQLCollection) const requests = (x.requests ?? []).map(translateToGQLRequest) + const auth = x.auth ?? "None" + const headers = x.headers ?? [] + const obj = makeCollection({ name, folders, requests, + auth, + headers, }) if (x.id) obj.id = x.id return obj } - diff --git a/packages/hoppscotch-data/src/rest/index.ts b/packages/hoppscotch-data/src/rest/index.ts index 0a427e135..6324c4188 100644 --- a/packages/hoppscotch-data/src/rest/index.ts +++ b/packages/hoppscotch-data/src/rest/index.ts @@ -20,6 +20,7 @@ export { HoppRESTAuth, HoppRESTAuthAPIKey, HoppRESTAuthBasic, + HoppRESTAuthInherit, HoppRESTAuthBearer, HoppRESTAuthNone, HoppRESTAuthOAuth2, diff --git a/packages/hoppscotch-data/src/rest/v/1.ts b/packages/hoppscotch-data/src/rest/v/1.ts index a7f680dd8..346717754 100644 --- a/packages/hoppscotch-data/src/rest/v/1.ts +++ b/packages/hoppscotch-data/src/rest/v/1.ts @@ -3,27 +3,29 @@ import { z } from "zod" import { V0_SCHEMA } from "./0" -export const FormDataKeyValue = z.object({ - key: z.string(), - active: z.boolean() -}).and( - z.union([ - z.object({ - isFile: z.literal(true), - value: z.array(z.instanceof(Blob).nullable()) - }), - z.object({ - isFile: z.literal(false), - value: z.string() - }) - ]) -) +export const FormDataKeyValue = z + .object({ + key: z.string(), + active: z.boolean(), + }) + .and( + z.union([ + z.object({ + isFile: z.literal(true), + value: z.array(z.instanceof(Blob).nullable()), + }), + z.object({ + isFile: z.literal(false), + value: z.string(), + }), + ]) + ) export type FormDataKeyValue = z.infer export const HoppRESTReqBodyFormData = z.object({ contentType: z.literal("multipart/form-data"), - body: z.array(FormDataKeyValue) + body: z.array(FormDataKeyValue), }) export type HoppRESTReqBodyFormData = z.infer @@ -31,11 +33,11 @@ export type HoppRESTReqBodyFormData = z.infer export const HoppRESTReqBody = z.union([ z.object({ contentType: z.literal(null), - body: z.literal(null).catch(null) + body: z.literal(null).catch(null), }), z.object({ contentType: z.literal("multipart/form-data"), - body: z.array(FormDataKeyValue).catch([]) + body: z.array(FormDataKeyValue).catch([]), }), z.object({ contentType: z.union([ @@ -48,14 +50,14 @@ export const HoppRESTReqBody = z.union([ z.literal("text/html"), z.literal("text/plain"), ]), - body: z.string().catch("") - }) + body: z.string().catch(""), + }), ]) export type HoppRESTReqBody = z.infer export const HoppRESTAuthNone = z.object({ - authType: z.literal("none") + authType: z.literal("none"), }) export type HoppRESTAuthNone = z.infer @@ -96,17 +98,26 @@ export const HoppRESTAuthAPIKey = z.object({ export type HoppRESTAuthAPIKey = z.infer -export const HoppRESTAuth = z.discriminatedUnion("authType", [ - HoppRESTAuthNone, - HoppRESTAuthBasic, - HoppRESTAuthBearer, - HoppRESTAuthOAuth2, - HoppRESTAuthAPIKey -]).and( - z.object({ - authActive: z.boolean(), - }) -) +export const HoppRESTAuthInherit = z.object({ + authType: z.literal("inherit"), +}) + +export type HoppRESTAuthInherit = z.infer + +export const HoppRESTAuth = z + .discriminatedUnion("authType", [ + HoppRESTAuthNone, + HoppRESTAuthInherit, + HoppRESTAuthBasic, + HoppRESTAuthBearer, + HoppRESTAuthOAuth2, + HoppRESTAuthAPIKey, + ]) + .and( + z.object({ + authActive: z.boolean(), + }) + ) export type HoppRESTAuth = z.infer @@ -114,7 +125,7 @@ export const HoppRESTParams = z.array( z.object({ key: z.string().catch(""), value: z.string().catch(""), - active: z.boolean().catch(true) + active: z.boolean().catch(true), }) ) @@ -124,7 +135,7 @@ export const HoppRESTHeaders = z.array( z.object({ key: z.string().catch(""), value: z.string().catch(""), - active: z.boolean().catch(true) + active: z.boolean().catch(true), }) ) @@ -144,17 +155,21 @@ const V1_SCHEMA = z.object({ auth: HoppRESTAuth, - body: HoppRESTReqBody + body: HoppRESTReqBody, }) -function parseRequestBody(x: z.infer): z.infer["body"] { +function parseRequestBody( + x: z.infer +): z.infer["body"] { return { contentType: "application/json", body: x.contentType === "application/json" ? x.rawParams ?? "" : "", } } -export function parseOldAuth(x: z.infer): z.infer["auth"] { +export function parseOldAuth( + x: z.infer +): z.infer["auth"] { if (!x.auth || x.auth === "None") return { authType: "none", @@ -183,7 +198,16 @@ export default defineVersion({ initial: false, schema: V1_SCHEMA, up(old: z.infer) { - const { url, path, headers, params, name, method, preRequestScript, testScript } = old + const { + url, + path, + headers, + params, + name, + method, + preRequestScript, + testScript, + } = old const endpoint = `${url}${path}` const body = parseRequestBody(old)