vkcom / api-schema-typescript-generator Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Для удобства чтения больших юнионов
// До
export declare type Foobar = 'foo' | 'bar'
// После
export declare type Foobar =
| 'foo'
| 'bar'
Если бекенд принимает список через запятую, то есть массив строк. То генерируется тип который указывает что нужно прокидывать строку. Хотелось бы иметь хоть какую-ту подсказку через комментарии, что туда не просто строка прокидывается, а список через запятую или можно алиас.
/** Список элементов через запятую */
type ArrayAsString = string
Сейчас типы в функцию вызова апи можно передавать только через дженерики. Хотелось бы иметь мапу с
method_name => method_request_params, (method_response1 | method_response2)
Зачем это нужно? Чтобы лучше контролировать строгость вызовов апи функций, если сделать дженерики обязательными, то люди смогут по ошибке передавать туда неверные параметры респонса, тем самым смысл типизации теряется. С мапой у тебя нет возможности ошибиться
Generate tuple from minItems
and maxItems
:
minItems
:
"propertyName": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 2
}
propertyName: [string, string, ...string[]];
maxItems
:
"propertyName": {
"type": "array",
"items": {
"type": "string"
},
"maxItems": 4
}
propertyName: [string, string, string, string];
Schema:
{
"some_response": {
"type": "object",
"properties": {
"response": {
"type": "object",
"properties": {
"user_id": {
"type": "integer"
},
"payload": {
"type": "object"
}
},
"required": [
"user_id",
"payload"
]
}
},
"required": [
"response"
]
}
}
Current output:
export interface SomeResponse {
user_id: number;
payload: {};
}
Desired output:
export interface SomeResponse {
user_id: number;
payload: {
[k: string]: unknown;
};
}
export const API_ERROR_ACCESS = 15
В #41 предлагалось генерировать константы для ошибок.
Кажется более эффективным способом именования было бы использование кода ошибки в имени.
// было
export const API_ERROR_ACCESS = 15
// стало
export const API_ERROR_15_ACCESS = 15
У этого 2 бенефита:
API_ERROR_15_
, без необходимости лазить по исходникам библиотекиКроме того, раз уж мы делаем брейкинг чейндж, можно сократить префикс до API_ERR_
, чтобы уменьшить длину констант.
enum'ы по ссылке, которые используются больше одного раза, всё-таки генерировать как отдельный union-тип, а не дублировать в каждом месте. Например, WallWallpostAttachmentType
.
export type RequestMap = {
'groups.get': {
params: GroupsGetParams;
response: GroupsGetResponse | GroupsGetExtendedResponse;
};
'friends.get': {
params: FriendsGetParams;
response: FriendsGetResponse | FriendsGetFieldsResponse;
};
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.