open-api-typescript-request-generator 0.0.1 → 0.0.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.
package/lib/types.js CHANGED
@@ -31,7 +31,7 @@ Object.defineProperty(exports, "__esModule", {
31
31
  exports.ResponseBodyType = exports.RequestFormItemType = exports.RequestQueryType = exports.RequestParamType = exports.RequestBodyType = exports.Required = exports.Method = void 0;
32
32
 
33
33
  __exportStar(require("json-schema"), exports);
34
- /** 请求方式 */
34
+ /** Request method */
35
35
 
36
36
 
37
37
  var Method;
@@ -45,82 +45,82 @@ var Method;
45
45
  Method["OPTIONS"] = "OPTIONS";
46
46
  Method["PATCH"] = "PATCH";
47
47
  })(Method = exports.Method || (exports.Method = {}));
48
- /** 是否必需 */
48
+ /** Required */
49
49
 
50
50
 
51
51
  var Required;
52
52
 
53
53
  (function (Required) {
54
- /** 不必需 */
54
+ /** Not required */
55
55
  Required["false"] = "0";
56
- /** 必需 */
56
+ /** Required */
57
57
 
58
58
  Required["true"] = "1";
59
59
  })(Required = exports.Required || (exports.Required = {}));
60
- /** 请求数据类型 */
60
+ /** Request body type */
61
61
 
62
62
 
63
63
  var RequestBodyType;
64
64
 
65
65
  (function (RequestBodyType) {
66
- /** 查询字符串 */
66
+ /** Query string */
67
67
  RequestBodyType["query"] = "query";
68
- /** 表单 */
68
+ /** Form */
69
69
 
70
70
  RequestBodyType["form"] = "form";
71
71
  /** JSON */
72
72
 
73
73
  RequestBodyType["json"] = "json";
74
- /** 纯文本 */
74
+ /** Plain text */
75
75
 
76
76
  RequestBodyType["text"] = "text";
77
- /** 文件 */
77
+ /** File */
78
78
 
79
79
  RequestBodyType["file"] = "file";
80
- /** 原始数据 */
80
+ /** Raw data */
81
81
 
82
82
  RequestBodyType["raw"] = "raw";
83
- /** 无请求数据 */
83
+ /** No request data */
84
84
 
85
85
  RequestBodyType["none"] = "none";
86
86
  })(RequestBodyType = exports.RequestBodyType || (exports.RequestBodyType = {}));
87
- /** 请求路径参数类型 */
87
+ /** Request path parameter type */
88
88
 
89
89
 
90
90
  var RequestParamType;
91
91
 
92
92
  (function (RequestParamType) {
93
- /** 字符串 */
93
+ /** String */
94
94
  RequestParamType["string"] = "string";
95
- /** 数字 */
95
+ /** Number */
96
96
 
97
97
  RequestParamType["number"] = "number";
98
98
  })(RequestParamType = exports.RequestParamType || (exports.RequestParamType = {}));
99
- /** 请求查询参数类型 */
99
+ /** Request query parameter type */
100
100
 
101
101
 
102
102
  var RequestQueryType;
103
103
 
104
104
  (function (RequestQueryType) {
105
- /** 字符串 */
105
+ /** String */
106
106
  RequestQueryType["string"] = "string";
107
- /** 数字 */
107
+ /** Number */
108
108
 
109
109
  RequestQueryType["number"] = "number";
110
110
  })(RequestQueryType = exports.RequestQueryType || (exports.RequestQueryType = {}));
111
- /** 请求表单条目类型 */
111
+ /** Request form item type */
112
112
 
113
113
 
114
114
  var RequestFormItemType;
115
115
 
116
116
  (function (RequestFormItemType) {
117
- /** 纯文本 */
117
+ /** Plain text */
118
118
  RequestFormItemType["text"] = "text";
119
- /** 文件 */
119
+ /** File */
120
120
 
121
121
  RequestFormItemType["file"] = "file";
122
122
  })(RequestFormItemType = exports.RequestFormItemType || (exports.RequestFormItemType = {}));
123
- /** 返回数据类型 */
123
+ /** Response body type */
124
124
 
125
125
 
126
126
  var ResponseBodyType;
