@dokploy/trpc-openapi 0.0.2 → 0.0.3
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/dist/generator/paths.d.ts.map +1 -1
- package/dist/generator/paths.js +2 -2
- package/dist/generator/paths.js.map +1 -1
- package/dist/generator/schema.d.ts.map +1 -1
- package/dist/generator/schema.js +3 -13
- package/dist/generator/schema.js.map +1 -1
- package/dist/types.d.ts +12 -10
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/procedure.d.ts +4 -3
- package/dist/utils/procedure.d.ts.map +1 -1
- package/dist/utils/procedure.js +29 -7
- package/dist/utils/procedure.js.map +1 -1
- package/package.json +14 -9
- package/assets/trpc-openapi-graph.png +0 -0
- package/assets/trpc-openapi-readme.png +0 -0
- package/assets/trpc-openapi.svg +0 -4
- package/examples/with-express/README.md +0 -11
- package/examples/with-express/package.json +0 -28
- package/examples/with-express/src/database.ts +0 -67
- package/examples/with-express/src/index.ts +0 -27
- package/examples/with-express/src/openapi.ts +0 -13
- package/examples/with-express/src/router.ts +0 -424
- package/examples/with-express/tsconfig.json +0 -102
- package/examples/with-interop/README.md +0 -10
- package/examples/with-interop/package.json +0 -13
- package/examples/with-interop/src/index.ts +0 -17
- package/examples/with-interop/tsconfig.json +0 -103
- package/examples/with-nextjs/.eslintrc.json +0 -3
- package/examples/with-nextjs/README.md +0 -12
- package/examples/with-nextjs/next-env.d.ts +0 -5
- package/examples/with-nextjs/next.config.js +0 -6
- package/examples/with-nextjs/package.json +0 -33
- package/examples/with-nextjs/public/favicon.ico +0 -0
- package/examples/with-nextjs/src/pages/_app.tsx +0 -7
- package/examples/with-nextjs/src/pages/api/[...trpc].ts +0 -18
- package/examples/with-nextjs/src/pages/api/openapi.json.ts +0 -10
- package/examples/with-nextjs/src/pages/api/trpc/[...trpc].ts +0 -9
- package/examples/with-nextjs/src/pages/index.tsx +0 -12
- package/examples/with-nextjs/src/server/database.ts +0 -67
- package/examples/with-nextjs/src/server/openapi.ts +0 -13
- package/examples/with-nextjs/src/server/router.ts +0 -425
- package/examples/with-nextjs/tsconfig.json +0 -24
- package/jest.config.ts +0 -12
- package/pnpm-workspace.yaml +0 -7
- package/src/adapters/express.ts +0 -20
- package/src/adapters/index.ts +0 -3
- package/src/adapters/next.ts +0 -64
- package/src/adapters/node-http/core.ts +0 -203
- package/src/adapters/node-http/errors.ts +0 -45
- package/src/adapters/node-http/input.ts +0 -76
- package/src/adapters/node-http/procedures.ts +0 -64
- package/src/adapters/standalone.ts +0 -19
- package/src/generator/index.ts +0 -51
- package/src/generator/paths.ts +0 -127
- package/src/generator/schema.ts +0 -238
- package/src/index.ts +0 -42
- package/src/types.ts +0 -79
- package/src/utils/method.ts +0 -8
- package/src/utils/path.ts +0 -12
- package/src/utils/procedure.ts +0 -45
- package/src/utils/zod.ts +0 -115
- package/test/adapters/express.test.ts +0 -150
- package/test/adapters/next.test.ts +0 -162
- package/test/adapters/standalone.test.ts +0 -1335
- package/test/generator.test.ts +0 -2897
- package/tsconfig.build.json +0 -4
- package/tsconfig.eslint.json +0 -5
- package/tsconfig.json +0 -19
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
-
import { initTRPC } from '@trpc/server';
|
|
3
|
-
import { NextApiRequest, NextApiResponse } from 'next';
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
CreateOpenApiNextHandlerOptions,
|
|
8
|
-
OpenApiMeta,
|
|
9
|
-
OpenApiResponse,
|
|
10
|
-
OpenApiRouter,
|
|
11
|
-
createOpenApiNextHandler,
|
|
12
|
-
} from '../../src';
|
|
13
|
-
|
|
14
|
-
const createContextMock = jest.fn();
|
|
15
|
-
const responseMetaMock = jest.fn();
|
|
16
|
-
const onErrorMock = jest.fn();
|
|
17
|
-
|
|
18
|
-
const clearMocks = () => {
|
|
19
|
-
createContextMock.mockClear();
|
|
20
|
-
responseMetaMock.mockClear();
|
|
21
|
-
onErrorMock.mockClear();
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const createOpenApiNextHandlerCaller = <TRouter extends OpenApiRouter>(
|
|
25
|
-
handlerOpts: CreateOpenApiNextHandlerOptions<TRouter>,
|
|
26
|
-
) => {
|
|
27
|
-
const openApiNextHandler = createOpenApiNextHandler({
|
|
28
|
-
router: handlerOpts.router,
|
|
29
|
-
createContext: handlerOpts.createContext ?? createContextMock,
|
|
30
|
-
responseMeta: handlerOpts.responseMeta ?? responseMetaMock,
|
|
31
|
-
onError: handlerOpts.onError ?? onErrorMock,
|
|
32
|
-
} as any);
|
|
33
|
-
|
|
34
|
-
return (req: { method: string; query: Record<string, any>; body?: any }) => {
|
|
35
|
-
return new Promise<{
|
|
36
|
-
statusCode: number;
|
|
37
|
-
headers: Record<string, any>;
|
|
38
|
-
body: OpenApiResponse | undefined;
|
|
39
|
-
/* eslint-disable-next-line @typescript-eslint/no-misused-promises, no-async-promise-executor */
|
|
40
|
-
}>(async (resolve, reject) => {
|
|
41
|
-
const headers = new Map();
|
|
42
|
-
let body: any;
|
|
43
|
-
const res: any = {
|
|
44
|
-
statusCode: undefined,
|
|
45
|
-
setHeader: (key: string, value: any) => headers.set(key, value),
|
|
46
|
-
end: (data: string) => {
|
|
47
|
-
body = JSON.parse(data);
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
await openApiNextHandler(
|
|
53
|
-
req as unknown as NextApiRequest,
|
|
54
|
-
res as unknown as NextApiResponse,
|
|
55
|
-
);
|
|
56
|
-
resolve({
|
|
57
|
-
statusCode: res.statusCode,
|
|
58
|
-
headers: Object.fromEntries(headers.entries()),
|
|
59
|
-
body,
|
|
60
|
-
});
|
|
61
|
-
} catch (error) {
|
|
62
|
-
reject(error);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const t = initTRPC.meta<OpenApiMeta>().context<any>().create();
|
|
69
|
-
|
|
70
|
-
describe('next adapter', () => {
|
|
71
|
-
afterEach(() => {
|
|
72
|
-
clearMocks();
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('with valid routes', async () => {
|
|
76
|
-
const appRouter = t.router({
|
|
77
|
-
sayHelloQuery: t.procedure
|
|
78
|
-
.meta({ openapi: { method: 'GET', path: '/say-hello' } })
|
|
79
|
-
.input(z.object({ name: z.string() }))
|
|
80
|
-
.output(z.object({ greeting: z.string() }))
|
|
81
|
-
.query(({ input }) => ({ greeting: `Hello ${input.name}!` })),
|
|
82
|
-
sayHelloMutation: t.procedure
|
|
83
|
-
.meta({ openapi: { method: 'POST', path: '/say-hello' } })
|
|
84
|
-
.input(z.object({ name: z.string() }))
|
|
85
|
-
.output(z.object({ greeting: z.string() }))
|
|
86
|
-
.mutation(({ input }) => ({ greeting: `Hello ${input.name}!` })),
|
|
87
|
-
sayHelloSlash: t.procedure
|
|
88
|
-
.meta({ openapi: { method: 'GET', path: '/say/hello' } })
|
|
89
|
-
.input(z.object({ name: z.string() }))
|
|
90
|
-
.output(z.object({ greeting: z.string() }))
|
|
91
|
-
.query(({ input }) => ({ greeting: `Hello ${input.name}!` })),
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
const openApiNextHandlerCaller = createOpenApiNextHandlerCaller({
|
|
95
|
-
router: appRouter,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
{
|
|
99
|
-
const res = await openApiNextHandlerCaller({
|
|
100
|
-
method: 'GET',
|
|
101
|
-
query: { trpc: 'say-hello', name: 'James' },
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
expect(res.statusCode).toBe(200);
|
|
105
|
-
expect(res.body).toEqual({ greeting: 'Hello James!' });
|
|
106
|
-
expect(createContextMock).toHaveBeenCalledTimes(1);
|
|
107
|
-
expect(responseMetaMock).toHaveBeenCalledTimes(1);
|
|
108
|
-
expect(onErrorMock).toHaveBeenCalledTimes(0);
|
|
109
|
-
|
|
110
|
-
clearMocks();
|
|
111
|
-
}
|
|
112
|
-
{
|
|
113
|
-
const res = await openApiNextHandlerCaller({
|
|
114
|
-
method: 'POST',
|
|
115
|
-
query: { trpc: 'say-hello' },
|
|
116
|
-
body: { name: 'James' },
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
expect(res.statusCode).toBe(200);
|
|
120
|
-
expect(res.body).toEqual({ greeting: 'Hello James!' });
|
|
121
|
-
expect(createContextMock).toHaveBeenCalledTimes(1);
|
|
122
|
-
expect(responseMetaMock).toHaveBeenCalledTimes(1);
|
|
123
|
-
expect(onErrorMock).toHaveBeenCalledTimes(0);
|
|
124
|
-
|
|
125
|
-
clearMocks();
|
|
126
|
-
}
|
|
127
|
-
{
|
|
128
|
-
const res = await openApiNextHandlerCaller({
|
|
129
|
-
method: 'GET',
|
|
130
|
-
query: { trpc: ['say', 'hello'], name: 'James' },
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
expect(res.statusCode).toBe(200);
|
|
134
|
-
expect(res.body).toEqual({ greeting: 'Hello James!' });
|
|
135
|
-
expect(createContextMock).toHaveBeenCalledTimes(1);
|
|
136
|
-
expect(responseMetaMock).toHaveBeenCalledTimes(1);
|
|
137
|
-
expect(onErrorMock).toHaveBeenCalledTimes(0);
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
test('with invalid path', async () => {
|
|
142
|
-
const appRouter = t.router({});
|
|
143
|
-
|
|
144
|
-
const openApiNextHandlerCaller = createOpenApiNextHandlerCaller({
|
|
145
|
-
router: appRouter,
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
const res = await openApiNextHandlerCaller({
|
|
149
|
-
method: 'GET',
|
|
150
|
-
query: {},
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
expect(res.statusCode).toBe(500);
|
|
154
|
-
expect(res.body).toEqual({
|
|
155
|
-
message: 'Query "trpc" not found - is the `trpc-openapi` file named `[...trpc].ts`?',
|
|
156
|
-
code: 'INTERNAL_SERVER_ERROR',
|
|
157
|
-
});
|
|
158
|
-
expect(createContextMock).toHaveBeenCalledTimes(0);
|
|
159
|
-
expect(responseMetaMock).toHaveBeenCalledTimes(0);
|
|
160
|
-
expect(onErrorMock).toHaveBeenCalledTimes(1);
|
|
161
|
-
});
|
|
162
|
-
});
|