@uiwwsw/react-query-helper 1.0.12 → 1.2.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 CHANGED
@@ -94,6 +94,16 @@ const config: AutoQueryConfig = {
94
94
  outputDir: "./src/options", // 생성된 코드가 저장될 경로
95
95
  ignoredFiles: ["domain.ts", "adaptor.ts"],
96
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
+ },
97
107
  };
98
108
 
99
109
  export default config;
@@ -138,6 +148,8 @@ npm exec react-query-helper -- --generate
138
148
  | `outputDir` | ✅ | 생성된 훅과 옵션 파일이 저장될 디렉토리 |
139
149
  | `ignoredFiles` | ❌ | 코드 생성에서 제외할 파일 이름 배열 |
140
150
  | `templateDir` | ❌ | 생성된 코드에서 `queryOption` 계열을 import 할 모듈 경로 또는 상대 디렉토리 |
151
+ | `analyzer` | ❌ | 어떤 함수를 읽을지 필터링하는 설정 (`exported-only`, `async-only`, include/exclude 등) |
152
+ | `template` | ❌ | 어떤 아티팩트를 생성할지와 이름 규칙을 제어하는 설정 |
141
153
 
142
154
  ## Infinite Query 기본값
143
155
 
@@ -205,6 +217,42 @@ export const createUserInfiniteQueryOption = infiniteOption(
205
217
 
206
218
  조직 공용 헬퍼를 따로 두고 있다면 `templateDir`로 생성 코드의 import 경로를 바꿀 수 있습니다.
207
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
+
240
+ 더 강하게 커스터마이징하려면 외부 모듈도 연결할 수 있습니다.
241
+
242
+ ```ts
243
+ const config: AutoQueryConfig = {
244
+ sourceDir: "./libs",
245
+ outputDir: "./src/options",
246
+ customAnalyzerPath: "./rqh.analyzer.mjs",
247
+ customTemplatePath: "./rqh.template.mjs",
248
+ };
249
+ ```
250
+
251
+ - `customAnalyzerPath`: 파일을 읽고 원하는 함수 메타 목록을 직접 반환
252
+ - `customTemplatePath`: 최종 생성 코드 문자열을 직접 반환
253
+
254
+ 즉 이제는 기본 규칙을 조금 바꾸는 수준이 아니라, 팀 전용 생성기처럼 확장할 수 있습니다.
255
+
208
256
  ```ts
209
257
  const config: AutoQueryConfig = {
210
258
  sourceDir: "./libs",
package/dist/cli.js CHANGED
@@ -1580,11 +1580,32 @@ import {
1580
1580
  sep
1581
1581
  } from "path";
1582
1582
  import { writeFileSync, existsSync as existsSync2, mkdirSync, unlinkSync } from "fs";
1583
+ import { pathToFileURL as pathToFileURL2 } from "url";
1583
1584
 
1584
1585
  // src/core/analyzer.ts
1585
1586
  import ts from "typescript";
1586
1587
  import { readFileSync } from "fs";
1587
- function analyzeFile(filePath) {
1588
+ function shouldIncludeFunction(info, config = {}) {
1589
+ const exportFilter = config.exportFilter ?? "exported-only";
1590
+ const functionMatchMode = config.functionMatchMode ?? "all";
1591
+ if (exportFilter === "exported-only" && !info.isExported) {
1592
+ return false;
1593
+ }
1594
+ if (functionMatchMode === "async-only" && !info.isAsync) {
1595
+ return false;
1596
+ }
1597
+ if (functionMatchMode === "sync-only" && info.isAsync) {
1598
+ return false;
1599
+ }
1600
+ if (config.includeNames?.length && !config.includeNames.includes(info.name)) {
1601
+ return false;
1602
+ }
1603
+ if (config.excludeNames?.includes(info.name)) {
1604
+ return false;
1605
+ }
1606
+ return true;
1607
+ }
1608
+ function analyzeFile(filePath, config = {}) {
1588
1609
  const sourceCode = readFileSync(filePath, "utf8");
1589
1610
  const scriptKind = filePath.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
1590
1611
  const sourceFile = ts.createSourceFile(filePath, sourceCode, ts.ScriptTarget.Latest, true, scriptKind);
@@ -1614,31 +1635,62 @@ function analyzeFile(filePath) {
1614
1635
  }
1615
1636
  }
1616
1637
  });
1617
- return functionInfos.filter((info) => info.isExported);
1638
+ return functionInfos.filter((info) => shouldIncludeFunction(info, config));
1618
1639
  }
1619
1640
 
1620
1641
  // src/core/generator.ts
1621
- function generateOptionsCode(functionInfos, importPath, { keySegments, templateImportPath }) {
1642
+ var DEFAULT_IMPORT_NAMES = {
1643
+ query: "queryOption",
1644
+ mutation: "mutationOption",
1645
+ infinite: "infiniteOption"
1646
+ };
1647
+ var DEFAULT_OUTPUT_NAMES = {
1648
+ query: "QueryOption",
1649
+ mutation: "MutationOption",
1650
+ infinite: "InfiniteQueryOption"
1651
+ };
1652
+ function getKeySegments(info, fileName, keySegments, keyStyle) {
1653
+ switch (keyStyle) {
1654
+ case "function-only":
1655
+ return [info.name];
1656
+ case "file-only":
1657
+ return [fileName, info.name];
1658
+ case "path":
1659
+ default:
1660
+ return [...keySegments, info.name];
1661
+ }
1662
+ }
1663
+ function generateOptionsCode(functionInfos, importPath, { keySegments, fileName, templateImportPath, template }) {
1622
1664
  if (functionInfos.length === 0) {
1623
1665
  return "";
1624
1666
  }
1667
+ const enabledArtifacts = template?.enabledArtifacts?.length ? template.enabledArtifacts : ["query", "mutation", "infinite"];
1668
+ const importNames = {
1669
+ ...DEFAULT_IMPORT_NAMES,
1670
+ ...template?.importNames
1671
+ };
1672
+ const outputNames = {
1673
+ ...DEFAULT_OUTPUT_NAMES,
1674
+ ...template?.outputNames
1675
+ };
1676
+ const helperImports = enabledArtifacts.map((artifact) => importNames[artifact]);
1625
1677
  const functionNames = functionInfos.map((info) => info.name);
1678
+ const keyStyle = template?.keyStyle ?? "path";
1626
1679
  let newContent = `import { ${functionNames.join(", ")} } from "${importPath}";
1627
1680
  `;
1628
- newContent += `import { queryOption, mutationOption, infiniteOption } from "${templateImportPath}";
1681
+ newContent += `import { ${helperImports.join(", ")} } from "${templateImportPath}";
1629
1682
 
1630
1683
  `;
1631
1684
  functionInfos.forEach((info) => {
1632
- const keyArray = JSON.stringify([...keySegments, info.name]);
1685
+ const keyArray = JSON.stringify(getKeySegments(info, fileName, keySegments, keyStyle));
1633
1686
  const keyName = `${info.name}Key`;
1634
1687
  newContent += `export const ${keyName} = ${keyArray} as const;
1635
1688
  `;
1636
- newContent += `export const ${info.name}QueryOption = queryOption(${keyName}, ${info.name});
1637
- `;
1638
- newContent += `export const ${info.name}MutationOption = mutationOption(${keyName}, ${info.name});
1689
+ enabledArtifacts.forEach((artifact) => {
1690
+ newContent += `export const ${info.name}${outputNames[artifact]} = ${importNames[artifact]}(${keyName}, ${info.name});
1639
1691
  `;
1640
- newContent += `export const ${info.name}InfiniteQueryOption = infiniteOption(${keyName}, ${info.name});
1641
-
1692
+ });
1693
+ newContent += `
1642
1694
  `;
1643
1695
  });
