Skip to content

Commit

Permalink
Merge branch 'main' into fix-eslint-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Philzen authored Aug 9, 2022
2 parents e8c4784 + 06df623 commit bb83b3e
Show file tree
Hide file tree
Showing 53 changed files with 2,008 additions and 2,354 deletions.
2 changes: 1 addition & 1 deletion .github/actions/check_test_project_fixture/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "check_test_project_fixture",
"private": true,
"dependencies": {
"@actions/core": "1.9.0",
"@actions/core": "1.9.1",
"@actions/exec": "1.1.1"
},
"packageManager": "[email protected]"
Expand Down
20 changes: 15 additions & 5 deletions .github/actions/check_test_project_fixture/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ __metadata:
version: 6
cacheKey: 8c0

"@actions/core@npm:1.9.0":
version: 1.9.0
resolution: "@actions/core@npm:1.9.0"
"@actions/core@npm:1.9.1":
version: 1.9.1
resolution: "@actions/core@npm:1.9.1"
dependencies:
"@actions/http-client": ^2.0.1
checksum: 66a238981d7f9aa3c8ca0d80eb87aa291203dabd5c8e60e50d5075632c643883337f342fd677470d6cfd3588c483f32897fbb5c03a400b9c60171a32d2cbeb8d
uuid: ^8.3.2
checksum: 9e568d0df7d659ad0738da66f167d411714f7351fab527fcb91dd6afbeb58006e6cf1f752adc567aab1b9043f96581b2457746f394f7a756f7b14b554d7c4710
languageName: node
linkType: hard

Expand Down Expand Up @@ -43,7 +44,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "check_test_project_fixture@workspace:."
dependencies:
"@actions/core": 1.9.0
"@actions/core": 1.9.1
"@actions/exec": 1.1.1
languageName: unknown
linkType: soft
Expand All @@ -54,3 +55,12 @@ __metadata:
checksum: e27e7e896f2426c1c747325b5f54efebc1a004647d853fad892b46d64e37591ccd0b97439470795e5262b5c0748d22beb4489a04a0a448029636670bfd801b75
languageName: node
linkType: hard

"uuid@npm:^8.3.2":
version: 8.3.2
resolution: "uuid@npm:8.3.2"
bin:
uuid: dist/bin/uuid
checksum: bcbb807a917d374a49f475fae2e87fdca7da5e5530820ef53f65ba1d12131bd81a92ecf259cc7ce317cbe0f289e7d79fdfebcef9bfa3087c8c8a2fa304c9be54
languageName: node
linkType: hard
2 changes: 1 addition & 1 deletion .github/actions/only_doc_changes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "only_doc_changes",
"private": true,
"dependencies": {
"@actions/core": "1.9.0",
"@actions/core": "1.9.1",
"@actions/exec": "1.1.1"
},
"packageManager": "[email protected]"
Expand Down
20 changes: 15 additions & 5 deletions .github/actions/only_doc_changes/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ __metadata:
version: 6
cacheKey: 8c0

"@actions/core@npm:1.9.0":
version: 1.9.0
resolution: "@actions/core@npm:1.9.0"
"@actions/core@npm:1.9.1":
version: 1.9.1
resolution: "@actions/core@npm:1.9.1"
dependencies:
"@actions/http-client": ^2.0.1
checksum: 66a238981d7f9aa3c8ca0d80eb87aa291203dabd5c8e60e50d5075632c643883337f342fd677470d6cfd3588c483f32897fbb5c03a400b9c60171a32d2cbeb8d
uuid: ^8.3.2
checksum: 9e568d0df7d659ad0738da66f167d411714f7351fab527fcb91dd6afbeb58006e6cf1f752adc567aab1b9043f96581b2457746f394f7a756f7b14b554d7c4710
languageName: node
linkType: hard

