Skip to content

Commit

Permalink
Several fixes for built-in cruds
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Dec 5, 2024
1 parent a3b11cd commit a4f0214
Show file tree
Hide file tree
Showing 37 changed files with 2,717 additions and 2,994 deletions.
6 changes: 4 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"ignore": ["dist", "docs", "example"]
},
"formatter": {
"enabled": true,
"indentStyle": "tab"
"enabled": true,
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 120
},
"organizeImports": {
"enabled": true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"README.md"
],
"scripts": {
"prepare": "husky install",
"prepare": "husky",
"build": "rm -rf dist/ && tsup src/index.ts --format cjs,esm --dts --config tsconfig.json",
"lint": "npx @biomejs/biome check src/ tests/ || (npx @biomejs/biome check --write src/ tests/; exit 1)",
"test": "vitest run --root tests",
Expand Down
71 changes: 34 additions & 37 deletions src/adapters/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,44 @@ import { OpenAPIHandler, type OpenAPIRouterType } from "../openapi";
import type { RouterOptions } from "../types";

export class HonoOpenAPIHandler extends OpenAPIHandler {
getRequest(args: any[]) {
return args[0].req.raw;
}
getRequest(args: any[]) {
return args[0].req.raw;
}

getUrlParams(args: any[]): Record<string, any> {
return args[0].req.param();
}
getUrlParams(args: any[]): Record<string, any> {
return args[0].req.param();
}
}

export function fromHono<M>(
router: M,
options?: RouterOptions,
): M & OpenAPIRouterType<M> {
const openapiRouter = new HonoOpenAPIHandler(router, options);
export function fromHono<M>(router: M, options?: RouterOptions): M & OpenAPIRouterType<M> {
const openapiRouter = new HonoOpenAPIHandler(router, options);

return new Proxy(router, {
get: (target: any, prop: string, ...args: any[]) => {
const _result = openapiRouter.handleCommonProxy(target, prop, ...args);
if (_result !== undefined) {
return _result;
}
return new Proxy(router, {
get: (target: any, prop: string, ...args: any[]) => {
const _result = openapiRouter.handleCommonProxy(target, prop, ...args);
if (_result !== undefined) {
return _result;
}

return (route: string, ...handlers: any[]) => {
if (prop !== "fetch") {
if (handlers.length === 1 && handlers[0].isChanfana === true) {
handlers = openapiRouter.registerNestedRouter({
method: prop,
path: route,
nestedRouter: handlers[0],
});
} else if (openapiRouter.allowedMethods.includes(prop)) {
handlers = openapiRouter.registerRoute({
method: prop,
path: route,
handlers: handlers,
});
}
}
return (route: string, ...handlers: any[]) => {
if (prop !== "fetch") {
if (handlers.length === 1 && handlers[0].isChanfana === true) {
handlers = openapiRouter.registerNestedRouter({
method: prop,
path: route,
nestedRouter: handlers[0],
});
} else if (openapiRouter.allowedMethods.includes(prop)) {
handlers = openapiRouter.registerRoute({
method: prop,
path: route,
handlers: handlers,
});
}
}

return Reflect.get(target, prop, ...args)(route, ...handlers);
};
},
});
return Reflect.get(target, prop, ...args)(route, ...handlers);
};
},
});
}
71 changes: 34 additions & 37 deletions src/adapters/ittyRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,44 @@ import { OpenAPIHandler, type OpenAPIRouterType } from "../openapi";
import type { RouterOptions } from "../types";

export class IttyRouterOpenAPIHandler extends OpenAPIHandler {
getRequest(args: any[]) {
return args[0];
}
getRequest(args: any[]) {
return args[0];
}

getUrlParams(args: any[]): Record<string, any> {
return args[0].params;
}
getUrlParams(args: any[]): Record<string, any> {
return args[0].params;
}
}

export function fromIttyRouter<M>(
router: M,
options?: RouterOptions,
): M & OpenAPIRouterType<M> {
const openapiRouter = new IttyRouterOpenAPIHandler(router, options);
export function fromIttyRouter<M>(router: M, options?: RouterOptions): M & OpenAPIRouterType<M> {
const openapiRouter = new IttyRouterOpenAPIHandler(router, options);

return new Proxy(router, {
get: (target: any, prop: string, ...args: any[]) => {
const _result = openapiRouter.handleCommonProxy(target, prop, ...args);
if (_result !== undefined) {
return _result;
}
return new Proxy(router, {
get: (target: any, prop: string, ...args: any[]) => {
const _result = openapiRouter.handleCommonProxy(target, prop, ...args);
if (_result !== undefined) {
return _result;
}

return (route: string, ...handlers: any[]) => {
if (prop !== "fetch") {
if (handlers.length === 1 && handlers[0].isChanfana === true) {
handlers = openapiRouter.registerNestedRouter({
method: prop,
path: route,
nestedRouter: handlers[0],
});
} else if (openapiRouter.allowedMethods.includes(prop)) {
handlers = openapiRouter.registerRoute({
method: prop,
path: route,
handlers: handlers,
});
}
}
return (route: string, ...handlers: any[]) => {
if (prop !== "fetch") {
if (handlers.length === 1 && handlers[0].isChanfana === true) {
handlers = openapiRouter.registerNestedRouter({
method: prop,
path: route,
nestedRouter: handlers[0],
});
} else if (openapiRouter.allowedMethods.includes(prop)) {
handlers = openapiRouter.registerRoute({
method: prop,
path: route,
handlers: handlers,
});
}
}

return Reflect.get(target, prop, ...args)(route, ...handlers);
};
},
});
return Reflect.get(target, prop, ...args)(route, ...handlers);
};
},
});
}
20 changes: 10 additions & 10 deletions src/contentTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { z } from "zod";
import { legacyTypeIntoZod } from "./zod/utils";

type JsonContent<T> = {
content: {
"application/json": {
schema: z.ZodType<T>;
};
};
content: {
"application/json": {
schema: z.ZodType<T>;
};
};
};

type InferSchemaType<T> = T extends z.ZodType ? z.infer<T> : T;

export const contentJson = <T>(schema: T): JsonContent<InferSchemaType<T>> => ({
content: {
"application/json": {
schema: schema instanceof z.ZodType ? schema : legacyTypeIntoZod(schema),
},
},
content: {
"application/json": {
schema: schema instanceof z.ZodType ? schema : legacyTypeIntoZod(schema),
},
},
});
Loading

0 comments on commit a4f0214

Please sign in to comment.