Skip to content

Commit

Permalink
feat: ✨ init mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
willin committed Dec 30, 2023
1 parent 1b13686 commit e91f7c2
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 47 deletions.
11 changes: 11 additions & 0 deletions .changeset/swift-cheetahs-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@hono-dev/zod-openapi-decorators": patch
"@hono-dev/zod-body-validator": patch
"@shared/typescript-config": patch
"@hono-dev/zod-openapi": patch
"@shared/eslint-config": patch
"@hono-dev/powered-by": patch
"@shared/hono-app": patch
---

init mvp
Binary file modified bun.lockb
Binary file not shown.
32 changes: 8 additions & 24 deletions packages/zod-openapi-decorators/src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIHono } from '@hono-dev/zod-openapi';
import { OpenAPIHono, createRoute } from '@hono-dev/zod-openapi';
import app from './app';
import {
ROUTE_PATHS,
Expand All @@ -24,26 +24,7 @@ export type MethodOptions = {
openapi: OpenApiOptions;
};

export type RoutingPath<P extends string> =
P extends `${infer Head}/{${infer Param}}${infer Tail}`
? `${Head}/:${Param}${RoutingPath<Tail>}`
: P;

export const createRoute = <P extends string, R extends RouteConfig>(
routeConfig: Omit<R, 'path'> & { path?: P | undefined }
) => {
return {
...routeConfig,
getRoutingPath(): RoutingPath<RouteConfig['path']> {
return routeConfig?.path?.replaceAll(
/\/{(.+?)}/g,
'/:$1'
) as RoutingPath<P>;
}
};
};

export function Route(prefix: string): ClassDecorator {
export function Route(prefix: string = ''): ClassDecorator {
return (targetConstructor) => {
const routes = pathsContainer.getMany(ROUTE_PATHS).filter(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
Expand Down Expand Up @@ -107,9 +88,11 @@ export function Route(prefix: string): ClassDecorator {
};
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function MethodDecoratorFactory<T>(method: HTTPMethods) {
return (path: string, options: MethodOptions): MethodDecorator => {
function MethodDecoratorFactory(method: HTTPMethods) {
return (
path: string = '',
options: MethodOptions = {} as MethodOptions
): MethodDecorator => {
return (targetConstructor, methodName) => {
pathsContainer.set({
id: ROUTE_PATHS,
Expand All @@ -130,6 +113,7 @@ function MethodDecoratorFactory<T>(method: HTTPMethods) {
};
};
}

export const Get = MethodDecoratorFactory('GET');
export const Post = MethodDecoratorFactory('POST');
export const Put = MethodDecoratorFactory('PUT');
Expand Down
4 changes: 2 additions & 2 deletions packages/zod-openapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ app.openapi(
You can generate OpenAPI v3.1 spec using the following methods:

```ts
app.doc31('/docs', { openapi: '3.1.0' }); // new endpoint
app.getOpenAPI31Document({ openapi: '3.1.0' }); // raw json
app.doc('/docs', { openapi: '3.1.0' }); // new endpoint
app.getOpenAPIDocument({ openapi: '3.1.0' }); // raw json
```

### The Registry
Expand Down
21 changes: 3 additions & 18 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
ZodRequestBody
} from '@asteasolutions/zod-to-openapi';
import {
OpenApiGeneratorV3,
OpenApiGeneratorV31,
OpenAPIRegistry
} from '@asteasolutions/zod-to-openapi';
Expand Down Expand Up @@ -366,12 +365,6 @@ export class OpenAPIHono<
};

getOpenAPIDocument = (config: OpenAPIObjectConfig) => {
const generator = new OpenApiGeneratorV3(this.openAPIRegistry.definitions);
const document = generator.generateDocument(config);
return document;
};

getOpenAPI31Document = (config: OpenAPIObjectConfig) => {
const generator = new OpenApiGeneratorV31(this.openAPIRegistry.definitions);
const document = generator.generateDocument(config);
return document;
Expand All @@ -388,17 +381,6 @@ export class OpenAPIHono<
}) as any;
};

doc31 = <P extends string>(
path: P,
configure: OpenAPIObjectConfigure<E, P>
): OpenAPIHono<E, S & ToSchema<'get', P, {}, {}>, BasePath> => {
return this.get(path, (c) => {
const config = typeof configure === 'function' ? configure(c) : configure;
const document = this.getOpenAPI31Document(config);
return c.json(document);
}) as any;
};

route<
SubPath extends string,
SubEnv extends Env,
Expand Down Expand Up @@ -494,6 +476,7 @@ type RoutingPath<P extends string> =
? `${Head}/:${Param}${RoutingPath<Tail>}`
: P;

/* eslint-disable @typescript-eslint/ban-ts-comment */
export const createRoute = <
P extends string,
R extends Omit<RouteConfig, 'path' | 'method'> & {
Expand All @@ -505,7 +488,9 @@ export const createRoute = <
) => {
return {
...routeConfig,
// @ts-ignore
getRoutingPath(): RoutingPath<R['path']> {
// @ts-ignore
return routeConfig.path.replaceAll(
/\/{(.+?)}/g,
'/:$1'
Expand Down
36 changes: 34 additions & 2 deletions shared/hono-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { timing } from 'hono/timing';

const app = new Hono();
const app = new Hono({
strict: false
});

export default app;
app.use(
'*',
timing({
totalDescription: false,
crossOrigin: true
})
);

app.use(
'*',
cors({
origin: '*',
credentials: true,
exposeHeaders: [
'content-length',
'x-powered-by',
'timing-allow-origin',
'server-timing'
],
maxAge: 3600
})
);

export function getVersion(pkg: unknown) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return typeof pkg === 'object' ? pkg?.version : 'unknown';
}

export { app };
2 changes: 1 addition & 1 deletion workers/openapi/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@shared/typescript-config/tsconfig.base.json",
"include": ["src/**/*.ts"],
"include": ["src/**/*.ts", "package.json"],
"exclude": ["node_modules"]
}

0 comments on commit e91f7c2

Please sign in to comment.