Skip to content

Commit 8124221

Browse files
committed
fix: handle unexpected date type reflection
1 parent 28f35cb commit 8124221

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/utils.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@ export function encodeByType(type: string, value: any): string | null {
1919
case 'string': {
2020
return encodeURIComponent(value);
2121
}
22-
default: {
23-
throw new Error(`unknown type in cursor: [${type}]${value}`);
22+
case 'object': {
23+
/**
24+
* if reflection type is Object, check whether an object is a date.
25+
* see: https://github.com/rbuckton/reflect-metadata/issues/84
26+
*/
27+
if (typeof value.getTime === 'function') {
28+
return (value as Date).getTime().toString();
29+
}
30+
31+
break;
2432
}
33+
default: break;
2534
}
35+
36+
throw new Error(`unknown type in cursor: [${type}]${value}`);
2637
}
2738

2839
export function decodeByType(type: string, value: string): string | number | Date {
2940
switch (type) {
41+
case 'object':
3042
case 'date': {
3143
const timestamp = parseInt(value, 10);
3244

test/reflection.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect } from 'chai';
2+
3+
import { encodeByType, decodeByType } from '../src/utils';
4+
5+
describe('Reflect.getMetadata Date type is Object test', () => {
6+
it('should encode cursor correctly with object type and date value', () => {
7+
const date = new Date();
8+
const encoded = encodeByType('object', date);
9+
10+
expect(encoded).to.be.a('string');
11+
});
12+
13+
it('should decode cursor correctly with object type and date string value', () => {
14+
const value = new Date().getTime().toString();
15+
const decoded = decodeByType('object', value);
16+
17+
expect(decoded).to.be.a('date');
18+
});
19+
});

0 commit comments

Comments
 (0)