Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add Copy ts type #1016

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions images/ts-inverse.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions images/ts.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 28 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"onCommand:rest-client.clear-history",
"onCommand:rest-client.save-response",
"onCommand:rest-client.save-response-body",
"onCommand:rest-client.copy-response-body",
"onCommand:rest-client.copy-response",
"onCommand:rest-client.copy-response-type",
"onCommand:rest-client.switch-environment",
"onCommand:rest-client.clear-aad-token-cache",
"onCommand:rest-client.cancel-request",
Expand Down Expand Up @@ -138,14 +139,23 @@
"category": "Rest Client"
},
{
"command": "rest-client.copy-response-body",
"title": "Copy Response Body",
"command": "rest-client.copy-response",
"title": "Copy Response",
"icon": {
"light": "./images/copy.svg",
"dark": "./images/copy-inverse.svg"
},
"category": "Rest Client"
},
{
"command": "rest-client.copy-response-type",
"title": "Copy Response type",
"icon": {
"light": "./images/ts.svg",
"dark": "./images/ts-inverse.svg"
},
"category": "Rest Client"
},
{
"command": "rest-client.generate-codesnippet",
"title": "Generate Code Snippet",
Expand Down Expand Up @@ -200,7 +210,11 @@
"when": "httpResponsePreviewFocus"
},
{
"command": "rest-client.copy-response-body",
"command": "rest-client.copy-response",
"when": "httpResponsePreviewFocus"
},
{
"command": "rest-client.copy-response-type",
"when": "httpResponsePreviewFocus"
},
{
Expand All @@ -211,19 +225,24 @@
"editor/title": [
{
"when": "httpResponsePreviewFocus",
"command": "rest-client.save-response",
"command": "rest-client.copy-response-type",
"group": "navigation@1"
},
{
"when": "httpResponsePreviewFocus",
"command": "rest-client.save-response-body",
"command": "rest-client.save-response",
"group": "navigation@2"
},
{
"when": "httpResponsePreviewFocus",
"command": "rest-client.copy-response-body",
"command": "rest-client.save-response-body",
"group": "navigation@3"
},
{
"when": "httpResponsePreviewFocus",
"command": "rest-client.copy-response",
"group": "navigation@4"
},
{
"when": "httpResponsePreviewFocus",
"command": "rest-client.fold-response",
Expand Down Expand Up @@ -633,7 +652,8 @@
"vscode:prepublish": "webpack --mode production",
"webpack": "webpack --mode development",
"watch": "webpack --mode development --watch --info-verbosity verbose",
"tslint": "tslint --project tsconfig.json"
"tslint": "tslint --project tsconfig.json",
"build:vsix":"vsce package"
},
"devDependencies": {
"@types/aws4": "^1.5.1",
Expand Down
132 changes: 132 additions & 0 deletions src/utils/json2ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* @Author beshanoe
* Latest commit a8b0e42 on 5 Jun 2017
* https://github.com/beshanoe/json2ts/blob/master/src/utils/json2.ts
*/

interface IJson2TsConfigPrivate {
prependWithI: boolean;
sortAlphabetically: boolean;
addExport: boolean;
useArrayGeneric: boolean;
optionalFields: boolean;
prefix: string;
rootObjectName: string;
}

export type IJson2TsConfig = Partial<IJson2TsConfigPrivate>;

export class Json2Ts {

private config: IJson2TsConfigPrivate;

private interfaces: {
[name: string]: {
[field: string]: string;
}
} = {};

constructor(
config: IJson2TsConfig = {}
) {
this.config = {
prependWithI: true,
sortAlphabetically: false,
addExport: false,
useArrayGeneric: false,
optionalFields: false,
prefix: '',
rootObjectName: 'RootObject',
...config
};
}

convert(json: {}) {
this.interfaces = {};
let result = `\n`;
this.unknownToTS(json);
result += this.interfacesToString();
return result;
}

private unknownToTS(value: {}, key: string | undefined = void 0) {
let type: string = typeof value;
if (type === 'object') {
if (Array.isArray(value)) {
type = this.arrayToTS(value, key);
} else {
type = this.objectToTS(value, key && this.capitalizeFirst(key));
}
}
return type;
}

private arrayToTS(array: {}[], key: string | undefined = void 0) {
let type = array.length ? void 0 : 'any';
for (const item of array) {
const itemType = this.unknownToTS(item, this.keyToTypeName(key));
if (type && itemType !== type) {
type = 'any';
break;
} else {
type = itemType;
}
}
return this.config.useArrayGeneric ? `Array<${type}>` : `${type}[]`;
}

private keyToTypeName(key: string | undefined = void 0) {
if (!key || key.length < 2) {
return key;
}
const [first, ...rest]: string[] = Array.prototype.slice.apply(key);
const last = rest.pop();
return [first.toUpperCase(), ...rest, last === 's' ? '' : last].join('');
}

private capitalizeFirst(str: string) {
const [first, ...rest]: string[] = Array.prototype.slice.apply(str);
return [first.toUpperCase(), ...rest].join('');
}

private objectToTS(obj: {}, type: string = this.config.rootObjectName) {
if (obj === null) {
return 'any';
}
const { prependWithI, prefix } = this.config;
if (prependWithI) {
type = `I${prefix || ''}${type}`;
}
if (!this.interfaces[type]) {
this.interfaces[type] = {};
}
const interfaceName = this.interfaces[type];
Object.keys(obj).forEach(key => {
const value = obj[key];
const fieldType = this.unknownToTS(value, key);
if (!interfaceName[key] || interfaceName[key].indexOf('any') === 0) {
interfaceName[key] = fieldType;
}
});
return type;
}

private interfacesToString() {
const { sortAlphabetically, addExport, optionalFields } = this.config;
return Object.keys(this.interfaces).map(name => {
const interfaceStr = [`${addExport ? 'export ' : ''}interface ${name} {`];
const fields = Object.keys(this.interfaces[name]);
if (sortAlphabetically) {
fields.sort();
}
fields
.forEach(field => {
const type = this.interfaces[name][field];
interfaceStr.push(` ${field}${optionalFields ? '?' : ''}: ${type};`);
});
interfaceStr.push('}\n');
return interfaceStr.join('\n');
}).join('\n');
}

}
26 changes: 23 additions & 3 deletions src/views/httpResponseWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { HttpResponse } from '../models/httpResponse';
import { PreviewOption } from '../models/previewOption';
import { trace } from '../utils/decorator';
import { disposeAll } from '../utils/dispose';
import { Json2Ts } from '../utils/json2ts';
import { MimeUtility } from '../utils/mimeUtility';
import { base64, getHeader, isJSONString } from '../utils/misc';
import { ResponseFormatUtility } from '../utils/responseFormatUtility';
Expand Down Expand Up @@ -51,7 +52,8 @@ export class HttpResponseWebview extends BaseWebview {
this.context.subscriptions.push(commands.registerCommand('rest-client.fold-response', this.foldResponseBody, this));
this.context.subscriptions.push(commands.registerCommand('rest-client.unfold-response', this.unfoldResponseBody, this));

this.context.subscriptions.push(commands.registerCommand('rest-client.copy-response-body', this.copyBody, this));
this.context.subscriptions.push(commands.registerCommand('rest-client.copy-response', this.copyResponse, this));
this.context.subscriptions.push(commands.registerCommand('rest-client.copy-response-type', this.copyResponseType, this));
this.context.subscriptions.push(commands.registerCommand('rest-client.save-response', this.save, this));
this.context.subscriptions.push(commands.registerCommand('rest-client.save-response-body', this.saveBody, this));
}
Expand Down Expand Up @@ -123,13 +125,31 @@ export class HttpResponseWebview extends BaseWebview {
this.activePanel?.webview.postMessage({ 'command': 'unfoldAll' });
}

@trace('Copy Response Body')
private async copyBody() {
@trace('Copy Response')
private async copyResponse() {
if (this.activeResponse) {
await this.clipboard.writeText(this.activeResponse.body);
}
}

@trace('Copy Response type')
private async copyResponseType() {
if (this.activeResponse) {
// await this.clipboard.writeText(this.activeResponse.body);
const config = {
prependWithI: true,
sortAlphabetically: false,
addExport: true,
useArrayGeneric: false,
optionalFields: true,
prefix: '',
rootObjectName: 'RootObject'
};
const parser = new Json2Ts(config);
await this.clipboard.writeText(parser.convert(JSON.parse(this.activeResponse.body)));
}
}

@trace('Save Response')
private async save() {
if (this.activeResponse) {
Expand Down