Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
feat: add function to safely generate rest request that maintains type contracts
Andrew Bastin137d562c864f
diff --git a/packages/hoppscotch-app/package.json b/packages/hoppscotch-app/package.jsonindex 11aae0494..c9b2c1e16 100644--- a/packages/hoppscotch-app/package.json+++ b/packages/hoppscotch-app/package.json@@ -56,7 +56,7 @@"@codemirror/tooltip": "^0.19.10","@codemirror/view": "^0.19.37","@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","@nuxtjs/axios": "^5.13.6","@nuxtjs/composition-api": "^0.31.0",diff --git a/packages/hoppscotch-data/package.json b/packages/hoppscotch-data/package.jsonindex d099fbf3a..795f52540 100644--- a/packages/hoppscotch-data/package.json+++ b/packages/hoppscotch-data/package.json@@ -1,6 +1,6 @@{"name": "@hoppscotch/data",- "version": "0.1.0",+ "version": "0.2.0","description": "Data Types, Validations and Migrations for Hoppscotch Public Data Structures","main": "dist/index.js","module": "true",@@ -27,6 +27,10 @@},"homepage": "https://github.com/hoppscotch/hoppscotch#readme","devDependencies": {+ "@types/lodash": "^4.14.178","tsup": "^5.11.10"+ },+ "dependencies": {+ "lodash": "^4.17.21"}}diff --git a/packages/hoppscotch-data/src/rest/index.ts b/packages/hoppscotch-data/src/rest/index.tsindex d3336b43d..d98e36849 100644--- a/packages/hoppscotch-data/src/rest/index.ts+++ b/packages/hoppscotch-data/src/rest/index.ts@@ -1,3 +1,4 @@+import cloneDeep from "lodash/cloneDeep"import { ValidContentTypes } from "./content-types"import { HoppRESTAuth } from "./HoppRESTAuth"@@ -56,6 +57,57 @@ export interface HoppRESTRequest {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(x: Omit<HoppRESTRequest, "v">): HoppRESTRequest {diff --git a/packages/hoppscotch-data/src/type-utils.d.ts b/packages/hoppscotch-data/src/type-utils.d.tsnew file mode 100644index 000000000..91440ddb8--- /dev/null+++ b/packages/hoppscotch-data/src/type-utils.d.ts@@ -0,0 +1,3 @@+interface Object {+ hasOwnProperty<K extends PropertyKey>(key: K): this is Record<K, unknown>;+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 22e9de81e..ace7077a6 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -75,7 +75,7 @@ importers:'@graphql-codegen/urql-introspection': ^2.1.1'@graphql-typed-document-node/core': ^3.1.1'@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'@nuxt/types': ^2.15.8'@nuxt/typescript-build': ^2.1.0@@ -315,8 +315,13 @@ importers:packages/hoppscotch-data:specifiers:+ '@types/lodash': ^4.14.178+ lodash: ^4.17.21tsup: ^5.11.10+ dependencies:+ lodash: 4.17.21devDependencies:+ '@types/lodash': 4.14.178tsup: 5.11.10packages/hoppscotch-js-sandbox: