Skip to content

Commit

Permalink
feat: added nestJsTimestampTypeWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipMantrov committed May 17, 2022
1 parent 3541b50 commit 5d14d97
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,43 @@ export type Utils = ReturnType<typeof makeDeepPartial> &
export function makeUtils(options: Options): Utils {
const bytes = makeByteUtils();
const longs = makeLongUtils(options, bytes);
const wrappers = makeNestJsWrappers(options);
return {
...bytes,
...makeDeepPartial(options, longs),
...makeObjectIdMethods(options),
...makeTimestampMethods(options, longs),
...longs,
...wrappers,
...makeComparisonUtils(),
...makeNiceGrpcServerStreamingMethodResult(),
};
}

function makeNestJsWrappers(options: Options) {
const wrappers = imp('wrappers@protobufjs/minimal');

const nestJsTimestampTypeWrapper =
options.nestJs && options.useDate === DateOption.DATE
? code`
if (${wrappers}) {
${wrappers}['.google.protobuf.Timestamp'] = {
fromObject(value: Date) {
return {
seconds: value.getTime() / 1000,
nanos: (value.getTime() % 1000) * 1e6,
};
},
toObject(message: { seconds: number; nanos: number }) {
return new Date(message.seconds * 1000 + message.nanos / 1e6);
},
} as any;
}`
: code``;

return { nestJsTimestampTypeWrapper };
}

function makeLongUtils(options: Options, bytes: ReturnType<typeof makeByteUtils>) {
// Regardless of which `forceLong` config option we're using, we always use
// the `long` library to either represent or at least sanity-check 64-bit values
Expand Down

0 comments on commit 5d14d97

Please sign in to comment.