yapi-apifox-to-typescript 4.2.3 → 5.0.1
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.
- package/lib/cjs/ApifoxToYApiData.js +312 -588
- package/lib/cjs/Generator.js +507 -1171
- package/lib/cjs/SwaggerToYApiServer.js +125 -336
- package/lib/cjs/_virtual/_rolldown/runtime.js +23 -0
- package/lib/cjs/cli.js +171 -280
- package/lib/cjs/helpers.js +125 -209
- package/lib/cjs/index.js +15 -19
- package/lib/cjs/mini-ut.js +220 -0
- package/lib/cjs/swaggerJsonToYApiData.js +268 -482
- package/lib/cjs/types.js +93 -93
- package/lib/cjs/utils.js +364 -769
- package/lib/esm/ApifoxToYApiData.d.ts +49 -0
- package/lib/esm/ApifoxToYApiData.js +310 -572
- package/lib/esm/Generator.d.ts +53 -0
- package/lib/esm/Generator.js +503 -1146
- package/lib/esm/SwaggerToYApiServer.d.ts +23 -0
- package/lib/esm/SwaggerToYApiServer.js +119 -317
- package/lib/esm/_virtual/_rolldown/runtime.js +7 -0
- package/lib/esm/cli.d.ts +4 -0
- package/lib/esm/cli.js +162 -251
- package/lib/esm/helpers.d.ts +49 -0
- package/lib/esm/helpers.js +125 -198
- package/lib/esm/index.d.ts +2 -897
- package/lib/esm/index.js +3 -2
- package/lib/esm/mini-ut.d.ts +105 -0
- package/lib/esm/mini-ut.js +198 -0
- package/lib/esm/swaggerJsonToYApiData.d.ts +6 -0
- package/lib/esm/swaggerJsonToYApiData.js +265 -469
- package/lib/esm/types.d.ts +698 -0
- package/lib/esm/types.js +87 -73
- package/lib/esm/utils.d.ts +95 -0
- package/lib/esm/utils.js +350 -720
- package/package.json +49 -59
package/lib/esm/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { Method, QueryStringArrayFormat, RequestBodyType, RequestFormItemType, RequestParamType, RequestQueryType, Required, ResponseBodyType } from "./types.js";
|
|
2
|
+
import { FileData, defineConfig, parseRequestData, prepare } from "./helpers.js";
|
|
3
|
+
export { FileData, Method, QueryStringArrayFormat, RequestBodyType, RequestFormItemType, RequestParamType, RequestQueryType, Required, ResponseBodyType, defineConfig, parseRequestData, prepare };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 轻量内部工具集。仅提供本项目实际使用的类型与基础函数,
|
|
3
|
+
* 语义与 `lodash`/同名的实现保持一致,避免引入重型依赖。
|
|
4
|
+
*
|
|
5
|
+
* 该文件属于内部实现,不通过 `index.ts` 对外暴露。
|
|
6
|
+
*/
|
|
7
|
+
/** 同步或异步返回值。 */
|
|
8
|
+
export type AsyncOrSync<T> = PromiseLike<T> | T;
|
|
9
|
+
/** 解包 Promise 返回类型的异步函数返回值类型。 */
|
|
10
|
+
export type AsyncReturnType<T extends (...args: any) => PromiseLike<any>> = T extends (...args: any) => PromiseLike<infer R> ? R : never;
|
|
11
|
+
/** 排除 `undefined` 后的类型。 */
|
|
12
|
+
export type Defined<T> = Exclude<T, undefined>;
|
|
13
|
+
/**
|
|
14
|
+
* 字面量联合类型。既能获得字面量自动补全,又允许同基础类型的其它值。
|
|
15
|
+
*/
|
|
16
|
+
export type LiteralUnion<LiteralType, BaseType extends string | number> = LiteralType | (BaseType & {
|
|
17
|
+
_?: never;
|
|
18
|
+
});
|
|
19
|
+
/** 严格约束 `K` 必须是 `T` 的键的 `Omit`。 */
|
|
20
|
+
export type OmitStrict<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
|
21
|
+
/** 同 `T | T[]`。 */
|
|
22
|
+
export type OneOrMore<T> = T | T[];
|
|
23
|
+
/** 是否为数组。 */
|
|
24
|
+
export declare function isArray(value: any): value is any[];
|
|
25
|
+
/** 是否为函数(对齐 `lodash.isFunction`,`class` 也视为函数)。 */
|
|
26
|
+
export declare function isFunction(value: any): value is (...args: any[]) => any;
|
|
27
|
+
/** 是否为对象(`lodash.isObject` 语义:非 `null` 的 `object` 或 `function`)。 */
|
|
28
|
+
export declare function isObject(value: any): value is Record<string, any>;
|
|
29
|
+
/** 是否为纯对象(构造器为 `Object` 或原型为 `null`)。 */
|
|
30
|
+
export declare function isPlainObject(value: any): value is Record<string, any>;
|
|
31
|
+
/** 是否为类 Promise 对象。 */
|
|
32
|
+
export declare function isPromiseLike<T = any>(value: any): value is PromiseLike<T>;
|
|
33
|
+
/**
|
|
34
|
+
* 是否为空(对齐 `lodash.isEmpty`):
|
|
35
|
+
* - `null`/`undefined` -> `true`
|
|
36
|
+
* - 数组 / 字符串 -> `length === 0`
|
|
37
|
+
* - Map / Set -> `size === 0`
|
|
38
|
+
* - 纯对象 -> 没有可枚举自有键
|
|
39
|
+
* - 数字 / 布尔 / 函数 -> `true`
|
|
40
|
+
*/
|
|
41
|
+
export declare function isEmpty(value: any): boolean;
|
|
42
|
+
/** 将任意值转为数组(`lodash.castArray` 语义)。 */
|
|
43
|
+
export declare function castArray<T>(value?: T | T[] | readonly T[]): T[];
|
|
44
|
+
/** 返回数组最后一项,非数组或空数组返回 `undefined`。 */
|
|
45
|
+
export declare function last<T>(list?: readonly T[] | null): T | undefined;
|
|
46
|
+
/** 数组去重(保持首次出现顺序)。 */
|
|
47
|
+
export declare function uniq<T>(list?: readonly T[] | null): T[];
|
|
48
|
+
/** `Object.values` 的宽松版:非对象返回 `[]`。 */
|
|
49
|
+
export declare function values<T>(obj: Record<string, T> | null | undefined): T[];
|
|
50
|
+
/**
|
|
51
|
+
* 遍历集合(`lodash.forEach` 语义)。
|
|
52
|
+
* - 数组:`(value, index)`
|
|
53
|
+
* - 对象:`(value, key)`
|
|
54
|
+
* 回调显式返回 `false` 时中断遍历。
|
|
55
|
+
*/
|
|
56
|
+
export declare function each<T>(collection: readonly T[] | null | undefined, iteratee: (value: T, key: number) => any): void;
|
|
57
|
+
export declare function each<T>(collection: Record<string, T> | null | undefined, iteratee: (value: T, key: string) => any): void;
|
|
58
|
+
/** 遍历对象自有可枚举键(`lodash.forOwn` 语义)。 */
|
|
59
|
+
export declare function forOwn<T>(obj: Record<string, T> | null | undefined, iteratee: (value: T, key: string) => any): void;
|
|
60
|
+
/**
|
|
61
|
+
* 查找集合中第一个满足条件的项(`lodash.find` 语义,支持函数 predicate)。
|
|
62
|
+
* 本项目实际调用均为数组或对象 + 函数式 predicate。
|
|
63
|
+
*/
|
|
64
|
+
export declare function find<T>(collection: readonly T[] | null | undefined, predicate: (value: T, key: number) => boolean): T | undefined;
|
|
65
|
+
export declare function find<T>(collection: Record<string, T> | null | undefined, predicate: (value: T, key: string) => boolean): T | undefined;
|
|
66
|
+
/** 按 `iteratee` 结果分组(`lodash.groupBy` 语义)。 */
|
|
67
|
+
export declare function groupBy<T>(list: readonly T[] | null | undefined, iteratee: (value: T) => string | number): Record<string, T[]>;
|
|
68
|
+
/** 浅拷贝对象,剔除指定顶层键(`lodash.omit` 简化版,够用即可)。 */
|
|
69
|
+
export declare function omit<T extends Record<string, any>, K extends keyof T>(obj: T, keys: readonly K[]): Omit<T, K>;
|
|
70
|
+
/** 基于 `iteratee` 重映射对象的键(`lodash.mapKeys` 简化版)。 */
|
|
71
|
+
export declare function mapKeys<T>(obj: Record<string, T> | null | undefined, iteratee: (value: T, key: string) => string): Record<string, T>;
|
|
72
|
+
/** 空函数,常用于 `.catch(noop)` 吞掉错误。 */
|
|
73
|
+
export declare function noop(): void;
|
|
74
|
+
/**
|
|
75
|
+
* 深克隆(对齐 vtils `cloneDeepFast`:只处理普通对象 / 数组 / Date,
|
|
76
|
+
* 其它引用值按浅拷贝,性能优于 JSON 版)。
|
|
77
|
+
*/
|
|
78
|
+
export declare function cloneDeepFast<T>(value: T): T;
|
|
79
|
+
/**
|
|
80
|
+
* 深度遍历(对齐 vtils `traverse`:数组按 index、纯对象按 key 递归;
|
|
81
|
+
* 回调形如 `(value, key, parent)`)。
|
|
82
|
+
*/
|
|
83
|
+
export declare function traverse(value: any, callback: (value: any, key: any, parent: any) => void): void;
|
|
84
|
+
/**
|
|
85
|
+
* 空行 / 缩进剥离(对齐 vtils `dedent`):
|
|
86
|
+
* - 支持模板字符串标签用法:`dedent\`...\``
|
|
87
|
+
* - 支持字符串参数用法:`dedent(text)`
|
|
88
|
+
* 行为:移除首尾空行,按最短公共前导空白对齐去缩进。
|
|
89
|
+
*/
|
|
90
|
+
export declare function dedent(input: string | TemplateStringsArray, ...values: any[]): string;
|
|
91
|
+
/**
|
|
92
|
+
* 缓存函数结果(对齐 `lodash.memoize` 简化版)。
|
|
93
|
+
* - 未传 `resolver` 时以第一个入参做 Map key;
|
|
94
|
+
* - 传入 `resolver` 时以 `resolver(...args)` 的结果做 Map key。
|
|
95
|
+
*/
|
|
96
|
+
export declare function memoize<F extends (...args: any[]) => any>(fn: F, resolver?: (...args: Parameters<F>) => any): F;
|
|
97
|
+
/**
|
|
98
|
+
* 以 Go 风格返回 `[err, value]`(对齐 vtils `run`)。
|
|
99
|
+
* 支持同步 / 异步函数;出错时 err 有值,value 为 `undefined`。
|
|
100
|
+
*/
|
|
101
|
+
export declare function run<T>(fn: () => T | PromiseLike<T>): Promise<[null, T] | [any, undefined]>;
|
|
102
|
+
/** 等待一段时间的 Promise,附带 `.cancel()`(对齐 vtils `wait`)。 */
|
|
103
|
+
export declare function wait<T = void>(milliseconds: number, value?: T): Promise<T> & {
|
|
104
|
+
cancel: () => void;
|
|
105
|
+
};
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
//#region src/mini-ut.ts
|
|
2
|
+
/** 是否为数组。 */
|
|
3
|
+
function isArray(value) {
|
|
4
|
+
return Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
/** 是否为函数(对齐 `lodash.isFunction`,`class` 也视为函数)。 */
|
|
7
|
+
function isFunction(value) {
|
|
8
|
+
return typeof value === "function";
|
|
9
|
+
}
|
|
10
|
+
/** 是否为对象(`lodash.isObject` 语义:非 `null` 的 `object` 或 `function`)。 */
|
|
11
|
+
function isObject(value) {
|
|
12
|
+
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
13
|
+
}
|
|
14
|
+
/** 是否为纯对象(构造器为 `Object` 或原型为 `null`)。 */
|
|
15
|
+
function isPlainObject(value) {
|
|
16
|
+
if (value === null || typeof value !== "object") return false;
|
|
17
|
+
const proto = Object.getPrototypeOf(value);
|
|
18
|
+
return proto === null || proto === Object.prototype;
|
|
19
|
+
}
|
|
20
|
+
/** 是否为类 Promise 对象。 */
|
|
21
|
+
function isPromiseLike(value) {
|
|
22
|
+
return value != null && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 是否为空(对齐 `lodash.isEmpty`):
|
|
26
|
+
* - `null`/`undefined` -> `true`
|
|
27
|
+
* - 数组 / 字符串 -> `length === 0`
|
|
28
|
+
* - Map / Set -> `size === 0`
|
|
29
|
+
* - 纯对象 -> 没有可枚举自有键
|
|
30
|
+
* - 数字 / 布尔 / 函数 -> `true`
|
|
31
|
+
*/
|
|
32
|
+
function isEmpty(value) {
|
|
33
|
+
if (value === null || value === void 0) return true;
|
|
34
|
+
if (isArray(value) || typeof value === "string") return value.length === 0;
|
|
35
|
+
if (value instanceof Map || value instanceof Set) return value.size === 0;
|
|
36
|
+
if (typeof value === "object") return Object.keys(value).length === 0;
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
/** 将任意值转为数组(`lodash.castArray` 语义)。 */
|
|
40
|
+
function castArray(value) {
|
|
41
|
+
if (value === void 0) return [];
|
|
42
|
+
return isArray(value) ? value.slice() : [value];
|
|
43
|
+
}
|
|
44
|
+
/** 返回数组最后一项,非数组或空数组返回 `undefined`。 */
|
|
45
|
+
function last(list) {
|
|
46
|
+
return isArray(list) && list.length ? list[list.length - 1] : void 0;
|
|
47
|
+
}
|
|
48
|
+
/** 数组去重(保持首次出现顺序)。 */
|
|
49
|
+
function uniq(list) {
|
|
50
|
+
return list ? Array.from(new Set(list)) : [];
|
|
51
|
+
}
|
|
52
|
+
/** `Object.values` 的宽松版:非对象返回 `[]`。 */
|
|
53
|
+
function values(obj) {
|
|
54
|
+
if (obj === null || obj === void 0) return [];
|
|
55
|
+
return Object.keys(obj).map((k) => obj[k]);
|
|
56
|
+
}
|
|
57
|
+
function each(collection, iteratee) {
|
|
58
|
+
if (collection === null || collection === void 0) return;
|
|
59
|
+
if (isArray(collection)) {
|
|
60
|
+
for (let i = 0; i < collection.length; i++) if (iteratee(collection[i], i) === false) return;
|
|
61
|
+
} else if (typeof collection === "object") {
|
|
62
|
+
for (const key of Object.keys(collection)) if (iteratee(collection[key], key) === false) return;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** 遍历对象自有可枚举键(`lodash.forOwn` 语义)。 */
|
|
66
|
+
function forOwn(obj, iteratee) {
|
|
67
|
+
if (obj === null || obj === void 0) return;
|
|
68
|
+
for (const key of Object.keys(obj)) if (iteratee(obj[key], key) === false) return;
|
|
69
|
+
}
|
|
70
|
+
function find(collection, predicate) {
|
|
71
|
+
if (collection === null || collection === void 0) return void 0;
|
|
72
|
+
if (isArray(collection)) {
|
|
73
|
+
for (let i = 0; i < collection.length; i++) if (predicate(collection[i], i)) return collection[i];
|
|
74
|
+
} else if (typeof collection === "object") {
|
|
75
|
+
for (const key of Object.keys(collection)) if (predicate(collection[key], key)) return collection[key];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** 按 `iteratee` 结果分组(`lodash.groupBy` 语义)。 */
|
|
79
|
+
function groupBy(list, iteratee) {
|
|
80
|
+
const result = {};
|
|
81
|
+
if (!list) return result;
|
|
82
|
+
for (const item of list) {
|
|
83
|
+
const key = String(iteratee(item));
|
|
84
|
+
if (!Object.prototype.hasOwnProperty.call(result, key)) result[key] = [];
|
|
85
|
+
result[key].push(item);
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
/** 浅拷贝对象,剔除指定顶层键(`lodash.omit` 简化版,够用即可)。 */
|
|
90
|
+
function omit(obj, keys) {
|
|
91
|
+
if (obj === null || obj === void 0) return obj;
|
|
92
|
+
const result = {};
|
|
93
|
+
const drop = new Set(keys);
|
|
94
|
+
for (const key of Object.keys(obj)) if (!drop.has(key)) result[key] = obj[key];
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
/** 基于 `iteratee` 重映射对象的键(`lodash.mapKeys` 简化版)。 */
|
|
98
|
+
function mapKeys(obj, iteratee) {
|
|
99
|
+
const result = {};
|
|
100
|
+
if (!obj) return result;
|
|
101
|
+
for (const key of Object.keys(obj)) result[iteratee(obj[key], key)] = obj[key];
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
/** 空函数,常用于 `.catch(noop)` 吞掉错误。 */
|
|
105
|
+
function noop() {}
|
|
106
|
+
/**
|
|
107
|
+
* 深克隆(对齐 vtils `cloneDeepFast`:只处理普通对象 / 数组 / Date,
|
|
108
|
+
* 其它引用值按浅拷贝,性能优于 JSON 版)。
|
|
109
|
+
*/
|
|
110
|
+
function cloneDeepFast(value) {
|
|
111
|
+
if (value === null || typeof value !== "object") return value;
|
|
112
|
+
if (value instanceof Date) return new Date(value.getTime());
|
|
113
|
+
if (isArray(value)) return value.map((item) => cloneDeepFast(item));
|
|
114
|
+
const o2 = {};
|
|
115
|
+
for (const key of Object.keys(value)) o2[key] = cloneDeepFast(value[key]);
|
|
116
|
+
return o2;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* 深度遍历(对齐 vtils `traverse`:数组按 index、纯对象按 key 递归;
|
|
120
|
+
* 回调形如 `(value, key, parent)`)。
|
|
121
|
+
*/
|
|
122
|
+
function traverse(value, callback) {
|
|
123
|
+
if (isArray(value)) value.forEach((item, index) => {
|
|
124
|
+
callback(item, index, value);
|
|
125
|
+
if (value[index] !== void 0) traverse(item, callback);
|
|
126
|
+
});
|
|
127
|
+
else if (isPlainObject(value)) forOwn(value, (item, key) => {
|
|
128
|
+
callback(item, key, value);
|
|
129
|
+
if (Object.prototype.hasOwnProperty.call(value, key)) traverse(item, callback);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 空行 / 缩进剥离(对齐 vtils `dedent`):
|
|
134
|
+
* - 支持模板字符串标签用法:`dedent\`...\``
|
|
135
|
+
* - 支持字符串参数用法:`dedent(text)`
|
|
136
|
+
* 行为:移除首尾空行,按最短公共前导空白对齐去缩进。
|
|
137
|
+
*/
|
|
138
|
+
function dedent(input, ...values) {
|
|
139
|
+
let text;
|
|
140
|
+
if (typeof input === "string") text = input;
|
|
141
|
+
else text = input.reduce((acc, chunk, i) => acc + chunk + (i < values.length ? String(values[i]) : ""), "");
|
|
142
|
+
const lines = text.split("\n");
|
|
143
|
+
let commonLeadingWhitespace = null;
|
|
144
|
+
let firstLineIndex = null;
|
|
145
|
+
let lastLineIndex = null;
|
|
146
|
+
for (let index = 0; index < lines.length; index++) {
|
|
147
|
+
const leading = lines[index].match(/^\s*/)[0];
|
|
148
|
+
if (leading.length !== lines[index].length) {
|
|
149
|
+
lastLineIndex = index;
|
|
150
|
+
if (firstLineIndex === null) firstLineIndex = index;
|
|
151
|
+
if (commonLeadingWhitespace === null || leading.length < commonLeadingWhitespace.length) commonLeadingWhitespace = leading;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (commonLeadingWhitespace === null || firstLineIndex === null || lastLineIndex === null) return text;
|
|
155
|
+
return lines.slice(firstLineIndex, lastLineIndex + 1).map((line) => line.substr(commonLeadingWhitespace.length)).join("\n");
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* 缓存函数结果(对齐 `lodash.memoize` 简化版)。
|
|
159
|
+
* - 未传 `resolver` 时以第一个入参做 Map key;
|
|
160
|
+
* - 传入 `resolver` 时以 `resolver(...args)` 的结果做 Map key。
|
|
161
|
+
*/
|
|
162
|
+
function memoize(fn, resolver) {
|
|
163
|
+
const cache = /* @__PURE__ */ new Map();
|
|
164
|
+
const wrapped = function(...args) {
|
|
165
|
+
const key = resolver ? resolver.apply(this, args) : args[0];
|
|
166
|
+
if (cache.has(key)) return cache.get(key);
|
|
167
|
+
const result = fn.apply(this, args);
|
|
168
|
+
cache.set(key, result);
|
|
169
|
+
return result;
|
|
170
|
+
};
|
|
171
|
+
return wrapped;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 以 Go 风格返回 `[err, value]`(对齐 vtils `run`)。
|
|
175
|
+
* 支持同步 / 异步函数;出错时 err 有值,value 为 `undefined`。
|
|
176
|
+
*/
|
|
177
|
+
function run(fn) {
|
|
178
|
+
return new Promise((resolve) => {
|
|
179
|
+
try {
|
|
180
|
+
const res = fn();
|
|
181
|
+
if (!isPromiseLike(res)) return resolve([null, res]);
|
|
182
|
+
Promise.resolve(res).then((value) => resolve([null, value]), (err) => resolve([err, void 0]));
|
|
183
|
+
} catch (err) {
|
|
184
|
+
resolve([err, void 0]);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/** 等待一段时间的 Promise,附带 `.cancel()`(对齐 vtils `wait`)。 */
|
|
189
|
+
function wait(milliseconds, value) {
|
|
190
|
+
let timer;
|
|
191
|
+
const promise = new Promise((resolve) => {
|
|
192
|
+
timer = setTimeout(() => resolve(value), milliseconds);
|
|
193
|
+
});
|
|
194
|
+
promise.cancel = () => clearTimeout(timer);
|
|
195
|
+
return promise;
|
|
196
|
+
}
|
|
197
|
+
//#endregion
|
|
198
|
+
export { castArray, cloneDeepFast, dedent, each, find, forOwn, groupBy, isArray, isEmpty, isFunction, isObject, isPlainObject, isPromiseLike, last, mapKeys, memoize, noop, omit, run, traverse, uniq, values, wait };
|