1644
1696
  return newContent;
@@ -1728,7 +1780,9 @@ async function loadConfig() {
1728
1780
  configDir: configDir2,
1729
1781
  resolvedSourceDir: resolve3(configDir2, mergedConfig.sourceDir),
1730
1782
  resolvedOutputDir: resolve3(configDir2, mergedConfig.outputDir),
1731
- resolvedTemplateDir: mergedConfig.templateDir && mergedConfig.templateDir.startsWith(".") ? resolve3(configDir2, mergedConfig.templateDir) : mergedConfig.templateDir
1783
+ resolvedTemplateDir: mergedConfig.templateDir && mergedConfig.templateDir.startsWith(".") ? resolve3(configDir2, mergedConfig.templateDir) : mergedConfig.templateDir,
1784
+ resolvedCustomAnalyzerPath: mergedConfig.customAnalyzerPath && mergedConfig.customAnalyzerPath.startsWith(".") ? resolve3(configDir2, mergedConfig.customAnalyzerPath) : mergedConfig.customAnalyzerPath,
1785
+ resolvedCustomTemplatePath: mergedConfig.customTemplatePath && mergedConfig.customTemplatePath.startsWith(".") ? resolve3(configDir2, mergedConfig.customTemplatePath) : mergedConfig.customTemplatePath
1732
1786
  };
