Skip to content

Commit 4f92648

Browse files
committed
Arktype kinda working
1 parent 2631f85 commit 4f92648

File tree

6 files changed

+43
-30
lines changed

6 files changed

+43
-30
lines changed

packages/dev-app/src/pages/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import dynamic from "next/dynamic";
2-
import { parseRouterWithOptions } from "trpc-ui/parse/parseRouter";
2+
// import { parseRouterWithOptions } from "trpc-ui/parse/parseRouter";
33
import { parseTRPCRouter } from "trpc-ui/parseV2/parse";
44
import { RootComponent } from "trpc-ui/react-app/Root";
55
import { trpc } from "trpc-ui/react-app/trpc";
@@ -8,17 +8,17 @@ import { appRouter } from "~/router";
88
// https://jsonforms.io/docs/
99

1010
console.log(`Using superjson: ${env.NEXT_PUBLIC_SUPERJSON}`);
11-
const parse = parseRouterWithOptions(appRouter, {
12-
transformer: env.NEXT_PUBLIC_SUPERJSON === "false" ? undefined : "superjson",
13-
});
11+
// const parse = parseRouterWithOptions(appRouter, {
12+
// transformer: env.NEXT_PUBLIC_SUPERJSON === "false" ? undefined : "superjson",
13+
// });
1414

1515
const parseV2 = parseTRPCRouter(appRouter);
1616

1717
const App = dynamic(
1818
Promise.resolve(() => (
1919
<RootComponent
2020
parsedRouter={parseV2}
21-
rootRouter={parse}
21+
// rootRouter={parse}
2222
options={{
2323
url: "http://localhost:3000/api/trpc",
2424
transformer:
@@ -28,7 +28,7 @@ const App = dynamic(
2828
description: "Hi there cool boi",
2929
},
3030
}}
31-
trpc={trpc}
31+
// trpc={trpc}
3232
/>
3333
)),
3434
{ ssr: false },

packages/dev-app/src/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ const postsRouter = createTRPCRouter({
115115
...input,
116116
};
117117
}),
118-
// createPostArkType: procedure
119-
// .input(type({ age: "string" }))
120-
// .query(({ input }) => {
121-
// return input;
122-
// }),
118+
createPostArkType: arktypeVal
119+
.input(type({ age: "string" }))
120+
.query(({ input }) => {
121+
return input;
122+
}),
123123
// createPost2: secondValidator
124124
// .meta({
125125
// schema:

packages/trpc-ui/src/parse/parseProcedure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function nodeAndInputSchemaFromInputs(
115115
};
116116
}
117117

