Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: experimental defineRouteMeta #2102

Merged
merged 35 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
2d41b59
openapi test and parser init
eriksLapins Jan 20, 2024
cc55d92
changed the config type to function
eriksLapins Jan 20, 2024
3d6a39e
first working example
Jan 20, 2024
5fbaf10
project cleanup author change
eriksLapins Jan 20, 2024
a2c0864
split schema into its own object
eriksLapins Jan 20, 2024
a697883
prettier formatting from test
eriksLapins Jan 20, 2024
1c6bd41
changed routes to relative to nitro
eriksLapins Jan 20, 2024
ceb7ee1
defineRouteMeta function and openapi schema at build time
eriksLapins Jan 21, 2024
76aa104
from regex to AST parsing
eriksLapins Jan 22, 2024
4b6f151
prettier lint
eriksLapins Jan 22, 2024
21264e2
added documentation for defineRouteMeta
eriksLapins Jan 22, 2024
8ad1bcd
fix to docs
eriksLapins Jan 22, 2024
0a2926a
Merge branch 'main' into main
eriksLapins Jan 23, 2024
c863fff
Merge branch 'main' into main
eriksLapins Jan 24, 2024
9123ab0
Merge branch 'main' into main
eriksLapins Feb 8, 2024
743c621
Merge branch 'main' into pr/eriksLapins/2102
pi0 Apr 4, 2024
c444513
chore: apply automated fixes
autofix-ci[bot] Apr 4, 2024
1d52917
refactor as rollup plugin for hmr support
pi0 Apr 4, 2024
0cba457
revert unnecessary await
pi0 Apr 9, 2024
30e2b84
small updates
pi0 Apr 9, 2024
0e742ab
update
pi0 Apr 9, 2024
9751c82
update docs
pi0 Apr 9, 2024
4c64ad2
update
pi0 Apr 9, 2024
517a555
update docs
pi0 Apr 10, 2024
d664984
add experimental guard for now
pi0 Apr 10, 2024
e77cd87
update impl
pi0 Apr 10, 2024
b3b9ae6
relax type checks
pi0 Apr 10, 2024
63ee23a
refactor names
pi0 Apr 10, 2024
6d51573
fix typo
pi0 Apr 10, 2024
1b183fc
revert extra change
pi0 Apr 10, 2024
29bbcbd
refactor types
pi0 Apr 10, 2024
6701bc5
update type import
pi0 Apr 10, 2024
fb6be11
add basic test
pi0 Apr 10, 2024
69b2855
Merge branch 'main' into main
pi0 Apr 10, 2024
ec75ad7
remove unused import
pi0 Apr 10, 2024
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
13 changes: 13 additions & 0 deletions examples/api-routes/api/test.get.ts
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
import { defineOpenAPISchema } from "#internal/nitro/routes/openapi";

export default defineEventHandler(() => "Test get handler");

defineOpenAPISchema({
eriksLapins marked this conversation as resolved.
Show resolved Hide resolved
routeBase: '/api/test',
method: 'get',
schema: {
tags: ["Test"],
summary: "some test",
description: "some test description",
deprecated: false,
},
});
27 changes: 27 additions & 0 deletions examples/api-routes/api/test.post.ts
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
import { defineOpenAPISchema } from "#internal/nitro/routes/openapi";

export default defineEventHandler(() => "Test post handler");

defineOpenAPISchema({
method: 'post',
routeBase: '/api/test',
eriksLapins marked this conversation as resolved.
Show resolved Hide resolved
schema: {
tags: [
"Test"
],
parameters: [
{
in: "query",
name: 'test',
required: true
},
],
responses: {
200: {
description: "OK"
},
404: {
description: "Not found"
},
},
},
});
6 changes: 5 additions & 1 deletion examples/api-routes/nitro.config.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export default defineNitroConfig({});
export default defineNitroConfig({
experimental: {
openAPI: true,
},
});
31 changes: 28 additions & 3 deletions src/runtime/routes/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,31 @@ import type {
ParameterObject,
PathsObject,
} from "openapi-typescript";
import { NitroOpenapiSchema } from "../../types";
import { handlersMeta } from "#internal/nitro/virtual/server-handlers";
import { useRuntimeConfig } from "#internal/nitro";
import { useRuntimeConfig, useStorage } from "#internal/nitro";

// Served as /_nitro/openapi.json
export default eventHandler((event) => {
export default eventHandler(async () => {
const base = useRuntimeConfig()?.app?.baseURL;
const paths = getPaths();

for (const path in paths) {
const methods = Object.keys(paths[path]);
for (const method of methods) {
const hasItem = await useStorage(path).hasItem(`openapi-${method}`);
if (hasItem) {
const storage = await useStorage(path).getItem(`openapi-${method}`);
if (typeof storage === "object") {
const schema = storage as NitroOpenapiSchema;
paths[path][method] = {
...paths[path][method],
...schema.schema,
};
}
}
}
}

return <OpenAPI3>{
openapi: "3.0.0",
Expand All @@ -27,7 +46,7 @@ export default eventHandler((event) => {
},
],
schemes: ["http"],
paths: getPaths(),
paths,
};
});

Expand Down Expand Up @@ -96,3 +115,9 @@ function defaultTags(route: string) {

return tags;
}

export function defineOpenAPISchema(schema: NitroOpenapiSchema) {
useStorage(schema.routeBase).setItem(`openapi-${schema.method}`, schema);
pi0 marked this conversation as resolved.
Show resolved Hide resolved

return schema;
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./nitro";
export * from "./handler";
export * from "./utils";
export * from "./module";
export * from "./openapi";
8 changes: 8 additions & 0 deletions src/types/openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { RouterMethod } from "h3";
import type { OperationObject } from "openapi-typescript";

export type NitroOpenapiSchema = {
routeBase: string;
method: RouterMethod;
schema: OperationObject;
};