1733
1787
  }
1734
1788
  const configDir = process.cwd();
@@ -1737,13 +1791,17 @@ async function loadConfig() {
1737
1791
  configDir,
1738
1792
  resolvedSourceDir: resolve3(configDir, defaultConfig.sourceDir),
1739
1793
  resolvedOutputDir: resolve3(configDir, defaultConfig.outputDir),
1740
- resolvedTemplateDir: defaultConfig.templateDir
1794
+ resolvedTemplateDir: defaultConfig.templateDir,
1795
+ resolvedCustomAnalyzerPath: undefined,
1796
+ resolvedCustomTemplatePath: undefined
1741
1797
  };
1742
1798
  }
1743
1799
 
1744
1800
  // src/cli.ts
1745
1801
  import prettier from "prettier";
1746
1802
  var config;
1803
+ var customAnalyzerModule = null;
1804
+ var customTemplateModule = null;
1747
1805
  var DEFAULT_CONFIG_FILE_NAME = "rqh.config.ts";
1748
1806
  var DEFAULT_CONFIG_TEMPLATE = `import type { AutoQueryConfig } from "@uiwwsw/react-query-helper";
1749
1807
 
@@ -1752,6 +1810,28 @@ const config: AutoQueryConfig = {
1752
1810
  outputDir: "./src/options",
1753
1811
  ignoredFiles: ["domain.ts", "adaptor.ts"],
1754
1812
  templateDir: "@uiwwsw/react-query-helper",
1813
+ analyzer: {
1814
+ exportFilter: "exported-only",
1815
+ functionMatchMode: "all",
1816
+ includeNames: [],
1817
+ excludeNames: [],
1818
+ },
1819
+ template: {
1820
+ enabledArtifacts: ["query", "mutation", "infinite"],
1821
+ keyStyle: "path",
1822
+ importNames: {
1823
+ query: "queryOption",
1824
+ mutation: "mutationOption",
1825
+ infinite: "infiniteOption",
1826
+ },
1827
+ outputNames: {
1828
+ query: "QueryOption",
1829
+ mutation: "MutationOption",
1830
+ infinite: "InfiniteQueryOption",
1831
+ },
1832
+ },
1833
+ customAnalyzerPath: "./rqh.analyzer.ts",
1834
+ customTemplatePath: "./rqh.template.ts",
1755
1835
  };
1756
1836
 
1757
1837
  export default config;
@@ -1793,7 +1873,8 @@ function runInit() {
1793
1873
  console.log(`✅ Created ${configPath}`);
1794
1874
  console.log("Next steps:");
1795
1875
  console.log("1. Update sourceDir/outputDir if needed.");
1796
- console.log("2. Run `react-query-helper --generate`.");
1876
+ console.log("2. Remove customAnalyzerPath/customTemplatePath if you don't need plugin hooks.");
1877
+ console.log("3. Run `react-query-helper --generate`.");
1797
1878
  }
1798
1879
  async function loadConfigOrExit() {
1799
1880
  try {
@@ -1811,6 +1892,39 @@ async function loadConfigOrExit() {
1811
1892
  return null;
1812
1893
  }
1813
1894
  }
1895
+ async function loadPluginModules() {
1896
+ customAnalyzerModule = null;
1897
+ customTemplateModule = null;
1898
+ if (config.resolvedCustomAnalyzerPath) {
1899
+ const imported = await import(pathToFileURL2(config.resolvedCustomAnalyzerPath).href);
1900
+ customAnalyzerModule = imported.default ?? imported;
1901
+ }
1902
+ if (config.resolvedCustomTemplatePath) {
1903
+ const imported = await import(pathToFileURL2(config.resolvedCustomTemplatePath).href);
1904
+ customTemplateModule = imported.default ?? imported;
1905
+ }
1906
+ }
1907
+ async function collectFunctionInfos(filePath) {
1908
+ if (customAnalyzerModule?.analyzeFile) {
1909
+ const result = await customAnalyzerModule.analyzeFile(filePath, config);
1910
+ return Array.isArray(result) ? result : [];
1911
+ }
1912
+ return analyzeFile(filePath, config.analyzer);
1913
+ }
1914
+ async function renderOptionsCode(params) {
1915
+ if (customTemplateModule?.generateOptionsCode) {
1916
+ return await customTemplateModule.generateOptionsCode({
1917
+ ...params,
1918
+ config
1919
+ });
1920
+ }
1921
+ return generateOptionsCode(params.functionInfos, params.importPath, {
1922
+ keySegments: params.keySegments,
1923
+ fileName: params.fileName,
1924
+ templateImportPath: params.templateImportPath,
1925
+ template: config.template
1926
+ });
1927
+ }
1814
1928
  async function processFile(filePath) {
1815
1929
  if (!shouldProcess(filePath)) {
1816
1930
  return;
@@ -1829,15 +1943,18 @@ async function processFile(filePath) {
1829
1943
  return;
1830
1944
  }
1831
1945
  try {
1832
- const functionInfos = analyzeFile(filePath);
1946
+ const functionInfos = await collectFunctionInfos(filePath);
1833
1947
  if (functionInfos.length === 0) {
1834
1948
  console.log(`No exported functions found in ${filePath}. Skipping code generation.`);
1835
1949
  return;
1836
1950
  }
1837
1951
  const outputFolder = relativeDir === "." ? outputRoot : join4(outputRoot, relativeDir);
1838
1952
  const outputFile = join4(outputFolder, `${fileName}Options.ts`);
1839
- const generatedCode = generateOptionsCode(functionInfos, normalizeModulePath(relative3(dirname4(outputFile), filePath.replace(new RegExp(`${extname3(filePath)}$`), "")) || ".", true), {
1953
+ const generatedCode = await renderOptionsCode({
1954
+ functionInfos,
1955
+ importPath: normalizeModulePath(relative3(dirname4(outputFile), filePath.replace(new RegExp(`${extname3(filePath)}$`), "")) || ".", true),
1840
1956
  keySegments: relativeDir === "." ? [fileName] : relativeDir.split(sep).filter(Boolean),
1957
+ fileName,
1841
1958
  templateImportPath: config.resolvedTemplateDir && config.templateDir?.startsWith(".") ? normalizeModulePath(relative3(dirname4(outputFile), config.resolvedTemplateDir) || ".", true) : config.resolvedTemplateDir ?? "@uiwwsw/react-query-helper"
1842
1959
  });
1843
1960
  if (!existsSync2(outputFolder)) {
@@ -1859,6 +1976,7 @@ async function runGenerate() {
1859
1976
  return;
1860
1977
  }
1861
1978
  config = loadedConfig;
1979
+ await loadPluginModules();
1862
1980
  const sourcePath = config.resolvedSourceDir;
1863
1981
  const outputRootPath = config.resolvedOutputDir;
1864
1982
  ensureDirectory(outputRootPath);
@@ -1889,6 +2007,7 @@ async function runWatch() {
1889
2007
  return;
1890
2008
  }
1891
2009
  config = loadedConfig;
2010
+ await loadPluginModules();
1892
2011
  const sourcePath = config.resolvedSourceDir;
1893
2012
  const outputRootPath = config.resolvedOutputDir;
1894
2013
  ensureDirectory(outputRootPath);
package/dist/config.d.ts CHANGED
@@ -3,11 +3,30 @@ 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;
15
+ customAnalyzerPath?: string;
16
+ customTemplatePath?: string;
17
+ }
18
+ export interface CustomAnalyzerModule {
19
+ analyzeFile: (filePath: string, config: ResolvedAutoQueryConfig) => Promise<unknown> | unknown;
20
+ }
21
+ export interface CustomTemplateModule {
22
+ generateOptionsCode: (params: {
23
+ functionInfos: unknown[];
24
+ importPath: string;
25
+ keySegments: string[];
26
+ fileName: string;
27
+ templateImportPath: string;
28
+ config: ResolvedAutoQueryConfig;
29
+ }) => Promise<string> | string;
11
30
  }
12
31
  export interface ResolvedAutoQueryConfig extends AutoQueryConfig {
13
32
  configPath?: string;
@@ -15,6 +34,8 @@ export interface ResolvedAutoQueryConfig extends AutoQueryConfig {
15
34
  resolvedSourceDir: string;
16
35
  resolvedOutputDir: string;
17
36
  resolvedTemplateDir?: string;
37
+ resolvedCustomAnalyzerPath?: string;
38
+ resolvedCustomTemplatePath?: string;
18
39
  }
19
40
  export declare function resolveConfigPath(cwd?: string): string | undefined;
20
41
  export declare function loadConfig(): Promise<ResolvedAutoQueryConfig>;
@@ -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;CACzB;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"}
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;IAC1B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,oBAAoB;IACnC,WAAW,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,uBAAuB,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;CAChG;AAED,MAAM,WAAW,oBAAoB;IACnC,mBAAmB,EAAE,CAAC,MAAM,EAAE;QAC5B,aAAa,EAAE,OAAO,EAAE,CAAC;QACzB,UAAU,EAAE,MAAM,CAAC;QACnB,WAAW,EAAE,MAAM,EAAE,CAAC;QACtB,QAAQ,EAAE,MAAM,CAAC;QACjB,kBAAkB,EAAE,MAAM,CAAC;QAC3B,MAAM,EAAE,uBAAuB,CAAC;KACjC,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;CAChC;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;IAC7B,0BAA0B,CAAC,EAAE,MAAM,CAAC;IACpC,0BAA0B,CAAC,EAAE,MAAM,CAAC;CACrC;AAsDD,wBAAgB,iBAAiB,CAAC,GAAG,SAAgB,sBAIpD;AAED,wBAAsB,UAAU,IAAI,OAAO,CAAC,uBAAuB,CAAC,CAyDnE"}
@@ -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;AAED,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,YAAY,EAAE,CA4C5D"}
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"}
@@ -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;CAC5B;AAED,wBAAgB,mBAAmB,CACjC,aAAa,EAAE,YAAY,EAAE,EAC7B,UAAU,EAAE,MAAM,EAClB,EAAE,WAAW,EAAE,kBAAkB,EAAE,EAAE,qBAAqB,GACzD,MAAM,CAqBR"}
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 analyzeFile(filePath) {
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.isExported);
54
+ return functionInfos.filter((info) => shouldIncludeFunction(info, config));
35
55
  }
36
56
  // src/core/generator.ts
37
- function generateOptionsCode(functionInfos, importPath, { keySegments, templateImportPath }) {
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 { queryOption, mutationOption, infiniteOption } from "${templateImportPath}";
96
+ newContent += `import { ${helperImports.join(", ")} } from "${templateImportPath}";
45
97
 
