@trpc-panel/core 1.0.0
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/README.md +173 -0
- package/lib/index.js +431 -0
- package/lib/index.mjs +427 -0
- package/lib/react-app/bundle.js +291 -0
- package/lib/react-app/bundle.js.map +1 -0
- package/lib/react-app/index.css +1076 -0
- package/lib/react-app/index.html +16 -0
- package/lib/src/index.d.ts +3 -0
- package/lib/src/meta.d.ts +9 -0
- package/lib/src/parse/__tests__/parseProcedure.test.d.ts +1 -0
- package/lib/src/parse/__tests__/parseRouter.test.d.ts +1 -0
- package/lib/src/parse/__tests__/utils/router.d.ts +159 -0
- package/lib/src/parse/__tests__/utils/schemas.d.ts +7 -0
- package/lib/src/parse/input-mappers/__tests__/zod/array.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/bigint.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/boolean.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/branded.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/default.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/discriminatedUnion.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/effects.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/enum.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/literal.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/null.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/nullable.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/number.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/object.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/optional.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/promise.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/string.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/undefined.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/__tests__/zod/void.test.d.ts +1 -0
- package/lib/src/parse/input-mappers/defaultReferences.d.ts +2 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodArrayDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodBigIntDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodBooleanFieldDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodBrandedDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodDefaultDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodDiscriminatedUnionDef.d.ts +16 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodEffectsDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodEnumDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodLiteralDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodNullDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodNullableDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodNumberDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodObjectDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodOptionalDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodPromiseDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodStringDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodUndefinedDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/parsers/parseZodVoidDef.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/selector.d.ts +3 -0
- package/lib/src/parse/input-mappers/zod/zod-types.d.ts +4 -0
- package/lib/src/parse/parseErrorLogs.d.ts +1 -0
- package/lib/src/parse/parseNodeTypes.d.ts +61 -0
- package/lib/src/parse/parseProcedure.d.ts +18 -0
- package/lib/src/parse/parseRouter.d.ts +21 -0
- package/lib/src/parse/routerType.d.ts +486 -0
- package/lib/src/parse/utils.d.ts +2 -0
- package/lib/src/render.d.ts +7 -0
- package/package.json +100 -0
package/README.md
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# tRPC.panel()
|
|
2
|
+
|
|
3
|
+
Probably the easiest and cheapest way to build a testing UI and documentation for your tRPC endpoints. tRPC panel automatically generates a UI for manually testing your tRPC backend with 0 overhead:
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
trpc panel moves as fast as your trpc backend with minimal effort.
|
|
8
|
+
|
|
9
|
+
Check out our [test app](https://app.trpcpanel.io)
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 🚀 Automatically inspect your tRPC router and recursively generate a typesafe UI
|
|
14
|
+
- 🕒 Zero overhead
|
|
15
|
+
- No output schemas (procedure return types can be inferred as nature intended)
|
|
16
|
+
- New procedures will be added to your UI as you create them in your backend
|
|
17
|
+
- No compilation required, works with any backend
|
|
18
|
+
- 📄 [Document](#documenting-procedures) your procedures and input parameters with minimal effort
|
|
19
|
+
- 🐦 Supports nested routers, and nested input objects. The structure of the UI maps one-to-one to your API's routers and procedures.
|
|
20
|
+
- 🧭 SideNav and VSCode-like procedure / router search to quickly find what you're looking for
|
|
21
|
+
- ✨ [Transform](#data-transformers) data with built in `superjson` support.
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
Install with your preferred package manager:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
yarn add trpc-panel
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
render your panel and return it from your backend (express example):
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import { renderTrpcPanel } from "trpc-panel";
|
|
35
|
+
// ...
|
|
36
|
+
app.use("/panel", (_, res) => {
|
|
37
|
+
return res.send(
|
|
38
|
+
renderTrpcPanel(myTrpcRouter, { url: "http://localhost:4000/trpc" })
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`trpc-panel` just renders as a string, so it can be used with any backend.
|
|
44
|
+
|
|
45
|
+
## NextJS / create-t3-app example
|
|
46
|
+
|
|
47
|
+
In Nextjs you'd want to create an api route somewhere like `src/pages/api/panel.ts` and send a text response:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import type { NextApiRequest, NextApiResponse } from "next";
|
|
51
|
+
import { renderTrpcPanel } from "trpc-panel";
|
|
52
|
+
import { appRouter } from "../../server/api/root";
|
|
53
|
+
|
|
54
|
+
export default async function handler(_: NextApiRequest, res: NextApiResponse) {
|
|
55
|
+
res.status(200).send(
|
|
56
|
+
renderTrpcPanel(appRouter, {
|
|
57
|
+
url: "http://localhost:3000/api/trpc",
|
|
58
|
+
transformer: "superjson",
|
|
59
|
+
})
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Then we can visit the url `http://localhost:3000/api/panel` to use the panel. Here we do `transformer: "superjson"` assuming we have `superjson` set as the transformer in tRPC (which create-t3-app does by default).
|
|
65
|
+
|
|
66
|
+
## Documenting Procedures
|
|
67
|
+
|
|
68
|
+
As of v1.1.0, `trpc-panel` supports documenting procedures.
|
|
69
|
+
|
|
70
|
+
Documentation is opt-in, meaning you only need to set it up if you want to use it. When docs are included for your trpc procedure, a "Docs" section will appear in your procedure:
|
|
71
|
+
|
|
72
|
+

|
|
73
|
+
|
|
74
|
+
### Procedure Descriptions
|
|
75
|
+
|
|
76
|
+
`trpc-panel` supports documenting procedures via trpc meta. First setup your trpc instance to be typed with `TRPCPanelMeta`:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import { initTRPC } from "@trpc/server";
|
|
80
|
+
import { TRPCPanelMeta } from "trpc-panel";
|
|
81
|
+
|
|
82
|
+
const t = initTRPC.meta<TRPCPanelMeta>().create();
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Then in your routers you can provide a description to the meta:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
export const appRouter = t.router({
|
|
89
|
+
sayHello: t.procedure
|
|
90
|
+
.meta({ /* 👉 */ description: "This shows in the panel." })
|
|
91
|
+
.input(z.object({ name: z.string() }))
|
|
92
|
+
.query(({ input }) => {
|
|
93
|
+
return { greeting: `Hello ${input.name}!` };
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Input Parameter Descriptions
|
|
99
|
+
|
|
100
|
+
`trpc-panel` supports documenting parameters via zod's `.describe()` method. This allows developers to quickly write documentation as they're writing schemas:
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
export const appRouter = t.router({
|
|
104
|
+
sayHello: t.procedure
|
|
105
|
+
.input(z.object({
|
|
106
|
+
name: z.string().describe("The name to say hello too.")
|
|
107
|
+
}))
|
|
108
|
+
.query(({ input }) => {
|
|
109
|
+
return { greeting: `Hello ${input.name}!` };
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Whatever you pass to `describe()` will appear in the docs section. Any input fields without a description will not appear in the docs section.
|
|
115
|
+
|
|
116
|
+
## Data Transformers
|
|
117
|
+
|
|
118
|
+
Trpc panel supports `superjson`, just pass it into the transformer option:
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
app.use("/panel", (_, res) => {
|
|
122
|
+
return res.send(
|
|
123
|
+
renderTrpcPanel(myTrpcRouter, {
|
|
124
|
+
url: "http://localhost:4000/trpc",
|
|
125
|
+
transformer: "superjson",
|
|
126
|
+
})
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Contributing
|
|
132
|
+
|
|
133
|
+
`trpc-panel` welcomes and encourages open source contributions. Please see our [contributing](./CONTRIBUTING.md) guide for information on how to develop locally.
|
|
134
|
+
|
|
135
|
+
## Limitations
|
|
136
|
+
|
|
137
|
+
Currently, tRPC panel supports tRPC v10 and v11 and only works with zod input schemas. With it's current design it would be feasible to easily add support for other input types as well
|
|
138
|
+
|
|
139
|
+
There are no plans to support v9 or other previous tRPC versions.
|
|
140
|
+
|
|
141
|
+
### Supported zod types
|
|
142
|
+
|
|
143
|
+
The following are supported
|
|
144
|
+
|
|
145
|
+
- Array
|
|
146
|
+
- BigInt
|
|
147
|
+
- Boolean
|
|
148
|
+
- Branded
|
|
149
|
+
- Default
|
|
150
|
+
- DiscriminatedUnion
|
|
151
|
+
- Effects
|
|
152
|
+
- Enum
|
|
153
|
+
- Literal
|
|
154
|
+
- Nullable
|
|
155
|
+
- Null
|
|
156
|
+
- Nullish
|
|
157
|
+
- Number
|
|
158
|
+
- Object
|
|
159
|
+
- Optional
|
|
160
|
+
- Promise
|
|
161
|
+
- String
|
|
162
|
+
- Undefined
|
|
163
|
+
|
|
164
|
+
We would like to add the following types:
|
|
165
|
+
|
|
166
|
+
- Union
|
|
167
|
+
- Tuple
|
|
168
|
+
- Record
|
|
169
|
+
- Never
|
|
170
|
+
- Map (superjson only)
|
|
171
|
+
- Set (superjson only)
|
|
172
|
+
- Date (superjson only)
|
|
173
|
+
- Any
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var node_url = require('node:url');
|
|
5
|
+
var node_path = require('node:path');
|
|
6
|
+
var zod = require('zod');
|
|
7
|
+
var zodToJsonSchema = require('zod-to-json-schema');
|
|
8
|
+
|
|
9
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
10
|
+
const TRPCPanelMetaSchema = zod.z.object({
|
|
11
|
+
description: zod.z.string().optional(),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
zod.z.object({});
|
|
15
|
+
const SharedProcedureDefPropertiesSchema = zod.z.object({
|
|
16
|
+
inputs: zod.z.unknown().array().optional(),
|
|
17
|
+
input: zod.z.unknown().optional(),
|
|
18
|
+
meta: TRPCPanelMetaSchema.optional(),
|
|
19
|
+
});
|
|
20
|
+
const QueryDefSchema = SharedProcedureDefPropertiesSchema.and(zod.z.union([
|
|
21
|
+
zod.z.object({
|
|
22
|
+
query: zod.z.literal(true),
|
|
23
|
+
}),
|
|
24
|
+
zod.z.object({
|
|
25
|
+
type: zod.z.literal("query"),
|
|
26
|
+
}),
|
|
27
|
+
]));
|
|
28
|
+
function isQueryDef(obj) {
|
|
29
|
+
return QueryDefSchema.safeParse(obj).success;
|
|
30
|
+
}
|
|
31
|
+
const MutationDefSchema = SharedProcedureDefPropertiesSchema.and(zod.z.union([
|
|
32
|
+
zod.z.object({
|
|
33
|
+
mutation: zod.z.literal(true),
|
|
34
|
+
}),
|
|
35
|
+
zod.z.object({
|
|
36
|
+
type: zod.z.literal("mutation"),
|
|
37
|
+
}),
|
|
38
|
+
]));
|
|
39
|
+
function isMutationDef(obj) {
|
|
40
|
+
return MutationDefSchema.safeParse(obj).success;
|
|
41
|
+
}
|
|
42
|
+
const SubscriptionDefSchema = SharedProcedureDefPropertiesSchema.and(zod.z.union([
|
|
43
|
+
zod.z.object({
|
|
44
|
+
subscription: zod.z.literal(true),
|
|
45
|
+
}),
|
|
46
|
+
zod.z.object({
|
|
47
|
+
type: zod.z.literal("subscription"),
|
|
48
|
+
}),
|
|
49
|
+
]));
|
|
50
|
+
function isSubscriptionDef(obj) {
|
|
51
|
+
return SubscriptionDefSchema.safeParse(obj).success;
|
|
52
|
+
}
|
|
53
|
+
const ProcedureDefSchema = QueryDefSchema.or(MutationDefSchema).or(SubscriptionDefSchema);
|
|
54
|
+
const RouterDefSchema = zod.z.object({
|
|
55
|
+
router: zod.z.literal(true).optional(),
|
|
56
|
+
type: zod.z.undefined(),
|
|
57
|
+
query: zod.z.undefined(),
|
|
58
|
+
mutation: zod.z.undefined(),
|
|
59
|
+
subscription: zod.z.undefined(),
|
|
60
|
+
inputs: zod.z.undefined(),
|
|
61
|
+
input: zod.z.undefined(),
|
|
62
|
+
});
|
|
63
|
+
const RouterSchema = zod.z.object({
|
|
64
|
+
_def: RouterDefSchema,
|
|
65
|
+
});
|
|
66
|
+
function isRouter(obj) {
|
|
67
|
+
return RouterSchema.safeParse(obj).success;
|
|
68
|
+
}
|
|
69
|
+
zod.z.object({
|
|
70
|
+
_def: ProcedureDefSchema,
|
|
71
|
+
});
|
|
72
|
+
function isProcedure(obj) {
|
|
73
|
+
if (typeof obj !== "function" || !("_def" in obj))
|
|
74
|
+
return false;
|
|
75
|
+
return ProcedureDefSchema.safeParse(obj._def).success;
|
|
76
|
+
}
|
|
77
|
+
zod.z.object({
|
|
78
|
+
_def: QueryDefSchema,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
function logParseError(procedurePath, error) {
|
|
82
|
+
console.warn(`trpc-panel: Failed to parse procedure ${procedurePath}, ${error}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function nodePropertiesFromRef(references) {
|
|
86
|
+
return Object.assign({ path: references.path }, (references.optional && { optional: true }));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const parseZodStringDef = (def, refs) => {
|
|
90
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
91
|
+
return Object.assign({ type: "string" }, nodePropertiesFromRef(refs));
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const parseZodArrayDef = (def, refs) => {
|
|
95
|
+
const { type } = def;
|
|
96
|
+
const childType = zodSelectorFunction(type._def, Object.assign(Object.assign({}, refs), { path: [] }));
|
|
97
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
98
|
+
return Object.assign({ type: "array", childType }, nodePropertiesFromRef(refs));
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const parseZodBooleanFieldDef = (def, refs) => {
|
|
102
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
103
|
+
return Object.assign({ type: "boolean" }, nodePropertiesFromRef(refs));
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
function isZodThreePointTwenty(def) {
|
|
107
|
+
return "optionsMap" in def;
|
|
108
|
+
}
|
|
109
|
+
function makeDefConsistent(def) {
|
|
110
|
+
const optionsMap = isZodThreePointTwenty(def) ? def.optionsMap : def.options;
|
|
111
|
+
return {
|
|
112
|
+
typeName: zod.ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
113
|
+
discriminator: def.discriminator,
|
|
114
|
+
options: optionsMap,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
const parseZodDiscriminatedUnionDef = (def, refs) => {
|
|
118
|
+
const defConsistent = makeDefConsistent(def);
|
|
119
|
+
const entries = Array.from(defConsistent.options.entries());
|
|
120
|
+
const nodeEntries = entries.map(([discriminatorValue, zodObj]) => [
|
|
121
|
+
discriminatorValue,
|
|
122
|
+
zodSelectorFunction(zodObj._def, refs),
|
|
123
|
+
]);
|
|
124
|
+
const nodesMap = Object.fromEntries(nodeEntries);
|
|
125
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
126
|
+
return Object.assign({ type: "discriminated-union", discriminatedUnionValues: entries.map(([n]) => n), discriminatedUnionChildrenMap: nodesMap, discriminatorName: def.discriminator }, nodePropertiesFromRef(refs));
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const parseZodEnumDef = (def, refs) => {
|
|
130
|
+
const values = def.values;
|
|
131
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
132
|
+
return Object.assign({ type: "enum", enumValues: values }, nodePropertiesFromRef(refs));
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const parseZodLiteralDef = (def, refs) => {
|
|
136
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
137
|
+
return Object.assign({ type: "literal", value: def.value }, nodePropertiesFromRef(refs));
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const parseZodNumberDef = (def, refs) => {
|
|
141
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
142
|
+
return Object.assign({ type: "number" }, nodePropertiesFromRef(refs));
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const parseZodObjectDef = (def, refs) => {
|
|
146
|
+
const shape = def.shape();
|
|
147
|
+
const children = {};
|
|
148
|
+
for (var propertyName of Object.keys(shape)) {
|
|
149
|
+
const node = zodSelectorFunction(shape[propertyName]._def, Object.assign(Object.assign({}, refs), { path: refs.path.concat([propertyName]) }));
|
|
150
|
+
children[propertyName] = node;
|
|
151
|
+
}
|
|
152
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
153
|
+
return Object.assign({ type: "object", children }, nodePropertiesFromRef(refs));
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const parseZodOptionalDef = (def, refs) => {
|
|
157
|
+
const parsedInner = zodSelectorFunction(def.innerType._def, refs);
|
|
158
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
159
|
+
return Object.assign(Object.assign({}, parsedInner), { optional: true });
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
function parseZodNullableDef(def, refs) {
|
|
163
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
164
|
+
return zodSelectorFunction(def.innerType._def, refs);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function parseZodBigIntDef(def, refs) {
|
|
168
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
169
|
+
return Object.assign({ type: "number" }, nodePropertiesFromRef(refs));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function parseZodBrandedDef(def, refs) {
|
|
173
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
174
|
+
return zodSelectorFunction(def.type._def, refs);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function parseZodDefaultDef(def, refs) {
|
|
178
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
179
|
+
return zodSelectorFunction(def.innerType._def, refs);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function parseZodEffectsDef(def, refs) {
|
|
183
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
184
|
+
return zodSelectorFunction(def.schema._def, refs);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function parseZodNullDef(def, refs) {
|
|
188
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
189
|
+
return Object.assign({ type: "literal", value: null }, nodePropertiesFromRef(refs));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function parseZodPromiseDef(def, refs) {
|
|
193
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
194
|
+
return zodSelectorFunction(def.type._def, refs);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function parseZodUndefinedDef(def, refs) {
|
|
198
|
+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
|
|
199
|
+
return Object.assign({ type: "literal", value: undefined }, nodePropertiesFromRef(refs));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function parseZodVoidDef(_, refs) {
|
|
203
|
+
return {
|
|
204
|
+
type: "literal",
|
|
205
|
+
value: undefined,
|
|
206
|
+
path: refs.path,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const zodSelectorFunction = (def, references) => {
|
|
211
|
+
switch (def.typeName) {
|
|
212
|
+
case zod.ZodFirstPartyTypeKind.ZodArray:
|
|
213
|
+
return parseZodArrayDef(def, references);
|
|
214
|
+
case zod.ZodFirstPartyTypeKind.ZodBoolean:
|
|
215
|
+
return parseZodBooleanFieldDef(def, references);
|
|
216
|
+
case zod.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
|
|
217
|
+
return parseZodDiscriminatedUnionDef(def, references);
|
|
218
|
+
case zod.ZodFirstPartyTypeKind.ZodEnum:
|
|
219
|
+
return parseZodEnumDef(def, references);
|
|
220
|
+
case zod.ZodFirstPartyTypeKind.ZodLiteral:
|
|
221
|
+
return parseZodLiteralDef(def, references);
|
|
222
|
+
case zod.ZodFirstPartyTypeKind.ZodNumber:
|
|
223
|
+
return parseZodNumberDef(def, references);
|
|
224
|
+
case zod.ZodFirstPartyTypeKind.ZodObject:
|
|
225
|
+
return parseZodObjectDef(def, references);
|
|
226
|
+
case zod.ZodFirstPartyTypeKind.ZodOptional:
|
|
227
|
+
return parseZodOptionalDef(def, references);
|
|
228
|
+
case zod.ZodFirstPartyTypeKind.ZodString:
|
|
229
|
+
return parseZodStringDef(def, references);
|
|
230
|
+
case zod.ZodFirstPartyTypeKind.ZodNullable:
|
|
231
|
+
return parseZodNullableDef(def, references);
|
|
232
|
+
case zod.ZodFirstPartyTypeKind.ZodBigInt:
|
|
233
|
+
return parseZodBigIntDef(def, references);
|
|
234
|
+
case zod.ZodFirstPartyTypeKind.ZodBranded:
|
|
235
|
+
return parseZodBrandedDef(def, references);
|
|
236
|
+
case zod.ZodFirstPartyTypeKind.ZodDefault:
|
|
237
|
+
return parseZodDefaultDef(def, references);
|
|
238
|
+
case zod.ZodFirstPartyTypeKind.ZodEffects:
|
|
239
|
+
return parseZodEffectsDef(def, references);
|
|
240
|
+
case zod.ZodFirstPartyTypeKind.ZodNull:
|
|
241
|
+
return parseZodNullDef(def, references);
|
|
242
|
+
case zod.ZodFirstPartyTypeKind.ZodPromise:
|
|
243
|
+
return parseZodPromiseDef(def, references);
|
|
244
|
+
case zod.ZodFirstPartyTypeKind.ZodUndefined:
|
|
245
|
+
return parseZodUndefinedDef(def, references);
|
|
246
|
+
case zod.ZodFirstPartyTypeKind.ZodVoid:
|
|
247
|
+
return parseZodVoidDef(def, references);
|
|
248
|
+
}
|
|
249
|
+
return { type: "unsupported", path: references.path };
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const inputParserMap = {
|
|
253
|
+
zod: (zodObject, refs) => {
|
|
254
|
+
return zodSelectorFunction(zodObject._def, refs);
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
const emptyZodObject = zod.z.object({});
|
|
258
|
+
function nodeAndInputSchemaFromInputs(inputs, _routerPath, options, addDataFunctions) {
|
|
259
|
+
if (!inputs.length) {
|
|
260
|
+
return {
|
|
261
|
+
parseInputResult: "success",
|
|
262
|
+
schema: zodToJsonSchema.zodToJsonSchema(emptyZodObject, {
|
|
263
|
+
errorMessages: true,
|
|
264
|
+
$refStrategy: "none",
|
|
265
|
+
target: "jsonSchema7"
|
|
266
|
+
}),
|
|
267
|
+
node: inputParserMap["zod"](emptyZodObject, {
|
|
268
|
+
path: [],
|
|
269
|
+
options,
|
|
270
|
+
addDataFunctions,
|
|
271
|
+
}),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
if (inputs.length !== 1) {
|
|
275
|
+
return { parseInputResult: "failure" };
|
|
276
|
+
}
|
|
277
|
+
const input = inputs[0];
|
|
278
|
+
return {
|
|
279
|
+
parseInputResult: "success",
|
|
280
|
+
schema: zodToJsonSchema.zodToJsonSchema(input, {
|
|
281
|
+
errorMessages: true,
|
|
282
|
+
$refStrategy: "none",
|
|
283
|
+
target: "jsonSchema7"
|
|
284
|
+
}),
|
|
285
|
+
node: zodSelectorFunction(input._def, {
|
|
286
|
+
path: [],
|
|
287
|
+
options,
|
|
288
|
+
addDataFunctions,
|
|
289
|
+
}),
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function parseProcedure(procedure, path, options) {
|
|
293
|
+
var _a, _b;
|
|
294
|
+
const { _def } = procedure;
|
|
295
|
+
const inputs = (_a = _def.inputs) !== null && _a !== void 0 ? _a : (_def.input ? [_def.input] : []);
|
|
296
|
+
const parseExtraData = {
|
|
297
|
+
parameterDescriptions: {},
|
|
298
|
+
};
|
|
299
|
+
const nodeAndInput = nodeAndInputSchemaFromInputs(inputs, path, options, {
|
|
300
|
+
addDescriptionIfExists: (def, refs) => {
|
|
301
|
+
if (def.description) {
|
|
302
|
+
parseExtraData.parameterDescriptions[refs.path.join(".")] =
|
|
303
|
+
def.description;
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
});
|
|
307
|
+
if (nodeAndInput.parseInputResult === "failure") {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
const t = (() => {
|
|
311
|
+
if (isQueryDef(_def))
|
|
312
|
+
return "query";
|
|
313
|
+
if (isMutationDef(_def))
|
|
314
|
+
return "mutation";
|
|
315
|
+
if (isSubscriptionDef(_def))
|
|
316
|
+
return "subscription";
|
|
317
|
+
return null;
|
|
318
|
+
})();
|
|
319
|
+
if (!t) {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
inputSchema: nodeAndInput.schema,
|
|
324
|
+
node: nodeAndInput.node,
|
|
325
|
+
nodeType: "procedure",
|
|
326
|
+
procedureType: t,
|
|
327
|
+
pathFromRootRouter: path,
|
|
328
|
+
extraData: Object.assign(Object.assign({}, parseExtraData), (((_b = procedure._def.meta) === null || _b === void 0 ? void 0 : _b.description) && {
|
|
329
|
+
description: procedure._def.meta.description,
|
|
330
|
+
})),
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const skipSet = new Set(["createCaller", "_def", "getErrorShape"]);
|
|
335
|
+
function parseRouter(router, routerPath, options) {
|
|
336
|
+
const children = {};
|
|
337
|
+
var hasChild = false;
|
|
338
|
+
for (var [procedureOrRouterPath, child] of Object.entries(router)) {
|
|
339
|
+
if (skipSet.has(procedureOrRouterPath))
|
|
340
|
+
continue;
|
|
341
|
+
const newPath = routerPath.concat([procedureOrRouterPath]);
|
|
342
|
+
const parsedNode = (() => {
|
|
343
|
+
if (isRouter(child)) {
|
|
344
|
+
return parseRouter(child, newPath, options);
|
|
345
|
+
}
|
|
346
|
+
if (isProcedure(child)) {
|
|
347
|
+
return parseProcedure(child, newPath, options);
|
|
348
|
+
}
|
|
349
|
+
return null;
|
|
350
|
+
})();
|
|
351
|
+
if (!parsedNode) {
|
|
352
|
+
logParseError(newPath.join("."), "Couldn't parse node.");
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
hasChild = true;
|
|
356
|
+
children[procedureOrRouterPath] = parsedNode;
|
|
357
|
+
}
|
|
358
|
+
if (!hasChild)
|
|
359
|
+
logParseError(routerPath.join("."), `Router doesn't have any successfully parsed children.`);
|
|
360
|
+
return { children, nodeType: "router", path: routerPath };
|
|
361
|
+
}
|
|
362
|
+
function parseRouterWithOptions(router, parseRouterOptions) {
|
|
363
|
+
if (!isRouter(router)) {
|
|
364
|
+
throw new Error("Non trpc router passed to trpc panel.");
|
|
365
|
+
}
|
|
366
|
+
return parseRouter(router, [], parseRouterOptions);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const defaultParseRouterOptions = {
|
|
370
|
+
logFailedProcedureParse: true,
|
|
371
|
+
transformer: "superjson",
|
|
372
|
+
};
|
|
373
|
+
const __dirname$1 = node_path.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href))));
|
|
374
|
+
const javascriptReplaceSymbol = "{{js}}";
|
|
375
|
+
const cssReplaceSymbol = "{{css}}";
|
|
376
|
+
const routerReplaceSymbol = '"{{parsed_router}}"';
|
|
377
|
+
const optionsReplaceSymbol = '"{{options}}"';
|
|
378
|
+
const bundlePath = __dirname$1 + "/react-app/bundle.js";
|
|
379
|
+
const indexPath = __dirname$1 + "/react-app/index.html";
|
|
380
|
+
const cssPath = __dirname$1 + "/react-app/index.css";
|
|
381
|
+
const bundleJs = fs.readFileSync(bundlePath).toString();
|
|
382
|
+
const indexHtml = fs.readFileSync(indexPath).toString();
|
|
383
|
+
const indexCss = fs.readFileSync(cssPath).toString();
|
|
384
|
+
function injectParams(string, injectionParams) {
|
|
385
|
+
var r = string;
|
|
386
|
+
for (var param of injectionParams) {
|
|
387
|
+
r = injectInString(param.searchFor, r, param.injectString);
|
|
388
|
+
}
|
|
389
|
+
return r;
|
|
390
|
+
}
|
|
391
|
+
function injectInString(searchFor, string, injectString) {
|
|
392
|
+
const startIndex = string.indexOf(searchFor);
|
|
393
|
+
return (string.slice(0, startIndex) +
|
|
394
|
+
injectString +
|
|
395
|
+
string.slice(startIndex + searchFor.length));
|
|
396
|
+
}
|
|
397
|
+
let cache = {
|
|
398
|
+
val: null,
|
|
399
|
+
};
|
|
400
|
+
function renderTrpcPanel(router, options) {
|
|
401
|
+
if (options.cache === true && cache.val)
|
|
402
|
+
return cache.val;
|
|
403
|
+
const bundleInjectionParams = [
|
|
404
|
+
{
|
|
405
|
+
searchFor: routerReplaceSymbol,
|
|
406
|
+
injectString: JSON.stringify(parseRouterWithOptions(router, Object.assign(Object.assign({}, defaultParseRouterOptions), options))),
|
|
407
|
+
},
|
|
408
|
+
{
|
|
409
|
+
searchFor: optionsReplaceSymbol,
|
|
410
|
+
injectString: JSON.stringify(options),
|
|
411
|
+
},
|
|
412
|
+
];
|
|
413
|
+
const bundleInjected = injectParams(bundleJs, bundleInjectionParams);
|
|
414
|
+
const script = `<script>${bundleInjected}</script>`;
|
|
415
|
+
const css = `<style>${indexCss}</style>`;
|
|
416
|
+
const htmlReplaceParams = [
|
|
417
|
+
{
|
|
418
|
+
searchFor: javascriptReplaceSymbol,
|
|
419
|
+
injectString: script,
|
|
420
|
+
},
|
|
421
|
+
{
|
|
422
|
+
searchFor: cssReplaceSymbol,
|
|
423
|
+
injectString: css,
|
|
424
|
+
},
|
|
425
|
+
];
|
|
426
|
+
cache.val = injectParams(indexHtml, htmlReplaceParams);
|
|
427
|
+
return cache.val;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
exports.parseRouterWithOptions = parseRouterWithOptions;
|
|
431
|
+
exports.renderTrpcPanel = renderTrpcPanel;
|