codegen.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import {
  2. FormDataKeyValue,
  3. HoppRESTHeader,
  4. HoppRESTParam,
  5. } from "../types/HoppRESTRequest"
  6. import { EffectiveHoppRESTRequest } from "../utils/EffectiveURL"
  7. import { CLibcurlCodegen } from "./generators/c-libcurl"
  8. import { CsRestsharpCodegen } from "./generators/cs-restsharp"
  9. import { CurlCodegen } from "./generators/curl"
  10. import { GoNativeCodegen } from "./generators/go-native"
  11. import { JavaOkhttpCodegen } from "./generators/java-okhttp"
  12. import { JavaUnirestCodegen } from "./generators/java-unirest"
  13. import { JavascriptFetchCodegen } from "./generators/javascript-fetch"
  14. import { JavascriptJqueryCodegen } from "./generators/javascript-jquery"
  15. import { JavascriptXhrCodegen } from "./generators/javascript-xhr"
  16. import { NodejsAxiosCodegen } from "./generators/nodejs-axios"
  17. import { NodejsNativeCodegen } from "./generators/nodejs-native"
  18. import { NodejsRequestCodegen } from "./generators/nodejs-request"
  19. import { NodejsUnirestCodegen } from "./generators/nodejs-unirest"
  20. import { PhpCurlCodegen } from "./generators/php-curl"
  21. import { PowershellRestmethodCodegen } from "./generators/powershell-restmethod"
  22. import { PythonHttpClientCodegen } from "./generators/python-http-client"
  23. import { PythonRequestsCodegen } from "./generators/python-requests"
  24. import { RubyNetHttpCodeGen } from "./generators/ruby-net-http"
  25. import { SalesforceApexCodegen } from "./generators/salesforce-apex"
  26. import { ShellHttpieCodegen } from "./generators/shell-httpie"
  27. import { ShellWgetCodegen } from "./generators/shell-wget"
  28. /* Register code generators here.
  29. * A code generator is defined as an object with the following structure.
  30. *
  31. * id: string
  32. * name: string
  33. * language: string // a string identifier used in ace editor for syntax highlighting
  34. * // see node_modules/ace-builds/src-noconflict/mode-** files for valid value
  35. * generator: (ctx) => string
  36. *
  37. */
  38. export const codegens = [
  39. CLibcurlCodegen,
  40. CsRestsharpCodegen,
  41. CurlCodegen,
  42. GoNativeCodegen,
  43. JavaOkhttpCodegen,
  44. JavaUnirestCodegen,
  45. JavascriptFetchCodegen,
  46. JavascriptJqueryCodegen,
  47. JavascriptXhrCodegen,
  48. NodejsAxiosCodegen,
  49. NodejsNativeCodegen,
  50. NodejsRequestCodegen,
  51. NodejsUnirestCodegen,
  52. PhpCurlCodegen,
  53. PowershellRestmethodCodegen,
  54. PythonHttpClientCodegen,
  55. PythonRequestsCodegen,
  56. RubyNetHttpCodeGen,
  57. SalesforceApexCodegen,
  58. ShellHttpieCodegen,
  59. ShellWgetCodegen,
  60. ]
  61. export type HoppCodegenContext = {
  62. name: string
  63. method: string
  64. uri: string
  65. url: string
  66. pathName: string
  67. auth: any // TODO: Change this
  68. httpUser: string | null
  69. httpPassword: string | null
  70. bearerToken: string | null
  71. headers: HoppRESTHeader[]
  72. params: HoppRESTParam[]
  73. bodyParams: FormDataKeyValue[]
  74. rawParams: string | null
  75. rawInput: boolean
  76. rawRequestBody: string | null
  77. contentType: string | null
  78. queryString: string
  79. }
  80. export function generateCodeWithGenerator(
  81. codegenID: string,
  82. context: HoppCodegenContext
  83. ) {
  84. if (codegenID) {
  85. const gen = codegens.find(({ id }) => id === codegenID)
  86. return gen ? gen.generator(context) : ""
  87. }
  88. return ""
  89. }
  90. function getCodegenAuth(
  91. request: EffectiveHoppRESTRequest
  92. ): Pick<
  93. HoppCodegenContext,
  94. "auth" | "bearerToken" | "httpUser" | "httpPassword"
  95. > {
  96. if (!request.auth.authActive || request.auth.authType === "none") {
  97. return {
  98. auth: "None",
  99. httpUser: null,
  100. httpPassword: null,
  101. bearerToken: null,
  102. }
  103. }
  104. if (request.auth.authType === "basic") {
  105. return {
  106. auth: "Basic Auth",
  107. httpUser: request.auth.username,
  108. httpPassword: request.auth.password,
  109. bearerToken: null,
  110. }
  111. } else {
  112. return {
  113. auth: "Bearer Token",
  114. httpUser: null,
  115. httpPassword: null,
  116. bearerToken: request.auth.token,
  117. }
  118. }
  119. }
  120. function getCodegenGeneralRESTInfo(
  121. request: EffectiveHoppRESTRequest
  122. ): Pick<
  123. HoppCodegenContext,
  124. | "name"
  125. | "uri"
  126. | "url"
  127. | "method"
  128. | "queryString"
  129. | "pathName"
  130. | "params"
  131. | "headers"
  132. > {
  133. const urlObj = new URL(request.effectiveFinalURL)
  134. request.effectiveFinalParams.forEach(({ key, value }) => {
  135. urlObj.searchParams.append(key, value)
  136. })
  137. // Remove authorization headers if auth is specified (because see #1798)
  138. const finalHeaders =
  139. request.auth.authActive && request.auth.authType !== "none"
  140. ? request.effectiveFinalHeaders
  141. .filter((x) => x.key.toLowerCase() !== "authorization")
  142. .map((x) => ({ ...x, active: true }))
  143. : request.effectiveFinalHeaders.map((x) => ({ ...x, active: true }))
  144. console.log(finalHeaders)
  145. return {
  146. name: request.name,
  147. uri: request.effectiveFinalURL,
  148. headers: finalHeaders,
  149. params: request.effectiveFinalParams.map((x) => ({ ...x, active: true })),
  150. method: request.method,
  151. url: urlObj.origin,
  152. queryString: urlObj.searchParams.toString(),
  153. pathName: urlObj.pathname,
  154. }
  155. }
  156. function getCodegenReqBodyData(
  157. request: EffectiveHoppRESTRequest
  158. ): Pick<
  159. HoppCodegenContext,
  160. "rawRequestBody" | "rawInput" | "contentType" | "bodyParams" | "rawParams"
  161. > {
  162. return {
  163. contentType: request.body.contentType,
  164. rawInput: request.body.contentType !== "multipart/form-data",
  165. rawRequestBody:
  166. request.body.contentType !== "multipart/form-data"
  167. ? request.body.body
  168. : null,
  169. bodyParams:
  170. request.body.contentType === "multipart/form-data"
  171. ? request.body.body
  172. : [],
  173. rawParams:
  174. request.body.contentType !== "multipart/form-data"
  175. ? request.body.body
  176. : null,
  177. }
  178. }
  179. export function generateCodegenContext(
  180. request: EffectiveHoppRESTRequest
  181. ): HoppCodegenContext {
  182. return {
  183. ...getCodegenAuth(request),
  184. ...getCodegenGeneralRESTInfo(request),
  185. ...getCodegenReqBodyData(request),
  186. }
  187. }