rpc4next-cli 0.3.0 → 0.3.1
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 +18 -0
- package/dist/index.js +4 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -324,6 +324,24 @@ async function callUserApi() {
|
|
|
324
324
|
|
|
325
325
|
- Next.js 14+ (App Router 使用)
|
|
326
326
|
- Node.js 20.9.0+
|
|
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 Gt from"node:path";import{parseArgs as Xt}from"node:util";import _t from"node:path";var A=["page.tsx","route.ts"];import O from"node:path";var J=(t,e)=>{let r=P(O.relative(O.dirname(t),e)).replace(/\.tsx?$/,"");return r.startsWith("../")||(r=`./${r}`),r},P=t=>t.replace(/\\/g,"/"),_=t=>O.relative(process.cwd(),t);import xt from"node:fs";import Ht from"node:path";var Z=["Query"];var F="Endpoint",W="QueryKey",M="ParamsKey",tt=[F,M,W],et="rpc4next/client";import mt from"node:fs";import L from"node:path";import{CATCH_ALL_PREFIX as Ft,DYNAMIC_PREFIX as Wt,HTTP_METHODS_EXCLUDE_OPTIONS as Mt,OPTIONAL_CATCH_ALL_PREFIX as
|
|
2
|
+
import Gt from"node:path";import{parseArgs as Xt}from"node:util";import _t from"node:path";var A=["page.tsx","route.ts"];import O from"node:path";var J=(t,e)=>{let r=P(O.relative(O.dirname(t),e)).replace(/\.tsx?$/,"");return r.startsWith("../")||(r=`./${r}`),r},P=t=>t.replace(/\\/g,"/"),_=t=>O.relative(process.cwd(),t);import xt from"node:fs";import Ht from"node:path";var Z=["Query"];var F="Endpoint",W="QueryKey",M="ParamsKey",tt=[F,M,W],et="rpc4next/client";import mt from"node:fs";import L from"node:path";import{CATCH_ALL_PREFIX as Ft,DYNAMIC_PREFIX as Wt,HTTP_METHODS_EXCLUDE_OPTIONS as Mt,OPTIONAL_CATCH_ALL_PREFIX as jt}from"rpc4next-shared";import j from"node:path";var S=new Map,N=new Map,rt=(t,e)=>{let r=j.resolve(e);[...t.keys()].forEach(s=>{let o=j.resolve(s);(o===r||r.startsWith(o+j.sep))&&t.delete(s)})},ot=t=>{rt(S,t)},st=t=>{rt(N,t)};import Ot from"node:fs";import Dt from"node:crypto";var nt=(t,e)=>{let r=Dt.createHash("md5").update(`${t}::${e}`).digest("hex").slice(0,16);return`${e}_${r}`};var I=(t,e)=>!t||!e?"":`Record<${t}, ${e}>`,b=t=>t.length===0||t.some(({name:e,type:r})=>!e||!r)?"":`{ ${t.map(({name:e,type:r})=>`"${e}": ${r}`).join(`${";"} `)}${t.length>1?";":""} }`,R=(t,e,r)=>!t||!e?"":r?`import type { ${t} as ${r} } from "${e}"${";"}`:`import type { ${t} } from "${e}"${";"}`;var at=(t,e,r,s)=>{let o=Ot.readFileSync(e,"utf8"),n=r(o);if(!n)return;let i=J(t,e),a=nt(i,n);return{importName:a,importPath:i,importStatement:R(n,i,a),type:s(n,a)}},pt=(t,e)=>at(t,e,r=>Z.find(s=>new RegExp(`export (interface ${s} ?{|type ${s} ?=)`).test(r)),(r,s)=>I(W,s)),ct=(t,e,r)=>at(t,e,s=>[r].find(o=>new RegExp(`export (async )?(function ${o} ?\\(|const ${o} ?=|\\{[^}]*\\b${o}\\b[^}]*\\} ?=|const \\{[^}]*\\b${o}\\b[^}]*\\} ?=|\\{[^}]*\\b${o}\\b[^}]*\\} from)`).test(s)),(s,o)=>b([{name:`$${s.toLowerCase()}`,type:`typeof ${o}`}]));var ut=new Set(A),lt=t=>{let e=S.get(t);if(e!==void 0)return e;let r=mt.readdirSync(t,{withFileTypes:!0});for(let s of r){let{name:o}=s,n=L.join(t,o);if(o==="node_modules"||o.startsWith("_")||o.startsWith("(.)")||o.startsWith("(..)")||o.startsWith("(...)"))return S.set(t,!1),!1;if(s.isFile()&&ut.has(o))return S.set(t,!0),!0;if(s.isDirectory()&<(n))return S.set(t,!0),!0}return S.set(t,!1),!1},Yt=(t,{isDynamic:e,isCatchAll:r,isOptionalCatchAll:s})=>{let o=t;return e&&(o=o.replace(/^\[+|\]+$/g,"")),(r||s)&&(o=o.replace(/^\.{3}/,"")),{paramName:o,keyName:`${s?jt:r?Ft:e?Wt:""}${o}`}},U=(t,e,r="",s=[])=>{let o=N.get(e);if(o!==void 0)return o;let n=r,i=r+" ",a=[],p=[],c=[],m=[],h=[...s],Q=mt.readdirSync(e,{withFileTypes:!0}).filter(E=>{if(E.isDirectory()){let T=L.join(e,E.name);return lt(T)}return ut.has(E.name)}).sort();for(let E of Q){let T=P(L.join(e,E.name));if(E.isDirectory()){let u=E.name,g=u.startsWith("(")&&u.endsWith(")"),l=u.startsWith("@"),f=u.startsWith("[[...")&&u.endsWith("]]"),d=u.startsWith("[...")&&u.endsWith("]"),x=u.startsWith("[")&&u.endsWith("]"),{paramName:bt,keyName:Rt}=Yt(u,{isDynamic:x,isCatchAll:d,isOptionalCatchAll:f}),Lt=x||d||f?[...h,{paramName:bt,routeType:{isDynamic:x,isCatchAll:d,isOptionalCatchAll:f,isGroup:g,isParallel:l}}]:h,V=g||l,{pathStructure:D,imports:vt,paramsTypes:wt}=U(t,T,V?n:i,Lt);if(p.push(...vt),m.push(...wt),V){let q=D.match(/^\s*\{([\s\S]*)\}\s*$/),z=D.trim();if(q)a.push(`${i}${q[1].trim()}`);else if(z)c.push(z);else throw new Error(`Invalid empty child path structure in grouped/parallel route: ${T}`)}else a.push(`${i}"${Rt}": ${D}`)}else{let u=pt(t,T);if(u){let{importStatement:g,importPath:l,type:f}=u;p.push({statement:g,path:l}),c.push(f)}if(Mt.forEach(g=>{let l=ct(t,T,g);if(l){let{importStatement:f,importPath:d,type:x}=l;p.push({statement:f,path:d}),c.push(x)}}),c.push(F),h.length>0){let g=h.map(({paramName:f,routeType:d})=>{let x=d.isCatchAll?"string[]":d.isOptionalCatchAll?"string[] | undefined":"string";return{name:f,type:x}}),l=b(g);m.push({paramsType:l,dirPath:L.dirname(T)}),c.push(I(M,l))}}}let v=c.join(" & "),w=a.length>0?`{${`
|
|
3
3
|
`}${a.join(`,${`
|
|
4
4
|
`}`)}${`
|
|
5
|
-
`}${
|
|
5
|
+
`}${n}}`:"",B={pathStructure:v&&w?`${v} & ${w}`:v||w,imports:p,paramsTypes:m};return N.set(e,B),B};var ft=(t,e)=>{let{pathStructure:r,imports:s,paramsTypes:o}=U(t,e),n=`export type PathStructure = ${r}${";"}`,i=s.length?`${s.sort((m,h)=>m.path.localeCompare(h.path,void 0,{numeric:!0})).map(m=>m.statement).join(`
|
|
6
6
|
`)}`:"",a=tt.filter(m=>r.includes(m)),p=R(a.join(" ,"),et),c=o.map(({paramsType:m,dirPath:h})=>({paramsType:`export type Params = ${m}${";"}`,dirPath:h}));return{pathStructure:`${p}${`
|
|
7
7
|
`}${i}${`
|
|
8
8
|
`}${`
|
|
9
|
-
`}${
|
|
9
|
+
`}${n}`,paramsTypes:c}};var Ut=()=>!!process.stdout?.isTTY;var H=t=>e=>Ut()?`\x1B[${t}m${e}\x1B[0m`:e,ht=H(36),gt=H(32),k=H(31);var X=(t,e,r="\u2192",s=24)=>`${t.padEnd(s)} ${r} ${e}`,G=(t=0)=>" ".repeat(t),dt=()=>({info:(t,e={})=>{let{indentLevel:r=0,event:s}=e,o=s?`${ht(`[${s}]`)} `:"";console.log(`${G(r)}${o}${t}`)},success:(t,e={})=>{let{indentLevel:r=0}=e;console.log(`${G(r)}${gt("\u2713")} ${t}`)},error:(t,e={})=>{let{indentLevel:r=0}=e;console.error(`${G(r)}${k("\u2717")} ${k(t)}`)}});var St=({baseDir:t,outputPath:e,paramsFileName:r,logger:s})=>{s.info("Generating types...",{event:"generate"});let{pathStructure:o,paramsTypes:n}=ft(e,t);xt.writeFileSync(e,o),s.success(X("Path structure type",_(e),"\u2192",20),{indentLevel:1}),r&&(n.forEach(({paramsType:i,dirPath:a})=>{let p=Ht.join(a,r);xt.writeFileSync(p,i)}),s.success(X("Params types",r,"\u2192",20),{indentLevel:1}))};import kt from"chokidar";var Pt=(t,e)=>{let r=null,s=!1,o=null,n=async(...i)=>{s=!0;try{await t(...i)}finally{if(s=!1,o){let a=o;o=null,n(...a)}}};return(...i)=>{r&&clearTimeout(r),r=setTimeout(()=>{if(s){o=i;return}n(...i)},e)}};var $t=(t,e,r)=>{r.info(`${_(t)}`,{event:"watch"});let s=p=>A.some(c=>p.endsWith(c)),o=new Set,n=Pt(async()=>{let p=Array.from(o);o.clear();for(let c of p)ot(c),st(c);await e()},300),i=kt.watch(t,{ignoreInitial:!0,ignored:(p,c)=>!!c?.isFile()&&!s(p)});i.on("ready",()=>{n(),i.on("all",(p,c)=>{if(s(c)){let m=_(c);r.info(m,{event:p}),o.add(c),n()}})}),i.on("error",p=>{p instanceof Error?r.error(`Watcher error: ${p.message}`):r.error(`Unknown watcher error: ${String(p)}`)});let a=()=>{i.close().then(()=>{r.info("Watcher closed.",{event:"watch"})}).catch(p=>{r.error(`Failed to close watcher: ${p.message}`)})};process.on("SIGINT",a),process.on("SIGTERM",a)};var Ct=(t,e,r,s)=>{try{return St({baseDir:t,outputPath:e,paramsFileName:r,logger:s}),0}catch(o){return o instanceof Error?s.error(`Failed to generate: ${o.message}`):s.error(`Unknown error occurred during generate: ${String(o)}`),1}},Nt=(t,e,r,s)=>{let o=P(_t.resolve(t)),n=P(_t.resolve(e)),i=typeof r.paramsFile=="string"?r.paramsFile:null;return r.paramsFile!==void 0&&!i?(s.error("Error: --params-file requires a filename."),1):r.watch?($t(o,()=>{Ct(o,n,i,s)},s),0):Ct(o,n,i,s)};function Kt(t){if(t.length===0)return[];if(t[0].startsWith("-"))return t;let e=Gt.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 K(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 Qt(t,e){let r=[],
|
|
23
|
+
`.trim();t.info(e)}function Qt(t,e){let r=[],s;for(let o=0;o<t.length;o++){let n=t[o],i=!1;for(let a of e)if(n.startsWith(`${a}=`)){s=n.slice(`${a}=`.length)||!0,i=!0;break}if(!i){if(e.includes(n)){let a=t[o+1];typeof a=="string"&&!a.startsWith("-")?(s=a,o++):s=!0;continue}r.push(n)}}return{args:r,value:s}}var It=(t,e=dt())=>{try{let r=Kt(t),{args:s,value:o}=Qt(r,["-p","--params-file"]),{values:n,positionals:i}=Xt({args:s,options:{watch:{type:"boolean",short:"w"},help:{type:"boolean",short:"h"}},allowPositionals:!0,strict:!0});n.help&&(K(e),process.exit(0));let a=i[0],p=i[1];(!a||!p)&&(e.error("Missing required arguments: <baseDir> <outputPath>"),K(e),process.exit(1));let c={watch:!!n.watch,...o!==void 0?{paramsFile:o}:{}};(async()=>{try{let m=await Nt(a,p,c,e);c.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)}`),K(e),process.exit(1)}};It(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.1",
|
|
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": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"chokidar": "^5.0.0",
|
|
33
|
-
"rpc4next-shared": "^0.
|
|
33
|
+
"rpc4next-shared": "^0.3.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"next": "^14.0.0 || ^15.0.0"
|