Skip to content

Commit

Permalink
Merge branch 'main' into fix-62-again
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Feb 3, 2021
2 parents a7df01d + e97fd00 commit 17fb53a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 11 deletions.
18 changes: 18 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@
"code",
"ideas"
]
},
{
"login": "samtgarson",
"name": "Sam Garson",
"avatar_url": "https://avatars.githubusercontent.com/u/6242344?v=4",
"profile": "https://www.samgarson.com",
"contributions": [
"bug"
]
},
{
"login": "markhughes",
"name": "Mark Hughes",
"avatar_url": "https://avatars.githubusercontent.com/u/1357323?v=4",
"profile": "http://twitter.com/_markeh",
"contributions": [
"bug"
]
}
],
"badgeTemplate": "<a href=\"#contributors\"><img src=\"https://img.shields.io/badge/all_contributors-<%= contributors.length %>-orange.svg?style=flat-square\" alt=\"All Contributors\"/></a>",
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<p align="center">
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
<a href="#contributors"><img src="https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square" alt="All Contributors"/></a>
<a href="#contributors"><img src="https://img.shields.io/badge/all_contributors-12-orange.svg?style=flat-square" alt="All Contributors"/></a>
<!-- ALL-CONTRIBUTORS-BADGE:END -->
<a href="https://www.npmjs.com/package/superjson">
<img alt="npm" src="https://img.shields.io/npm/v/superjson" />
Expand Down Expand Up @@ -237,6 +237,8 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center"><a href="https://haspar.us"><img src="https://avatars0.githubusercontent.com/u/15332326?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Piotr Monwid-Olechnowicz</b></sub></a><br /><a href="#ideas-hasparus" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="http://kattcorp.com"><img src="https://avatars1.githubusercontent.com/u/459267?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alex Johansson</b></sub></a><br /><a href="https://github.com/blitz-js/superjson/commits?author=KATT" title="Code">💻</a> <a href="https://github.com/blitz-js/superjson/commits?author=KATT" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/simonedelmann"><img src="https://avatars.githubusercontent.com/u/2821076?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Simon Edelmann</b></sub></a><br /><a href="https://github.com/blitz-js/superjson/issues?q=author%3Asimonedelmann" title="Bug reports">🐛</a> <a href="https://github.com/blitz-js/superjson/commits?author=simonedelmann" title="Code">💻</a> <a href="#ideas-simonedelmann" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="https://www.samgarson.com"><img src="https://avatars.githubusercontent.com/u/6242344?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sam Garson</b></sub></a><br /><a href="https://github.com/blitz-js/superjson/issues?q=author%3Asamtgarson" title="Bug reports">🐛</a></td>
<td align="center"><a href="http://twitter.com/_markeh"><img src="https://avatars.githubusercontent.com/u/1357323?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mark Hughes</b></sub></a><br /><a href="https://github.com/blitz-js/superjson/issues?q=author%3Amarkhughes" title="Bug reports">🐛</a></td>
</tr>
</table>

Expand Down
31 changes: 30 additions & 1 deletion src/class-registry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
import { Registry } from './registry';
import { Class } from './types';

export const ClassRegistry = new Registry<Class>(c => c.name);
export interface RegisterOptions {
identifier?: string;
allowProps?: string[];
}

class _ClassRegistry extends Registry<Class> {
constructor() {
super(c => c.name);
}

private classToAllowedProps = new Map<Class, string[]>();

register(value: Class, options?: string | RegisterOptions): void {
if (typeof options === 'object') {
if (options.allowProps) {
this.classToAllowedProps.set(value, options.allowProps);
}

super.register(value, options.identifier);
} else {
super.register(value, options);
}
}

getAllowedProps(value: Class): string[] | undefined {
return this.classToAllowedProps.get(value);
}
}

