From ee3c7168d679f0b40257b7bb25ba1bc374774585 Mon Sep 17 00:00:00 2001 From: Yiming Date: Sat, 30 Mar 2024 17:20:47 -0700 Subject: [PATCH] chore(server): change default exports to named ones for compatibility (#1194) --- packages/server/package.json | 1 + packages/server/src/api/index.ts | 2 ++ packages/server/src/api/rest/index.ts | 2 ++ packages/server/src/api/rpc/index.ts | 2 ++ packages/server/src/express/index.ts | 2 +- packages/server/src/express/middleware.ts | 6 ++++-- packages/server/src/fastify/index.ts | 2 +- packages/server/src/fastify/plugin.ts | 6 +++++- packages/server/src/next/app-route-handler.ts | 4 ++-- packages/server/src/next/pages-route-handler.ts | 4 ++-- packages/server/src/sveltekit/handler.ts | 2 ++ packages/server/src/sveltekit/index.ts | 2 +- packages/server/tests/api/rpc.test.ts | 4 ++-- 13 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 packages/server/src/api/index.ts diff --git a/packages/server/package.json b/packages/server/package.json index 53944cfe8..77a24b6b9 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -59,6 +59,7 @@ }, "exports": { "./package.json": "./package.json", + "./api": "./api/index.js", "./api/rest": "./api/rest/index.js", "./api/rpc": "./api/rpc/index.js", "./express": "./express/index.js", diff --git a/packages/server/src/api/index.ts b/packages/server/src/api/index.ts new file mode 100644 index 000000000..88e96c11e --- /dev/null +++ b/packages/server/src/api/index.ts @@ -0,0 +1,2 @@ +export { RPCApiHandler } from './rpc'; +export { RestApiHandler } from './rest'; diff --git a/packages/server/src/api/rest/index.ts b/packages/server/src/api/rest/index.ts index 5fbf05ee8..c65fd7da1 100644 --- a/packages/server/src/api/rest/index.ts +++ b/packages/server/src/api/rest/index.ts @@ -1664,3 +1664,5 @@ export default function makeHandler(options: Options) { const handler = new RequestHandler(options); return handler.handleRequest.bind(handler); } + +export { makeHandler as RestApiHandler }; diff --git a/packages/server/src/api/rpc/index.ts b/packages/server/src/api/rpc/index.ts index 79e26bd79..a7fb44d72 100644 --- a/packages/server/src/api/rpc/index.ts +++ b/packages/server/src/api/rpc/index.ts @@ -290,3 +290,5 @@ export default function makeHandler() { const handler = new RequestHandler(); return handler.handleRequest.bind(handler); } + +export { makeHandler as RPCApiHandler }; diff --git a/packages/server/src/express/index.ts b/packages/server/src/express/index.ts index 7cbc0a0c4..1def0479b 100644 --- a/packages/server/src/express/index.ts +++ b/packages/server/src/express/index.ts @@ -1,2 +1,2 @@ -export { default as ZenStackMiddleware } from './middleware'; +export { ZenStackMiddleware } from './middleware'; export * from './middleware'; diff --git a/packages/server/src/express/middleware.ts b/packages/server/src/express/middleware.ts index 200c57bd6..67a185704 100644 --- a/packages/server/src/express/middleware.ts +++ b/packages/server/src/express/middleware.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { DbClientContract } from '@zenstackhq/runtime'; import type { Handler, Request, Response } from 'express'; -import RPCAPIHandler from '../api/rpc'; +import { RPCApiHandler } from '../api/rpc'; import { loadAssets } from '../shared'; import { AdapterBaseOptions } from '../types'; @@ -32,7 +32,7 @@ export interface MiddlewareOptions extends AdapterBaseOptions { const factory = (options: MiddlewareOptions): Handler => { const { modelMeta, zodSchemas } = loadAssets(options); - const requestHandler = options.handler || RPCAPIHandler(); + const requestHandler = options.handler || RPCApiHandler(); return async (request, response, next) => { const prisma = (await options.getPrisma(request, response)) as DbClientContract; @@ -83,3 +83,5 @@ const factory = (options: MiddlewareOptions): Handler => { }; export default factory; + +export { factory as ZenStackMiddleware }; diff --git a/packages/server/src/fastify/index.ts b/packages/server/src/fastify/index.ts index 29eb98aed..486b76c64 100644 --- a/packages/server/src/fastify/index.ts +++ b/packages/server/src/fastify/index.ts @@ -1,2 +1,2 @@ -export { default as ZenStackFastifyPlugin } from './plugin'; +export { ZenStackFastifyPlugin } from './plugin'; export * from './plugin'; diff --git a/packages/server/src/fastify/plugin.ts b/packages/server/src/fastify/plugin.ts index 8651e0bb0..a69d9ac98 100644 --- a/packages/server/src/fastify/plugin.ts +++ b/packages/server/src/fastify/plugin.ts @@ -62,4 +62,8 @@ const pluginHandler: FastifyPluginCallback = (fastify, options, d done(); }; -export default fp(pluginHandler); +const plugin = fp(pluginHandler); + +export default plugin; + +export { plugin as ZenStackFastifyPlugin }; diff --git a/packages/server/src/next/app-route-handler.ts b/packages/server/src/next/app-route-handler.ts index 71121d151..5c8cbe0e5 100644 --- a/packages/server/src/next/app-route-handler.ts +++ b/packages/server/src/next/app-route-handler.ts @@ -3,7 +3,7 @@ import { DbClientContract } from '@zenstackhq/runtime'; import { NextRequest, NextResponse } from 'next/server'; import { AppRouteRequestHandlerOptions } from '.'; -import RPCAPIHandler from '../api/rpc'; +import { RPCApiHandler } from '../api'; import { loadAssets } from '../shared'; type Context = { params: { path: string[] } }; @@ -19,7 +19,7 @@ export default function factory( ): (req: NextRequest, context: Context) => Promise { const { modelMeta, zodSchemas } = loadAssets(options); - const requestHandler = options.handler || RPCAPIHandler(); + const requestHandler = options.handler || RPCApiHandler(); return async (req: NextRequest, context: Context) => { const prisma = (await options.getPrisma(req)) as DbClientContract; diff --git a/packages/server/src/next/pages-route-handler.ts b/packages/server/src/next/pages-route-handler.ts index 0752775da..dd25b0c6c 100644 --- a/packages/server/src/next/pages-route-handler.ts +++ b/packages/server/src/next/pages-route-handler.ts @@ -3,7 +3,7 @@ import { DbClientContract } from '@zenstackhq/runtime'; import { NextApiRequest, NextApiResponse } from 'next'; import { PagesRouteRequestHandlerOptions } from '.'; -import RPCAPIHandler from '../api/rpc'; +import { RPCApiHandler } from '../api'; import { loadAssets } from '../shared'; /** @@ -17,7 +17,7 @@ export default function factory( ): (req: NextApiRequest, res: NextApiResponse) => Promise { const { modelMeta, zodSchemas } = loadAssets(options); - const requestHandler = options.handler || RPCAPIHandler(); + const requestHandler = options.handler || RPCApiHandler(); return async (req: NextApiRequest, res: NextApiResponse) => { const prisma = (await options.getPrisma(req, res)) as DbClientContract; diff --git a/packages/server/src/sveltekit/handler.ts b/packages/server/src/sveltekit/handler.ts index 3d346f762..f5d1b7995 100644 --- a/packages/server/src/sveltekit/handler.ts +++ b/packages/server/src/sveltekit/handler.ts @@ -83,3 +83,5 @@ export default function createHandler(options: HandlerOptions): Handle { return resolve(event); }; } + +export { createHandler as SvelteKitHandler }; diff --git a/packages/server/src/sveltekit/index.ts b/packages/server/src/sveltekit/index.ts index 83f2980bb..7f040d76f 100644 --- a/packages/server/src/sveltekit/index.ts +++ b/packages/server/src/sveltekit/index.ts @@ -1,2 +1,2 @@ -export { default as SvelteKitHandler } from './handler'; +export { SvelteKitHandler } from './handler'; export * from './handler'; diff --git a/packages/server/tests/api/rpc.test.ts b/packages/server/tests/api/rpc.test.ts index f3326f570..432abec2c 100644 --- a/packages/server/tests/api/rpc.test.ts +++ b/packages/server/tests/api/rpc.test.ts @@ -5,7 +5,7 @@ import { CrudFailureReason, type ZodSchemas } from '@zenstackhq/runtime'; import { loadSchema } from '@zenstackhq/testtools'; import { Decimal } from 'decimal.js'; import SuperJSON from 'superjson'; -import RPCAPIHandler from '../../src/api/rpc'; +import { RPCApiHandler } from '../../src/api'; import { schema } from '../utils'; describe('RPC API Handler Tests', () => { @@ -408,7 +408,7 @@ describe('RPC API Handler Tests', () => { }); function makeHandler(zodSchemas?: ZodSchemas) { - const _handler = RPCAPIHandler(); + const _handler = RPCApiHandler(); return async (args: any) => { const r = await _handler({ ...args, url: new URL(`http://localhost/${args.path}`), modelMeta, zodSchemas }); return {