@gopowerteam/request-generate 0.2.3 → 0.3.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 (34) hide show
  1. package/dist/chunk-C0xms8kb.cjs +34 -0
  2. package/dist/index.cjs +1105 -0
  3. package/dist/index.d.cts +739 -0
  4. package/dist/index.d.mts +739 -0
  5. package/dist/index.mjs +937 -1040
  6. package/dist/vite-plugin/index.cjs +238 -0
  7. package/dist/vite-plugin/index.d.cts +13 -0
  8. package/dist/vite-plugin/index.d.mts +14 -0
  9. package/dist/vite-plugin/index.mjs +118 -118
  10. package/package.json +29 -29
  11. package/dist/chunk-WF3XBEPN.mjs +0 -18
  12. package/dist/chunk-XXPGZHWZ.js +0 -10
  13. package/dist/index.d.ts +0 -165
  14. package/dist/index.js +0 -1203
  15. package/dist/vite-plugin/index.d.ts +0 -13
  16. package/dist/vite-plugin/index.js +0 -233
  17. /package/dist/templates/{export-model.hbs → templates/export-model.hbs} +0 -0
  18. /package/dist/templates/{export-service.hbs → templates/export-service.hbs} +0 -0
  19. /package/dist/templates/{partials → templates/partials}/export-description.hbs +0 -0
  20. /package/dist/templates/{partials → templates/partials}/export-header.hbs +0 -0
  21. /package/dist/templates/{partials → templates/partials}/export-model-field.hbs +0 -0
  22. /package/dist/templates/{partials → templates/partials}/export-model-import.hbs +0 -0
  23. /package/dist/templates/{partials → templates/partials}/export-model-type.hbs +0 -0
  24. /package/dist/templates/{partials → templates/partials}/export-operation-params-body.hbs +0 -0
  25. /package/dist/templates/{partials → templates/partials}/export-operation-params-path.hbs +0 -0
  26. /package/dist/templates/{partials → templates/partials}/export-operation-params-query.hbs +0 -0
  27. /package/dist/templates/{partials → templates/partials}/export-operation-response.hbs +0 -0
  28. /package/dist/templates/{partials → templates/partials}/export-schema-type.hbs +0 -0
  29. /package/dist/templates/{partials → templates/partials}/export-service-class.hbs +0 -0
  30. /package/dist/templates/{partials → templates/partials}/export-service-import.hbs +0 -0
  31. /package/dist/templates/{partials → templates/partials}/export-service-namespace-type.hbs +0 -0
  32. /package/dist/templates/{partials → templates/partials}/export-service-namespace.hbs +0 -0
  33. /package/dist/templates/{partials → templates/partials}/export-service-operation.hbs +0 -0
  34. /package/dist/templates/{partials → templates/partials}/is-required.hbs +0 -0
