procedure-call.ejs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <%
  2. const { utils, route, config } = it;
  3. const { requestBodyInfo, responseBodyInfo, specificArgNameResolver } = route;
  4. const { _, getInlineParseContent, getParseContent, parseSchema, getComponentByRef, require } = utils;
  5. const { parameters, path, method, payload, query, formData, security, requestParams } = route.request;
  6. const { type, errorType, contentTypes } = route.response;
  7. const { HTTP_CLIENT, RESERVED_REQ_PARAMS_ARG_NAMES } = config.constants;
  8. const routeDocs = includeFile("./route-docs", { config, route, utils });
  9. const queryName = (query && query.name) || "query";
  10. const pathParams = _.values(parameters);
  11. const pathParamsNames = _.map(pathParams, "name");
  12. const isFetchTemplate = config.httpClientType === HTTP_CLIENT.FETCH;
  13. const requestConfigParam = {
  14. name: specificArgNameResolver.resolve(RESERVED_REQ_PARAMS_ARG_NAMES),
  15. optional: true,
  16. type: "RequestParams",
  17. defaultValue: "{}",
  18. }
  19. const argToTmpl = ({ name, optional, type, defaultValue }) => `${name}${!defaultValue && optional ? '?' : ''}: ${type}${defaultValue ? ` = ${defaultValue}` : ''}`;
  20. const rawWrapperArgs = config.extractRequestParams ?
  21. _.compact([
  22. requestParams && {
  23. name: pathParams.length ? `{ ${_.join(pathParamsNames, ", ")}, ...${queryName} }` : queryName,
  24. optional: false,
  25. type: getInlineParseContent(requestParams),
  26. },
  27. ...(!requestParams ? pathParams : []),
  28. payload,
  29. requestConfigParam,
  30. ]) :
  31. _.compact([
  32. ...pathParams,
  33. query,
  34. payload,
  35. requestConfigParam,
  36. ])
  37. const wrapperArgs = _
  38. // Sort by optionality
  39. .sortBy(rawWrapperArgs, [o => o.optional])
  40. .map(argToTmpl)
  41. .join(', ')
  42. // RequestParams["type"]
  43. const requestContentKind = {
  44. "JSON": "ContentType.Json",
  45. "URL_ENCODED": "ContentType.UrlEncoded",
  46. "FORM_DATA": "ContentType.FormData",
  47. "TEXT": "ContentType.Text",
  48. }
  49. // RequestParams["format"]
  50. const responseContentKind = {
  51. "JSON": '"json"',
  52. "IMAGE": '"blob"',
  53. "FORM_DATA": isFetchTemplate ? '"formData"' : '"document"'
  54. }
  55. const bodyTmpl = _.get(payload, "name") || null;
  56. const queryTmpl = (query != null && queryName) || null;
  57. const bodyContentKindTmpl = requestContentKind[requestBodyInfo.contentKind] || null;
  58. const responseFormatTmpl = responseContentKind[responseBodyInfo.success && responseBodyInfo.success.schema && responseBodyInfo.success.schema.contentKind] || null;
  59. const securityTmpl = security ? 'true' : null;
  60. const describeReturnType = () => {
  61. if (!config.toJS) return "";
  62. switch(config.httpClientType) {
  63. case HTTP_CLIENT.AXIOS: {
  64. return `Promise<AxiosResponse<${type}>>`
  65. }
  66. default: {
  67. return `Promise<HttpResponse<${type}, ${errorType}>`
  68. }
  69. }
  70. }
  71. %>
  72. /**
  73. <%~ routeDocs.description %>
  74. *<% /* Here you can add some other JSDoc tags */ %>
  75. <%~ routeDocs.lines %>
  76. */
  77. <%~ route.routeName.usage %> = (<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
  78. <%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
  79. path: `<%~ path %>`,
  80. method: '<%~ _.upperCase(method) %>',
  81. <%~ queryTmpl ? `query: ${queryTmpl},` : '' %>
  82. <%~ bodyTmpl ? `body: ${bodyTmpl},` : '' %>
  83. <%~ securityTmpl ? `secure: ${securityTmpl},` : '' %>
  84. <%~ bodyContentKindTmpl ? `type: ${bodyContentKindTmpl},` : '' %>
  85. <%~ responseFormatTmpl ? `format: ${responseFormatTmpl},` : '' %>
  86. ...<%~ _.get(requestConfigParam, "name") %>,
  87. })