fastify-flux-cli 2.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/LICENSE +19 -0
  2. package/cli.js +13 -0
  3. package/dist/commands/build.d.ts +3 -0
  4. package/dist/commands/build.d.ts.map +1 -0
  5. package/dist/commands/build.js +18 -0
  6. package/dist/commands/build.js.map +1 -0
  7. package/dist/commands/new.d.ts +1 -0
  8. package/dist/commands/new.d.ts.map +1 -0
  9. package/dist/commands/new.js +2 -0
  10. package/dist/commands/new.js.map +1 -0
  11. package/dist/commands/sdk.d.ts +3 -0
  12. package/dist/commands/sdk.d.ts.map +1 -0
  13. package/dist/commands/sdk.js +16 -0
  14. package/dist/commands/sdk.js.map +1 -0
  15. package/dist/commands/start.d.ts +3 -0
  16. package/dist/commands/start.d.ts.map +1 -0
  17. package/dist/commands/start.js +128 -0
  18. package/dist/commands/start.js.map +1 -0
  19. package/dist/helper/api-sdk.js +16 -0
  20. package/dist/helper/api-sdk.js.map +1 -0
  21. package/dist/helper/config.d.ts +3 -0
  22. package/dist/helper/config.d.ts.map +1 -0
  23. package/dist/helper/config.js +31 -0
  24. package/dist/helper/config.js.map +1 -0
  25. package/dist/helper/esbuild.d.ts +2 -0
  26. package/dist/helper/esbuild.d.ts.map +1 -0
  27. package/dist/helper/esbuild.js +43 -0
  28. package/dist/helper/esbuild.js.map +1 -0
  29. package/dist/helper/generateMeta.d.ts +2 -0
  30. package/dist/helper/generateMeta.d.ts.map +1 -0
  31. package/dist/helper/generateMeta.js +27 -0
  32. package/dist/helper/generateMeta.js.map +1 -0
  33. package/dist/helper/generateSchema.d.ts +13 -0
  34. package/dist/helper/generateSchema.d.ts.map +1 -0
  35. package/dist/helper/generateSchema.js +63 -0
  36. package/dist/helper/generateSchema.js.map +1 -0
  37. package/dist/helper/generateSdk.d.ts +3 -0
  38. package/dist/helper/generateSdk.d.ts.map +1 -0
  39. package/dist/helper/generateSdk.js +35 -0
  40. package/dist/helper/generateSdk.js.map +1 -0
  41. package/dist/helper/getControllerFunctions.d.ts +7 -0
  42. package/dist/helper/getControllerFunctions.d.ts.map +1 -0
  43. package/dist/helper/getControllerFunctions.js +62 -0
  44. package/dist/helper/getControllerFunctions.js.map +1 -0
  45. package/dist/helper/index.d.ts +5 -0
  46. package/dist/helper/index.d.ts.map +1 -0
  47. package/dist/helper/index.js +24 -0
  48. package/dist/helper/index.js.map +1 -0
  49. package/dist/helper/killProcess.d.ts +2 -0
  50. package/dist/helper/killProcess.d.ts.map +1 -0
  51. package/dist/helper/killProcess.js +11 -0
  52. package/dist/helper/killProcess.js.map +1 -0
  53. package/dist/helper/prettify.d.ts +1 -0
  54. package/dist/helper/prettify.d.ts.map +1 -0
  55. package/dist/helper/prettify.js +2 -0
  56. package/dist/helper/prettify.js.map +1 -0
  57. package/dist/helper/writeFile.d.ts +2 -0
  58. package/dist/helper/writeFile.d.ts.map +1 -0
  59. package/dist/helper/writeFile.js +12 -0
  60. package/dist/helper/writeFile.js.map +1 -0
  61. package/dist/index.d.ts +2 -0
  62. package/dist/index.d.ts.map +1 -0
  63. package/dist/index.js +11 -0
  64. package/dist/index.js.map +1 -0
  65. package/dist/log.d.ts +9 -0
  66. package/dist/log.d.ts.map +1 -0
  67. package/dist/log.js +24 -0
  68. package/dist/log.js.map +1 -0
  69. package/dist/piscina/index.d.ts +9 -0
  70. package/dist/piscina/index.d.ts.map +1 -0
  71. package/dist/piscina/index.js +24 -0
  72. package/dist/piscina/index.js.map +1 -0
  73. package/dist/piscina/worker.d.ts +8 -0
  74. package/dist/piscina/worker.d.ts.map +1 -0
  75. package/dist/piscina/worker.js +35 -0
  76. package/dist/piscina/worker.js.map +1 -0
  77. package/dist/schema/__tests__/emptObject.test.d.ts +2 -0
  78. package/dist/schema/__tests__/emptObject.test.d.ts.map +1 -0
  79. package/dist/schema/__tests__/emptObject.test.js +27 -0
  80. package/dist/schema/__tests__/emptObject.test.js.map +1 -0
  81. package/dist/schema/__tests__/nullableSchema.test.d.ts +2 -0
  82. package/dist/schema/__tests__/nullableSchema.test.d.ts.map +1 -0
  83. package/dist/schema/__tests__/nullableSchema.test.js +141 -0
  84. package/dist/schema/__tests__/nullableSchema.test.js.map +1 -0
  85. package/dist/schema/convertNullToNullable.d.ts +3 -0
  86. package/dist/schema/convertNullToNullable.d.ts.map +1 -0
  87. package/dist/schema/convertNullToNullable.js +23 -0
  88. package/dist/schema/convertNullToNullable.js.map +1 -0
  89. package/dist/schema/generateSchema.d.ts +13 -0
  90. package/dist/schema/generateSchema.d.ts.map +1 -0
  91. package/dist/schema/generateSchema.js +66 -0
  92. package/dist/schema/generateSchema.js.map +1 -0
  93. package/dist/schema/helper/add-properties.d.ts +2 -0
  94. package/dist/schema/helper/add-properties.d.ts.map +1 -0
  95. package/dist/schema/helper/add-properties.js +11 -0
  96. package/dist/schema/helper/add-properties.js.map +1 -0
  97. package/dist/schema/helper/convertEmptyObject.d.ts +2 -0
  98. package/dist/schema/helper/convertEmptyObject.d.ts.map +1 -0
  99. package/dist/schema/helper/convertEmptyObject.js +9 -0
  100. package/dist/schema/helper/convertEmptyObject.js.map +1 -0
  101. package/dist/schema/helper/convertNullToNullable.d.ts +2 -0
  102. package/dist/schema/helper/convertNullToNullable.d.ts.map +1 -0
  103. package/dist/schema/helper/convertNullToNullable.js +33 -0
  104. package/dist/schema/helper/convertNullToNullable.js.map +1 -0
  105. package/dist/schema/nullableSchema.test.d.ts +2 -0
  106. package/dist/schema/nullableSchema.test.d.ts.map +1 -0
  107. package/dist/schema/nullableSchema.test.js +93 -0
  108. package/dist/schema/nullableSchema.test.js.map +1 -0
  109. package/dist/types.d.ts +13 -0
  110. package/dist/types.d.ts.map +1 -0
  111. package/dist/types.js +2 -0
  112. package/dist/types.js.map +1 -0
  113. package/package.json +57 -0
  114. package/sdk-templates/api.eta +65 -0
  115. package/sdk-templates/axios-http-client.eta +133 -0
  116. package/sdk-templates/http-client.eta +3 -0
  117. package/sdk-templates/procedure-call.eta +99 -0
  118. package/sdk-templates/route-types.eta +28 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convertEmptyObject.d.ts","sourceRoot":"","sources":["../../../src/schema/helper/convertEmptyObject.ts"],"names":[],"mappings":"AAEA,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAMnD"}
