chore: update types
This commit is contained in:
@@ -8,7 +8,7 @@ export const getDefaultRESTRequest = (): HoppRESTRequest => ({
|
||||
headers: [],
|
||||
method: "GET",
|
||||
auth: {
|
||||
authType: "none",
|
||||
authType: "inherit",
|
||||
authActive: true,
|
||||
},
|
||||
preRequestScript: "",
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { HoppRESTRequest } from "@hoppscotch/data"
|
||||
|
||||
export type HoppInheritedProperty = {
|
||||
parentId: string
|
||||
parentName: string
|
||||
auth?: HoppRESTRequest["auth"]
|
||||
headers?: HoppRESTRequest["headers"]
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
|
||||
@@ -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<T extends SupportedReqTypes> = {
|
||||
v: number
|
||||
@@ -13,6 +15,9 @@ export type HoppCollection<T extends SupportedReqTypes> = {
|
||||
folders: HoppCollection<T>[]
|
||||
requests: T[]
|
||||
|
||||
auth: T["auth"]
|
||||
headers: T["headers"]
|
||||
|
||||
id?: string // For Firestore ID data
|
||||
}
|
||||
|
||||
@@ -27,7 +32,7 @@ export function makeCollection<T extends SupportedReqTypes>(
|
||||
): HoppCollection<T> {
|
||||
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<HoppRESTRequest>({
|
||||
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<HoppGQLRequest>({
|
||||
name,
|
||||
folders,
|
||||
requests,
|
||||
auth,
|
||||
headers,
|
||||
})
|
||||
|
||||
if (x.id) obj.id = x.id
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ export {
|
||||
HoppRESTAuth,
|
||||
HoppRESTAuthAPIKey,
|
||||
HoppRESTAuthBasic,
|
||||
HoppRESTAuthInherit,
|
||||
HoppRESTAuthBearer,
|
||||
HoppRESTAuthNone,
|
||||
HoppRESTAuthOAuth2,
|
||||
|
||||
@@ -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<typeof FormDataKeyValue>
|
||||
|
||||
export const HoppRESTReqBodyFormData = z.object({
|
||||
contentType: z.literal("multipart/form-data"),
|
||||
body: z.array(FormDataKeyValue)
|
||||
body: z.array(FormDataKeyValue),
|
||||
})
|
||||
|
||||
export type HoppRESTReqBodyFormData = z.infer<typeof HoppRESTReqBodyFormData>
|
||||
@@ -31,11 +33,11 @@ export type HoppRESTReqBodyFormData = z.infer<typeof HoppRESTReqBodyFormData>
|
||||
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<typeof HoppRESTReqBody>
|
||||
|
||||
export const HoppRESTAuthNone = z.object({
|
||||
authType: z.literal("none")
|
||||
authType: z.literal("none"),
|
||||
})
|
||||
|
||||
export type HoppRESTAuthNone = z.infer<typeof HoppRESTAuthNone>
|
||||
@@ -96,17 +98,26 @@ export const HoppRESTAuthAPIKey = z.object({
|
||||
|
||||
export type HoppRESTAuthAPIKey = z.infer<typeof HoppRESTAuthAPIKey>
|
||||
|
||||
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<typeof HoppRESTAuthInherit>
|
||||
|
||||
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>
|
||||
|
||||
@@ -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<typeof V0_SCHEMA>): z.infer<typeof V1_SCHEMA>["body"] {
|
||||
function parseRequestBody(
|
||||
x: z.infer<typeof V0_SCHEMA>
|
||||
): z.infer<typeof V1_SCHEMA>["body"] {
|
||||
return {
|
||||
contentType: "application/json",
|
||||
body: x.contentType === "application/json" ? x.rawParams ?? "" : "",
|
||||
}
|
||||
}
|
||||
|
||||
export function parseOldAuth(x: z.infer<typeof V0_SCHEMA>): z.infer<typeof V1_SCHEMA>["auth"] {
|
||||
export function parseOldAuth(
|
||||
x: z.infer<typeof V0_SCHEMA>
|
||||
): z.infer<typeof V1_SCHEMA>["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<typeof V0_SCHEMA>) {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user