Skip to content

Commit

Permalink
fix(nestjs-trpc): support superjson transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Jan 25, 2025
1 parent 8e921eb commit 528a731
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/nestjs-trpc/lib/generators/generator.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { SchemaImports, TRPCContext } from '../interfaces';
import type { Class } from 'type-fest';
import type { RootConfigTypes } from '@trpc/server/dist/core/internals/config';

export interface GeneratorModuleOptions {
rootModuleFilePath: string;
context?: Class<TRPCContext>;
outputDirPath?: string;
schemaFileImports?: Array<SchemaImports>;
transformer?: RootConfigTypes['transformer'];
}
5 changes: 4 additions & 1 deletion packages/nestjs-trpc/lib/generators/generator.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ export class GeneratorModule implements OnModuleInit {
}

async onModuleInit() {
await this.trpcGenerator.generateSchemaFile(this.options.schemaFileImports);
await this.trpcGenerator.generateSchemaFile(
this.options.schemaFileImports,
this.options.transformer,
);
await this.trpcGenerator.generateHelpersFile(this.options.context);
}
}
22 changes: 20 additions & 2 deletions packages/nestjs-trpc/lib/generators/static.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import {
import { Injectable } from '@nestjs/common';
import { SourceFileImportsMap } from '../interfaces/generator.interface';
import * as path from 'node:path';
import type { RootConfigTypes } from '@trpc/server/dist/core/internals/config';

@Injectable()
export class StaticGenerator {
public generateStaticDeclaration(sourceFile: SourceFile): void {
public generateStaticDeclaration(
sourceFile: SourceFile,
transformer?: RootConfigTypes['transformer'],
): void {
sourceFile.addImportDeclaration({
kind: StructureKind.ImportDeclaration,
moduleSpecifier: '@trpc/server',
Expand All @@ -23,10 +27,24 @@ export class StaticGenerator {
namedImports: ['z'],
});

if (transformer != null)
sourceFile.addImportDeclaration({
kind: StructureKind.ImportDeclaration,
moduleSpecifier: 'superjson',
defaultImport: 'superjson',
});

sourceFile.addVariableStatements([
{
declarationKind: VariableDeclarationKind.Const,
declarations: [{ name: 't', initializer: 'initTRPC.create()' }],
declarations: [
{
name: 't',
initializer: transformer
? 'initTRPC.create({ transformer: superjson })'
: 'initTRPC.create()',
},
],
},
{
declarationKind: VariableDeclarationKind.Const,
Expand Down
7 changes: 6 additions & 1 deletion packages/nestjs-trpc/lib/generators/trpc.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
TYPESCRIPT_PROJECT,
} from './generator.constants';
import * as process from 'node:process';
import type { RootConfigTypes } from '@trpc/server/dist/core/internals/config';

@Injectable()
export class TRPCGenerator implements OnModuleInit {
Expand Down Expand Up @@ -73,6 +74,7 @@ export class TRPCGenerator implements OnModuleInit {

public async generateSchemaFile(
schemaImports?: Array<SchemaImports> | undefined,
transformer?: RootConfigTypes['transformer'],
): Promise<void> {
try {
const routers = this.routerFactory.getRouters();
Expand All @@ -87,7 +89,10 @@ export class TRPCGenerator implements OnModuleInit {
return { name, path, alias, instance: { ...route }, procedures };
});

this.staticGenerator.generateStaticDeclaration(this.appRouterSourceFile);
this.staticGenerator.generateStaticDeclaration(
this.appRouterSourceFile,
transformer,
);

if (schemaImports != null && schemaImports.length > 0) {
const schemaImportNames: Array<string> = schemaImports.map(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RootConfigTypes } from '@trpc/server/dist/core/internals/config';
import type { RootConfigTypes } from '@trpc/server/dist/core/internals/config';
import { ErrorFormatter } from '@trpc/server/dist/error/formatter';
import { TRPCErrorShape } from '@trpc/server/dist/rpc';
import { TRPCContext } from './context.interface';
Expand Down

0 comments on commit 528a731

Please sign in to comment.