feat:release · lowcoding/lowcode-vscode@3fd5488 · GitHub
Skip to content

Commit 3fd5488

Browse files
committed
feat:release
1 parent ceeb079 commit 3fd5488

11 files changed

Lines changed: 400 additions & 98 deletions

File tree

README.md

Lines changed: 1 addition & 59 deletions

asset/icon.png

5.44 KB
Loading

package.json

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,67 @@
11
{
22
"name": "yapi-code",
33
"displayName": "yapi-code",
4-
"description": "",
4+
"description": "根据 yapi 接口定义快速生成 TypeScript 类型,支持自定义模板",
5+
"author": "wjkang <ruoxieme@gmail.com>",
56
"version": "0.0.1",
7+
"icon": "asset/icon.png",
8+
"publisher": "wjkang",
69
"engines": {
7-
"vscode": "^1.46.0"
10+
"vscode": "^1.42.0"
811
},
912
"categories": [
1013
"Other"
1114
],
15+
"keywords": [
16+
"yapi",
17+
"typescript",
18+
"generate code",
19+
"json"
20+
],
1221
"activationEvents": [
13-
"onCommand:yapi-code.generateFetchFun",
14-
"onCommand:yapi-code.generateFetchFunByJSON"
22+
"onCommand:yapi-code.generateCode",
23+
"onCommand:yapi-code.generateMockCode"
1524
],
1625
"main": "./out/extension.js",
1726
"contributes": {
1827
"commands": [
1928
{
20-
"command": "yapi-code.generateFetchFun",
21-
"title": "新增接口定义"
29+
"command": "yapi-code.generateCode",
30+
"title": "YAPI->生成代码"
31+
},
32+
{
33+
"command": "yapi-code.generateMockCode",
34+
"title": "YAPI->生成mock代码"
2235
}
2336
],
2437
"menus": {
2538
"editor/context": [
2639
{
27-
"command": "yapi-code.generateFetchFun"
40+
"command": "yapi-code.generateCode"
41+
},
42+
{
43+
"command": "yapi-code.generateMockCode"
2844
}
2945
]
3046
},
3147
"configuration": {
3248
"type": "object",
33-
"title": "yapi-code configuration",
49+
"title": "yapi-code 配置项",
3450
"properties": {
3551
"yapi.domain": {
3652
"type": "string",
3753
"default": "",
38-
"description": "api 域名"
54+
"description": "域名"
55+
},
56+
"yapi.project": {
57+
"type": "array",
58+
"default": [],
59+
"description": "项目列表。eg.[{\"name\": \"项目\",\"token\": \"token\"}]"
60+
},
61+
"yapi.codeTemplate": {
62+
"type": "array",
63+
"default": [],
64+
"description": "模板列表。eg.[{\"name\": \"模板名称\",\"template\": \"模板字符串\"}]"
3965
}
4066
}
4167
}
@@ -49,16 +75,29 @@
4975
"test": "node ./out/test/runTest.js"
5076
},
5177
"devDependencies": {
52-
"@types/vscode": "^1.47.0",
78+
"@types/copy-paste": "^1.1.30",
5379
"@types/glob": "^7.1.1",
5480
"@types/mocha": "^7.0.2",
5581
"@types/node": "^13.11.0",
56-
"eslint": "^6.8.0",
57-
"@typescript-eslint/parser": "^2.30.0",
82+
"@types/prettier": "^2.0.2",
83+
"@types/readable-stream": "^2.3.9",
84+
"@types/urijs": "^1.19.9",
85+
"@types/vscode": "1.42.0",
5886
"@typescript-eslint/eslint-plugin": "^2.30.0",
87+
"@typescript-eslint/parser": "^2.30.0",
88+
"eslint": "^6.8.0",
5989
"glob": "^7.1.6",
6090
"mocha": "^7.1.2",
6191
"typescript": "^3.8.3",
6292
"vscode-test": "^1.3.0"
93+
},
94+
"dependencies": {
95+
"axios": "^0.19.2",
96+
"copy-paste": "^1.3.0",
97+
"handlebars": "^4.7.6",
98+
"is-url": "^1.2.4",
99+
"json-schema-to-typescript": "^9.1.1",
100+
"quicktype-core": "^6.0.66",
101+
"strip-comments": "^2.0.1"
63102
}
64103
}

