Skip to content

Commit

Permalink
Merge pull request #94 from italodeandra/main
Browse files Browse the repository at this point in the history
create `deserializeProps` utility
  • Loading branch information
Skn0tt authored Sep 17, 2021
2 parents 87eb281 + c861f0c commit c52e8a7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NodePath, PluginObj, PluginPass } from '@babel/core';
import { addNamed as addNamedImport } from '@babel/helper-module-imports';
import {
arrayExpression,
callExpression,
ClassDeclaration,
classExpression,
Expand All @@ -10,15 +11,13 @@ import {
functionExpression,
isClassDeclaration,
isExportDefaultDeclaration,
isExportNamedDeclaration,
isFunctionDeclaration,
isFunctionExpression,
isIdentifier,
isVariableDeclaration,
stringLiteral,
variableDeclaration,
variableDeclarator,
arrayExpression,
stringLiteral,
} from '@babel/types';
import * as nodePath from 'path';

Expand Down
14 changes: 7 additions & 7 deletions src/tools.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SuperJSON from 'superjson';
import * as hoistNonReactStatics from 'hoist-non-react-statics';
import type { GetServerSideProps } from 'next';
import * as React from 'react';
import SuperJSON from 'superjson';

type SuperJSONProps<P> = P & {
_superjson?: ReturnType<typeof SuperJSON.serialize>['meta'];
Expand Down Expand Up @@ -49,16 +49,16 @@ export function withSuperJSONProps<P>(
};
}

export function deserializeProps<P>(serializedProps: SuperJSONProps<P>): P {
const { _superjson, ...props } = serializedProps;
return SuperJSON.deserialize({ json: props as any, meta: _superjson });
}

export function withSuperJSONPage<P>(
Page: React.ComponentType<P>
): React.ComponentType<SuperJSONProps<P>> {
function WithSuperJSON(serializedProps: SuperJSONProps<P>) {
const { _superjson, ...props } = serializedProps;
return (
<Page
{...SuperJSON.deserialize({ json: props as any, meta: _superjson })}
/>
);
return <Page {...deserializeProps<P>(serializedProps)} />;
}

hoistNonReactStatics(WithSuperJSON, Page);
Expand Down

0 comments on commit c52e8a7

Please sign in to comment.