@gopowerteam/request-generate 0.3.1 → 0.3.3

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