@uiwwsw/react-query-helper 1.0.11 → 1.1.0
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/README.md +43 -1
- package/dist/cli.js +85 -12
- package/dist/config.d.ts +4 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/core/analyzer.d.ts +9 -1
- package/dist/core/analyzer.d.ts.map +1 -1
- package/dist/core/generator.d.ts +11 -1
- package/dist/core/generator.d.ts.map +1 -1
- package/dist/index.js +61 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,10 +69,18 @@ npm install --save-dev @uiwwsw/react-query-helper
|
|
|
69
69
|
|
|
70
70
|
설치 후 아래 명령으로 루트에 `rqh.config.ts`를 자동 생성할 수 있습니다.
|
|
71
71
|
|
|
72
|
+
```bash
|
|
73
|
+
npx @uiwwsw/react-query-helper --init
|
|
74
|
+
# 또는
|
|
75
|
+
bunx @uiwwsw/react-query-helper --init
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
이미 프로젝트에 설치한 뒤라면 아래처럼 실행해도 됩니다.
|
|
79
|
+
|
|
72
80
|
```bash
|
|
73
81
|
npx react-query-helper --init
|
|
74
82
|
# 또는
|
|
75
|
-
|
|
83
|
+
npm exec react-query-helper -- --init
|
|
76
84
|
```
|
|
77
85
|
|
|
78
86
|
생성되는 파일 예시는 다음과 같습니다.
|
|
@@ -86,6 +94,16 @@ const config: AutoQueryConfig = {
|
|
|
86
94
|
outputDir: "./src/options", // 생성된 코드가 저장될 경로
|
|
87
95
|
ignoredFiles: ["domain.ts", "adaptor.ts"],
|
|
88
96
|
templateDir: "@uiwwsw/react-query-helper",
|
|
97
|
+
analyzer: {
|
|
98
|
+
exportFilter: "exported-only",
|
|
99
|
+
functionMatchMode: "all",
|
|
100
|
+
includeNames: [],
|
|
101
|
+
excludeNames: [],
|
|
102
|
+
},
|
|
103
|
+
template: {
|
|
104
|
+
enabledArtifacts: ["query", "mutation", "infinite"],
|
|
105
|
+
keyStyle: "path",
|
|
106
|
+
},
|
|
89
107
|
};
|
|
90
108
|
|
|
91
109
|
export default config;
|
|
@@ -109,6 +127,8 @@ export default config;
|
|
|
109
127
|
```bash
|
|
110
128
|
bun run watch # 실시간 변경 감지
|
|
111
129
|
bun run generate # 전체 파일 일괄 생성
|
|
130
|
+
# 또는
|
|
131
|
+
npm exec react-query-helper -- --generate
|
|
112
132
|
```
|
|
113
133
|
|
|
114
134
|
## CLI 명령어
|
|
@@ -128,6 +148,8 @@ bun run generate # 전체 파일 일괄 생성
|
|
|
128
148
|
| `outputDir` | ✅ | 생성된 훅과 옵션 파일이 저장될 디렉토리 |
|
|
129
149
|
| `ignoredFiles` | ❌ | 코드 생성에서 제외할 파일 이름 배열 |
|
|
130
150
|
| `templateDir` | ❌ | 생성된 코드에서 `queryOption` 계열을 import 할 모듈 경로 또는 상대 디렉토리 |
|
|
151
|
+
| `analyzer` | ❌ | 어떤 함수를 읽을지 필터링하는 설정 (`exported-only`, `async-only`, include/exclude 등) |
|
|
152
|
+
| `template` | ❌ | 어떤 아티팩트를 생성할지와 이름 규칙을 제어하는 설정 |
|
|
131
153
|
|
|
132
154
|
## Infinite Query 기본값
|
|
133
155
|
|
|
@@ -195,6 +217,26 @@ export const createUserInfiniteQueryOption = infiniteOption(
|
|
|
195
217
|
|
|
196
218
|
조직 공용 헬퍼를 따로 두고 있다면 `templateDir`로 생성 코드의 import 경로를 바꿀 수 있습니다.
|
|
197
219
|
|
|
220
|
+
또한 `template` / `analyzer` 설정으로 현재 팀 구조에 맞게 생성 규칙을 바꿀 수 있습니다.
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
const config: AutoQueryConfig = {
|
|
224
|
+
sourceDir: "./libs",
|
|
225
|
+
outputDir: "./src/options",
|
|
226
|
+
analyzer: {
|
|
227
|
+
functionMatchMode: "async-only",
|
|
228
|
+
excludeNames: ["debugHelper"],
|
|
229
|
+
},
|
|
230
|
+
template: {
|
|
231
|
+
enabledArtifacts: ["query"],
|
|
232
|
+
keyStyle: "function-only",
|
|
233
|
+
outputNames: {
|
|
234
|
+
query: "CustomQueryOption",
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
```
|
|
239
|
+
|
|
198
240
|
```ts
|
|
199
241
|
const config: AutoQueryConfig = {
|
|
200
242
|
sourceDir: "./libs",
|
package/dist/cli.js
CHANGED
|
@@ -1584,7 +1584,27 @@ import { writeFileSync, existsSync as existsSync2, mkdirSync, unlinkSync } from
|
|
|
1584
1584
|
// src/core/analyzer.ts
|
|
1585
1585
|
import ts from "typescript";
|
|
1586
1586
|
import { readFileSync } from "fs";
|
|
1587
|
-
function
|
|
1587
|
+
function shouldIncludeFunction(info, config = {}) {
|
|
1588
|
+
const exportFilter = config.exportFilter ?? "exported-only";
|
|
1589
|
+
const functionMatchMode = config.functionMatchMode ?? "all";
|
|
1590
|
+
if (exportFilter === "exported-only" && !info.isExported) {
|
|
1591
|
+
return false;
|
|
1592
|
+
}
|
|
1593
|
+
if (functionMatchMode === "async-only" && !info.isAsync) {
|
|
1594
|
+
return false;
|
|
1595
|
+
}
|
|
1596
|
+
if (functionMatchMode === "sync-only" && info.isAsync) {
|
|
1597
|
+
return false;
|
|
1598
|
+
}
|
|
1599
|
+
if (config.includeNames?.length && !config.includeNames.includes(info.name)) {
|
|
1600
|
+
return false;
|
|
1601
|
+
}
|
|
1602
|
+
if (config.excludeNames?.includes(info.name)) {
|
|
1603
|
+
return false;
|
|
1604
|
+
}
|
|
1605
|
+
return true;
|
|
1606
|
+
}
|
|
1607
|
+
function analyzeFile(filePath, config = {}) {
|
|
1588
1608
|
const sourceCode = readFileSync(filePath, "utf8");
|
|
1589
1609
|
const scriptKind = filePath.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
1590
1610
|
const sourceFile = ts.createSourceFile(filePath, sourceCode, ts.ScriptTarget.Latest, true, scriptKind);
|
|
@@ -1614,31 +1634,62 @@ function analyzeFile(filePath) {
|
|
|
1614
1634
|
}
|
|
1615
1635
|
}
|
|
1616
1636
|
});
|
|
1617
|
-
return functionInfos.filter((info) => info
|
|
1637
|
+
return functionInfos.filter((info) => shouldIncludeFunction(info, config));
|
|
1618
1638
|
}
|
|
1619
1639
|
|
|
1620
1640
|
// src/core/generator.ts
|
|
1621
|
-
|
|
1641
|
+
var DEFAULT_IMPORT_NAMES = {
|
|
1642
|
+
query: "queryOption",
|
|
1643
|
+
mutation: "mutationOption",
|
|
1644
|
+
infinite: "infiniteOption"
|
|
1645
|
+
};
|
|
1646
|
+
var DEFAULT_OUTPUT_NAMES = {
|
|
1647
|
+
query: "QueryOption",
|
|
1648
|
+
mutation: "MutationOption",
|
|
1649
|
+
infinite: "InfiniteQueryOption"
|
|
1650
|
+
};
|
|
1651
|
+
function getKeySegments(info, fileName, keySegments, keyStyle) {
|
|
1652
|
+
switch (keyStyle) {
|
|
1653
|
+
case "function-only":
|
|
1654
|
+
return [info.name];
|
|
1655
|
+
case "file-only":
|
|
1656
|
+
return [fileName, info.name];
|
|
1657
|
+
case "path":
|
|
1658
|
+
default:
|
|
1659
|
+
return [...keySegments, info.name];
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
function generateOptionsCode(functionInfos, importPath, { keySegments, fileName, templateImportPath, template }) {
|
|
1622
1663
|
if (functionInfos.length === 0) {
|
|
1623
1664
|
return "";
|
|
1624
1665
|
}
|
|
1666
|
+
const enabledArtifacts = template?.enabledArtifacts?.length ? template.enabledArtifacts : ["query", "mutation", "infinite"];
|
|
1667
|
+
const importNames = {
|
|
1668
|
+
...DEFAULT_IMPORT_NAMES,
|
|
1669
|
+
...template?.importNames
|
|
1670
|
+
};
|
|
1671
|
+
const outputNames = {
|
|
1672
|
+
...DEFAULT_OUTPUT_NAMES,
|
|
1673
|
+
...template?.outputNames
|
|
1674
|
+
};
|
|
1675
|
+
const helperImports = enabledArtifacts.map((artifact) => importNames[artifact]);
|
|
1625
1676
|
const functionNames = functionInfos.map((info) => info.name);
|
|
1677
|
+
const keyStyle = template?.keyStyle ?? "path";
|
|
1626
1678
|
let newContent = `import { ${functionNames.join(", ")} } from "${importPath}";
|
|
1627
1679
|
`;
|
|
1628
|
-
newContent += `import {
|
|
1680
|
+
newContent += `import { ${helperImports.join(", ")} } from "${templateImportPath}";
|
|
1629
1681
|
|
|
1630
1682
|
`;
|
|
1631
1683
|
functionInfos.forEach((info) => {
|
|
1632
|
-
const keyArray = JSON.stringify(
|
|
1684
|
+
const keyArray = JSON.stringify(getKeySegments(info, fileName, keySegments, keyStyle));
|
|
1633
1685
|
const keyName = `${info.name}Key`;
|
|
1634
1686
|
newContent += `export const ${keyName} = ${keyArray} as const;
|
|
1635
1687
|
`;
|
|
1636
|
-
|
|
1688
|
+
enabledArtifacts.forEach((artifact) => {
|
|
1689
|
+
newContent += `export const ${info.name}${outputNames[artifact]} = ${importNames[artifact]}(${keyName}, ${info.name});
|
|
1637
1690
|
`;
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
newContent += `export const ${info.name}InfiniteQueryOption = infiniteOption(${keyName}, ${info.name});
|
|
1641
|
-
|
|
1691
|
+
});
|
|
1692
|
+
newContent += `
|
|
1642
1693
|
`;
|
|
1643
1694
|
});
|
|
1644
1695
|
return newContent;
|
|
@@ -1752,6 +1803,26 @@ const config: AutoQueryConfig = {
|
|
|
1752
1803
|
outputDir: "./src/options",
|
|
1753
1804
|
ignoredFiles: ["domain.ts", "adaptor.ts"],
|
|
1754
1805
|
templateDir: "@uiwwsw/react-query-helper",
|
|
1806
|
+
analyzer: {
|
|
1807
|
+
exportFilter: "exported-only",
|
|
1808
|
+
functionMatchMode: "all",
|
|
1809
|
+
includeNames: [],
|
|
1810
|
+
excludeNames: [],
|
|
1811
|
+
},
|
|
1812
|
+
template: {
|
|
1813
|
+
enabledArtifacts: ["query", "mutation", "infinite"],
|
|
1814
|
+
keyStyle: "path",
|
|
1815
|
+
importNames: {
|
|
1816
|
+
query: "queryOption",
|
|
1817
|
+
mutation: "mutationOption",
|
|
1818
|
+
infinite: "infiniteOption",
|
|
1819
|
+
},
|
|
1820
|
+
outputNames: {
|
|
1821
|
+
query: "QueryOption",
|
|
1822
|
+
mutation: "MutationOption",
|
|
1823
|
+
infinite: "InfiniteQueryOption",
|
|
1824
|
+
},
|
|
1825
|
+
},
|
|
1755
1826
|
};
|
|
1756
1827
|
|
|
1757
1828
|
export default config;
|
|
@@ -1829,7 +1900,7 @@ async function processFile(filePath) {
|
|
|
1829
1900
|
return;
|
|
1830
1901
|
}
|
|
1831
1902
|
try {
|
|
1832
|
-
const functionInfos = analyzeFile(filePath);
|
|
1903
|
+
const functionInfos = analyzeFile(filePath, config.analyzer);
|
|
1833
1904
|
if (functionInfos.length === 0) {
|
|
1834
1905
|
console.log(`No exported functions found in ${filePath}. Skipping code generation.`);
|
|
1835
1906
|
return;
|
|
@@ -1838,7 +1909,9 @@ async function processFile(filePath) {
|
|
|
1838
1909
|
const outputFile = join4(outputFolder, `${fileName}Options.ts`);
|
|
1839
1910
|
const generatedCode = generateOptionsCode(functionInfos, normalizeModulePath(relative3(dirname4(outputFile), filePath.replace(new RegExp(`${extname3(filePath)}$`), "")) || ".", true), {
|
|
1840
1911
|
keySegments: relativeDir === "." ? [fileName] : relativeDir.split(sep).filter(Boolean),
|
|
1841
|
-
|
|
1912
|
+
fileName,
|
|
1913
|
+
templateImportPath: config.resolvedTemplateDir && config.templateDir?.startsWith(".") ? normalizeModulePath(relative3(dirname4(outputFile), config.resolvedTemplateDir) || ".", true) : config.resolvedTemplateDir ?? "@uiwwsw/react-query-helper",
|
|
1914
|
+
template: config.template
|
|
1842
1915
|
});
|
|
1843
1916
|
if (!existsSync2(outputFolder)) {
|
|
1844
1917
|
mkdirSync(outputFolder, { recursive: true });
|
package/dist/config.d.ts
CHANGED
|
@@ -3,11 +3,15 @@ export declare class ConfigLoadError extends Error {
|
|
|
3
3
|
readonly cause?: unknown | undefined;
|
|
4
4
|
constructor(message: string, configPath?: string | undefined, cause?: unknown | undefined);
|
|
5
5
|
}
|
|
6
|
+
import type { AnalyzerConfig } from "./core/analyzer";
|
|
7
|
+
import type { TemplateConfig } from "./core/generator";
|
|
6
8
|
export interface AutoQueryConfig {
|
|
7
9
|
sourceDir: string;
|
|
8
10
|
outputDir: string;
|
|
9
11
|
templateDir?: string;
|
|
10
12
|
ignoredFiles?: string[];
|
|
13
|
+
analyzer?: AnalyzerConfig;
|
|
14
|
+
template?: TemplateConfig;
|
|
11
15
|
}
|
|
12
16
|
export interface ResolvedAutoQueryConfig extends AutoQueryConfig {
|
|
13
17
|
configPath?: string;
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAOA,qBAAa,eAAgB,SAAQ,KAAK;aAGtB,UAAU,CAAC,EAAE,MAAM;aACnB,KAAK,CAAC,EAAE,OAAO;gBAF/B,OAAO,EAAE,MAAM,EACC,UAAU,CAAC,EAAE,MAAM,YAAA,EACnB,KAAK,CAAC,EAAE,OAAO,YAAA;CAKlC;AAED,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAOA,qBAAa,eAAgB,SAAQ,KAAK;aAGtB,UAAU,CAAC,EAAE,MAAM;aACnB,KAAK,CAAC,EAAE,OAAO;gBAF/B,OAAO,EAAE,MAAM,EACC,UAAU,CAAC,EAAE,MAAM,YAAA,EACnB,KAAK,CAAC,EAAE,OAAO,YAAA;CAKlC;AAED,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,QAAQ,CAAC,EAAE,cAAc,CAAC;CAC3B;AAED,MAAM,WAAW,uBAAwB,SAAQ,eAAe;IAC9D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAsDD,wBAAgB,iBAAiB,CAAC,GAAG,SAAgB,sBAIpD;AAED,wBAAsB,UAAU,IAAI,OAAO,CAAC,uBAAuB,CAAC,CA+CnE"}
|
package/dist/core/analyzer.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
export type ExportFilter = "exported-only" | "all";
|
|
2
|
+
export type FunctionMatchMode = "all" | "async-only" | "sync-only";
|
|
3
|
+
export interface AnalyzerConfig {
|
|
4
|
+
exportFilter?: ExportFilter;
|
|
5
|
+
functionMatchMode?: FunctionMatchMode;
|
|
6
|
+
includeNames?: string[];
|
|
7
|
+
excludeNames?: string[];
|
|
8
|
+
}
|
|
1
9
|
export interface FunctionInfo {
|
|
2
10
|
name: string;
|
|
3
11
|
parameters: string[];
|
|
4
12
|
isAsync: boolean;
|
|
5
13
|
isExported: boolean;
|
|
6
14
|
}
|
|
7
|
-
export declare function analyzeFile(filePath: string): FunctionInfo[];
|
|
15
|
+
export declare function analyzeFile(filePath: string, config?: AnalyzerConfig): FunctionInfo[];
|
|
8
16
|
//# sourceMappingURL=analyzer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analyzer.d.ts","sourceRoot":"","sources":["../../src/core/analyzer.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,OAAO,CAAC;CACrB;
|
|
1
|
+
{"version":3,"file":"analyzer.d.ts","sourceRoot":"","sources":["../../src/core/analyzer.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,YAAY,GAAG,eAAe,GAAG,KAAK,CAAC;AACnD,MAAM,MAAM,iBAAiB,GAAG,KAAK,GAAG,YAAY,GAAG,WAAW,CAAC;AAEnE,MAAM,WAAW,cAAc;IAC7B,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,OAAO,CAAC;CACrB;AAgCD,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,EAChB,MAAM,GAAE,cAAmB,GAC1B,YAAY,EAAE,CAwDhB"}
|
package/dist/core/generator.d.ts
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import type { FunctionInfo } from "./analyzer";
|
|
2
|
+
export type GeneratedArtifact = "query" | "mutation" | "infinite";
|
|
3
|
+
export type KeyStyle = "path" | "file-only" | "function-only";
|
|
4
|
+
export interface TemplateConfig {
|
|
5
|
+
importNames?: Partial<Record<GeneratedArtifact, string>>;
|
|
6
|
+
outputNames?: Partial<Record<GeneratedArtifact, string>>;
|
|
7
|
+
enabledArtifacts?: GeneratedArtifact[];
|
|
8
|
+
keyStyle?: KeyStyle;
|
|
9
|
+
}
|
|
2
10
|
interface GenerateOptionsParams {
|
|
3
11
|
keySegments: string[];
|
|
12
|
+
fileName: string;
|
|
4
13
|
templateImportPath: string;
|
|
14
|
+
template?: TemplateConfig;
|
|
5
15
|
}
|
|
6
|
-
export declare function generateOptionsCode(functionInfos: FunctionInfo[], importPath: string, { keySegments, templateImportPath }: GenerateOptionsParams): string;
|
|
16
|
+
export declare function generateOptionsCode(functionInfos: FunctionInfo[], importPath: string, { keySegments, fileName, templateImportPath, template }: GenerateOptionsParams): string;
|
|
7
17
|
export {};
|
|
8
18
|
//# sourceMappingURL=generator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/core/generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,UAAU,qBAAqB;IAC7B,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,kBAAkB,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/core/generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,MAAM,iBAAiB,GAAG,OAAO,GAAG,UAAU,GAAG,UAAU,CAAC;AAClE,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,WAAW,GAAG,eAAe,CAAC;AAE9D,MAAM,WAAW,cAAc;IAC7B,WAAW,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC,CAAC;IACzD,WAAW,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC,CAAC;IACzD,gBAAgB,CAAC,EAAE,iBAAiB,EAAE,CAAC;IACvC,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB;AAED,UAAU,qBAAqB;IAC7B,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,EAAE,cAAc,CAAC;CAC3B;AA+BD,wBAAgB,mBAAmB,CACjC,aAAa,EAAE,YAAY,EAAE,EAC7B,UAAU,EAAE,MAAM,EAClB,EAAE,WAAW,EAAE,QAAQ,EAAE,kBAAkB,EAAE,QAAQ,EAAE,EAAE,qBAAqB,GAC7E,MAAM,CA0CR"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
1
1
|
// src/core/analyzer.ts
|
|
2
2
|
import ts from "typescript";
|
|
3
3
|
import { readFileSync } from "fs";
|
|
4
|
-
function
|
|
4
|
+
function shouldIncludeFunction(info, config = {}) {
|
|
5
|
+
const exportFilter = config.exportFilter ?? "exported-only";
|
|
6
|
+
const functionMatchMode = config.functionMatchMode ?? "all";
|
|
7
|
+
if (exportFilter === "exported-only" && !info.isExported) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
if (functionMatchMode === "async-only" && !info.isAsync) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (functionMatchMode === "sync-only" && info.isAsync) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (config.includeNames?.length && !config.includeNames.includes(info.name)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
if (config.excludeNames?.includes(info.name)) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
function analyzeFile(filePath, config = {}) {
|
|
5
25
|
const sourceCode = readFileSync(filePath, "utf8");
|
|
6
26
|
const scriptKind = filePath.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
7
27
|
const sourceFile = ts.createSourceFile(filePath, sourceCode, ts.ScriptTarget.Latest, true, scriptKind);
|
|
@@ -31,30 +51,61 @@ function analyzeFile(filePath) {
|
|
|
31
51
|
}
|
|
32
52
|
}
|
|
33
53
|
});
|
|
34
|
-
return functionInfos.filter((info) => info
|
|
54
|
+
return functionInfos.filter((info) => shouldIncludeFunction(info, config));
|
|
35
55
|
}
|
|
36
56
|
// src/core/generator.ts
|
|
37
|
-
|
|
57
|
+
var DEFAULT_IMPORT_NAMES = {
|
|
58
|
+
query: "queryOption",
|
|
59
|
+
mutation: "mutationOption",
|
|
60
|
+
infinite: "infiniteOption"
|
|
61
|
+
};
|
|
62
|
+
var DEFAULT_OUTPUT_NAMES = {
|
|
63
|
+
query: "QueryOption",
|
|
64
|
+
mutation: "MutationOption",
|
|
65
|
+
infinite: "InfiniteQueryOption"
|
|
66
|
+
};
|
|
67
|
+
function getKeySegments(info, fileName, keySegments, keyStyle) {
|
|
68
|
+
switch (keyStyle) {
|
|
69
|
+
case "function-only":
|
|
70
|
+
return [info.name];
|
|
71
|
+
case "file-only":
|
|
72
|
+
return [fileName, info.name];
|
|
73
|
+
case "path":
|
|
74
|
+
default:
|
|
75
|
+
return [...keySegments, info.name];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function generateOptionsCode(functionInfos, importPath, { keySegments, fileName, templateImportPath, template }) {
|
|
38
79
|
if (functionInfos.length === 0) {
|
|
39
80
|
return "";
|
|
40
81
|
}
|
|
82
|
+
const enabledArtifacts = template?.enabledArtifacts?.length ? template.enabledArtifacts : ["query", "mutation", "infinite"];
|
|
83
|
+
const importNames = {
|
|
84
|
+
...DEFAULT_IMPORT_NAMES,
|
|
85
|
+
...template?.importNames
|
|
86
|
+
};
|
|
87
|
+
const outputNames = {
|
|
88
|
+
...DEFAULT_OUTPUT_NAMES,
|
|
89
|
+
...template?.outputNames
|
|
90
|
+
};
|
|
91
|
+
const helperImports = enabledArtifacts.map((artifact) => importNames[artifact]);
|
|
41
92
|
const functionNames = functionInfos.map((info) => info.name);
|
|
93
|
+
const keyStyle = template?.keyStyle ?? "path";
|
|
42
94
|
let newContent = `import { ${functionNames.join(", ")} } from "${importPath}";
|
|
43
95
|
`;
|
|
44
|
-
newContent += `import {
|
|
96
|
+
newContent += `import { ${helperImports.join(", ")} } from "${templateImportPath}";
|
|
45
97
|
|
|
46
98
|
`;
|
|
47
99
|
functionInfos.forEach((info) => {
|
|
48
|
-
const keyArray = JSON.stringify(
|
|
100
|
+
const keyArray = JSON.stringify(getKeySegments(info, fileName, keySegments, keyStyle));
|
|
49
101
|
const keyName = `${info.name}Key`;
|
|
50
102
|
newContent += `export const ${keyName} = ${keyArray} as const;
|
|
51
103
|
`;
|
|
52
|
-
|
|
104
|
+
enabledArtifacts.forEach((artifact) => {
|
|
105
|
+
newContent += `export const ${info.name}${outputNames[artifact]} = ${importNames[artifact]}(${keyName}, ${info.name});
|
|
53
106
|
`;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
newContent += `export const ${info.name}InfiniteQueryOption = infiniteOption(${keyName}, ${info.name});
|
|
57
|
-
|
|
107
|
+
});
|
|
108
|
+
newContent += `
|
|
58
109
|
`;
|
|
59
110
|
});
|
|
60
111
|
return newContent;
|