chore: update types

This commit is contained in:
nivedin
2023-11-28 23:23:36 +05:30
committed by Andrew Bastin
parent 59735c15e4
commit d326063659
7 changed files with 124 additions and 47 deletions

View File

@@ -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
}

View File

@@ -20,6 +20,7 @@ export {
HoppRESTAuth,
HoppRESTAuthAPIKey,
HoppRESTAuthBasic,
HoppRESTAuthInherit,
HoppRESTAuthBearer,
HoppRESTAuthNone,
HoppRESTAuthOAuth2,

View File

@@ -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)