Skip to content

Commit

Permalink
Merge pull request #46 from blitz-js/idiomatic-props
Browse files Browse the repository at this point in the history
Use idiomatic props
  • Loading branch information
Skn0tt authored Feb 23, 2021
2 parents 8698545 + 812f592 commit d480e77
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
20 changes: 18 additions & 2 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@
dependencies:
"@babel/types" "^7.12.5"

"@babel/helper-module-imports@^7.12.13":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.12.13.tgz#ec67e4404f41750463e455cc3203f6a32e93fcb0"
integrity sha512-NGmfvRp9Rqxy0uHSSVP+SRIW1q31a7Ji10cLBcqSDUngGentY4FRiHOFZFE1CLU5eiL0oE8reH7Tg1y99TDM/g==
dependencies:
"@babel/types" "^7.12.13"

"@babel/helper-module-transforms@^7.12.1":
version "7.12.1"
resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.12.1.tgz#7954fec71f5b32c48e4b303b437c34453fd7247c"
Expand Down Expand Up @@ -980,6 +987,15 @@
lodash "^4.17.19"
to-fast-properties "^2.0.0"

"@babel/types@^7.12.13":
version "7.12.17"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.12.17.tgz#9d711eb807e0934c90b8b1ca0eb1f7230d150963"
integrity sha512-tNMDjcv/4DIcHxErTgwB9q2ZcYyN0sUfgGKUK/mm1FJK7Wz+KstoEekxrl/tBiNDgLK1HGi+sppj1An/1DR4fQ==
dependencies:
"@babel/helper-validator-identifier" "^7.12.11"
lodash "^4.17.19"
to-fast-properties "^2.0.0"

"@hapi/[email protected]":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@hapi/accept/-/accept-5.0.1.tgz#068553e867f0f63225a506ed74e899441af53e10"
Expand Down Expand Up @@ -1568,9 +1584,9 @@ babel-plugin-module-resolver@^4.0.0:
resolve "^1.13.1"

"babel-plugin-superjson-next@file:..":
version "0.1.10"
version "0.2.1"
dependencies:
"@babel/helper-module-imports" "^7.12.5"
"@babel/helper-module-imports" "^7.12.13"
hoist-non-react-statics "^3.3.2"

[email protected]:
Expand Down
33 changes: 14 additions & 19 deletions src/tools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import * as hoistNonReactStatics from 'hoist-non-react-statics';
import type { GetServerSideProps } from 'next';
import * as React from 'react';

type SuperJSONProps = ReturnType<typeof SuperJSON.serialize> & {
_superjson: true;
type SuperJSONProps<P> = P & {
_superjson?: ReturnType<typeof SuperJSON.serialize>['meta'];
};

export function withSuperJSONProps<P>(
gssp: GetServerSideProps<P>
): GetServerSideProps<SuperJSONProps> {
): GetServerSideProps<SuperJSONProps<P>> {
return async function withSuperJSON(...args) {
const result = await gssp(...args);

Expand All @@ -18,14 +18,10 @@ export function withSuperJSONProps<P>(
}

const { json, meta } = SuperJSON.serialize(result.props);
const props = json as any;

const props: SuperJSONProps = {
json,
_superjson: true,
};

if (Boolean(meta)) {
props.meta = meta;
if (meta) {
props._superjson = meta;
}

return {
Expand All @@ -37,15 +33,14 @@ export function withSuperJSONProps<P>(

export function withSuperJSONPage<P>(
Page: React.ComponentType<P>
): React.ComponentType<SuperJSONProps> {
function WithSuperJSON(serializedProps: SuperJSONProps) {
if (!serializedProps._superjson) {
const PageWithoutProps = Page as React.ComponentType<{}>;
return <PageWithoutProps {...serializedProps as any as P} />;
}

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

hoistNonReactStatics(WithSuperJSON, Page);
Expand Down

0 comments on commit d480e77

Please sign in to comment.