@@ -1,13 +0,0 @@
1
- import { PluginOption } from 'vite';
2
-
3
- interface PluginOptions {
4
- alias: string;
5
- dir: string;
6
- dts: string | boolean;
7
- }
8
- /**
9
- * Request插件
10
- */
11
- declare const _default: (options: PluginOptions) => PluginOption;
12
-
13
- export { _default as default };
@@ -1,233 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }require('../chunk-XXPGZHWZ.js');
2
-
3
- // src/vite-plugin/index.ts
4
- var _child_process = require('child_process');
5
- var _fs = require('fs'); var fs = _interopRequireWildcard(_fs);
6
- var _path = require('path'); var path = _interopRequireWildcard(_path);
7
- var _process = require('process'); var _process2 = _interopRequireDefault(_process);
8
- var _handlebars = require('handlebars'); var _handlebars2 = _interopRequireDefault(_handlebars);
9
-
10
- // src/vite-plugin/templates.ts
11
- var useRequestFunctionCodeTemplate = `
12
- export function useRequest(
13
- ...selects
14
- ) {
15
- const services = selects.map((select)=>{
16
- const service = select(serviceMap)
17
- return new service()
18
- })
19
-
20
- if(services.length === 1){
21
- return services[0]
22
- }else{
23
- return services
24
- }
25
- }
26
- `;
27
- var useRequestFunctionTypeTemplate = `
28
- export function useRequest<T1,T2,T3,T4,T5,T6>(
29
- select1: ((services: typeof serviceMap) => { new (): T1 }),
30
- select2: ((services: typeof serviceMap) => { new (): T2 }),
31
- select3: ((services: typeof serviceMap) => { new (): T3 }),
32
- select4: ((services: typeof serviceMap) => { new (): T4 }),
33
- select5: ((services: typeof serviceMap) => { new (): T5 }),
34
- select6: ((services: typeof serviceMap) => { new (): T6 }),
35
- ): [T1,T2,T3,T4,T5,T6]
36
- export function useRequest<T1,T2,T3,T4,T5>(
37
- select1: ((services: typeof serviceMap) => { new (): T1 }),
38
- select2: ((services: typeof serviceMap) => { new (): T2 }),
39
- select3: ((services: typeof serviceMap) => { new (): T3 }),
40
- select4: ((services: typeof serviceMap) => { new (): T4 }),
41
- select5: ((services: typeof serviceMap) => { new (): T5 }),
42
- ): [T1,T2,T3,T4,T5]
43
- export function useRequest<T1,T2,T3,T4>(
44
- select1: ((services: typeof serviceMap) => { new (): T1 }),
45
- select2: ((services: typeof serviceMap) => { new (): T2 }),
46
- select3: ((services: typeof serviceMap) => { new (): T3 }),
47
- select4: ((services: typeof serviceMap) => { new (): T4 }),
48
- ): [T1,T2,T3,T4]
49
- export function useRequest<T1,T2,T3>(
50
- select1: ((services: typeof serviceMap) => { new (): T1 }),
51
- select2: ((services: typeof serviceMap) => { new (): T2 }),
52
- select3: ((services: typeof serviceMap) => { new (): T3 }),
53
- ): [T1,T2,T3]
54
- export function useRequest<T1,T2>(
55
- select1: ((services: typeof serviceMap) => { new (): T1 }),
56
- select2: ((services: typeof serviceMap) => { new (): T2 }),
57
- ): [T1,T2]
58
- export function useRequest<T>(
59
- select: ((services: typeof serviceMap) => { new (): T })
60
- ): T
61
- export function useRequest<T>(
62
- ...selects: ((services: typeof serviceMap) => { new (): T })[]
63
- ): T | T[]
64
- `;
65
- var importServiceTemplate = `
66
- {{#if groups}}
67
- {{#each groups}}
68
- {{#each services}}
69
- import { {{{name}}} as {{group}}_{{name}} } from '{{path}}'
70
- {{/each}}
71
- {{/each}}
72
- {{else}}
73
- {{#each services}}
74
- import { {{{name}}} } from '{{path}}'
75
- {{/each}}
76
- {{/if}}
77
- `;
78
- var defineServiceMapTemplate = `
79
- {{#if groups}}
80
- const serviceMap = {
81
- {{#each groups}}
82
- {{{name}}}: {
83
- {{#each services}}
84
- {{{name}}}: {{group}}_{{name}},
85
- {{/each}}
86
- },
87
- {{/each}}
88
- }
89
- {{else}}
90
- const serviceMap = {
91
- {{#each services}}
92
- {{{name}}},
93
- {{/each}}
94
- }
95
- {{/if}}
96
- `;
97
- var generateCodeTemplate = `
98
- ${importServiceTemplate.trim()}
99
-
100
- ${defineServiceMapTemplate.trim()}
101
-
102
- ${useRequestFunctionCodeTemplate.trim()}
103
- `;
104
- var generateDeclareTemplate = `declare module '{{{MODULE_ID}}}' {
105
- ${importServiceTemplate.trim()}
106
-
107
- ${defineServiceMapTemplate.trim()}
108
-
109
- ${useRequestFunctionTypeTemplate.trim()}
110
- }
111
- `;
112
-
113
- // src/vite-plugin/index.ts
114
- var MODULE_ID = "virtual:request";
115
- var DECLARATION_FILE = "request.d.ts";
116
- var viteConfig;
117
- var GerneratedCodeStr = "";
118
- function genretateDeclareAndCode(options) {
119
- const paths = getServicePaths(options);
120
- const services = getServiceItems(paths);
121
- const groups = getServiceGroups(services);
122
- if (services && services.length) {
123
- generateCode(services, groups);
124
- if (options.dts !== false) {
125
- generateDeclare(services, groups, options);
126
- }
127
- }
128
- }
129
- function generateRequestCode() {
130
- const generateScript = path.resolve(viteConfig.root, "node_modules", ".bin", "request-generate");
131
- _child_process.execSync.call(void 0, `${generateScript}`, {
132
- env: {
133
- ..._process2.default.env,
134
- FORCE_COLOR: "1"
135
- }
136
- });
137
- }
138
- var vite_plugin_default = (options) => {
139
- return {
140
- name: "vite-plugin-vue-request",
141
- enforce: "pre",
142
- configResolved(config) {
143
- viteConfig = config;
144
- },
145
- resolveId(id) {
146
- return id === MODULE_ID ? MODULE_ID : void 0;
147
- },
148
- async buildStart() {
149
- await generateRequestCode();
150
- await genretateDeclareAndCode(options);
151
- },
152
- load(id) {
153
- if (id !== MODULE_ID)
154
- return;
155
- return GerneratedCodeStr;
156
- }
157
- };
158
- };
159
- function getServicePaths(options) {
160
- const paths = [];
161
- const walk = (dir) => {
162
- fs.readdirSync(dir).forEach((file) => {
163
- const fullpath = path.join(dir, file).replace(/\\/g, "/");
164
- const stat = fs.statSync(fullpath);
165
- if (stat.isFile() && fullpath.endsWith("Service.ts")) {
166
- paths.push(fullpath);
167
- } else if (stat.isDirectory()) {
168
- const subdir = path.join(dir, file);
169
- walk(subdir);
170
- }
171
- });
172
- };
173
- const { replacement } = viteConfig.resolve.alias.find(
174
- (alias) => alias.find === options.alias
175
- );
176
- if (fs.existsSync(path.resolve(options.dir))) {
177
- walk(path.resolve(options.dir));
178
- }
179
- return paths.map(
180
- (filepath) => filepath.replace(replacement.replace(/\\/g, "/"), options.alias)
181
- );
182
- }
183
- function getServiceItems(paths) {
184
- const toCaseString = (str = "") => str.replace(/-(\w)/g, (_, $1) => $1.toUpperCase()).replace(/^\S/, (s) => s.toUpperCase());
185
- return paths.map((filePath) => {
186
- const [name] = /[^\\]+(?=\.ts$)/.exec(toCaseString(path.basename(filePath))) || [];
187
- const [group] = /(?<=^.\/http\/)(.*?)(?=\/.*?Service\.ts$)/.exec(filePath) || [];
188
- return {
189
- name: toCaseString(name),
190
- group: group !== "services" ? toCaseString(group) : void 0,
191
- path: filePath.replace(/\.ts$/g, "")
192
- };
193
- });
194
- }
195
- function getServiceGroups(services) {
196
- return services.reduce((r, s) => {
197
- if (!s.group) {
198
- return r;
199
- }
200
- const group = r.find((x) => x.name === s.group) || {
201
- name: s.group,
202
- services: []
203
- };
204
- group.services.push(s);
205
- if (!r.find((g) => g === group)) {
206
- r.push(group);
207
- }
208
- return r;
209
- }, []);
210
- }
211
- function generateCode(services, groups) {
212
- const template = _handlebars2.default.compile(generateCodeTemplate);
213
- GerneratedCodeStr = template({
214
- groups: groups.length ? groups : void 0,
215
- services
216
- });
217
- }
218
- function generateDeclare(services, groups, options) {
219
- const template = _handlebars2.default.compile(generateDeclareTemplate);
220
- const content = template({
221
- groups: groups.length ? groups : void 0,
222
- services,
223
- MODULE_ID
224
- });
225
- fs.writeFileSync(
226
- path.resolve(viteConfig.root, typeof options.dts === "string" ? options.dts : DECLARATION_FILE),
227
- content.replace(/\r\n/g, "\n"),
228
- "utf-8"
229
- );
230
- }
231
-
232
-
233
- exports.default = vite_plugin_default;