@easydocs/nextjs 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +76 -0
- package/dist/index.cjs +96 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +70 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 EasyDocs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @easydocs/nextjs
|
|
2
|
+
|
|
3
|
+
EasyDocs handler wrapper for [Next.js](https://nextjs.org/) App Router and Pages Router.
|
|
4
|
+
|
|
5
|
+
> Next.js middleware runs in the Edge runtime and cannot access response bodies.
|
|
6
|
+
> EasyDocs wraps individual route handlers instead — no URL changes needed.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @easydocs/nextjs
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## App Router
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
// app/api/users/route.ts
|
|
18
|
+
import { withEasydocs } from '@easydocs/nextjs'
|
|
19
|
+
import { NextResponse } from 'next/server'
|
|
20
|
+
|
|
21
|
+
export const GET = withEasydocs(async (req) => {
|
|
22
|
+
return NextResponse.json({ users: [] })
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
// with config
|
|
26
|
+
export const POST = withEasydocs(
|
|
27
|
+
async (req) => {
|
|
28
|
+
const body = await req.json()
|
|
29
|
+
return NextResponse.json({ created: true }, { status: 201 })
|
|
30
|
+
},
|
|
31
|
+
{ ai: { provider: 'anthropic' } }
|
|
32
|
+
)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Dynamic routes expose params automatically:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
// app/api/users/[id]/route.ts
|
|
39
|
+
export const GET = withEasydocs(async (req, { params }) => {
|
|
40
|
+
const { id } = await params
|
|
41
|
+
return NextResponse.json({ id })
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Pages Router
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
// pages/api/users.ts
|
|
49
|
+
import { withEasydocsPagesHandler } from '@easydocs/nextjs'
|
|
50
|
+
|
|
51
|
+
export default withEasydocsPagesHandler((req, res) => {
|
|
52
|
+
res.json({ users: [] })
|
|
53
|
+
})
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Configuration
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
withEasydocs(handler, {
|
|
60
|
+
ai: { provider: 'openai', model: 'gpt-4o' },
|
|
61
|
+
storage: { url: 'file:./docs.sqlite' },
|
|
62
|
+
capture: { ignoreRoutes: ['/api/health'] },
|
|
63
|
+
})
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## View your docs
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
npm install -D @easydocs/dashboard
|
|
70
|
+
npx easydocs dashboard
|
|
71
|
+
|
|
72
|
+
# Or export to a file
|
|
73
|
+
npx easydocs export > openapi.json
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
See [@easydocs/core](../core) for the full configuration reference.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
withEasydocs: () => withEasydocs,
|
|
24
|
+
withEasydocsPagesHandler: () => withEasydocsPagesHandler
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var import_core = require("@easydocs/core");
|
|
28
|
+
function withEasydocs(handler, config) {
|
|
29
|
+
return async (req, ctx) => {
|
|
30
|
+
const startedAt = Date.now();
|
|
31
|
+
const response = await handler(req, ctx);
|
|
32
|
+
let responseBody = null;
|
|
33
|
+
try {
|
|
34
|
+
responseBody = await response.clone().json();
|
|
35
|
+
} catch {
|
|
36
|
+
}
|
|
37
|
+
let resolvedParams = {};
|
|
38
|
+
if (ctx?.params) {
|
|
39
|
+
resolvedParams = ctx.params instanceof Promise ? await ctx.params : ctx.params;
|
|
40
|
+
}
|
|
41
|
+
let requestBody = null;
|
|
42
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
43
|
+
try {
|
|
44
|
+
requestBody = await req.clone().json();
|
|
45
|
+
} catch {
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
(0, import_core.capture)(
|
|
49
|
+
{
|
|
50
|
+
method: req.method,
|
|
51
|
+
path: req.nextUrl.pathname,
|
|
52
|
+
query: Object.fromEntries(req.nextUrl.searchParams.entries()),
|
|
53
|
+
params: resolvedParams,
|
|
54
|
+
body: requestBody,
|
|
55
|
+
response: responseBody,
|
|
56
|
+
status: response.status,
|
|
57
|
+
requestHeaders: Object.fromEntries(req.headers.entries()),
|
|
58
|
+
responseHeaders: Object.fromEntries(response.headers.entries()),
|
|
59
|
+
durationMs: Date.now() - startedAt
|
|
60
|
+
},
|
|
61
|
+
config
|
|
62
|
+
);
|
|
63
|
+
return response;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function withEasydocsPagesHandler(handler, config) {
|
|
67
|
+
return async (req, res) => {
|
|
68
|
+
const startedAt = Date.now();
|
|
69
|
+
const originalJson = res.json.bind(res);
|
|
70
|
+
res.json = function(body) {
|
|
71
|
+
(0, import_core.capture)(
|
|
72
|
+
{
|
|
73
|
+
method: req.method ?? "GET",
|
|
74
|
+
path: req.url?.split("?")[0] ?? "/",
|
|
75
|
+
query: req.query,
|
|
76
|
+
params: {},
|
|
77
|
+
body: req.body,
|
|
78
|
+
response: body,
|
|
79
|
+
status: res.statusCode,
|
|
80
|
+
requestHeaders: req.headers,
|
|
81
|
+
responseHeaders: res.getHeaders(),
|
|
82
|
+
durationMs: Date.now() - startedAt
|
|
83
|
+
},
|
|
84
|
+
config
|
|
85
|
+
);
|
|
86
|
+
return originalJson(body);
|
|
87
|
+
};
|
|
88
|
+
await handler(req, res);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
+
0 && (module.exports = {
|
|
93
|
+
withEasydocs,
|
|
94
|
+
withEasydocsPagesHandler
|
|
95
|
+
});
|
|
96
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { capture } from '@easydocs/core'\nimport type { EasyDocsConfig, HttpMethod } from '@easydocs/core'\n\n// ─── Local structural types (avoid importing from next at build time) ──────────\n\ninterface NextURL {\n pathname: string\n searchParams: URLSearchParams\n}\n\ninterface NextRequestLike {\n method: string\n nextUrl: NextURL\n headers: Headers\n clone(): { json(): Promise<unknown> }\n}\n\ninterface NextApiRequestLike {\n method?: string\n url?: string\n query: Record<string, string | string[]>\n body: unknown\n headers: Record<string, string | string[] | undefined>\n}\n\ninterface NextApiResponseLike {\n statusCode: number\n json: (body: unknown) => NextApiResponseLike\n getHeaders(): Record<string, string | string[] | number | undefined>\n}\n\n// ─── App Router ───────────────────────────────────────────────────────────────\n\ntype AppRouterContext = { params?: Promise<Record<string, string>> | Record<string, string> }\ntype AppRouterHandler = (req: NextRequestLike, ctx?: AppRouterContext) => Promise<Response> | Response\n\nexport function withEasydocs(handler: AppRouterHandler, config?: EasyDocsConfig): AppRouterHandler {\n return async (req, ctx) => {\n const startedAt = Date.now()\n const response = await handler(req, ctx)\n\n let responseBody: unknown = null\n try {\n responseBody = await response.clone().json()\n } catch {\n // non-JSON response — skip body capture\n }\n\n let resolvedParams: Record<string, string> = {}\n if (ctx?.params) {\n resolvedParams =\n ctx.params instanceof Promise ? await ctx.params : (ctx.params as Record<string, string>)\n }\n\n let requestBody: unknown = null\n if (req.method !== 'GET' && req.method !== 'HEAD') {\n try {\n requestBody = await req.clone().json()\n } catch {\n // non-JSON body\n }\n }\n\n capture(\n {\n method: req.method as HttpMethod,\n path: req.nextUrl.pathname,\n query: Object.fromEntries(req.nextUrl.searchParams.entries()),\n params: resolvedParams,\n body: requestBody,\n response: responseBody,\n status: response.status,\n requestHeaders: Object.fromEntries(req.headers.entries()),\n responseHeaders: Object.fromEntries(response.headers.entries()),\n durationMs: Date.now() - startedAt,\n },\n config\n )\n\n return response\n }\n}\n\n// ─── Pages Router ─────────────────────────────────────────────────────────────\n\ntype PagesHandler = (req: NextApiRequestLike, res: NextApiResponseLike) => void | Promise<void>\n\nexport function withEasydocsPagesHandler(\n handler: PagesHandler,\n config?: EasyDocsConfig\n): PagesHandler {\n return async (req, res) => {\n const startedAt = Date.now()\n const originalJson = res.json.bind(res)\n\n res.json = function (body: unknown) {\n capture(\n {\n method: (req.method ?? 'GET') as HttpMethod,\n path: req.url?.split('?')[0] ?? '/',\n query: req.query as Record<string, string>,\n params: {},\n body: req.body,\n response: body,\n status: res.statusCode,\n requestHeaders: req.headers as Record<string, string>,\n responseHeaders: res.getHeaders() as Record<string, string>,\n durationMs: Date.now() - startedAt,\n },\n config\n )\n return originalJson(body)\n }\n\n await handler(req, res)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAwB;AAoCjB,SAAS,aAAa,SAA2B,QAA2C;AACjG,SAAO,OAAO,KAAK,QAAQ;AACzB,UAAM,YAAY,KAAK,IAAI;AAC3B,UAAM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AAI;AAAA,UACZ,gBAAgB,IAAI;AAAA,UACpB,iBAAiB,IAAI,WAAW;AAAA,UAChC,YAAY,KAAK,IAAI,IAAI;AAAA,QAC3B;AAAA,QACA;AAAA,MACF;AACA,aAAO,aAAa,IAAI;AAAA,IAC1B;AAEA,UAAM,QAAQ,KAAK,GAAG;AAAA,EACxB;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EasyDocsConfig } from '@easydocs/core';
|
|
2
|
+
|
|
3
|
+
interface NextURL {
|
|
4
|
+
pathname: string;
|
|
5
|
+
searchParams: URLSearchParams;
|
|
6
|
+
}
|
|
7
|
+
interface NextRequestLike {
|
|
8
|
+
method: string;
|
|
9
|
+
nextUrl: NextURL;
|
|
10
|
+
headers: Headers;
|
|
11
|
+
clone(): {
|
|
12
|
+
json(): Promise<unknown>;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
interface NextApiRequestLike {
|
|
16
|
+
method?: string;
|
|
17
|
+
url?: string;
|
|
18
|
+
query: Record<string, string | string[]>;
|
|
19
|
+
body: unknown;
|
|
20
|
+
headers: Record<string, string | string[] | undefined>;
|
|
21
|
+
}
|
|
22
|
+
interface NextApiResponseLike {
|
|
23
|
+
statusCode: number;
|
|
24
|
+
json: (body: unknown) => NextApiResponseLike;
|
|
25
|
+
getHeaders(): Record<string, string | string[] | number | undefined>;
|
|
26
|
+
}
|
|
27
|
+
type AppRouterContext = {
|
|
28
|
+
params?: Promise<Record<string, string>> | Record<string, string>;
|
|
29
|
+
};
|
|
30
|
+
type AppRouterHandler = (req: NextRequestLike, ctx?: AppRouterContext) => Promise<Response> | Response;
|
|
31
|
+
declare function withEasydocs(handler: AppRouterHandler, config?: EasyDocsConfig): AppRouterHandler;
|
|
32
|
+
type PagesHandler = (req: NextApiRequestLike, res: NextApiResponseLike) => void | Promise<void>;
|
|
33
|
+
declare function withEasydocsPagesHandler(handler: PagesHandler, config?: EasyDocsConfig): PagesHandler;
|
|
34
|
+
|
|
35
|
+
export { withEasydocs, withEasydocsPagesHandler };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EasyDocsConfig } from '@easydocs/core';
|
|
2
|
+
|
|
3
|
+
interface NextURL {
|
|
4
|
+
pathname: string;
|
|
5
|
+
searchParams: URLSearchParams;
|
|
6
|
+
}
|
|
7
|
+
interface NextRequestLike {
|
|
8
|
+
method: string;
|
|
9
|
+
nextUrl: NextURL;
|
|
10
|
+
headers: Headers;
|
|
11
|
+
clone(): {
|
|
12
|
+
json(): Promise<unknown>;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
interface NextApiRequestLike {
|
|
16
|
+
method?: string;
|
|
17
|
+
url?: string;
|
|
18
|
+
query: Record<string, string | string[]>;
|
|
19
|
+
body: unknown;
|
|
20
|
+
headers: Record<string, string | string[] | undefined>;
|
|
21
|
+
}
|
|
22
|
+
interface NextApiResponseLike {
|
|
23
|
+
statusCode: number;
|
|
24
|
+
json: (body: unknown) => NextApiResponseLike;
|
|
25
|
+
getHeaders(): Record<string, string | string[] | number | undefined>;
|
|
26
|
+
}
|
|
27
|
+
type AppRouterContext = {
|
|
28
|
+
params?: Promise<Record<string, string>> | Record<string, string>;
|
|
29
|
+
};
|
|
30
|
+
type AppRouterHandler = (req: NextRequestLike, ctx?: AppRouterContext) => Promise<Response> | Response;
|
|
31
|
+
declare function withEasydocs(handler: AppRouterHandler, config?: EasyDocsConfig): AppRouterHandler;
|
|
32
|
+
type PagesHandler = (req: NextApiRequestLike, res: NextApiResponseLike) => void | Promise<void>;
|
|
33
|
+
declare function withEasydocsPagesHandler(handler: PagesHandler, config?: EasyDocsConfig): PagesHandler;
|
|
34
|
+
|
|
35
|
+
export { withEasydocs, withEasydocsPagesHandler };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { capture } from "@easydocs/core";
|
|
3
|
+
function withEasydocs(handler, config) {
|
|
4
|
+
return async (req, ctx) => {
|
|
5
|
+
const startedAt = Date.now();
|
|
6
|
+
const response = await handler(req, ctx);
|
|
7
|
+
let responseBody = null;
|
|
8
|
+
try {
|
|
9
|
+
responseBody = await response.clone().json();
|
|
10
|
+
} catch {
|
|
11
|
+
}
|
|
12
|
+
let resolvedParams = {};
|
|
13
|
+
if (ctx?.params) {
|
|
14
|
+
resolvedParams = ctx.params instanceof Promise ? await ctx.params : ctx.params;
|
|
15
|
+
}
|
|
16
|
+
let requestBody = null;
|
|
17
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
18
|
+
try {
|
|
19
|
+
requestBody = await req.clone().json();
|
|
20
|
+
} catch {
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
capture(
|
|
24
|
+
{
|
|
25
|
+
method: req.method,
|
|
26
|
+
path: req.nextUrl.pathname,
|
|
27
|
+
query: Object.fromEntries(req.nextUrl.searchParams.entries()),
|
|
28
|
+
params: resolvedParams,
|
|
29
|
+
body: requestBody,
|
|
30
|
+
response: responseBody,
|
|
31
|
+
status: response.status,
|
|
32
|
+
requestHeaders: Object.fromEntries(req.headers.entries()),
|
|
33
|
+
responseHeaders: Object.fromEntries(response.headers.entries()),
|
|
34
|
+
durationMs: Date.now() - startedAt
|
|
35
|
+
},
|
|
36
|
+
config
|
|
37
|
+
);
|
|
38
|
+
return response;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function withEasydocsPagesHandler(handler, config) {
|
|
42
|
+
return async (req, res) => {
|
|
43
|
+
const startedAt = Date.now();
|
|
44
|
+
const originalJson = res.json.bind(res);
|
|
45
|
+
res.json = function(body) {
|
|
46
|
+
capture(
|
|
47
|
+
{
|
|
48
|
+
method: req.method ?? "GET",
|
|
49
|
+
path: req.url?.split("?")[0] ?? "/",
|
|
50
|
+
query: req.query,
|
|
51
|
+
params: {},
|
|
52
|
+
body: req.body,
|
|
53
|
+
response: body,
|
|
54
|
+
status: res.statusCode,
|
|
55
|
+
requestHeaders: req.headers,
|
|
56
|
+
responseHeaders: res.getHeaders(),
|
|
57
|
+
durationMs: Date.now() - startedAt
|
|
58
|
+
},
|
|
59
|
+
config
|
|
60
|
+
);
|
|
61
|
+
return originalJson(body);
|
|
62
|
+
};
|
|
63
|
+
await handler(req, res);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
withEasydocs,
|
|
68
|
+
withEasydocsPagesHandler
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { capture } from '@easydocs/core'\nimport type { EasyDocsConfig, HttpMethod } from '@easydocs/core'\n\n// ─── Local structural types (avoid importing from next at build time) ──────────\n\ninterface NextURL {\n pathname: string\n searchParams: URLSearchParams\n}\n\ninterface NextRequestLike {\n method: string\n nextUrl: NextURL\n headers: Headers\n clone(): { json(): Promise<unknown> }\n}\n\ninterface NextApiRequestLike {\n method?: string\n url?: string\n query: Record<string, string | string[]>\n body: unknown\n headers: Record<string, string | string[] | undefined>\n}\n\ninterface NextApiResponseLike {\n statusCode: number\n json: (body: unknown) => NextApiResponseLike\n getHeaders(): Record<string, string | string[] | number | undefined>\n}\n\n// ─── App Router ───────────────────────────────────────────────────────────────\n\ntype AppRouterContext = { params?: Promise<Record<string, string>> | Record<string, string> }\ntype AppRouterHandler = (req: NextRequestLike, ctx?: AppRouterContext) => Promise<Response> | Response\n\nexport function withEasydocs(handler: AppRouterHandler, config?: EasyDocsConfig): AppRouterHandler {\n return async (req, ctx) => {\n const startedAt = Date.now()\n const response = await handler(req, ctx)\n\n let responseBody: unknown = null\n try {\n responseBody = await response.clone().json()\n } catch {\n // non-JSON response — skip body capture\n }\n\n let resolvedParams: Record<string, string> = {}\n if (ctx?.params) {\n resolvedParams =\n ctx.params instanceof Promise ? await ctx.params : (ctx.params as Record<string, string>)\n }\n\n let requestBody: unknown = null\n if (req.method !== 'GET' && req.method !== 'HEAD') {\n try {\n requestBody = await req.clone().json()\n } catch {\n // non-JSON body\n }\n }\n\n capture(\n {\n method: req.method as HttpMethod,\n path: req.nextUrl.pathname,\n query: Object.fromEntries(req.nextUrl.searchParams.entries()),\n params: resolvedParams,\n body: requestBody,\n response: responseBody,\n status: response.status,\n requestHeaders: Object.fromEntries(req.headers.entries()),\n responseHeaders: Object.fromEntries(response.headers.entries()),\n durationMs: Date.now() - startedAt,\n },\n config\n )\n\n return response\n }\n}\n\n// ─── Pages Router ─────────────────────────────────────────────────────────────\n\ntype PagesHandler = (req: NextApiRequestLike, res: NextApiResponseLike) => void | Promise<void>\n\nexport function withEasydocsPagesHandler(\n handler: PagesHandler,\n config?: EasyDocsConfig\n): PagesHandler {\n return async (req, res) => {\n const startedAt = Date.now()\n const originalJson = res.json.bind(res)\n\n res.json = function (body: unknown) {\n capture(\n {\n method: (req.method ?? 'GET') as HttpMethod,\n path: req.url?.split('?')[0] ?? '/',\n query: req.query as Record<string, string>,\n params: {},\n body: req.body,\n response: body,\n status: res.statusCode,\n requestHeaders: req.headers as Record<string, string>,\n responseHeaders: res.getHeaders() as Record<string, string>,\n durationMs: Date.now() - startedAt,\n },\n config\n )\n return originalJson(body)\n }\n\n await handler(req, res)\n }\n}\n"],"mappings":";AAAA,SAAS,eAAe;AAoCjB,SAAS,aAAa,SAA2B,QAA2C;AACjG,SAAO,OAAO,KAAK,QAAQ;AACzB,UAAM,YAAY,KAAK,IAAI;AAC3B,UAAM,WAAW,MAAM,QAAQ,KAAK,GAAG;AAEvC,QAAI,eAAwB;AAC5B,QAAI;AACF,qBAAe,MAAM,SAAS,MAAM,EAAE,KAAK;AAAA,IAC7C,QAAQ;AAAA,IAER;AAEA,QAAI,iBAAyC,CAAC;AAC9C,QAAI,KAAK,QAAQ;AACf,uBACE,IAAI,kBAAkB,UAAU,MAAM,IAAI,SAAU,IAAI;AAAA,IAC5D;AAEA,QAAI,cAAuB;AAC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|
2
|
+
"name": "@easydocs/nextjs",
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist",
|
|
6
|
+
"README.md"
|
|
7
|
+
],
|
|
8
|
+
"description": "EasyDocs handler wrapper for Next.js App Router and Pages Router",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.cjs",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"require": "./dist/index.cjs",
|
|
17
|
+
"types": "./dist/index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@easydocs/core": "0.1.4"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"next": ">=14.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"next": "^15.1.6",
|
|
28
|
+
"tsup": "^8.3.0",
|
|
29
|
+
"typescript": "^5"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup",
|
|
33
|
+
"dev": "tsup --watch",
|
|
34
|
+
"typecheck": "tsc --noEmit"
|
|
35
|
+
}
|
|
36
|
+
}
|