mobx-tanstack-query-api 0.0.78 → 0.0.80
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/codegen/index.d.ts +2 -0
- package/codegen/index.d.ts.map +1 -1
- package/codegen/index.js +12 -1
- package/codegen/templates/meta-info.tmpl.d.ts +1 -1
- package/codegen/templates/meta-info.tmpl.d.ts.map +1 -1
- package/codegen/templates/meta-info.tmpl.js +20 -2
- package/codegen/utils/remove-unused-types.d.ts +4 -0
- package/codegen/utils/remove-unused-types.d.ts.map +1 -0
- package/codegen/utils/remove-unused-types.js +87 -0
- package/package.json +7 -1
package/codegen/index.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export interface GenerateQueryApiParams {
|
|
|
22
22
|
requestPathPrefix?: string;
|
|
23
23
|
requestPathSuffix?: string;
|
|
24
24
|
requestInfoPrefix?: string;
|
|
25
|
+
removeUnusedTypes?: boolean;
|
|
25
26
|
formatExportGroupName?: (groupName: string, utils: CodegenDataUtils) => string;
|
|
26
27
|
/**
|
|
27
28
|
* Group endpoints and collect it into object
|
|
@@ -72,6 +73,7 @@ export interface GenerateQueryApiParams {
|
|
|
72
73
|
libImports?: {
|
|
73
74
|
'mobx-tanstack-query-api'?: string;
|
|
74
75
|
};
|
|
76
|
+
tsconfigPath?: string;
|
|
75
77
|
transforms?: {
|
|
76
78
|
groupEnumValue?: (group: string, namespace?: Maybe<string>) => string;
|
|
77
79
|
tagEnumValue?: (tag: string, namespace?: Maybe<string>) => string;
|
package/codegen/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/codegen/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/codegen/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEtC,OAAO,EAAE,SAAS,EAAE,YA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AAC,EAC5E,gBAAgB,CACjB,CAAC;AAEF,eAAO,MAAM,WAAW,GACtB,QAAQ,sBAAsB,GAAG,sBAAsB,EAAE,KACxD,OAAO,CAAC,IAAI,CA6ed,CAAC"}
|
package/codegen/index.js
CHANGED
|
@@ -7,13 +7,19 @@ import { dataContractsFileTmpl } from './templates/data-contracts-file.tmpl.js';
|
|
|
7
7
|
import { endpointPerFileTmpl } from './templates/endpoint-per-file.tmpl.js';
|
|
8
8
|
import { indexTsForEndpointPerFileTmpl } from './templates/index-ts-for-endpoint-per-file.tmpl.js';
|
|
9
9
|
import { metaInfoTmpl } from './templates/meta-info.tmpl.js';
|
|
10
|
+
import { removeUnusedTypes } from './utils/remove-unused-types.js';
|
|
10
11
|
const __filename = fileURLToPath(import.meta.url);
|
|
11
12
|
const __dirname = path.dirname(__filename);
|
|
13
|
+
const __execdirname = process.cwd();
|
|
12
14
|
export const generateApi = async (params) => {
|
|
13
15
|
if (Array.isArray(params)) {
|
|
14
16
|
await Promise.all(params.map((param) => generateApi(param)));
|
|
15
17
|
return;
|
|
16
18
|
}
|
|
19
|
+
const tsconfigPath = params.tsconfigPath
|
|
20
|
+
? path.resolve(__execdirname, params.tsconfigPath)
|
|
21
|
+
: path.resolve(__execdirname, './tsconfig.json');
|
|
22
|
+
console.info('using tsconfig', tsconfigPath);
|
|
17
23
|
const importFileParams = {
|
|
18
24
|
queryClient: !params.queryClient || typeof params.queryClient === 'string'
|
|
19
25
|
? {
|
|
@@ -309,7 +315,7 @@ export * as ${exportGroupName} from './endpoints';
|
|
|
309
315
|
}
|
|
310
316
|
// #endregion
|
|
311
317
|
}
|
|
312
|
-
const metaInfo = (namespace ?? nonEmptyGroups.size > 0)
|
|
318
|
+
const metaInfo = (namespace ?? (nonEmptyGroups.size > 0 || tagsSet.size > 0))
|
|
313
319
|
? {
|
|
314
320
|
namespace,
|
|
315
321
|
groupNames: [...nonEmptyGroups.values()],
|
|
@@ -376,4 +382,9 @@ export * as ${namespace} from './__exports';
|
|
|
376
382
|
}),
|
|
377
383
|
});
|
|
378
384
|
}
|
|
385
|
+
if (params.removeUnusedTypes) {
|
|
386
|
+
removeUnusedTypes({
|
|
387
|
+
dir: params.output,
|
|
388
|
+
});
|
|
389
|
+
}
|
|
379
390
|
};
|
|
@@ -8,5 +8,5 @@ export interface MetaInfoTmplParams {
|
|
|
8
8
|
}
|
|
9
9
|
export declare const formatGroupNameEnumKey: (groupName: string, { _ }: CodegenDataUtils) => Capitalize<string>;
|
|
10
10
|
export declare const formatTagNameEnumKey: (tagName: string, utils: CodegenDataUtils) => Capitalize<string>;
|
|
11
|
-
export declare const metaInfoTmpl: ({ metaInfo, utils, formatTSContent, codegenParams, }: MetaInfoTmplParams) => Promise<string>;
|
|
11
|
+
export declare const metaInfoTmpl: ({ metaInfo, utils, formatTSContent, codegenParams, ...other }: MetaInfoTmplParams) => Promise<string>;
|
|
12
12
|
//# sourceMappingURL=meta-info.tmpl.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"meta-info.tmpl.d.ts","sourceRoot":"","sources":["../../../src/codegen/templates/meta-info.tmpl.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"meta-info.tmpl.d.ts","sourceRoot":"","sources":["../../../src/codegen/templates/meta-info.tmpl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,QAAQ,EACT,MAAM,aAAa,CAAC;AAIrB,MAAM,WAAW,kBAAkB;IACjC,eAAe,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACrD,aAAa,EAAE,sBAAsB,CAAC;IACtC,QAAQ,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC1B,KAAK,EAAE,gBAAgB,CAAC;CACzB;AAED,eAAO,MAAM,sBAAsB,GACjC,WAAW,MAAM,EACjB,OAAO,gBAAgB,uBACgB,CAAC;AAE1C,eAAO,MAAM,oBAAoB,GAC/B,SAAS,MAAM,EACf,OAAO,gBAAgB,uBACkB,CAAC;AAE5C,eAAO,MAAM,YAAY,GAAU,+DAMhC,kBAAkB,oBA4CpB,CAAC"}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { LINTERS_IGNORE } from './constants.js';
|
|
2
2
|
export const formatGroupNameEnumKey = (groupName, { _ }) => _.upperFirst(_.camelCase(groupName));
|
|
3
3
|
export const formatTagNameEnumKey = (tagName, utils) => formatGroupNameEnumKey(tagName, utils);
|
|
4
|
-
export const metaInfoTmpl = async ({ metaInfo, utils, formatTSContent, codegenParams, }) => {
|
|
4
|
+
export const metaInfoTmpl = async ({ metaInfo, utils, formatTSContent, codegenParams, ...other }) => {
|
|
5
|
+
const tagsMap = new Map(other.configuration?.apiConfig?.tags?.map((it) => [
|
|
6
|
+
it.name,
|
|
7
|
+
it,
|
|
8
|
+
]));
|
|
5
9
|
return await formatTSContent(`${LINTERS_IGNORE}
|
|
6
10
|
${[
|
|
7
11
|
metaInfo?.namespace && `export const namespace = "${metaInfo?.namespace}";`,
|
|
@@ -14,7 +18,21 @@ export const enum Group {
|
|
|
14
18
|
metaInfo?.tags?.length &&
|
|
15
19
|
`
|
|
16
20
|
export const enum Tag {
|
|
17
|
-
${metaInfo?.tags
|
|
21
|
+
${metaInfo?.tags
|
|
22
|
+
.map((tagName) => {
|
|
23
|
+
const tagData = tagsMap.get(tagName);
|
|
24
|
+
let description = tagData?.description;
|
|
25
|
+
if (!description) {
|
|
26
|
+
description = utils._.words(tagName).join(' ');
|
|
27
|
+
}
|
|
28
|
+
return [
|
|
29
|
+
description && `/** ${description} */`,
|
|
30
|
+
`${formatTagNameEnumKey(tagName, utils)} = "${codegenParams.transforms?.tagEnumValue?.(tagName) ?? tagName}"`,
|
|
31
|
+
]
|
|
32
|
+
.filter(Boolean)
|
|
33
|
+
.join('\n');
|
|
34
|
+
})
|
|
35
|
+
.join(',\n')}
|
|
18
36
|
}
|
|
19
37
|
`,
|
|
20
38
|
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remove-unused-types.d.ts","sourceRoot":"","sources":["../../../src/codegen/utils/remove-unused-types.ts"],"names":[],"mappings":"AAyGA,eAAO,MAAM,iBAAiB,GAAU,SAAS;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,kBAS/D,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Project, SyntaxKind, } from 'ts-morph';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
const removeUnusedTypesItteration = async ({ dir }) => {
|
|
4
|
+
// Создаем проект в памяти
|
|
5
|
+
const project = new Project();
|
|
6
|
+
// Добавляем все TS/TSX файлы из указанной директории
|
|
7
|
+
project.addSourceFilesAtPaths([
|
|
8
|
+
path.join(dir, '**/*.ts'),
|
|
9
|
+
path.join(dir, '**/*.tsx'),
|
|
10
|
+
]);
|
|
11
|
+
const sourceFiles = project.getSourceFiles();
|
|
12
|
+
const typeDeclarations = new Map();
|
|
13
|
+
const usedTypes = new Set();
|
|
14
|
+
let dataContractsSourceFile;
|
|
15
|
+
// Шаг 1: Собираем все объявления типов
|
|
16
|
+
for (const file of sourceFiles) {
|
|
17
|
+
if (file.getFilePath().includes(`${dir}/data-contracts.ts`)) {
|
|
18
|
+
dataContractsSourceFile = file;
|
|
19
|
+
}
|
|
20
|
+
// Интерфейсы
|
|
21
|
+
for (const intf of file.getInterfaces()) {
|
|
22
|
+
const name = intf.getName();
|
|
23
|
+
typeDeclarations.set(name, {
|
|
24
|
+
node: intf,
|
|
25
|
+
file: file.getFilePath(),
|
|
26
|
+
line: intf.getStartLineNumber(),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
// Type-алиасы
|
|
30
|
+
for (const typeAlias of file.getTypeAliases()) {
|
|
31
|
+
const name = typeAlias.getName();
|
|
32
|
+
typeDeclarations.set(name, {
|
|
33
|
+
node: typeAlias,
|
|
34
|
+
file: file.getFilePath(),
|
|
35
|
+
line: typeAlias.getStartLineNumber(),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Шаг 2: Ищем использования типов
|
|
40
|
+
for (const file of sourceFiles) {
|
|
41
|
+
// Проверяем все идентификаторы в файле
|
|
42
|
+
const identifiers = file.getDescendantsOfKind(SyntaxKind.Identifier);
|
|
43
|
+
for (const identifier of identifiers) {
|
|
44
|
+
const name = identifier.getText();
|
|
45
|
+
if (typeDeclarations.has(name)) {
|
|
46
|
+
const parent = identifier.getParent();
|
|
47
|
+
const declaration = typeDeclarations.get(name).node;
|
|
48
|
+
// Игнорируем само объявление типа
|
|
49
|
+
const isDeclaration = parent === declaration;
|
|
50
|
+
// Игнорируем экспорт типа
|
|
51
|
+
const isExport = parent?.getKind() === SyntaxKind.ExportSpecifier;
|
|
52
|
+
if (!isDeclaration && !isExport) {
|
|
53
|
+
usedTypes.add(name);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!dataContractsSourceFile) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
let removedCount = 0;
|
|
62
|
+
const exportedDeclarations = dataContractsSourceFile.getExportedDeclarations();
|
|
63
|
+
// Шаг 3: Фильтруем неиспользуемые типы
|
|
64
|
+
for (const [name] of typeDeclarations) {
|
|
65
|
+
if (!usedTypes.has(name) && exportedDeclarations.has(name)) {
|
|
66
|
+
const declarations = exportedDeclarations.get(name);
|
|
67
|
+
declarations?.forEach((declaration) => {
|
|
68
|
+
if ('remove' in declaration) {
|
|
69
|
+
declaration.remove();
|
|
70
|
+
removedCount++;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (removedCount > 0) {
|
|
76
|
+
dataContractsSourceFile.saveSync();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
export const removeUnusedTypes = async ({ dir }) => {
|
|
80
|
+
const itterations = Array.from({ length: 3 })
|
|
81
|
+
.fill(null)
|
|
82
|
+
// eslint-disable-next-line unicorn/consistent-function-scoping
|
|
83
|
+
.map(() => () => removeUnusedTypesItteration({ dir }));
|
|
84
|
+
for await (const itteration of itterations) {
|
|
85
|
+
itteration();
|
|
86
|
+
}
|
|
87
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mobx-tanstack-query-api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.80",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"author": "js2me",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"http-status-code-types": "^0.0.2",
|
|
26
26
|
"mobx-tanstack-query": "^5.3.1",
|
|
27
27
|
"swagger-typescript-api": "13.2.1",
|
|
28
|
+
"ts-morph": "^26.0.0",
|
|
28
29
|
"yummies": "^3.2.9"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
@@ -112,6 +113,11 @@
|
|
|
112
113
|
"default": "./codegen/templates/new-endpoint.tmpl.js",
|
|
113
114
|
"types": "./codegen/templates/new-endpoint.tmpl.d.ts"
|
|
114
115
|
},
|
|
116
|
+
"./codegen/utils/remove-unused-types": {
|
|
117
|
+
"import": "./codegen/utils/remove-unused-types.js",
|
|
118
|
+
"default": "./codegen/utils/remove-unused-types.js",
|
|
119
|
+
"types": "./codegen/utils/remove-unused-types.d.ts"
|
|
120
|
+
},
|
|
115
121
|
".": {
|
|
116
122
|
"import": "./index.js",
|
|
117
123
|
"default": "./index.js",
|