Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,56 @@
"types": "./dist/express.d.cts",
"default": "./dist/express.cjs"
}
},
"./elysia": {
"import": {
"types": "./dist/elysia.d.ts",
"default": "./dist/elysia.js"
},
"require": {
"types": "./dist/elysia.d.cts",
"default": "./dist/elysia.cjs"
}
},
"./fastify": {
"import": {
"types": "./dist/fastify.d.ts",
"default": "./dist/fastify.js"
},
"require": {
"types": "./dist/fastify.d.cts",
"default": "./dist/fastify.cjs"
}
},
"./next": {
"import": {
"types": "./dist/next.d.ts",
"default": "./dist/next.js"
},
"require": {
"types": "./dist/next.d.cts",
"default": "./dist/next.cjs"
}
},
"./hono": {
"import": {
"types": "./dist/hono.d.ts",
"default": "./dist/hono.js"
},
"require": {
"types": "./dist/hono.d.cts",
"default": "./dist/hono.cjs"
}
},
"./nuxt": {
"import": {
"types": "./dist/nuxt.d.ts",
"default": "./dist/nuxt.js"
},
"require": {
"types": "./dist/nuxt.d.cts",
"default": "./dist/nuxt.cjs"
}
}
},
"dependencies": {
Expand All @@ -69,22 +119,25 @@
"@zenstackhq/typescript-config": "workspace:*",
"@zenstackhq/vitest-config": "workspace:*",
"body-parser": "^2.2.0",
"elysia": "^1.3.1",
"express": "^5.0.0",
"next": "^15.0.0",
"fastify": "^5.6.1",
"fastify-plugin": "^5.1.0",
"elysia": "^1.3.1",
"h3": "^1.15.4",
"hono": "^4.6.3",
"next": "^15.0.0",
"nuxt": "^4.2.0",
"supertest": "^7.1.4",
"zod": "~3.25.0"
},
"peerDependencies": {
"elysia": "^1.3.0",
"express": "^5.0.0",
"next": "^15.0.0",
"fastify": "^5.0.0",
"fastify-plugin": "^5.0.0",
"elysia": "^1.3.0",
"hono": "^4.6.0",
"next": "^15.0.0",
"nuxt": "^4.0.0",
"zod": "catalog:"
},
"peerDependenciesMeta": {
Expand All @@ -105,6 +158,9 @@
},
"hono": {
"optional": true
},
"nuxt": {
"optional": true
}
}
}
3 changes: 1 addition & 2 deletions packages/server/src/adapter/hono/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ClientContract } from '@zenstackhq/orm';
import type { SchemaDef } from '@zenstackhq/orm/schema';
import type { Context, MiddlewareHandler } from 'hono';
import { routePath } from 'hono/route';
import type { ContentfulStatusCode } from 'hono/utils/http-status';
import { logInternalError, type CommonAdapterOptions } from '../common';

Expand All @@ -25,7 +24,7 @@ export function createHonoHandler<Schema extends SchemaDef>(options: HonoOptions
const url = new URL(ctx.req.url);
const query = Object.fromEntries(url.searchParams);

const path = ctx.req.path.substring(routePath(ctx).length - 1);
const path = ctx.req.path.substring(ctx.req.routePath.length - 1);
if (!path) {
return ctx.json({ message: 'missing path parameter' }, 400);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/adapter/next/app-route-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SchemaDef } from '@zenstackhq/orm/schema';
import { NextRequest, NextResponse } from 'next/server';
import type { AppRouteRequestHandlerOptions } from '.';
import { log } from '../../api/utils';
import { logInternalError } from '../common';

type Context = { params: Promise<{ path: string[] }> };

Expand Down Expand Up @@ -59,7 +59,7 @@ export default function factory<Schema extends SchemaDef>(
});
return NextResponse.json(r.body, { status: r.status });
} catch (err) {
log(options.apiHandler.log, 'error', `An unhandled error occurred while processing the request: ${err}${err instanceof Error ? '\n' + err.stack : ''}`);
logInternalError(options.apiHandler.log, err);
return NextResponse.json({ message: 'An internal server error occurred' }, { status: 500 });
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/adapter/next/pages-route-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SchemaDef } from '@zenstackhq/orm/schema';
import type { NextApiRequest, NextApiResponse } from 'next';
import type { PageRouteRequestHandlerOptions } from '.';
import { log } from '../../api/utils';
import { logInternalError } from '../common';

/**
* Creates a Next.js API endpoint "pages" router request handler that handles ZenStack CRUD requests.
Expand Down Expand Up @@ -35,7 +35,7 @@ export default function factory<Schema extends SchemaDef>(
});
res.status(r.status).send(r.body);
} catch (err) {
log(options.apiHandler.log, 'error', `An unhandled error occurred while processing the request: ${err}${err instanceof Error ? '\n' + err.stack : ''}`);
logInternalError(options.apiHandler.log, err);
res.status(500).send({ message: 'An internal server error occurred' });
}
};
Expand Down
57 changes: 57 additions & 0 deletions packages/server/src/adapter/nuxt/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { ClientContract } from '@zenstackhq/orm';
import type { SchemaDef } from '@zenstackhq/orm/schema';
import {
H3Event,
defineEventHandler,
getQuery,
getRouterParams,
readBody,
type EventHandlerRequest
} from 'h3';
import { setResponseStatus } from 'nuxt/app';
import { logInternalError, type CommonAdapterOptions } from '../common';

/**
* Nuxt request handler options
*/
export interface HandlerOptions<Schema extends SchemaDef> extends CommonAdapterOptions<Schema> {
/**
* Callback for getting a ZenStackClient for the given request
*/
getClient: (event: H3Event<EventHandlerRequest>) => ClientContract<Schema> | Promise<ClientContract<Schema>>;
}

export function createEventHandler<Schema extends SchemaDef>(options: HandlerOptions<Schema>) {
return defineEventHandler(async (event) => {
const client = await options.getClient(event);
if (!client) {
setResponseStatus(event, 500);
return { message: 'unable to get ZenStackClient from request context' };
}

const routerParam = getRouterParams(event);
const query = await getQuery(event);

let reqBody: unknown;
if (event.method === 'POST' || event.method === 'PUT' || event.method === 'PATCH') {
reqBody = await readBody(event);
}

try {
const { status, body } = await options.apiHandler.handleRequest({
method: event.method,
path: routerParam['_']!,
query: query as Record<string, string | string[]>,
requestBody: reqBody,
client,
});

setResponseStatus(event, status);
return body;
} catch (err) {
setResponseStatus(event, 500);
logInternalError(options.apiHandler.log, err);
return { message: 'An internal server error occurred' };
}
});
}
1 change: 1 addition & 0 deletions packages/server/src/adapter/nuxt/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './handler';
4 changes: 4 additions & 0 deletions packages/server/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export default defineConfig({
api: 'src/api/index.ts',
express: 'src/adapter/express/index.ts',
next: 'src/adapter/next/index.ts',
fastify: 'src/adapter/fastify/index.ts',
elysia: 'src/adapter/elysia/index.ts',
nuxt: 'src/adapter/nuxt/index.ts',
hono: 'src/adapter/hono/index.ts',
},
outDir: 'dist',
splitting: false,
Expand Down
Loading