Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Parameter to Configure Custom Codec #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/nestjs-nats-jetstream-transport/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ import { NatsRequestRecord } from './utils/nats-request-record-builder';
@Injectable()
export class NatsJetStreamClientProxy extends ClientProxy {
private nc: NatsConnection;
private codec: Codec<JSON>;
private codec: Codec<unknown>;

constructor(
@Inject(NATS_JETSTREAM_OPTIONS) private options: NatsJetStreamClientOptions,
) {
super();
this.codec = JSONCodec();

// Use provided codec if exists, otherwise use JSONCodec as default
if (options.connectionOptions.codec) {
this.codec = options.connectionOptions.codec;
} else {
this.codec = JSONCodec();
}
}

async connect(): Promise<NatsConnection> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ConnectionOptions, NatsConnection } from 'nats';
import { Codec, ConnectionOptions, NatsConnection } from 'nats';

export interface NatsConnectionOptions extends ConnectionOptions {
name: string;
codec?: Codec<unknown>;
connectedHook?: (nc: NatsConnection) => void;
}
10 changes: 8 additions & 2 deletions packages/nestjs-nats-jetstream-transport/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ export class NatsJetStreamServer
{
readonly transportId: symbol = NATS_JETSTREAM_TRANSPORT;
private nc: NatsConnection;
private codec: Codec<JSON>;
private codec: Codec<unknown>;
private jsm: JetStreamManager;

constructor(private options: NatsJetStreamServerOptions) {
super();
this.codec = JSONCodec();

// Use provided codec if exists, otherwise use JSONCodec as default
if (options.connectionOptions.codec) {
this.codec = options.connectionOptions.codec;
} else {
this.codec = JSONCodec();
}
}

async listen(callback: () => void) {
Expand Down