src/commands/generateCode.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as vscode from 'vscode';
2+
import { getClipboardText, isYapiId, jsonIsValid } from '../lib';
3+
import { genCodeByYapi } from '../genCode/genCodeByYapi';
4+
import { genCodeByJson } from '../genCode/genCodeByJson';
5+
6+
const { window } = vscode;
7+
8+
export const generateCode = () => {
9+
return vscode.commands.registerTextEditorCommand(
10+
'yapi-code.generateCode',
11+
async () => {
12+
const clipboardText = getClipboardText().trim();
13+
14+
const validYapiId = isYapiId(clipboardText);
15+
16+
const validJson = jsonIsValid(clipboardText);
17+
18+
if (!validYapiId && !validJson) {
19+
window.showErrorMessage('请复制Yapi接口ID或JSON字符串');
20+
return;
21+
}
22+
23+
if (validYapiId) {
24+
await genCodeByYapi(clipboardText);
25+
} else {
26+
await genCodeByJson(clipboardText);
27+
}
28+
},
29+
);
30+
};

src/compile.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { compile as c, registerHelper } from 'handlebars';
2+
3+
type YapiInfo = {
4+
query_path: { path: string };
5+
method: string;
6+
title: string;
7+
project_id: number;
8+
req_params: {
9+
name: string;
10+
desc: string;
11+
}[];
12+
_id: number;
13+
req_query: { required: '0' | '1'; name: string }[];
14+
res_body_type: 'raw' | 'json';
15+
res_body: string;
16+
username: string;
17+
};
18+
19+
registerHelper('notEmpty', (array: []) => {
20+
return array.length > 0;
21+
});
22+
23+
registerHelper('eq', (arg1, arg2) => {
24+
return arg1 === arg2;
25+
});
26+
27+
registerHelper('in', (item, array: any[], item2) => {
28+
return array.indexOf(item) > -1;
29+
});
30+
31+
registerHelper('firstUpperCase', (value) => {
32+
return value.slice(0, 1).toUpperCase() + value.slice(1);
33+
});
34+
35+
registerHelper('index', (array: any[], index: number) => {
36+
return array[index];
37+
});
38+
39+
export const compile = (
40+
templateString: string,
41+
model: {
42+
type: string;
43+
funcName: string;
44+
typeName: string;
45+
inputValues: string[];
46+
api?: YapiInfo;
47+
},
48+
) => {
49+
const template = c(templateString);
50+
return template(model);
51+
};

src/config.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as vscode from 'vscode';
2+
3+
/**
4+
* 获取域名
5+
*
6+
* @returns
7+
*/
8+
export const getDomain = () => {
9+
return vscode.workspace.getConfiguration().get('yapi.domain', '');
10+
};
11+
12+
/**
13+
* 获取项目列表
14+
*
15+
* @returns
16+
*/
17+
export const getProjectList = () => {
18+
return vscode.workspace
19+
.getConfiguration()
20+
.get<{ name: string; token: string; domain: string }[]>('yapi.project', []);
21+
};
22+
23+
/**
24+
* 获取代码模板列表
25+
*
26+
* @returns
27+
*/
28+
export const getCodeTemplateList = () => {
29+
return vscode.workspace
30+
.getConfiguration()
31+
.get<{ name: string; template: string }[]>('yapi.codeTemplate', []);
32+
};

src/extension.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
11
import * as vscode from 'vscode';
2-
3-
const { window } = vscode;
2+
import { generateCode } from './commands/generateCode';
43

54
export function activate(context: vscode.ExtensionContext) {
6-
vscode.workspace
7-
.getConfiguration()
8-
.update('yapi.domain', 'hahahahahahah', true);
9-
let disposable = vscode.commands.registerTextEditorCommand(
10-
'yapi-code.generateFetchFun',
11-
async () => {
12-
vscode.window.showInformationMessage(
13-
vscode.workspace.getConfiguration().get('yapi.domain', ''),
14-
);
15-
// const result = await window.showInputBox({
16-
// value: 'abcdef',
17-
// valueSelection: [2, 4],
18-
// placeHolder: 'For example: fedcba. But not: 123',
19-
// validateInput: (text) => {
20-
// window.showInformationMessage(`Validating: ${text}`);
21-
// return text === '123' ? 'Not 123!' : null;
22-
// },
23-
// });
24-
// window.showInformationMessage(`Got: ${result}`);
25-
},
26-
);
27-
28-
context.subscriptions.push(disposable);
5+
context.subscriptions.push(generateCode());
296

307
context.subscriptions.push(
318
vscode.commands.registerTextEditorCommand(
32-
'yapi-code.generateFetchFunByJSON',
33-
() => {},
9+
'yapi-code.generateMockCode',
10+
() => {
11+
vscode.window.showInformationMessage('敬请期待');
12+
},
3413
),
3514
);
3615
}

src/genCode/genCodeByJson.ts

Lines changed: 35 additions & 0 deletions

0 commit comments

Comments
 (0)