@truxie/nitro 0.1.0-test.6 → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 vannguyen799
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @truxie/nitro
2
+
3
+ Mount a truxie application inside Nitro / h3 — Nuxt server routes, or Nitro on its own.
4
+
5
+ ```
6
+ npm install @truxie/nitro
7
+ ```
8
+
9
+ ```ts
10
+ // server/api/[...].ts
11
+ import {createControllerDispatcher} from '@truxie/nitro'
12
+ import {app} from '~/server/app'
13
+
14
+ export default createControllerDispatcher({app})
15
+ ```
16
+
17
+ One catch-all handler serves every `@Controller` route, and every `@WebSocketGateway` upgrade on the same path — crossws peers are adapted to truxie's `WsPeer`, so a gateway is portable between adapters.
18
+
19
+ ## What it does
20
+
21
+ `matchRoute`, then the application's pipeline: middlewares → guards → pipes → interceptors → handler → filters. Body parsing goes through h3 (so urlencoded bodies decode), with `@RawBody()` and `@FormData()` honoured when the handler asks for them. A malformed JSON body is reported as a 400 rather than arriving as an empty object.
22
+
23
+ A returned `Response` is passed through, a `redirect()` becomes `sendRedirect`, and a status recorded by a filter is applied with `setResponseStatus`.
24
+
25
+ ## One handler, without a controller
26
+
27
+ For a single endpoint that does not want a controller class:
28
+
29
+ ```ts
30
+ export default defineHandler(app, {guards: [AuthGuard]}, async ctx => {
31
+ return {ok: true}
32
+ })
33
+ ```
34
+
35
+ It runs the same pipeline, including the application's global guards and filters.
36
+
37
+ MIT.
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import {BaseExecutionContext,createPipeline,getRouteParams}from'truxie';import {eventHandler}from'h3';var h=Object.defineProperty;var s=(e,t)=>h(e,"name",{value:t,configurable:true});var l=class extends BaseExecutionContext{static{s(this,"NitroExecutionContext");}constructor(t,o,r){let a=g(t),u=t.context.params??{},i=x(t);super(t,o,r,a,u,i),t.context.auth&&this.setAuth(t.context.auth);}};function x(e){let t=new AbortController,o=e.node?.req,r=e.node?.res;if(o&&r){let a=false;r.on("finish",()=>{a=true;}),o.on("close",()=>{a||t.abort("Client disconnected");});}return t}s(x,"createRequestAbortController");function g(e){try{let t=e.node?.req?.url??"",o=t.indexOf("?");if(o===-1)return {};let r=new URLSearchParams(t.slice(o)),a={};return r.forEach((u,i)=>{a[i]=u;}),a}catch{return {}}}s(g,"getQuerySafe");function y(e,t,o){let r=e.getContainer(),u=createPipeline(r,{guards:[],interceptors:[],pipes:[],filters:[]},{guards:t.guards,interceptors:t.interceptors,pipes:t.pipes,filters:t.filters,handler:o});return async i=>{let f=new l(i,o,r);if(t.parseBody!==false)try{let{readBody:d}=await import('h3'),n=await d(i);n!=null&&f.setBody(n);}catch{}return u(f)}}s(y,"nitroHandler");function C(e,t,o){return y(e,typeof t=="function"?{}:t,typeof t=="function"?t:o)}s(C,"defineHandler");function R(e){let{app:t}=e,o=eventHandler(async r=>{let a=r.method.toUpperCase(),i=(r.path??r.node?.req?.url??"").split("?")[0]??"",f=t.matchRoute(a,i);if(!f){let{createError:m}=await import('h3');throw m({statusCode:404,statusMessage:"Not Found"})}let{route:d,params:n}=f;r.context.params={...r.context.params,...n};let c=new l(r,()=>{},t.getContainer());await q(r,c,d.controllerClass,d.methodName);let p=await t.handleRequest(d,c);if(p.isRedirect){let{sendRedirect:m}=await import('h3');return m(r,p.redirectUrl,p.redirectStatus)}return p.value});return t.hasGateways()&&t.createWebSocketHooks({adaptPeer:D,adaptMessage:N,getUrl:s(r=>r.request?.url??r.context._url??"/","getUrl"),getPeers:s(r=>r.peers,"getPeers")}).then(r=>{o.__websocket__=r;}),o}s(R,"createControllerDispatcher");async function q(e,t,o,r){if(e.method.toUpperCase()==="GET")return;let a=getRouteParams(o,r),u=e.node?.req?.headers?.["content-type"]??"",i=a?.some(n=>n.type==="rawBody")??false,f=a?.some(n=>n.type==="formData")??false,d=a?.some(n=>n.type==="body")??false;try{if(f&&u.includes("multipart/")){let{readMultipartFormData:n}=await import('h3'),c=await n(e);c&&t.setFormData(c.map(p=>({name:p.name??"",filename:p.filename,type:p.type,data:Buffer.from(p.data)})));}else if(i){let{readRawBody:n}=await import('h3'),c=await n(e);if(c!=null&&(t.setRawBody(c),d&&typeof c=="string"))try{t.setBody(JSON.parse(c));}catch{}}else if(!u.includes("multipart/")){let{readBody:n}=await import('h3'),c=await n(e);c!=null&&t.setBody(c);}}catch{}}s(q,"parseBody");function D(e){return {get id(){return e.id},get remoteAddress(){return e.remoteAddress},get context(){return e.context},send(t,o){e.send(typeof t=="string"?t:JSON.stringify(t),o);},publish(t,o,r){e.publish(t,typeof o=="string"?o:JSON.stringify(o),r);},subscribe:s(t=>e.subscribe(t),"subscribe"),unsubscribe:s(t=>e.unsubscribe(t),"unsubscribe"),close:s((t,o)=>e.close(t,o),"close"),terminate:s(()=>e.terminate?.()??e.close(),"terminate"),get peers(){return e.peers??new Set},get topics(){return e.topics??new Set}}}s(D,"adaptCrosswsPeer");function N(e){return {get rawData(){return e.rawData??e},text(){return typeof e.text=="function"?e.text():typeof e=="string"?e:String(e)},json(){if(typeof e.json=="function")return e.json();let t=typeof e.text=="function"?e.text():String(e);return JSON.parse(t)}}}s(N,"adaptCrosswsMessage");export{l as NitroExecutionContext,R as createControllerDispatcher,C as defineHandler,y as nitro,y as nitroHandler};
1
+ import {BaseExecutionContext,getRouteParams,applyJsonBody}from'truxie';import {eventHandler}from'h3';var w=Object.defineProperty;var a=(e,t)=>w(e,"name",{value:t,configurable:true});var l=class extends BaseExecutionContext{static{a(this,"NitroExecutionContext");}constructor(t,o,r){let n=b(t),i=t.context.params??{},d=g(t);super(t,o,r,n,i,d),t.context.auth&&this.setAuth(t.context.auth);}};function g(e){let t=new AbortController,o=e.node?.req,r=e.node?.res;if(o&&r){let n=false;r.on("finish",()=>{n=true;}),o.on("close",()=>{n||t.abort("Client disconnected");});}return t}a(g,"createRequestAbortController");function b(e){try{let t=e.node?.req?.url??"",o=t.indexOf("?");if(o===-1)return {};let r=new URLSearchParams(t.slice(o)),n={};return r.forEach((i,d)=>{n[d]=i;}),n}catch{return {}}}a(b,"getQuerySafe");function y(e,t,o){let r=e.getContainer(),n;return async i=>{n||(n=e.createHandler({middlewares:t.middlewares,guards:t.guards,interceptors:t.interceptors,pipes:t.pipes,filters:t.filters,handler:o}));let d=new l(i,o,r);if(t.parseBody!==false)try{let{readBody:p}=await import('h3'),f=await p(i);f!=null&&d.setBody(f);}catch{}return n(d)}}a(y,"nitroHandler");function C(e,t,o){return y(e,typeof t=="function"?{}:t,typeof t=="function"?t:o)}a(C,"defineHandler");function q(e){let{app:t}=e,o=eventHandler(async r=>{let n=r.method.toUpperCase(),d=(r.path??r.node?.req?.url??"").split("?")[0]??"",p=t.matchRoute(n,d);if(!p){let{createError:m}=await import('h3');throw m({statusCode:404,statusMessage:"Not Found"})}let{route:f,params:c}=p;r.context.params={...r.context.params,...c};let s=new l(r,()=>{},t.getContainer());await D(r,s,f.controllerClass,f.methodName);let u=await t.handleRequest(f,s);if(u.isRedirect){let{sendRedirect:m}=await import('h3');return m(r,u.redirectUrl,u.redirectStatus)}if(u.value instanceof Response)return u.value;let h=s.getData("__statusCode");if(h!==void 0){let{setResponseStatus:m}=await import('h3');m(r,h);}return u.value});return t.hasGateways()&&t.createWebSocketHooks({adaptPeer:E,adaptMessage:N,getUrl:a(r=>r.request?.url??r.context._url??"/","getUrl"),getPeers:a(r=>r.peers,"getPeers")}).then(r=>{o.__websocket__=r;}),o}a(q,"createControllerDispatcher");async function D(e,t,o,r){if(e.method.toUpperCase()==="GET")return;let n=getRouteParams(o,r),i=e.node?.req?.headers?.["content-type"]??"",d=n?.some(c=>c.type==="rawBody")??false,p=n?.some(c=>c.type==="formData")??false,f=n?.some(c=>c.type==="body")??false;try{if(p&&i.includes("multipart/")){let{readMultipartFormData:c}=await import('h3'),s=await c(e);s&&t.setFormData(s.map(u=>({name:u.name??"",filename:u.filename,type:u.type,data:Buffer.from(u.data)})));}else if(d){let{readRawBody:c}=await import('h3'),s=await c(e);s!=null&&(t.setRawBody(s),f&&typeof s=="string"&&applyJsonBody(t,s,i));}else if(!i.includes("multipart/")){let{readBody:c}=await import('h3');try{let s=await c(e);s!=null&&t.setBody(s);}catch(s){t.setBodyParseError(s instanceof Error?s:new Error(String(s)));}}}catch{}}a(D,"parseBody");function E(e){return {get id(){return e.id},get remoteAddress(){return e.remoteAddress},get context(){return e.context},send(t,o){e.send(typeof t=="string"?t:JSON.stringify(t),o);},publish(t,o,r){e.publish(t,typeof o=="string"?o:JSON.stringify(o),r);},subscribe:a(t=>e.subscribe(t),"subscribe"),unsubscribe:a(t=>e.unsubscribe(t),"unsubscribe"),close:a((t,o)=>e.close(t,o),"close"),terminate:a(()=>e.terminate?.()??e.close(),"terminate"),get peers(){return e.peers??new Set},get topics(){return e.topics??new Set}}}a(E,"adaptCrosswsPeer");function N(e){return {get rawData(){return e.rawData??e},text(){return typeof e.text=="function"?e.text():typeof e=="string"?e:String(e)},json(){if(typeof e.json=="function")return e.json();let t=typeof e.text=="function"?e.text():String(e);return JSON.parse(t)}}}a(N,"adaptCrosswsMessage");export{l as NitroExecutionContext,q as createControllerDispatcher,C as defineHandler,y as nitro,y as nitroHandler};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@truxie/nitro",
3
- "version": "0.1.0-test.6",
4
- "description": "Nitro adapter for truxie — controller dispatcher and handler utilities",
3
+ "version": "0.1.1",
4
+ "description": "Nitro adapter for truxie \u2014 controller dispatcher and handler utilities",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",
@@ -15,16 +15,18 @@
15
15
  },