Expand Down Expand Up @@ -43,7 +44,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "only_doc_changes@workspace:."
dependencies:
"@actions/core": 1.9.0
"@actions/core": 1.9.1
"@actions/exec": 1.1.1
languageName: unknown
linkType: soft
Expand All @@ -54,3 +55,12 @@ __metadata:
checksum: e27e7e896f2426c1c747325b5f54efebc1a004647d853fad892b46d64e37591ccd0b97439470795e5262b5c0748d22beb4489a04a0a448029636670bfd801b75
languageName: node
linkType: hard

"uuid@npm:^8.3.2":
version: 8.3.2
resolution: "uuid@npm:8.3.2"
bin:
uuid: dist/bin/uuid
checksum: bcbb807a917d374a49f475fae2e87fdca7da5e5530820ef53f65ba1d12131bd81a92ecf259cc7ce317cbe0f289e7d79fdfebcef9bfa3087c8c8a2fa304c9be54
languageName: node
linkType: hard
18 changes: 18 additions & 0 deletions docs/docs/tutorial/chapter2/cells.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,24 @@ export const Success = ({ posts }: CellSuccessProps<ArticlesQuery>) => {
</TabItem>
</Tabs>

<ShowForTs>

:::tip Using generated types

At this point, you might see an error in your Cell while trying to import from `types/graphql`: "The type ArticlesQuery does not exist"

When you have the dev server (via `yarn rw dev`) running, the CLI watches files for changes and triggers type generation automatically, but you can trigger it manually too by running:

```bash
yarn rw g types
```

This looks at your Cell's `QUERY` and—as long as it's valid—tries to automatically create a TypeScript type for you to use in your code.

:::

</ShowForTs>

Let's plug this cell into our `HomePage` and see what happens:

<Tabs groupId="js-ts">
Expand Down
60 changes: 60 additions & 0 deletions docs/docs/tutorial/chapter7/rbac.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,66 @@ export const getCurrentUser = async (session) => {
</TabItem>
</Tabs>
<ShowForTs>
### Fixing the hasRole function
At this point, you might notice an error in your `api/src/lib/auth.ts` file, in the `hasRole` function. TypeScript is trying to help you here, by highlighting that roles can never be an array of strings:
```ts title="api/src/lib/auth.ts"
export const hasRole = (roles: AllowedRoles): boolean => {

// ...

} else if (Array.isArray(currentUserRoles)) {
// 👇 TypeScript will now be telling you 'some' doesn't exist on type never:
// highlight-next-line
return currentUserRoles?.some((allowedRole) => roles === allowedRole)
}
}
```
This is because we now know that the type of `currentUser.roles` is a `string` based on the type being returned from Prisma. So you can safely remove the block of code where it's checking if roles is an array:
```diff title="api/src/lib/auth.ts"
export const hasRole = (roles: AllowedRoles): boolean => {
if (!isAuthenticated()) {
return false
}

const currentUserRoles = context.currentUser?.roles

if (typeof roles === 'string') {
if (typeof currentUserRoles === 'string') {
// roles to check is a string, currentUser.roles is a string
return currentUserRoles === roles
- } else if (Array.isArray(currentUserRoles)) {
- // roles to check is a string, currentUser.roles is an array
- return currentUserRoles?.some((allowedRole) => roles === allowedRole)
}
}

if (Array.isArray(roles)) {
if (Array.isArray(currentUserRoles)) {
// roles to check is an array, currentUser.roles is an array
return currentUserRoles?.some((allowedRole) =>
roles.includes(allowedRole)
)
} else if (typeof context?.currentUser?.roles === 'string') {
// roles to check is an array, currentUser.roles is a string
return roles.some(
(allowedRole) => context.currentUser?.roles === allowedRole
)
}
}

// roles not found
return false
}
```
</ShowForTs>
### Restricting Access via Routes
The easiest way to prevent access to an entire URL is via the Router. The `<Private>` component takes a prop `roles` in which you can give a list of only those role(s) that should have access:
Expand Down
8 changes: 4 additions & 4 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
]
},
"dependencies": {
"@docusaurus/core": "2.0.0-rc.1",
"@docusaurus/plugin-content-docs": "2.0.0-rc.1",
"@docusaurus/preset-classic": "2.0.0-rc.1",
"@docusaurus/core": "2.0.1",
"@docusaurus/plugin-content-docs": "2.0.1",
"@docusaurus/preset-classic": "2.0.1",
"@mdx-js/react": "1.6.22",
"clsx": "1.2.1",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-player": "2.10.1"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "2.0.0-rc.1",
"@docusaurus/module-type-aliases": "2.0.1",
"@tsconfig/docusaurus": "1.0.6",
"typescript": "4.7.4"
}
Expand Down
18 changes: 18 additions & 0 deletions docs/versioned_docs/version-2.2/tutorial/chapter2/cells.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,24 @@ export const Success = ({ posts }: CellSuccessProps<ArticlesQuery>) => {
</TabItem>
</Tabs>

<ShowForTs>

:::tip Using generated types

At this point, you might see an error in your Cell while trying to import from `types/graphql`: "The type ArticlesQuery does not exist"

When you have the dev server (via `yarn rw dev`) running, the CLI watches files for changes and triggers type generation automatically, but you can trigger it manually too by running:

```bash
yarn rw g types
```

This looks at your Cell's `QUERY` and—as long as it's valid—tries to automatically create a TypeScript type for you to use in your code.

:::

</ShowForTs>

Let's plug this cell into our `HomePage` and see what happens:

<Tabs groupId="js-ts">
Expand Down
60 changes: 60 additions & 0 deletions docs/versioned_docs/version-2.2/tutorial/chapter7/rbac.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,66 @@ export const getCurrentUser = async (session) => {
</TabItem>
</Tabs>
<ShowForTs>
### Fixing the hasRole function
At this point, you might notice an error in your `api/src/lib/auth.ts` file, in the `hasRole` function. TypeScript is trying to help you here, by highlighting that roles can never be an array of strings:
```ts title="api/src/lib/auth.ts"
export const hasRole = (roles: AllowedRoles): boolean => {

// ...

} else if (Array.isArray(currentUserRoles)) {
// 👇 TypeScript will now be telling you 'some' doesn't exist on type never:
// highlight-next-line
return currentUserRoles?.some((allowedRole) => roles === allowedRole)
}
}
```
This is because we now know that the type of `currentUser.roles` is a `string` based on the type being returned from Prisma. So you can safely remove the block of code where it's checking if roles is an array:
```diff title="api/src/lib/auth.ts"
export const hasRole = (roles: AllowedRoles): boolean => {
if (!isAuthenticated()) {
return false
}

const currentUserRoles = context.currentUser?.roles

if (typeof roles === 'string') {
if (typeof currentUserRoles === 'string') {
// roles to check is a string, currentUser.roles is a string
return currentUserRoles === roles
- } else if (Array.isArray(currentUserRoles)) {
- // roles to check is a string, currentUser.roles is an array
- return currentUserRoles?.some((allowedRole) => roles === allowedRole)
}
}

if (Array.isArray(roles)) {
if (Array.isArray(currentUserRoles)) {
// roles to check is an array, currentUser.roles is an array
return currentUserRoles?.some((allowedRole) =>
roles.includes(allowedRole)
)
} else if (typeof context?.currentUser?.roles === 'string') {
// roles to check is an array, currentUser.roles is a string
return roles.some(
(allowedRole) => context.currentUser?.roles === allowedRole
)
}
}

// roles not found
return false
}
```
</ShowForTs>
### Restricting Access via Routes
The easiest way to prevent access to an entire URL is via the Router. The `<Private>` component takes a prop `roles` in which you can give a list of only those role(s) that should have access:
Expand Down
Loading

0 comments on commit bb83b3e

Please sign in to comment.