118-
export function parseProcedure(
118+
function parseProcedure(
119119
procedure: Procedure,
120120
path: string[],
121121
options: TrpcPanelExtraOptions,

packages/trpc-ui/src/parse/parseRouter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type Router, isProcedure, isRouter } from "./routerType";
33
import type { AnyTRPCRouter } from "@trpc/server";
44
import type { zodToJsonSchema } from "zod-to-json-schema";
55
import { logParseError } from "./parseErrorLogs";
6-
import { type ParsedProcedure, parseProcedure } from "./parseProcedure";
6+
import { type ParsedProcedure } from "./parseProcedure";
77

88
// TODO this should be more specific, as it hurts the type safety lower down
99
export type JSON7SchemaType = ReturnType<typeof zodToJsonSchema>;
@@ -42,9 +42,9 @@ function parseRouter(
4242
if (isRouter(child)) {
4343
return parseRouter(child, newPath, options);
4444
}
45-
if (isProcedure(child)) {
46-
return parseProcedure(child, newPath, options);
47-
}
45+
// if (isProcedure(child)) {
46+
// return parseProcedure(child, newPath, options);
47+
// }
4848
return null;
4949
})();
5050
if (!parsedNode) {
@@ -67,7 +67,7 @@ export type TrpcPanelExtraOptions = {
6767
transformer?: "superjson";
6868
};
6969

70-
export function parseRouterWithOptions(
70+
function parseRouterWithOptions(
7171
router: AnyTRPCRouter,
7272
parseRouterOptions: TrpcPanelExtraOptions,
7373
) {

packages/trpc-ui/src/parseV2/parse.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { toJsonSchema } from "@valibot/to-json-schema";
22
import type { Type as ArkTypeValidator } from "arktype";
3-
import { mergeConfigs } from "tailwind-merge";
43
import * as v from "valibot";
54
import { zodToJsonSchema } from "zod-to-json-schema";
65
import * as z3 from "zod/v3";
76
import * as z4 from "zod/v4";
87
import { detectValidatorType } from "./detectValidator";
98
import type { ParsedTRPCRouter, Router } from "./types";
9+
import type { JSONSchema7Object } from "json-schema";
1010

1111
export function parseRootRouter(router: any): Router {
1212
return parseTRPCRouter(router, []) as unknown as Router;
@@ -101,13 +101,15 @@ export function parseTRPCRouter(
101101
}
102102
} else if (validatorType === "arktype") {
103103
console.log("arktype");
104+
console.log(item._def.inputs);
105+
jsonSchema = arkToJson(item._def.inputs);
104106
// const merged = item._def.inputs.reduce(
105107
// (merge: ArkTypeValidator, curr: ArkTypeValidator) => {
106108
// merge.and(curr);
107109
// },
108110
// );
109111
// jsonSchema = merged.toJsonSchema();
110-
jsonSchema = item._def.inputs.toJSONSchema();
112+
// jsonSchema = item._def.inputs.toJSONSchema();
111113
}
112114
}
113115

@@ -147,3 +149,24 @@ export function parseTRPCRouter(
147149

148150
return result;
149151
}
152+
153+
function arkToJson(inputs: ArkTypeValidator[]): JSONSchema7Object {
154+
if (inputs.length === 1) {
155+
return inputs[0]?.toJsonSchema();
156+
}
157+
if (inputs.length > 1) {
158+
const [first, ...rest] = inputs;
159+
return arkRecursive(first, rest);
160+
}
161+
return {};
162+
}
163+
function arkRecursive(
164+
base: ArkTypeValidator,
165+
rest: ArkTypeValidator[],
166+
): JSONSchema7Object {
167+
if (rest.length === 0) {
168+
return base.toJsonSchema();
169+
}
170+
const [first, ...left] = rest;
171+
return arkRecursive(base.and(first), left);
172+
}

packages/trpc-ui/src/react-app/Root.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@ import {
3838
import { Container } from "./v2/Container";
3939

4040
export function RootComponent({
41-
rootRouter,
4241
parsedRouter,
4342
options,
44-
trpc,
4543
}: {
46-
rootRouter: ParsedRouter; // The old one
4744
parsedRouter: ParsedTRPCRouter; //* The new one
4845
options: RenderOptions;
49-
trpc: ReturnType<typeof createTRPCReact>;
5046
}) {
5147
return (
5248
<NuqsAdapter>
@@ -57,11 +53,7 @@ export function RootComponent({
5753
<RenderOptionsProvider options={options} router={parsedRouter}>
5854
<SearchOverlay>
5955
<div className="relative flex h-full w-full flex-1 flex-col">
60-
<AppInnards
61-
rootRouter={rootRouter}
62-
options={options}
63-
parsedRouter={parsedRouter}
64-
/>
56+
<AppInnards options={options} parsedRouter={parsedRouter} />
6557
</div>
6658
</SearchOverlay>
6759
</RenderOptionsProvider>
@@ -74,11 +66,9 @@ export function RootComponent({
7466
}
7567

7668
function AppInnards({
77-
rootRouter,
7869
options,
7970
parsedRouter,
8071
}: {
81-
rootRouter: ParsedRouter;
8272
parsedRouter: ParsedTRPCRouter;
8373
options: RenderOptions;
8474
}) {

0 commit comments

Comments
 (0)