16
16
  "files": [
17
17
  "dist",
18
- "README.md"
18
+ "README.md",
19
+ "LICENSE"
19
20
  ],
20
21
  "scripts": {
21
22
  "build": "tsup",
22
23
  "dev": "tsup --watch",
23
24
  "typecheck": "tsc --noEmit",
24
- "clean": "rm -rf dist"
25
+ "clean": "rm -rf dist",
26
+ "prepublishOnly": "npm run build"
25
27
  },
26
28
  "peerDependencies": {
27
- "truxie": ">=0.1.0",
29
+ "truxie": "^0.1.1",
28
30
  "h3": ">=1.0.0"
29
31
  },
30
32
  "devDependencies": {
@@ -36,6 +38,26 @@
36
38
  },
37
39
  "license": "MIT",
38
40
  "engines": {
39
- "node": ">=18.0.0"
40
- }
41
+ "node": ">=20.0.0"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "git+https://github.com/vannguyen799/truxie.git",
49
+ "directory": "packages/nitro"
50
+ },
51
+ "homepage": "https://github.com/vannguyen799/truxie/tree/main/packages/nitro#readme",
52
+ "bugs": {
53
+ "url": "https://github.com/vannguyen799/truxie/issues"
54
+ },
55
+ "keywords": [
56
+ "truxie",
57
+ "nitro",
58
+ "h3",
59
+ "nuxt",
60
+ "adapter",
61
+ "backend"
62
+ ]
41
63
  }