Passing a object with readonly attributes through tRPC

I have a class (provided by a library)
import { Asset, GameMode, IMatch, MapName, Participant, PlatformRegion, PubgAPI, Roster, SeasonState, Telemetry } from '..';

export declare class Match {
private _id;
private _dateCreated;
private _duration;
private _gameMode;
private _isCustomMatch;
private _map;
private _patchVersion;
private _seasonState;
private _shardId;
private _participants;
private _rosters;
private _asset?;
private constructor();
static get(api: PubgAPI, matchId: string): Promise<Match>;
static fromDetail(matchDetail: IMatch): Match;

readonly id: string;
readonly dateCreated: Date;
readonly duration: number;
readonly gameMode: GameMode;
readonly isCustomMatch: boolean;
readonly map: MapName;
readonly patchVersion: string | undefined;
readonly seasonState: SeasonState;
readonly shardId: PlatformRegion;
readonly asset: Asset;
readonly participants: Participant[];
readonly rosters: Roster[];

getParticipantById(id: string): Participant | undefined;
getParticipantByName(name: string): Participant | undefined;

getWinners(): Participant[];
getTelemetry(api: PubgAPI): Promise<Telemetry>;
}
import { Asset, GameMode, IMatch, MapName, Participant, PlatformRegion, PubgAPI, Roster, SeasonState, Telemetry } from '..';

export declare class Match {
private _id;
private _dateCreated;
private _duration;
private _gameMode;
private _isCustomMatch;
private _map;
private _patchVersion;
private _seasonState;
private _shardId;
private _participants;
private _rosters;
private _asset?;
private constructor();
static get(api: PubgAPI, matchId: string): Promise<Match>;
static fromDetail(matchDetail: IMatch): Match;

readonly id: string;
readonly dateCreated: Date;
readonly duration: number;
readonly gameMode: GameMode;
readonly isCustomMatch: boolean;
readonly map: MapName;
readonly patchVersion: string | undefined;
readonly seasonState: SeasonState;
readonly shardId: PlatformRegion;
readonly asset: Asset;
readonly participants: Participant[];
readonly rosters: Roster[];

getParticipantById(id: string): Participant | undefined;
getParticipantByName(name: string): Participant | undefined;

getWinners(): Participant[];
getTelemetry(api: PubgAPI): Promise<Telemetry>;
}
I want to send this / the data of the readonly attributes to the client, but either while sending it directly or while destructing the object everything get's prefixed with and underscore (because it is private and readonly I think ?). The problem is if I try to access this data on the client Typescript will throw an error because the attribute is "private". How can I clone this data and or send it so that I can access it on the client ?
4 Replies
cje
cje2y ago
https://www.jsonrpc.org/specification you can generally only transmit json with a transformer like superjson you can also transmit some other stuff like dates
Tilo
Tilo2y ago
So do I have to manually type out something like ?:
{
id: match.id,
duration: match.duration,
(etc.)
}
{
id: match.id,
duration: match.duration,
(etc.)
}
And then send this object
cje
cje2y ago
id probably stick a method on the object that gives you something you can send if you're already doing oop anyway
Tilo
Tilo2y ago
The object is created by a library so nothing much I can do there, I'll probably write some helper functions so I can create something send able, thanks for the help though.
Want results from more Discord servers?
Add your server