46
98
  `;
47
99
  functionInfos.forEach((info) => {
48
- const keyArray = JSON.stringify([...keySegments, info.name]);
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
- newContent += `export const ${info.name}QueryOption = queryOption(${keyName}, ${info.name});
104
+ enabledArtifacts.forEach((artifact) => {
105
+ newContent += `export const ${info.name}${outputNames[artifact]} = ${importNames[artifact]}(${keyName}, ${info.name});
53
106
  `;
54
- newContent += `export const ${info.name}MutationOption = mutationOption(${keyName}, ${info.name});
55
- `;
56
- newContent += `export const ${info.name}InfiniteQueryOption = infiniteOption(${keyName}, ${info.name});
57
-
107
+ });
108
+ newContent += `
58
109
  `;
59
110
  });
60
111
  return newContent;
@@ -143,7 +194,9 @@ async function loadConfig() {
143
194
  configDir: configDir2,
144
195
  resolvedSourceDir: resolve(configDir2, mergedConfig.sourceDir),
145
196
  resolvedOutputDir: resolve(configDir2, mergedConfig.outputDir),
146
- resolvedTemplateDir: mergedConfig.templateDir && mergedConfig.templateDir.startsWith(".") ? resolve(configDir2, mergedConfig.templateDir) : mergedConfig.templateDir
197
+ resolvedTemplateDir: mergedConfig.templateDir && mergedConfig.templateDir.startsWith(".") ? resolve(configDir2, mergedConfig.templateDir) : mergedConfig.templateDir,
198
+ resolvedCustomAnalyzerPath: mergedConfig.customAnalyzerPath && mergedConfig.customAnalyzerPath.startsWith(".") ? resolve(configDir2, mergedConfig.customAnalyzerPath) : mergedConfig.customAnalyzerPath,
199
+ resolvedCustomTemplatePath: mergedConfig.customTemplatePath && mergedConfig.customTemplatePath.startsWith(".") ? resolve(configDir2, mergedConfig.customTemplatePath) : mergedConfig.customTemplatePath
147
200
  };
148
201
  }
149
202
  const configDir = process.cwd();
@@ -152,7 +205,9 @@ async function loadConfig() {
152
205
  configDir,
153
206
  resolvedSourceDir: resolve(configDir, defaultConfig.sourceDir),
154
207
  resolvedOutputDir: resolve(configDir, defaultConfig.outputDir),
155
- resolvedTemplateDir: defaultConfig.templateDir
208
+ resolvedTemplateDir: defaultConfig.templateDir,
209
+ resolvedCustomAnalyzerPath: undefined,
210
+ resolvedCustomTemplatePath: undefined
156
211
  };
157
212
  }
158
213
  // src/utils/query.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiwwsw/react-query-helper",
3
- "version": "1.0.12",
3
+ "version": "1.2.0",
4
4
  "module": "dist/index.js",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",