export const ClassRegistry = new _ClassRegistry();
46 changes: 42 additions & 4 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('stringify & parse', () => {
outputAnnotations?: Annotations;
customExpectations?: (value: any) => void;
skipOnNode10?: boolean;
dontExpectEquality?: boolean;
}
> = {
'works for objects': {
Expand Down Expand Up @@ -191,9 +192,6 @@ describe('stringify & parse', () => {
output: ({ e }: any) => {
expect(e.name).toBe('Error');
expect(e.message).toBe('epic fail');
expect(e.stack.startsWith('Error: epic fail\n at Suite.')).toBe(
true
);
},
outputAnnotations: {
values: {
Expand Down Expand Up @@ -519,6 +517,27 @@ describe('stringify & parse', () => {
},
},

'works with custom allowedProps': {
input: () => {
class User {
constructor(public username: string, public password: string) {}
}
SuperJSON.registerClass(User, { allowProps: ['username'] });
return new User('bongocat', 'supersecurepassword');
},
output: {
username: 'bongocat',
},
outputAnnotations: {
values: [['class', 'User']],
},
customExpectations(value) {
expect(value.password).toBeUndefined();
expect(value.username).toBe('bongocat');
},
dontExpectEquality: true,
},

'works for undefined, issue #48': {
input: undefined,
output: null,
Expand Down Expand Up @@ -567,6 +586,7 @@ describe('stringify & parse', () => {
outputAnnotations: expectedOutputAnnotations,
customExpectations,
skipOnNode10,
dontExpectEquality,
},
] of Object.entries(cases)) {
let testFunc = test;
Expand All @@ -590,7 +610,9 @@ describe('stringify & parse', () => {
expect(meta).toEqual(expectedOutputAnnotations);

const untransformed = SuperJSON.deserialize({ json, meta });
expect(untransformed).toEqual(inputValue);
if (!dontExpectEquality) {
expect(untransformed).toEqual(inputValue);
}
customExpectations?.(untransformed);
});
}
Expand Down Expand Up @@ -855,3 +877,19 @@ test('regression #95: no undefined', () => {

expect(parsed).toEqual(input);
});

test('regression #108: Error#stack should not be included by default', () => {
const input = new Error("Beep boop, you don't wanna see me. I'm an error!");
expect(input).toHaveProperty('stack');

const { stack: thatShouldBeUndefined } = SuperJSON.parse(
SuperJSON.stringify(input)
) as any;
expect(thatShouldBeUndefined).toBeUndefined();

SuperJSON.allowErrorProps('stack');
const { stack: thatShouldExist } = SuperJSON.parse(
SuperJSON.stringify(input)
) as any;
expect(thatShouldExist).toEqual(input.stack);
});
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Class,
JSONValue,
} from './types';
import { ClassRegistry } from './class-registry';
import { ClassRegistry, RegisterOptions } from './class-registry';
import { SymbolRegistry } from './symbol-registry';
import {
CustomTransfomer,
Expand Down Expand Up @@ -52,8 +52,8 @@ const stringify = (object: SuperJSONValue): string =>
export const parse = <T = unknown>(string: string): T =>
deserialize(JSON.parse(string));

const registerClass = (v: Class, identifier?: string) =>
ClassRegistry.register(v, identifier);
const registerClass = (v: Class, options?: RegisterOptions) =>
ClassRegistry.register(v, options);

const registerSymbol = (v: Symbol, identifier?: string) =>
SymbolRegistry.register(v, identifier);
Expand Down
14 changes: 12 additions & 2 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ const simpleRules = [
const baseError: any = {
name: v.name,
message: v.message,
stack: v.stack,
};

allowedErrorProps.forEach(prop => {
Expand Down Expand Up @@ -232,7 +231,18 @@ const classRule = compositeTransformation(
const identifier = ClassRegistry.getIdentifier(clazz.constructor);
return ['class', identifier!];
},
v => v,
clazz => {
const allowedProps = ClassRegistry.getAllowedProps(clazz.constructor);
if (!allowedProps) {
return clazz;
}

const result: any = {};
allowedProps.forEach(prop => {
result[prop] = clazz[prop];
});
return result;
},
(v, a) => {
const clazz = ClassRegistry.getValue(a[1]);

Expand Down

0 comments on commit 17fb53a

Please sign in to comment.