rpc4next-cli 0.3.0 → 0.3.2
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 +19 -1
- package/dist/index.js +7 -7
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -323,7 +323,25 @@ async function callUserApi() {
|
|
|
323
323
|
## 🚧 Requirements
|
|
324
324
|
|
|
325
325
|
- Next.js 14+ (App Router 使用)
|
|
326
|
-
- Node.js 20.
|
|
326
|
+
- Node.js 20.19.2+
|
|
327
|
+
- aqua (Node.js / Bun のバージョン管理)
|
|
328
|
+
|
|
329
|
+
```bash
|
|
330
|
+
aqua i
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
- `aqua.yaml` を更新したら、チェックサムを更新してください
|
|
334
|
+
|
|
335
|
+
```bash
|
|
336
|
+
aqua update-checksum
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
- CI / ローカルで設定を強制する場合は以下を利用してください
|
|
340
|
+
|
|
341
|
+
```bash
|
|
342
|
+
export AQUA_ENFORCE_CHECKSUM=true
|
|
343
|
+
export AQUA_ENFORCE_REQUIRE_CHECKSUM=true
|
|
344
|
+
```
|
|
327
345
|
|
|
328
346
|
---
|
|
329
347
|
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
`}${
|
|
2
|
+
import Jt from"node:path";import{parseArgs as Zt}from"node:util";import _t from"node:path";var R=["page.tsx","route.ts"];import M from"node:path";var st=(t,e)=>{let r=S(M.relative(M.dirname(t),e)).replace(/\.tsx?$/,"");return r.startsWith("../")||(r=`./${r}`),r},S=t=>t.replace(/\\/g,"/"),x=t=>M.relative(process.cwd(),t);import J from"node:fs";import Vt from"node:path";var ot=["Query"];var H="Endpoint",k="QueryKey",G="ParamsKey",it=[H,G,k],at="rpc4next/client";import dt from"node:fs";import A from"node:path";import{CATCH_ALL_PREFIX as kt,DYNAMIC_PREFIX as Gt,OPTIONAL_CATCH_ALL_PREFIX as Kt}from"rpc4next-shared";import K from"node:path";var y=new Map,N=new Map,ct=(t,e)=>{let r=K.resolve(e);[...t.keys()].forEach(s=>{let n=K.resolve(s);(n===r||r.startsWith(n+K.sep))&&t.delete(s)})},pt=t=>{ct(y,t)},mt=t=>{ct(N,t)};import jt from"node:fs";import{HTTP_METHODS_EXCLUDE_OPTIONS as Ut}from"rpc4next-shared";import Yt from"node:crypto";var ut=(t,e)=>{let r=Yt.createHash("md5").update(`${t}::${e}`).digest("hex").slice(0,16);return`${e}_${r}`};var v=(t,e)=>!t||!e?"":`Record<${t}, ${e}>`,L=t=>t.length===0||t.some(({name:e,type:r})=>!e||!r)?"":`{ ${t.map(({name:e,type:r})=>`"${e}": ${r}`).join(`${";"} `)}${t.length>1?";":""} }`,D=(t,e,r)=>!t||!e?"":r?`import type { ${t} as ${r} } from "${e}"${";"}`:`import type { ${t} } from "${e}"${";"}`;var ft=(t,e,r,s)=>{let n=st(t,e),i=ut(n,r);return{importName:i,importPath:n,importStatement:D(r,n,i),type:s(r,i),exportName:r}},Mt=t=>ot.find(e=>new RegExp(`export (interface ${e} ?{|type ${e} ?=)`).test(t)),Ht=(t,e)=>new RegExp(`export (async )?(function ${e} ?\\(|const ${e} ?=|\\{[^}]*\\b${e}\\b[^}]*\\} ?=|const \\{[^}]*\\b${e}\\b[^}]*\\} ?=|\\{[^}]*\\b${e}\\b[^}]*\\} from)`).test(t),ht=(t,e)=>{let r=jt.readFileSync(e,"utf8"),s=Mt(r),n=s?ft(t,e,s,(o,c)=>v(k,c)):void 0,i=Ut.filter(o=>Ht(r,o)).map(o=>ft(t,e,o,(c,a)=>L([{name:`$${c.toLowerCase()}`,type:`typeof ${a}`}])));return{...n?{query:n}:{},routes:i}};var gt=new Set(R),Xt=t=>t.startsWith("(.)")||t.startsWith("(..)")||t.startsWith("(...)"),Qt=t=>{try{return decodeURIComponent(t)}catch{return t}},X=t=>{let e=t.startsWith("(")&&t.endsWith(")"),r=t.startsWith("@"),s=t.startsWith("[[...")&&t.endsWith("]]"),n=t.startsWith("[...")&&t.endsWith("]"),i=t.startsWith("[")&&t.endsWith("]"),o=t.startsWith("_"),c=Xt(t),a=Qt(t);return{isCatchAll:n,isDynamic:i,isGroup:e,isIntercept:c,isOptionalCatchAll:s,isParallel:r,isPrivate:o,staticKeyName:a}},Et=t=>{let e=y.get(t);if(e!==void 0)return e;let r=A.basename(t),s=X(r);if(r==="node_modules"||s.isPrivate||s.isIntercept)return y.set(t,!1),!1;let n=dt.readdirSync(t,{withFileTypes:!0});for(let i of n){let{name:o}=i,c=A.join(t,o),a=X(o);if(!(o==="node_modules"||a.isPrivate||a.isIntercept)){if(i.isFile()&>.has(o))return y.set(t,!0),!0;if(i.isDirectory()&&Et(c))return y.set(t,!0),!0}}return y.set(t,!1),!1},qt=(t,{isDynamic:e,isCatchAll:r,isOptionalCatchAll:s})=>{let n=t;return e&&(n=n.replace(/^\[+|\]+$/g,"")),(r||s)&&(n=n.replace(/^\.{3}/,"")),{paramName:n,keyName:`${s?Kt:r?kt:e?Gt:""}${n}`}},q=(t,e,r="",s=[])=>{let n=N.get(e);if(n!==void 0)return n;let i=r,o=r+" ",c=[],a=[],p=[],m=[],h=[...s],tt=dt.readdirSync(e,{withFileTypes:!0}).filter(E=>{if(E.isDirectory()){let T=A.join(e,E.name);return Et(T)}return gt.has(E.name)}).sort();for(let E of tt){let T=S(A.join(e,E.name));if(E.isDirectory()){let $=E.name,{isGroup:C,isParallel:d,isOptionalCatchAll:u,isCatchAll:l,isDynamic:f,isPrivate:j,isIntercept:vt,staticKeyName:Lt}=X($);if(j||vt)continue;let{paramName:Dt,keyName:wt}=qt($,{isDynamic:f,isCatchAll:l,isOptionalCatchAll:u}),Ot=f||l||u?[...h,{paramName:Dt,routeType:{isDynamic:f,isCatchAll:l,isOptionalCatchAll:u,isGroup:C,isParallel:d}}]:h,rt=C||d,{pathStructure:U,imports:Ft,paramsTypes:Wt}=q(t,T,rt?i:o,Ot);if(a.push(...Ft),m.push(...Wt),rt){let b=U.match(/^\s*\{([\s\S]*)\}\s*$/),nt=U.trim();if(b)c.push(`${o}${b[1].trim()}`);else if(nt)p.push(nt);else throw new Error(`Invalid empty child path structure in grouped/parallel route: ${T}`)}else{let b=f||l||u?wt:Lt;c.push(`${o}"${b}": ${U}`)}}else{let{query:$,routes:C}=ht(t,T);if($){let{importStatement:d,importPath:u,type:l}=$;a.push({statement:d,path:u}),p.push(l)}if(C.forEach(d=>{let{importStatement:u,importPath:l,type:f}=d;a.push({statement:u,path:l}),p.push(f)}),p.push(H),h.length>0){let d=h.map(({paramName:l,routeType:f})=>{let j=f.isCatchAll?"string[]":f.isOptionalCatchAll?"string[] | undefined":"string";return{name:l,type:j}}),u=L(d);m.push({paramsType:u,dirPath:A.dirname(T)}),p.push(v(G,u))}}}let W=p.join(" & "),Y=c.length>0?`{${`
|
|
3
|
+
`}${c.join(`,${`
|
|
4
4
|
`}`)}${`
|
|
5
|
-
`}${
|
|
6
|
-
`)}`:"",
|
|
7
|
-
`}${
|
|
5
|
+
`}${i}}`:"",et={pathStructure:W&&Y?`${W} & ${Y}`:W||Y,imports:a,paramsTypes:m};return N.set(e,et),et};var yt=(t,e)=>{let{pathStructure:r,imports:s,paramsTypes:n}=q(t,e),i=`export type PathStructure = ${r}${";"}`,o=s.length?`${s.sort((m,h)=>m.path.localeCompare(h.path,void 0,{numeric:!0})).map(m=>m.statement).join(`
|
|
6
|
+
`)}`:"",c=it.filter(m=>r.includes(m)),a=D(c.join(" ,"),at),p=n.map(({paramsType:m,dirPath:h})=>({paramsType:`export type Params = ${m}${";"}`,dirPath:h}));return{pathStructure:`${a}${`
|
|
7
|
+
`}${o}${`
|
|
8
8
|
`}${`
|
|
9
|
-
`}${
|
|
9
|
+
`}${i}`,paramsTypes:p}};var Bt=()=>!!process.stdout?.isTTY;var B=t=>e=>Bt()?`\x1B[${t}m${e}\x1B[0m`:e,Tt=B(36),St=B(32),V=B(31);var _=(t,e,r="\u2192",s=24)=>`${t.padEnd(s)} ${r} ${e}`,z=(t=0)=>" ".repeat(t),xt=()=>({info:(t,e={})=>{let{indentLevel:r=0,event:s}=e,n=s?`${Tt(`[${s}]`)} `:"";console.log(`${z(r)}${n}${t}`)},success:(t,e={})=>{let{indentLevel:r=0}=e;console.log(`${z(r)}${St("\u2713")} ${t}`)},error:(t,e={})=>{let{indentLevel:r=0}=e;console.error(`${z(r)}${V("\u2717")} ${V(t)}`)}});var Pt=(t,e)=>J.existsSync(t)&&J.readFileSync(t,"utf8")===e?!1:(J.writeFileSync(t,e),!0),$t=({baseDir:t,outputPath:e,paramsFileName:r,logger:s})=>{s.info("Generating types...",{event:"generate"});let{pathStructure:n,paramsTypes:i}=yt(e,t);if(Pt(e,n)?s.success(_("Path structure type",x(e),"\u2192",20),{indentLevel:1}):s.info(_("Unchanged path type",x(e),"\u2192",20),{indentLevel:1}),r){let o=!1;i.forEach(({paramsType:c,dirPath:a})=>{let p=Vt.join(a,r),m=Pt(p,c);o=o||m}),o?s.success(_("Params types",r,"\u2192",20),{indentLevel:1}):s.info(_("Unchanged params",r,"\u2192",20),{indentLevel:1})}};import zt from"chokidar";var It=(t,e)=>{let r=null,s=!1,n=null,i=async(...o)=>{s=!0;try{await t(...o)}finally{if(s=!1,n){let c=n;n=null,i(...c)}}};return(...o)=>{r&&clearTimeout(r),r=setTimeout(()=>{if(s){n=o;return}i(...o)},e)}};var At=(t,e,r)=>{r.info(`${x(t)}`,{event:"watch"});let s=a=>R.some(p=>a.endsWith(p)),n=new Set,i=It(async()=>{let a=Array.from(n);n.clear();for(let p of a)pt(p),mt(p);await e()},300),o=zt.watch(t,{ignoreInitial:!0,ignored:(a,p)=>!!p?.isFile()&&!s(a)});o.on("ready",()=>{i(),o.on("all",(a,p)=>{if(s(p)){let m=x(p);r.info(m,{event:a}),n.add(p),i()}})}),o.on("error",a=>{a instanceof Error?r.error(`Watcher error: ${a.message}`):r.error(`Unknown watcher error: ${String(a)}`)});let c=()=>{o.close().then(()=>{r.info("Watcher closed.",{event:"watch"})}).catch(a=>{r.error(`Failed to close watcher: ${a.message}`)})};process.on("SIGINT",c),process.on("SIGTERM",c)};var Ct=(t,e,r,s)=>{try{return $t({baseDir:t,outputPath:e,paramsFileName:r,logger:s}),0}catch(n){return n instanceof Error?s.error(`Failed to generate: ${n.message}`):s.error(`Unknown error occurred during generate: ${String(n)}`),1}},Rt=(t,e,r,s)=>{let n=S(_t.resolve(t)),i=S(_t.resolve(e)),o=typeof r.paramsFile=="string"?r.paramsFile:null;return r.paramsFile!==void 0&&!o?(s.error("Error: --params-file requires a filename."),1):r.watch?(At(n,()=>{Ct(n,i,o,s)},s),0):Ct(n,i,o,s)};function te(t){if(t.length===0)return[];if(t[0].startsWith("-"))return t;let e=Jt.basename(t[0]).toLowerCase();return new Set(["node","node.exe","bun","bun.exe","deno","deno.exe"]).has(e)&&t.length>=2?t.slice(2):t}function Z(t){let e=`
|
|
10
10
|
Generate RPC client type definitions based on the Next.js path structure.
|
|
11
11
|
|
|
12
12
|
Usage:
|
|
@@ -20,7 +20,7 @@ Options:
|
|
|
20
20
|
-w, --watch Watch mode: regenerate on file changes
|
|
21
21
|
-p, --params-file [filename] Generate params types file (optional filename)
|
|
22
22
|
-h, --help Show help
|
|
23
|
-
`.trim();t.info(e)}function
|
|
23
|
+
`.trim();t.info(e)}function ee(t,e){let r=[],s;for(let n=0;n<t.length;n++){let i=t[n],o=!1;for(let c of e)if(i.startsWith(`${c}=`)){s=i.slice(`${c}=`.length)||!0,o=!0;break}if(!o){if(e.includes(i)){let c=t[n+1];typeof c=="string"&&!c.startsWith("-")?(s=c,n++):s=!0;continue}r.push(i)}}return{args:r,value:s}}var Nt=(t,e=xt())=>{try{let r=te(t),{args:s,value:n}=ee(r,["-p","--params-file"]),{values:i,positionals:o}=Zt({args:s,options:{watch:{type:"boolean",short:"w"},help:{type:"boolean",short:"h"}},allowPositionals:!0,strict:!0});i.help&&(Z(e),process.exit(0));let c=o[0],a=o[1];(!c||!a)&&(e.error("Missing required arguments: <baseDir> <outputPath>"),Z(e),process.exit(1));let p={watch:!!i.watch,...n!==void 0?{paramsFile:n}:{}};(async()=>{try{let m=await Rt(c,a,p,e);p.watch||process.exit(m)}catch(m){e.error(`Unexpected error occurred:${m instanceof Error?m.message:String(m)}`),process.exit(1)}})()}catch(r){e.error(r instanceof Error?r.message:`Invalid arguments: ${String(r)}`),Z(e),process.exit(1)}};Nt(process.argv);
|
|
24
24
|
/*!
|
|
25
25
|
* Inspired by pathpida (https://github.com/aspida/pathpida),
|
|
26
26
|
* especially the design and UX of its CLI.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rpc4next-cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Command line interface for rpc4next. Generates RPC client type definitions from Next.js routes.",
|
|
5
5
|
"homepage": "https://github.com/watanabe-1/rpc4next#readme",
|
|
6
6
|
"repository": {
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"author": "watanabe-1",
|
|
13
13
|
"type": "module",
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=20.19.2"
|
|
16
|
+
},
|
|
14
17
|
"bin": {
|
|
15
18
|
"rpc4next": "dist/index.js"
|
|
16
19
|
},
|
|
@@ -30,7 +33,7 @@
|
|
|
30
33
|
},
|
|
31
34
|
"dependencies": {
|
|
32
35
|
"chokidar": "^5.0.0",
|
|
33
|
-
"rpc4next-shared": "^0.
|
|
36
|
+
"rpc4next-shared": "^0.3.0"
|
|
34
37
|
},
|
|
35
38
|
"peerDependencies": {
|
|
36
39
|
"next": "^14.0.0 || ^15.0.0"
|