Skip to content

Commit

Permalink
feat: is server side context type guard (#19)
Browse files Browse the repository at this point in the history
* feat: is server side context type guard

* update readme

* Update README.md
  • Loading branch information
ricokahler authored Jan 4, 2021
1 parent affda60 commit 04f5554
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ const useBlogPost = createDataHook('BlogPost', async (context) => {
export default useBlogPost;
```

<details>
<summary>
TypeScript User?

> Note: For TypeScript users, if you're planning on only using the data hook in the context of `getServerSideProps`, you can import the provided [type guard](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards), `isServerSidePropsContext`, to narrow the type of the incoming context.
</summary>

```tsx
import { createDataHook, isServerSidePropsContext } from 'next-data-hooks';

const useServerSideData = createDataHook('Data', async (context) => {
if (!isServerSidePropsContext(context)) {
throw new Error('This data hook only works in getServerSideProps.');
}

// here, the type of `context` has been narrowed to the server side conext
const query = context.req.query;
});

export default useServerSideData;
```

</details>

2. Use the data hook in a component. Add it to a static prop in an array with other data hooks to compose them downward.

```tsx
Expand Down Expand Up @@ -135,7 +160,7 @@ BlogPostComponent.dataHooks = [
export default BlogPostComponent;
```

3. Pass the data hooks down in `getStaticProps`.
3. Pass the data hooks down in `getStaticProps` or `getServerSideProps`.

```tsx
import { getDataHooksProps } from 'next-data-hooks';
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-data-hooks",
"version": "0.4.0",
"version": "0.5.0",
"description": "Use `getStaticProps` as react hooks",
"private": true,
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as createDataHook } from './create-data-hook';
export { default as getDataHooksProps } from './get-data-hooks-props';
export { default as NextDataHooksContext } from './next-data-hooks-context';
export { default as NextDataHooksProvider } from './next-data-hooks-provider';
export { default as isServerSidePropsContext } from './is-server-side-props-context';
6 changes: 6 additions & 0 deletions src/is-server-side-props-context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import isServerSidePropsContext from './is-server-side-props-context';

it('returns true if there is a req and res', () => {
expect(isServerSidePropsContext({ req: true, res: true } as any)).toBe(true);
expect(isServerSidePropsContext({} as any)).toBe(false);
});
14 changes: 14 additions & 0 deletions src/is-server-side-props-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { GetServerSidePropsContext, GetStaticPropsContext } from 'next';

function isServerSidePropsContext(
context: GetServerSidePropsContext | GetStaticPropsContext
): context is GetServerSidePropsContext {
if (typeof context !== 'object' || !context) return false;

return (
!!(context as GetServerSidePropsContext).req &&
!!(context as GetServerSidePropsContext).res
);
}

export default isServerSidePropsContext;

0 comments on commit 04f5554

Please sign in to comment.