Skip to content

Commit

Permalink
Added transformers compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
polvallverdu committed Oct 25, 2024
1 parent e6e40b1 commit 6f3ca2f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,25 @@ interface RouteInfo {
}

// ExtractedRouter contains the route map for easy lookup.
type ExtractedRouter = Record<string, RouteInfo>;
type ExtractedRouter = {
routes: Record<string, RouteInfo>;
transformer?: { input: any; output: any };
};
```

```json
{
"some.route": {
"path": "some.route",
"routeType": "query or mutation",
"input": "json schema or null",
"output": "json schema or null"
"routes": {
"some.route": {
"path": "some.route",
"routeType": "query or mutation",
"input": "json schema or null",
"output": "json schema or null"
}
},
"transformer": {
"input": { "json": "{{SLOT}}" },
"output": { "json": "{{SLOT}}" }
}
}
```
18 changes: 16 additions & 2 deletions src/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,27 @@ interface RouteInfo {
}

// ExtractedRouter contains the route map for easy lookup.
type ExtractedRouter = Record<string, RouteInfo>;
type ExtractedRouter = {
routes: Record<string, RouteInfo>;
transformer?: { input: string; output: string };
};

// Function to extract routes from a TRPC router.
export function extractRouter<TRouter extends AnyRouter>(
router: TRouter
): ExtractedRouter {
const extractedRouter: Record<string, RouteInfo> = {}; // The route map to be returned.
let transformer: { input: any; output: any } | undefined;

if (!router._def._config.transformer._default) {
// TODO: Add transformer
const inputSerializer = new router._def._config.transformer.input();
const outputSerializer = new router._def._config.transformer.output();
transformer = {
input: inputSerializer.serialize("{{SLOT}}"),
output: outputSerializer.serialize("{{SLOT}}"),
};
}

// Recursive helper function to traverse routers and extract route information.
function extractRoutes(currentRouter: AnyRouter, prefix: string = "") {
Expand Down Expand Up @@ -69,5 +83,5 @@ export function extractRouter<TRouter extends AnyRouter>(
// Start the extraction from the root router.
extractRoutes(router);

return extractedRouter;
return { routes: extractedRouter, transformer };
}

0 comments on commit 6f3ca2f

Please sign in to comment.