@@ -128,15 +128,15 @@ var ResponseBodyType;
128
128
  (function (ResponseBodyType) {
129
129
  /** JSON */
130
130
  ResponseBodyType["json"] = "json";
131
- /** 纯文本 */
131
+ /** Plain text */
132
132
 
133
133
  ResponseBodyType["text"] = "text";
134
134
  /** XML */
135
135
 
136
136
  ResponseBodyType["xml"] = "xml";
137
- /** 原始数据 */
137
+ /** Raw data */
138
138
 
139
- ResponseBodyType["raw"] = "raw"; // yapi 实际上返回的是 json,有另外的字段指示其是否是 json schema
139
+ ResponseBodyType["raw"] = "raw"; // yapi actually returns json, with another field indicating whether it is json schema
140
140
 
141
141
  /** JSON Schema */
142
142
  // jsonSchema = 'json-schema',
package/lib/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import prettier from 'prettier';
2
- import { Interface, PropDefinitions, Config } from './types';
2
+ import { Interface, PropDefinitions } from './types';
3
3
  import { JSONSchema4 } from 'json-schema';
4
4
  /**
5
5
  * 抛出错误。
@@ -61,7 +61,7 @@ export declare function propDefinitionsToJsonSchema(propDefinitions: PropDefinit
61
61
  * 获取prettier配置
62
62
  * @returns
63
63
  */
64
- export declare function getPrettier(filePath?: string): prettier.Options;
64
+ export declare function getPrettier(): prettier.Options;
65
65
  export declare function preprocessSchema(schema: JSONSchema4): JSONSchema4;
66
66
  /**
67
67
  * 根据 JSONSchema 对象生产 TypeScript 类型定义。
@@ -72,7 +72,7 @@ export declare function preprocessSchema(schema: JSONSchema4): JSONSchema4;
72
72
  */
73
73
  export declare function jsonSchemaToTsCode(jsonSchema: JSONSchema4, typeName: string): Promise<string>;
74
74
  export declare function getRequestDataJsonSchema(interfaceInfo: Interface): JSONSchema4;
75
- export declare function getResponseDataJsonSchema(interfaceInfo: Interface, dataKey?: string): JSONSchema4;
75
+ export declare function getResponseDataJsonSchema(interfaceInfo: Interface): JSONSchema4;
76
76
  export declare function sortByWeights<T extends {
77
77
  weights: number[];
78
78
  }>(list: T[]): T[];
@@ -83,15 +83,9 @@ export declare function sortByWeights<T extends {
83
83
  * @returns
84
84
  * https://prettier.io/docs/en/options.html
85
85
  */
86
- export declare function formatContent(content: string, prettierConfigPath?: string): string;
86
+ export declare function formatContent(content: string): string;
87
87
  /**
88
88
  * 通用生成文件顶部注释
89
89
  * @returns
90
90
  */
91
91
  export declare function topNotesContent(): string;
92
- /**
93
- * filter处理函数
94
- * @param interfaceFilter
95
- * @returns
96
- */
97
- export declare const filterHandler: (interfaceFilter?: Config['filter']) => (path: string, id?: number) => boolean;
package/lib/utils.js CHANGED
@@ -180,7 +180,7 @@ var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
180
180
  Object.defineProperty(exports, "__esModule", {
181
181
  value: true
182
182
  });
183
- exports.filterHandler = exports.topNotesContent = exports.formatContent = exports.sortByWeights = exports.getResponseDataJsonSchema = exports.getRequestDataJsonSchema = exports.jsonSchemaToTsCode = exports.preprocessSchema = exports.getPrettier = exports.propDefinitionsToJsonSchema = exports.mockjsTemplateToJsonSchema = exports.jsonToJsonSchema = exports.jsonSchemaStringToJsonSchema = exports.processJsonSchema = exports.getNormalizedRelativePath = exports.toUnixPath = exports.throwError = void 0;
183
+ exports.topNotesContent = exports.formatContent = exports.sortByWeights = exports.getResponseDataJsonSchema = exports.getRequestDataJsonSchema = exports.jsonSchemaToTsCode = exports.preprocessSchema = exports.getPrettier = exports.propDefinitionsToJsonSchema = exports.mockjsTemplateToJsonSchema = exports.jsonToJsonSchema = exports.jsonSchemaStringToJsonSchema = exports.processJsonSchema = exports.getNormalizedRelativePath = exports.toUnixPath = exports.throwError = void 0;
184
184
 
185
185
  var json5_1 = __importDefault(require("json5"));
186
186
 
@@ -427,13 +427,8 @@ exports.propDefinitionsToJsonSchema = propDefinitionsToJsonSchema;
427
427
  * @returns
428
428
  */
429
429
 
430
- function getPrettier(filePath) {
431
- // 从项目中获取prettier配置文件
432
- var configPath = prettier_1.default.resolveConfigFile.sync(filePath && path_1.default.resolve(filePath) || process.cwd());
433
- var config = configPath && prettier_1.default.resolveConfig.sync(configPath) || {};
434
- return config ? __assign({
435
- parser: 'babel-ts'
436
- }, config) : {
430
+ function getPrettier() {
431
+ return {
437
432
  printWidth: 120,
438
433
  tabWidth: 2,
439
434
  singleQuote: true,
@@ -678,7 +673,7 @@ function getRequestDataJsonSchema(interfaceInfo) {
678
673
 
679
674
  exports.getRequestDataJsonSchema = getRequestDataJsonSchema;
680
675
 
681
- function getResponseDataJsonSchema(interfaceInfo, dataKey) {
676
+ function getResponseDataJsonSchema(interfaceInfo) {
682
677
  var jsonSchema = {};
683
678
 
684
679
  switch (interfaceInfo.res_body_type) {
@@ -695,12 +690,6 @@ function getResponseDataJsonSchema(interfaceInfo, dataKey) {
695
690
  };
696
691
  break;
697
692
  }
698
- /* istanbul ignore if */
699
-
700
-
701
- if (dataKey && jsonSchema && jsonSchema.properties && jsonSchema.properties[dataKey]) {
702
- jsonSchema = jsonSchema.properties[dataKey];
703
- }
704
693
 
705
694
  return jsonSchema;
706
695
  }
@@ -738,9 +727,9 @@ exports.sortByWeights = sortByWeights;
738
727
  * https://prettier.io/docs/en/options.html
739
728
  */
740
729
 
741
- function formatContent(content, prettierConfigPath) {
730
+ function formatContent(content) {
742
731
  // 从项目中获取prettier配置文件
743
- var config = getPrettier(prettierConfigPath);
732
+ var config = getPrettier();
744
733
  var prettyOutputContent = prettier_1.default.format(content, config);
745
734
  return prettyOutputContent;
746
735
  }
@@ -752,32 +741,7 @@ exports.formatContent = formatContent;
752
741
  */
753
742
 
754
743
  function topNotesContent() {
755
- return "\n /**\n * Created By api-ts-generator\n */\n\n ";
744
+ return "\n /**\n * Created By open-api-typescript-request-generator\n */\n\n ";
756
745
  }
757
746
 
758
- exports.topNotesContent = topNotesContent;
759
- /**
760
- * filter处理函数
761
- * @param interfaceFilter
762
- * @returns
763
- */
764
-
765
- var filterHandler = function filterHandler(interfaceFilter) {
766
- return function (path, id) {
767
- if (interfaceFilter instanceof RegExp && !interfaceFilter.test(path)) {
768
- return false;
769
- }
770
-
771
- if (interfaceFilter instanceof Array && !interfaceFilter.includes(path)) {
772
- return false;
773
- }
774
-
775
- if (interfaceFilter instanceof Function && !interfaceFilter(path, id)) {
776
- return false;
777
- }
778
-
779
- return true;
780
- };
781
- };
782
-
783
- exports.filterHandler = filterHandler;
747
+ exports.topNotesContent = topNotesContent;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "open-api-typescript-request-generator",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "author": "zimv <zimudashiren@163.com>",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./es/index.js",
@@ -15,7 +15,8 @@
15
15
  "scripts": {
16
16
  "build": "father build",
17
17
  "gen": "rm -rf ./api & tsx test/gen.ts --type-check false gen",
18
- "init": "tsx watch test/gen.ts --type-check false init",
18
+ "gen:api-1": "tsx test/gen.ts --type-check false gen -n api-1",
19
+ "init": "tsx test/gen.ts --type-check false init",
19
20
  "publishOnly": "npm run build",
20
21
  "release": "npm run build && standard-version && git push --follow-tags origin master && npm publish",
21
22
  "requestTest": "tsx watch test/requestTest.ts --type-check false",
@@ -1,157 +0,0 @@
1
- /**
2
- * 获取yapi数据
3
- */
4
- import { Options } from 'got';
5
- import { CategoryList, Category, Project, SyntheticalConfig, Interface, BaseInterfaceInfo } from './types';
6
- import prompts from 'prompts';
7
- declare type ApiConfig = SyntheticalConfig & {
8
- method?: Options['method'];
9
- /** 异常了是否执行 `process.exit()` */
10
- errorExit?: boolean;
11
- };
12
- declare const inputs: readonly [{
13
- readonly type: "text";
14
- readonly name: "email";
15
- readonly message: "请输入yapi的邮箱账号";
16
- readonly validate: prompts.PrevCaller<"email", string | boolean>;
17
- }, {
18
- readonly type: "password";
19
- readonly name: "password";
20
- readonly message: "请输入密码";
21
- readonly validate: prompts.PrevCaller<"password", string | boolean>;
22
- }];
23
- declare type NameValueKey = typeof inputs[number]['name'];
24
- /**
25
- * 登录提示框,账号密码录入
26
- * @returns
27
- */
28
- export declare const loginPrompts: (serverUrl?: string) => Promise<{
29
- email: string;
30
- password: string;
31
- }>;
32
- export declare type LoginProps = {
33
- [k in NameValueKey]: string;
34
- };
35
- export declare type LoginResponseData = {
36
- email: string;
37
- role: string;
38
- study: boolean;
39
- type: string;
40
- uid: number;
41
- username: string;
42
- };
43
- declare type FetchApiData = {
44
- token?: string;
45
- [k: string]: any;
46
- };
47
- /**
48
- *
49
- * @param url 请求方法
50
- * @param query
51
- * @returns
52
- */
53
- export declare const fetchApi: <T>(path: string, data: FetchApiData, config?: ApiConfig) => Promise<T>;
54
- /**
55
- * 获取项目token
56
- */
57
- export declare const fetchToken: (id: number, config?: ApiConfig) => Promise<string>;
58
- /**
59
- * 获取项目基本信息 by id
60
- */
61
- export declare const fetchProjectById: (config: ApiConfig) => Promise<Project>;
62
- /**
63
- * 获取项目基本信息
64
- *
65
- */
66
- export declare const fetchProject: (config: ApiConfig) => Promise<Project>;
67
- /**
68
- * 通过导出接口获取项目下的所有接口 by id
69
- */
70
- export declare const fetchExportById: (config: ApiConfig) => Promise<Category[]>;
71
- /**
72
- * 通过导出接口获取项目下的所有接口
73
- */
74
- export declare const fetchExport: (config: ApiConfig) => Promise<Category[]>;
75
- /**
76
- * 获取所有接口 by id
77
- * @param param0
78
- * @returns
79
- */
80
- export declare const fetchInterfaceListById: (config: ApiConfig) => Promise<Category[]>;
81
- /**
82
- * 获取所有接口
83
- * @param param0
84
- * @returns
85
- */
86
- export declare const fetchInterfaceList: (config: ApiConfig) => Promise<Category[]>;
87
- /**
88
- * 获取项目信息 by id
89
- * @param config
90
- * @returns
91
- */
92
- export declare const fetchProjectInfoById: (config: ApiConfig) => Promise<{
93
- cats: CategoryList;
94
- getMockUrl: () => string;
95
- getDevUrl: (devEnvName: string) => string;
96
- getProdUrl: (prodEnvName: string) => string;
97
- _id: number;
98
- name: string;
99
- desc: string; /** 异常了是否执行 `process.exit()` */
100
- basepath: string;
101
- tag: string[];
102
- env: {
103
- name: string;
104
- domain: string;
105
- }[];
106
- token?: string;
107
- cat: Category[];
108
- components?: object[];
109
- }>;
110
- /**
111
- * 获取项目信息
112
- * @param config
113
- * @returns
114
- */
115
- export declare const fetchProjectInfo: (config: ApiConfig) => Promise<{
116
- cats: CategoryList;
117
- getMockUrl: () => string;
118
- getDevUrl: (devEnvName: string) => string;
119
- getProdUrl: (prodEnvName: string) => string;
120
- _id: number;
121
- name: string;
122
- desc: string; /** 异常了是否执行 `process.exit()` */
123
- basepath: string;
124
- tag: string[];
125
- env: {
126
- name: string;
127
- domain: string;
128
- }[];
129
- token?: string;
130
- cat: Category[];
131
- components?: object[];
132
- }>;
133
- /**
134
- * 获取单个接口信息 by id
135
- */
136
- export declare const fetchInterfaceById: (id: number, config?: ApiConfig) => Promise<Interface>;
137
- /**
138
- * 获取分类下的接口列表信息 by id
139
- */
140
- export declare const fetchCatInterfaceById: (catid: number, config?: ApiConfig) => Promise<BaseInterfaceInfo[]>;
141
- /**
142
- * 批量获取分类下的详细接口信息
143
- * @param categoryList
144
- * @param config
145
- * @returns
146
- */
147
- export declare const getInterfacesByCategoryIds: (categoryIds: number[], config?: ApiConfig) => Promise<Interface[]>;
148
- /**
149
- * 获取项目信息和接口
150
- * @param config
151
- * @returns
152
- */
153
- export declare const getProjectInfoAndInterfaces: (config: ApiConfig) => Promise<{
154
- projectInfo: Project;
155
- allInterfaceList: Category[];
156
- }>;
157
- export {};