@truxie/nitro 0.1.0-test.6

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.
@@ -0,0 +1,52 @@
1
+ import { H3Event, EventHandler } from 'h3';
2
+ import { BaseExecutionContext, Container, PipelineOptions, TruxieApplication, ExecutionContext } from 'truxie';
3
+
4
+ declare class NitroExecutionContext extends BaseExecutionContext<H3Event> {
5
+ constructor(event: H3Event, handler: Function, container: Container);
6
+ }
7
+
8
+ interface NitroHandlerOptions extends PipelineOptions {
9
+ parseBody?: boolean;
10
+ }
11
+ type NitroHandlerFn = (ctx: ExecutionContext<H3Event>) => unknown | Promise<unknown>;
12
+ declare function nitroHandler(app: TruxieApplication, options: NitroHandlerOptions, handler: NitroHandlerFn): EventHandler;
13
+
14
+ /**
15
+ * Create a Nitro EventHandler backed by the truk pipeline.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { app } from '~/server/backend/app'
20
+ *
21
+ * export default defineHandler(app, async (ctx) => {
22
+ * const body = ctx.getBody()
23
+ * return sendSuccess(result)
24
+ * })
25
+ *
26
+ * // With options:
27
+ * export default defineHandler(app, { guards: [AuthGuard] }, async (ctx) => { ... })
28
+ * ```
29
+ */
30
+ declare function defineHandler(app: TruxieApplication, optionsOrHandler: NitroHandlerOptions | NitroHandlerFn, maybeHandler?: NitroHandlerFn): EventHandler;
31
+
32
+ interface ControllerDispatcherOptions {
33
+ /** The initialized TruxieApplication instance */
34
+ app: TruxieApplication;
35
+ }
36
+ /**
37
+ * Create a Nitro catch-all EventHandler that dispatches HTTP requests
38
+ * to @Controller methods AND WebSocket upgrades to @WebSocketGateway classes.
39
+ *
40
+ * Both are auto-discovered from module providers.
41
+ * All dispatch logic lives in TruxieApplication — this adapter only
42
+ * handles h3-specific body parsing and crossws type conversion.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * // src/server/api/[...].ts
47
+ * export default createControllerDispatcher({ app })
48
+ * ```
49
+ */
50
+ declare function createControllerDispatcher(options: ControllerDispatcherOptions): EventHandler;
51
+
52
+ export { type ControllerDispatcherOptions, NitroExecutionContext, type NitroHandlerOptions, createControllerDispatcher, defineHandler, nitroHandler as nitro, nitroHandler };
package/dist/index.js ADDED
@@ -0,0 +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};
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@truxie/nitro",
3
+ "version": "0.1.0-test.6",
4
+ "description": "Nitro adapter for truxie — controller dispatcher and handler utilities",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup",
22
+ "dev": "tsup --watch",
23
+ "typecheck": "tsc --noEmit",
24
+ "clean": "rm -rf dist"
25
+ },
26
+ "peerDependencies": {
27
+ "truxie": ">=0.1.0",
28
+ "h3": ">=1.0.0"
29
+ },
30
+ "devDependencies": {
31
+ "@swc/core": "^1.15.18",
32
+ "@types/node": "^22.10.5",
33
+ "h3": "^1.13.0",
34
+ "tsup": "^8.0.0",
35
+ "typescript": "^5.0.0"
36
+ },
37
+ "license": "MIT",
38
+ "engines": {
39
+ "node": ">=18.0.0"
40
+ }
41
+ }