@@ -0,0 +1,9 @@
1
+ import _ from 'lodash';
2
+ export function convertEmptyObject(object) {
3
+ return _.cloneDeepWith(object, (value) => {
4
+ if (_.isEqual(value, { type: 'object' })) {
5
+ return { ...value, additionalProperties: true };
6
+ }
7
+ });
8
+ }
9
+ //# sourceMappingURL=convertEmptyObject.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convertEmptyObject.js","sourceRoot":"","sources":["../../../src/schema/helper/convertEmptyObject.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ,CAAC;AAEvB,MAAM,UAAU,kBAAkB,CAAC,MAAW;IAC5C,OAAO,CAAC,CAAC,aAAa,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;QACvC,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,EAAE;YACxC,OAAO,EAAE,GAAG,KAAK,EAAE,oBAAoB,EAAE,IAAI,EAAE,CAAC;SACjD;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function convertNullToNullable(object: any): any;
2
+ //# sourceMappingURL=convertNullToNullable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convertNullToNullable.d.ts","sourceRoot":"","sources":["../../../src/schema/helper/convertNullToNullable.ts"],"names":[],"mappings":"AAaA,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAuBtD"}
@@ -0,0 +1,33 @@
1
+ import _ from 'lodash';
2
+ function handleAnyOf(anyOf) {
3
+ const hasTypeNull = anyOf.find((x) => x.type === 'null');
4
+ if (!hasTypeNull || anyOf.length !== 2) {
5
+ return;
6
+ }
7
+ const noneNull = anyOf.find((x) => x.type !== 'null');
8
+ return { nullable: true, ...noneNull };
9
+ }
10
+ export function convertNullToNullable(object) {
11
+ return _.cloneDeepWith(object, (value) => {
12
+ if (value['anyOf']) {
13
+ const result = handleAnyOf(value['anyOf']);
14
+ if (result) {
15
+ return convertNullToNullable(result);
16
+ }
17
+ }
18
+ if (value.type && _.isArray(value.type) && value.type.includes('null')) {
19
+ if (value.type.includes('null')) {
20
+ value.nullable = true;
21
+ }
22
+ const notNullTypes = value.type.filter((x) => x !== 'null');
23
+ if (notNullTypes.length === 1) {
24
+ value.type = notNullTypes[0];
25
+ }
26
+ else {
27
+ value.type = notNullTypes;
28
+ }
29
+ return convertNullToNullable(value);
30
+ }
31
+ });
32
+ }
33
+ //# sourceMappingURL=convertNullToNullable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convertNullToNullable.js","sourceRoot":"","sources":["../../../src/schema/helper/convertNullToNullable.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ,CAAC;AAEvB,SAAS,WAAW,CAAC,KAA2B;IAC9C,MAAM,WAAW,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAM,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC;IAC9D,IAAI,CAAC,WAAW,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE;QACtC,OAAO;KACR;IAED,MAAM,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAM,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC;IAE3D,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,QAAQ,EAAE,CAAC;AACzC,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,MAAW;IAC/C,OAAO,CAAC,CAAC,aAAa,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;QACvC,IAAI,KAAK,CAAC,OAAO,CAAC,EAAE;YAClB,MAAM,MAAM,GAAG,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;YAC3C,IAAI,MAAM,EAAE;gBACV,OAAO,qBAAqB,CAAC,MAAM,CAAC,CAAC;aACtC;SACF;QAED,IAAI,KAAK,CAAC,IAAI,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;YACtE,IAAI,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;gBAC/B,KAAK,CAAC,QAAQ,GAAG,IAAI,CAAC;aACvB;YACD,MAAM,YAAY,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC;YACpE,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE;gBAC7B,KAAK,CAAC,IAAI,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;aAC9B;iBAAM;gBACL,KAAK,CAAC,IAAI,GAAG,YAAY,CAAC;aAC3B;YAED,OAAO,qBAAqB,CAAC,KAAK,CAAC,CAAC;SACrC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=nullableSchema.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nullableSchema.test.d.ts","sourceRoot":"","sources":["../../src/schema/nullableSchema.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,93 @@
1
+ import { convertNullToNullable } from './convertNullToNullable';
2
+ const input = {
3
+ UpdateTodo: {
4
+ type: 'object',
5
+ properties: {
6
+ text: {
7
+ type: 'string',
8
+ },
9
+ foo: {
10
+ anyOf: [
11
+ {
12
+ type: 'null',
13
+ },
14
+ {
15
+ type: 'string',
16
+ },
17
+ {
18
+ type: 'number',
19
+ },
20
+ ],
21
+ },
22
+ user: {
23
+ anyOf: [
24
+ {
25
+ type: 'null',
26
+ },
27
+ {
28
+ type: 'object',
29
+ properties: {
30
+ id: {
31
+ type: 'number',
32
+ },
33
+ anyOf: [
34
+ {
35
+ type: 'null',
36
+ },
37
+ {
38
+ type: 'object',
39
+ properties: {
40
+ id: {
41
+ type: 'number',
42
+ },
43
+ },
44
+ },
45
+ ],
46
+ },
47
+ },
48
+ ],
49
+ },
50
+ },
51
+ },
52
+ };
53
+ test('convert anyOf to nullable', () => {
54
+ expect(convertNullToNullable(input)).toMatchInlineSnapshot(`
55
+ Object {
56
+ "UpdateTodo": Object {
57
+ "properties": Object {
58
+ "foo": Object {
59
+ "anyOf": Array [
60
+ Object {
61
+ "type": "null",
62
+ },
63
+ Object {
64
+ "type": "string",
65
+ },
66
+ Object {
67
+ "type": "number",
68
+ },
69
+ ],
70
+ },
71
+ "text": Object {
72
+ "type": "string",
73
+ },
74
+ "user": Object {
75
+ "nullable": true,
76
+ "properties": Object {
77
+ "nullable": true,
78
+ "properties": Object {
79
+ "id": Object {
80
+ "type": "number",
81
+ },
82
+ },
83
+ "type": "object",
84
+ },
85
+ "type": "object",
86
+ },
87
+ },
88
+ "type": "object",
89
+ },
90
+ }
91
+ `);
92
+ });
93
+ //# sourceMappingURL=nullableSchema.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nullableSchema.test.js","sourceRoot":"","sources":["../../src/schema/nullableSchema.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAEhE,MAAM,KAAK,GAAG;IACZ,UAAU,EAAE;QACV,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE;YACV,IAAI,EAAE;gBACJ,IAAI,EAAE,QAAQ;aACf;YACD,GAAG,EAAE;gBACH,KAAK,EAAE;oBACL;wBACE,IAAI,EAAE,MAAM;qBACb;oBACD;wBACE,IAAI,EAAE,QAAQ;qBACf;oBACD;wBACE,IAAI,EAAE,QAAQ;qBACf;iBACF;aACF;YACD,IAAI,EAAE;gBACJ,KAAK,EAAE;oBACL;wBACE,IAAI,EAAE,MAAM;qBACb;oBACD;wBACE,IAAI,EAAE,QAAQ;wBACd,UAAU,EAAE;4BACV,EAAE,EAAE;gCACF,IAAI,EAAE,QAAQ;6BACf;4BACD,KAAK,EAAE;gCACL;oCACE,IAAI,EAAE,MAAM;iCACb;gCACD;oCACE,IAAI,EAAE,QAAQ;oCACd,UAAU,EAAE;wCACV,EAAE,EAAE;4CACF,IAAI,EAAE,QAAQ;yCACf;qCACF;iCACF;6BACF;yBACF;qBACF;iBACF;aACF;SACF;KACF;CACF,CAAC;AAEF,IAAI,CAAC,2BAA2B,EAAE,GAAG,EAAE;IACrC,MAAM,CAAC,qBAAqB,CAAC,KAAK,CAAC,CAAC,CAAC,qBAAqB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqC1D,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { BuildOptions } from 'esbuild';
2
+ import { GenerateApiParams } from 'swagger-typescript-api';
3
+ export interface FluxProjectConfig {
4
+ sdk?: GenerateApiParams;
5
+ run: string[];
6
+ }
7
+ export interface FluxCliConfig {
8
+ entry: string;
9
+ outdir: string;
10
+ esbuild?: BuildOptions;
11
+ tasks: FluxProjectConfig[];
12
+ }
13
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAE3D,MAAM,WAAW,iBAAiB;IAChC,GAAG,CAAC,EAAE,iBAAiB,CAAC;IACxB,GAAG,EAAE,MAAM,EAAE,CAAC;CACf;AAED,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB,KAAK,EAAE,iBAAiB,EAAE,CAAC;CAC5B"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "fastify-flux-cli",
3
+ "version": "2.19.0",
4
+ "description": "",
5
+ "main": "./dist/index.js",
6
+ "exports": "./dist/index.js",
7
+ "bin": {
8
+ "flux": "./cli.js"
9
+ },
10
+ "author": "",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "@apidevtools/json-schema-ref-parser": "^9.0.9",
14
+ "@types/lodash": "^4.14.182",
15
+ "chalk": "^5.0.1",
16
+ "chokidar": "^3.5.3",
17
+ "commander": "^9.4.0",
18
+ "esbuild": "^0.14.49",
19
+ "execa": "^6.1.0",
20
+ "fast-glob": "^3.2.11",
21
+ "lodash": "^4.17.21",
22
+ "p-map": "5.5.0",
23
+ "pidtree": "^0.6.0",
24
+ "piscina": "^3.2.0",
25
+ "source-map-support": "^0.5.21",
26
+ "swagger-typescript-api": "^9.3.1",
27
+ "ts-json-schema-generator": "^1.0.0",
28
+ "ts-morph": "^15.1.0",
29
+ "typescript": "4.7.4"
30
+ },
31
+ "devDependencies": {
32
+ "@fluxapi/common": "npm:fastify-flux@2.19.0",
33
+ "@types/fs-extra": "^9.0.13",
34
+ "@types/jest": "^28.1.6",
35
+ "@types/node": "^18.0.6",
36
+ "jest": "^28.1.3",
37
+ "ts-jest": "^28.0.7"
38
+ },
39
+ "peerDependencies": {
40
+ "esbuild": "0.x"
41
+ },
42
+ "files": [
43
+ "cli.js",
44
+ "dist/",
45
+ "sdk-templates/"
46
+ ],
47
+ "type": "module",
48
+ "engines": {
49
+ "node": ">=14"
50
+ },
51
+ "scripts": {
52
+ "clean": "rm -rf dist/",
53
+ "test": "jest",
54
+ "dev": "tsc --watch",
55
+ "build": "tsc"
56
+ }
57
+ }
@@ -0,0 +1,65 @@
1
+ <%
2
+ const { apiConfig, routes, utils, config } = it;
3
+ const { info, servers, externalDocs } = apiConfig;
4
+ const { _, require, formatDescription } = utils;
5
+
6
+ const server = (servers && servers[0]) || { url: "" };
7
+
8
+ const descriptionLines = _.compact([
9
+ `@title ${info.title || "No title"}`,
10
+ info.version && `@version ${info.version}`,
11
+ info.license && `@license ${_.compact([
12
+ info.license.name,
13
+ info.license.url && `(${info.license.url})`,
14
+ ]).join(" ")}`,
15
+ info.termsOfService && `@termsOfService ${info.termsOfService}`,
16
+ server.url && `@baseUrl ${server.url}`,
17
+ externalDocs.url && `@externalDocs ${externalDocs.url}`,
18
+ info.contact && `@contact ${_.compact([
19
+ info.contact.name,
20
+ info.contact.email && `<${info.contact.email}>`,
21
+ info.contact.url && `(${info.contact.url})`,
22
+ ]).join(" ")}`,
23
+ info.description && " ",
24
+ info.description && _.replace(formatDescription(info.description), /\n/g, "\n * "),
25
+ ]);
26
+
27
+ %>
28
+
29
+ <% if (config.httpClientType === config.constants.HTTP_CLIENT.AXIOS) { %> import { AxiosRequestConfig, AxiosResponse } from "axios"; <% } %>
30
+
31
+ <% if (descriptionLines.length) { %>
32
+ /**
33
+ <% descriptionLines.forEach((descriptionLine) => { %>
34
+ * <%~ descriptionLine %>
35
+
36
+ <% }) %>
37
+ */
38
+ <% } %>
39
+ export class Api<SecurityDataType extends unknown><% if (!config.singleHttpClient) { %> extends HttpClient<SecurityDataType> <% } %> {
40
+
41
+ <% if(config.singleHttpClient) { %>
42
+ http: HttpClient<SecurityDataType>;
43
+
44
+ constructor (http: HttpClient<SecurityDataType>) {
45
+ this.http = http;
46
+ }
47
+ <% } %>
48
+
49
+
50
+ <% routes.outOfModule && routes.outOfModule.forEach((route) => { %>
51
+
52
+ <%~ includeFile('./procedure-call.eta', { ...it, route }) %>
53
+
54
+ <% }) %>
55
+
56
+ <% routes.combined && routes.combined.forEach(({ routes = [], moduleName }) => { %>
57
+ <%~ moduleName %> = {
58
+ <% routes.forEach((route) => { %>
59
+
60
+ <%~ includeFile('./procedure-call.eta', { ...it, route }) %>
61
+
62
+ <% }) %>
63
+ }
64
+ <% }) %>
65
+ }
@@ -0,0 +1,133 @@
1
+ <%
2
+ const { apiConfig, generateResponses, config } = it;
3
+ %>
4
+
5
+ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, ResponseType } from "axios";
6
+
7
+ export type QueryParamsType = Record<string | number, any>;
8
+
9
+ export interface FullRequestParams extends Omit<AxiosRequestConfig, "data" | "params" | "url" | "responseType"> {
10
+ /** set parameter to `true` for call `securityWorker` for this request */
11
+ secure?: boolean;
12
+ /** request path */
13
+ path: string;
14
+ /** content type of request body */
15
+ type?: ContentType;
16
+ /** query params */
17
+ query?: QueryParamsType;
18
+ /** format of response (i.e. response.json() -> format: "json") */
19
+ format?: ResponseType;
20
+ /** request body */
21
+ body?: unknown;
22
+ }
23
+
24
+ export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
25
+
26
+ export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequestConfig, "data" | "cancelToken"> {
27
+ securityWorker?: (securityData: SecurityDataType | null) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void;
28
+ secure?: boolean;
29
+ format?: ResponseType;
30
+ }
31
+
32
+ export enum ContentType {
33
+ Json = "application/json",
34
+ FormData = "multipart/form-data",
35
+ UrlEncoded = "application/x-www-form-urlencoded",
36
+ }
37
+
38
+ export class HttpClient<SecurityDataType = unknown> {
39
+ public instance: AxiosInstance;
40
+ private securityData: SecurityDataType | null = null;
41
+ private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
42
+ private secure?: boolean;
43
+ private format?: ResponseType;
44
+
45
+ constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
46
+ this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" })
47
+ this.secure = secure;
48
+ this.format = format;
49
+ this.securityWorker = securityWorker;
50
+ }
51
+
52
+ public setSecurityData = (data: SecurityDataType | null) => {
53
+ this.securityData = data
54
+ }
55
+
56
+ private mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig {
57
+ return {
58
+ ...this.instance.defaults,
59
+ ...params1,
60
+ ...(params2 || {}),
61
+ headers: {
62
+ ...(params1.headers || {}),
63
+ ...((params2 && params2.headers) || {}),
64
+ },
65
+ };
66
+ }
67
+
68
+ private createFormData(input: Record<string, unknown>): FormData {
69
+ return Object.keys(input || {}).reduce((formData, key) => {
70
+ const property = input[key];
71
+ formData.append(
72
+ key,
73
+ property instanceof Blob ?
74
+ property :
75
+ typeof property === "object" && property !== null ?
76
+ JSON.stringify(property) :
77
+ `${property}`
78
+ );
79
+ return formData;
80
+ }, new FormData())
81
+ }
82
+
83
+ public request = async <T = any, _E = any>({
84
+ secure,
85
+ path,
86
+ type,
87
+ query,
88
+ format,
89
+ body,
90
+ ...params
91
+ }: FullRequestParams): Promise<T> => {
92
+ const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
93
+ const requestParams = this.mergeRequestParams(params, secureParams);
94
+ const responseFormat = (format && this.format) || void 0;
95
+
96
+ if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
97
+ body = this.createFormData(body as Record<string, unknown>);
98
+ }
99
+
100
+ if (!type) {
101
+ type = ContentType.Json;
102
+ }
103
+
104
+ if (!body) {
105
+ body = {};
106
+ }
107
+
108
+ try {
109
+ const result: any = await this.instance.request({
110
+ ...requestParams,
111
+ headers: {
112
+ ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
113
+ ...(requestParams.headers || {}),
114
+ },
115
+ params: query,
116
+ responseType: responseFormat,
117
+ data: body,
118
+ url: path,
119
+ });
120
+
121
+ return result.data;
122
+ } catch(err) {
123
+ if (axios.isAxiosError(err)) {
124
+ err.message += ` [${err.config.method}] ${err.config.url}`;
125
+ if (err.response) {
126
+ (err as any).data = err.response.data;
127
+ }
128
+ }
129
+
130
+ throw err;
131
+ }
132
+ };
133
+ }
@@ -0,0 +1,3 @@
1
+ <% const { config } = it; %>
2
+ <% /* https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-clients/ */ %>
3
+ <%~ includeFile(`./axios-http-client`, it) %>
@@ -0,0 +1,99 @@
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("@base/route-docs", { config, route, utils });
9
+ const queryName = (query && query.name) || "query";
10
+ const pathParams = _.values(parameters);
11
+ const pathParamsNames = _.map(pathParams, "name");
12
+
13
+ const isFetchTemplate = config.httpClientType === HTTP_CLIENT.FETCH;
14
+
15
+ const requestConfigParam = {
16
+ name: specificArgNameResolver.resolve(RESERVED_REQ_PARAMS_ARG_NAMES),
17
+ optional: true,
18
+ type: "RequestParams",
19
+ defaultValue: "{}",
20
+ }
21
+
22
+ const argToTmpl = ({ name, optional, type, defaultValue }) => `${name}${!defaultValue && optional ? '?' : ''}: ${type}${defaultValue ? ` = ${defaultValue}` : ''}`;
23
+
24
+ const rawWrapperArgs = config.extractRequestParams ?
25
+ _.compact([
26
+ requestParams && {
27
+ name: pathParams.length ? `{ ${_.join(pathParamsNames, ", ")}, ...${queryName} }` : queryName,
28
+ optional: false,
29
+ type: getInlineParseContent(requestParams),
30
+ },
31
+ ...(!requestParams ? pathParams : []),
32
+ payload,
33
+ requestConfigParam,
34
+ ]) :
35
+ _.compact([
36
+ ...pathParams,
37
+ query,
38
+ payload,
39
+ requestConfigParam,
40
+ ])
41
+
42
+ const wrapperArgs = _
43
+ // Sort by optionality
44
+ .sortBy(rawWrapperArgs, [o => o.optional])
45
+ .map(argToTmpl)
46
+ .join(', ')
47
+
48
+ // RequestParams["type"]
49
+ const requestContentKind = {
50
+ "JSON": "ContentType.Json",
51
+ "URL_ENCODED": "ContentType.UrlEncoded",
52
+ "FORM_DATA": "ContentType.FormData",
53
+ }
54
+ // RequestParams["format"]
55
+ const responseContentKind = {
56
+ "JSON": '"json"',
57
+ "IMAGE": '"blob"',
58
+ "FORM_DATA": isFetchTemplate ? '"formData"' : '"document"'
59
+ }
60
+
61
+ const bodyTmpl = _.get(payload, "name") || null;
62
+ const queryTmpl = (query != null && queryName) || null;
63
+ const bodyContentKindTmpl = requestContentKind[requestBodyInfo.contentKind] || null;
64
+ const responseFormatTmpl = responseContentKind[responseBodyInfo.success && responseBodyInfo.success.schema && responseBodyInfo.success.schema.contentKind] || null;
65
+ const securityTmpl = security ? 'true' : null;
66
+
67
+ const describeReturnType = () => {
68
+ if (!config.toJS) return "";
69
+
70
+ switch(config.httpClientType) {
71
+ case HTTP_CLIENT.AXIOS: {
72
+ return `Promise<AxiosResponse<${type}>>`
73
+ }
74
+ default: {
75
+ return `Promise<HttpResponse<${type}, ${errorType}>`
76
+ }
77
+ }
78
+ }
79
+
80
+ %>
81
+ /**
82
+ <%~ routeDocs.description %>
83
+
84
+ * <% /* Here you can add some other JSDoc tags */ %>
85
+
86
+ <%~ routeDocs.lines %>
87
+
88
+ */
89
+ <%~ route.routeName.usage %><%~ route.namespace ? ': ' : ' = ' %>(<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
90
+ <%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
91
+ path: `<%~ path %>`,
92
+ method: '<%~ _.upperCase(method) %>',
93
+ <%~ queryTmpl ? `query: ${queryTmpl},` : '' %>
94
+ <%~ bodyTmpl ? `body: ${bodyTmpl},` : '' %>
95
+ <%~ securityTmpl ? `secure: ${securityTmpl},` : '' %>
96
+ <%~ bodyContentKindTmpl ? `type: ${bodyContentKindTmpl},` : '' %>
97
+ <%~ responseFormatTmpl ? `format: ${responseFormatTmpl},` : '' %>
98
+ ...<%~ _.get(requestConfigParam, "name") %>,
99
+ })<%~ route.namespace ? ',' : '' %>
@@ -0,0 +1,28 @@
1
+ <%
2
+ const { utils, config, routes, modelTypes } = it;
3
+ const { _, classNameCase } = utils;
4
+ const dataContracts = config.modular ? _.map(modelTypes, "name") : [];
5
+ %>
6
+
7
+ <% if (dataContracts.length) { %>
8
+ import { <%~ dataContracts.join(", ") %> } from "./<%~ config.fileNames.dataContracts %>"
9
+ <% } %>
10
+
11
+ <%
12
+ /* TODO: outOfModule, combined should be attributes of route, which will allow to avoid duplication of code */
13
+ %>
14
+
15
+ <% routes.outOfModule && routes.outOfModule.forEach(({ routes = [] }) => { %>
16
+ <% routes.forEach((route) => { %>
17
+ <%~ includeFile('@base/route-type.eta', { ...it, route }) %>
18
+ <% }) %>
19
+ <% }) %>
20
+
21
+ <% routes.combined && routes.combined.forEach(({ routes = [], moduleName }) => { %>
22
+ export namespace <%~ classNameCase(moduleName) %> {
23
+ <% routes.forEach((route) => { %>
24
+ <%~ includeFile('@base/route-type.eta', { ...it, route }) %>
25
+ <% }) %>
26
+ }
27
+
28
+ <% }) %>