codegen.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 addhttps(url: string) {
  121. if (!/^(?:f|ht)tps?:\/\//.test(url)) {
  122. url = "https://" + url
  123. }
  124. return url
  125. }
  126. function getCodegenGeneralRESTInfo(
  127. request: EffectiveHoppRESTRequest
  128. ): Pick<
  129. HoppCodegenContext,
  130. | "name"
  131. | "uri"
  132. | "url"
  133. | "method"
  134. | "queryString"
  135. | "pathName"
  136. | "params"
  137. | "headers"
  138. > {
  139. let urlObj: URL
  140. try {
  141. urlObj = new URL(request.effectiveFinalURL)
  142. } catch (error) {
  143. urlObj = new URL(addhttps(request.effectiveFinalURL))
  144. }
  145. request.effectiveFinalParams.forEach(({ key, value }) => {
  146. urlObj.searchParams.append(key, value)
  147. })
  148. // Remove authorization headers if auth is specified (because see #1798)
  149. const finalHeaders =
  150. request.auth.authActive && request.auth.authType !== "none"
  151. ? request.effectiveFinalHeaders
  152. .filter((x) => x.key.toLowerCase() !== "authorization")
  153. .map((x) => ({ ...x, active: true }))
  154. : request.effectiveFinalHeaders.map((x) => ({ ...x, active: true }))
  155. return {
  156. name: request.name,
  157. uri: request.effectiveFinalURL,
  158. headers: finalHeaders,
  159. params: request.effectiveFinalParams.map((x) => ({ ...x, active: true })),
  160. method: request.method,
  161. url: urlObj.origin,
  162. queryString: `${urlObj.searchParams}`,
  163. pathName: urlObj.pathname,
  164. }
  165. }
  166. function getCodegenReqBodyData(
  167. request: EffectiveHoppRESTRequest
  168. ): Pick<
  169. HoppCodegenContext,
  170. "rawRequestBody" | "rawInput" | "contentType" | "bodyParams" | "rawParams"
  171. > {
  172. return {
  173. contentType: request.body.contentType,
  174. rawInput: request.body.contentType !== "multipart/form-data",
  175. rawRequestBody:
  176. request.body.contentType !== "multipart/form-data"
  177. ? request.body.body
  178. : null,
  179. bodyParams:
  180. request.body.contentType === "multipart/form-data"
  181. ? request.body.body
  182. : [],
  183. rawParams:
  184. request.body.contentType !== "multipart/form-data"
  185. ? request.body.body
  186. : null,
  187. }
  188. }
  189. export function generateCodegenContext(
  190. request: EffectiveHoppRESTRequest
  191. ): HoppCodegenContext {
  192. return {
  193. ...getCodegenAuth(request),
  194. ...getCodegenGeneralRESTInfo(request),
  195. ...getCodegenReqBodyData(request),
  196. }
  197. }