feat: add function to safely generate rest request that maintains type contracts
This commit is contained in:
@@ -56,7 +56,7 @@
|
|||||||
"@codemirror/tooltip": "^0.19.10",
|
"@codemirror/tooltip": "^0.19.10",
|
||||||
"@codemirror/view": "^0.19.37",
|
"@codemirror/view": "^0.19.37",
|
||||||
"@hoppscotch/codemirror-lang-graphql": "workspace:^0.1.0",
|
"@hoppscotch/codemirror-lang-graphql": "workspace:^0.1.0",
|
||||||
"@hoppscotch/data": "workspace:^0.1.0",
|
"@hoppscotch/data": "workspace:^0.2.0",
|
||||||
"@hoppscotch/js-sandbox": "workspace:^1.0.0",
|
"@hoppscotch/js-sandbox": "workspace:^1.0.0",
|
||||||
"@nuxtjs/axios": "^5.13.6",
|
"@nuxtjs/axios": "^5.13.6",
|
||||||
"@nuxtjs/composition-api": "^0.31.0",
|
"@nuxtjs/composition-api": "^0.31.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hoppscotch/data",
|
"name": "@hoppscotch/data",
|
||||||
"version": "0.1.0",
|
"version": "0.2.0",
|
||||||
"description": "Data Types, Validations and Migrations for Hoppscotch Public Data Structures",
|
"description": "Data Types, Validations and Migrations for Hoppscotch Public Data Structures",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "true",
|
"module": "true",
|
||||||
@@ -27,6 +27,10 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/hoppscotch/hoppscotch#readme",
|
"homepage": "https://github.com/hoppscotch/hoppscotch#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/lodash": "^4.14.178",
|
||||||
"tsup": "^5.11.10"
|
"tsup": "^5.11.10"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"lodash": "^4.17.21"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import cloneDeep from "lodash/cloneDeep"
|
||||||
import { ValidContentTypes } from "./content-types"
|
import { ValidContentTypes } from "./content-types"
|
||||||
import { HoppRESTAuth } from "./HoppRESTAuth"
|
import { HoppRESTAuth } from "./HoppRESTAuth"
|
||||||
|
|
||||||
@@ -56,6 +57,57 @@ export interface HoppRESTRequest {
|
|||||||
body: HoppRESTReqBody
|
body: HoppRESTReqBody
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safely tries to extract REST Request data from an unknown value.
|
||||||
|
* If we fail to detect certain bits, we just resolve it to the default value
|
||||||
|
* @param x The value to extract REST Request data from
|
||||||
|
* @param defaultReq The default REST Request to source from
|
||||||
|
*/
|
||||||
|
export function safelyExtractRESTRequest(
|
||||||
|
x: unknown,
|
||||||
|
defaultReq: HoppRESTRequest
|
||||||
|
): HoppRESTRequest {
|
||||||
|
const req = cloneDeep(defaultReq)
|
||||||
|
|
||||||
|
// TODO: A cleaner way to do this ?
|
||||||
|
if (!!x && typeof x === "object") {
|
||||||
|
if (x.hasOwnProperty("v") && typeof x.v === "string")
|
||||||
|
req.v = x.v
|
||||||
|
|
||||||
|
if (x.hasOwnProperty("id") && typeof x.id === "string")
|
||||||
|
req.id = x.id
|
||||||
|
|
||||||
|
if (x.hasOwnProperty("name") && typeof x.name === "string")
|
||||||
|
req.name = x.name
|
||||||
|
|
||||||
|
if (x.hasOwnProperty("method") && typeof x.method === "string")
|
||||||
|
req.method = x.method
|
||||||
|
|
||||||
|
if (x.hasOwnProperty("endpoint") && typeof x.endpoint === "string")
|
||||||
|
req.endpoint = x.endpoint
|
||||||
|
|
||||||
|
if (x.hasOwnProperty("preRequestScript") && typeof x.preRequestScript === "string")
|
||||||
|
req.preRequestScript = x.preRequestScript
|
||||||
|
|
||||||
|
if (x.hasOwnProperty("testScript") && typeof x.testScript === "string")
|
||||||
|
req.testScript = x.testScript
|
||||||
|
|
||||||
|
if (x.hasOwnProperty("body") && typeof x.body === "object" && !!x.body)
|
||||||
|
req.body = x.body as any // TODO: Deep nested checks
|
||||||
|
|
||||||
|
if (x.hasOwnProperty("auth") && typeof x.auth === "object" && !!x.auth)
|
||||||
|
req.auth = x.auth as any // TODO: Deep nested checks
|
||||||
|
|
||||||
|
if (x.hasOwnProperty("params") && Array.isArray(x.params))
|
||||||
|
req.params = x.params // TODO: Deep nested checks
|
||||||
|
|
||||||
|
if (x.hasOwnProperty("headers") && Array.isArray(x.headers))
|
||||||
|
req.headers = x.headers // TODO: Deep nested checks
|
||||||
|
}
|
||||||
|
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
export function makeRESTRequest(
|
export function makeRESTRequest(
|
||||||
x: Omit<HoppRESTRequest, "v">
|
x: Omit<HoppRESTRequest, "v">
|
||||||
): HoppRESTRequest {
|
): HoppRESTRequest {
|
||||||
|
|||||||
3
packages/hoppscotch-data/src/type-utils.d.ts
vendored
Normal file
3
packages/hoppscotch-data/src/type-utils.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
interface Object {
|
||||||
|
hasOwnProperty<K extends PropertyKey>(key: K): this is Record<K, unknown>;
|
||||||
|
}
|
||||||
7
pnpm-lock.yaml
generated
7
pnpm-lock.yaml
generated
@@ -75,7 +75,7 @@ importers:
|
|||||||
'@graphql-codegen/urql-introspection': ^2.1.1
|
'@graphql-codegen/urql-introspection': ^2.1.1
|
||||||
'@graphql-typed-document-node/core': ^3.1.1
|
'@graphql-typed-document-node/core': ^3.1.1
|
||||||
'@hoppscotch/codemirror-lang-graphql': workspace:^0.1.0
|
'@hoppscotch/codemirror-lang-graphql': workspace:^0.1.0
|
||||||
'@hoppscotch/data': workspace:^0.1.0
|
'@hoppscotch/data': workspace:^0.2.0
|
||||||
'@hoppscotch/js-sandbox': workspace:^1.0.0
|
'@hoppscotch/js-sandbox': workspace:^1.0.0
|
||||||
'@nuxt/types': ^2.15.8
|
'@nuxt/types': ^2.15.8
|
||||||
'@nuxt/typescript-build': ^2.1.0
|
'@nuxt/typescript-build': ^2.1.0
|
||||||
@@ -315,8 +315,13 @@ importers:
|
|||||||
|
|
||||||
packages/hoppscotch-data:
|
packages/hoppscotch-data:
|
||||||
specifiers:
|
specifiers:
|
||||||
|
'@types/lodash': ^4.14.178
|
||||||
|
lodash: ^4.17.21
|
||||||
tsup: ^5.11.10
|
tsup: ^5.11.10
|
||||||
|
dependencies:
|
||||||
|
lodash: 4.17.21
|
||||||
devDependencies:
|
devDependencies:
|
||||||
|
'@types/lodash': 4.14.178
|
||||||
tsup: 5.11.10
|
tsup: 5.11.10
|
||||||
|
|
||||||
packages/hoppscotch-js-sandbox:
|
packages/hoppscotch-js-sandbox:
|
||||||
|
|||||||
Reference in New Issue
Block a user