Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

Add: Powershell RestMethod codegen (#1233)

Raul Piraces Alastuey881d7fa5ee09
diff --git a/helpers/codegen/codegen.js b/helpers/codegen/codegen.js
index 681a8683f..fdc3a728b 100644
--- a/helpers/codegen/codegen.js
+++ b/helpers/codegen/codegen.js
@@ -5,6 +5,7 @@ import { JSAxiosCodegen } from "./generators/js-axios"
import { GoNativeCodegen } from "./generators/go-native"
import { NodeJsRequestCodegen } from "./generators/nodejs-request"
import { JSjQueryCodegen } from "./generators/js-jQuery"
+import { PowerShellRestMethod } from "./generators/powershell"
/* Register code generators here.
* A code generator is defined as an object with the following structure.
@@ -22,6 +23,7 @@ export const codegens = [
GoNativeCodegen,
NodeJsRequestCodegen,
JSjQueryCodegen,
+ PowerShellRestMethod,
]
export function generateCodeWithGenerator(codegenID, context) {
diff --git a/helpers/codegen/generators/powershell.js b/helpers/codegen/generators/powershell.js
new file mode 100644
index 000000000..67acd2425
--- /dev/null
+++ b/helpers/codegen/generators/powershell.js
@@ -0,0 +1,61 @@
+export const PowerShellRestMethod = {
+ id: "powershell-restmethod",
+ name: "Powershell RestMethod",
+ generator: ({
+ url,
+ pathName,
+ queryString,
+ auth,
+ httpUser,
+ httpPassword,
+ bearerToken,
+ method,
+ rawInput,
+ rawParams,
+ rawRequestBody,
+ contentType,
+ headers,
+ }) => {
+ const methodsWithBody = ["Put", "Post", "Delete"]
+ const formattedMethod = method[0].toUpperCase() + method.substring(1).toLowerCase()
+ const includeBody = methodsWithBody.indexOf(formattedMethod) >= 0
+ const requestString = []
+ let genHeaders = []
+ let variables = ""
+
+ requestString.push(
+ `Invoke-RestMethod -Method '${formattedMethod}' -Uri '${url}${pathName}${queryString}'`
+ )
+ const requestBody = rawInput ? rawParams : rawRequestBody
+
+ if (requestBody.length !== 0 && includeBody) {
+ variables = variables.concat(`$body = @'\n${requestBody}\n'@\n\n`)
+ }
+ if (headers) {
+ headers.forEach(({ key, value }) => {
+ if (key) genHeaders.push(` '${key}' = '${value}'\n`)
+ })
+ }
+
+ if (contentType) {
+ genHeaders.push(` 'Content-Type' = '${contentType}; charset=utf-8'\n`)
+ requestString.push(` -ContentType '${contentType}; charset=utf-8'`)
+ }
+
+ if (auth === "Basic Auth") {
+ const basic = `${httpUser}:${httpPassword}`
+ genHeaders.push(
+ ` 'Authorization' = 'Basic ${window.btoa(unescape(encodeURIComponent(basic)))}'\n`
+ )
+ } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
+ genHeaders.push(` 'Authorization' = 'Bearer ${bearerToken}'\n`)
+ }
+ genHeaders = genHeaders.join("").slice(0, -1)
+ variables = variables.concat(`$headers = @{\n${genHeaders}\n}\n`)
+ requestString.push(` -Headers $headers`)
+ if (includeBody) {
+ requestString.push(` -Body $body`)
+ }
+ return `${variables}\n${requestString.join("")}`
+ },
+}