Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support multiple abilities for components #35

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,27 @@ Instead of:
<button>Edit</button>
```

#### Multiple abilities

If you possess multiple abilities, you can provide an array of abilities to the components. The component will render only if **all** of the abilities **match** the specified requirements of the component.

```vue
<Can :ability="[editPost, deletePost]" :args="[[post], [post]]" />

<Cannot :ability="[editPost, deletePost]" :args="[[post], [post]]" />

<Bouncer :ability="[editPost, deletePost]" :args="[[post], [post]]">
<template #can>
<button>Edit</button>
<button>Delete</button>
</template>

<template #cannot>
<p>You're not allowed to edit or delete the post.</p>
</template>
</Bouncer>
```

## Contribution

<details>
Expand Down
24 changes: 20 additions & 4 deletions playground/app/app.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<script setup lang="ts">
import type { Product } from '~~/shared/types/product'
import { createProduct, editProduct, deleteProduct } from '~~/shared/abilities'
import { createProduct, editProduct, deleteProduct, createCategory } from '~~/shared/abilities'

const product = ref<Product>({
id: 1,
name: 'Product 1',
price: 100,
ownerId: 1,
})

const editableProduct = ref<Product>({
id: 2,
name: 'Product 2',
price: 200,
ownerId: 2,
})
</script>

<template>
Expand All @@ -33,7 +40,16 @@ const product = ref<Product>({
<Can
:ability="createProduct"
>
I can create a product.
<p>I can create a product.</p>
</Can>

<Can
:ability="[editProduct, createCategory]"
:args="[[editableProduct]]"
as="p"
data-attrs="create-and-edit-product"
>
I can edit a product and create a new category.
</Can>

<Cannot
Expand Down Expand Up @@ -63,11 +79,11 @@ const product = ref<Product>({
:args="[product]"
>
<template #can>
I can delete a product.
<p>I can delete a product.</p>
</template>

<template #cannot>
I cannot delete a product.
<p>I cannot delete a product.</p>
</template>
</Bouncer>
</div>
Expand Down
2 changes: 2 additions & 0 deletions playground/shared/abilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export const editProduct = defineAbility((user: User, product: Product) => {
export const deleteProduct = defineAbility((user: User, product: Product) => {
return user.id === product.ownerId
})

export const createCategory = defineAbility(() => true)
9 changes: 7 additions & 2 deletions src/runtime/components/Bouncer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Primitive } from './Primitive'
import { allows, ref, watchEffect } from '#imports'

const props = defineProps<{
ability: Ability
args?: BouncerArgs<Ability>
ability: Ability | Ability[]
args?: BouncerArgs<Ability> | BouncerArgs<Ability>[]
as?: string | Component
}>()

Expand All @@ -18,6 +18,11 @@ watchEffect(async () => {
})

async function resolve() {
if (Array.isArray(props.ability)) {
const results = await Promise.all(props.ability.map((ability, index) => allows(ability, ...(props.args?.[index] ?? [] as any))))
return results.every(Boolean)
}

return await allows(props.ability, ...(props.args ?? [] as any))
}
</script>
Expand Down
9 changes: 7 additions & 2 deletions src/runtime/components/Can.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Primitive } from './Primitive'
import { allows, ref, watchEffect } from '#imports'

const props = defineProps<{
ability: Ability
args?: BouncerArgs<Ability>
ability: Ability | Ability[]
args?: BouncerArgs<Ability> | BouncerArgs<Ability>[]
as?: string | Component
}>()

Expand All @@ -18,6 +18,11 @@ watchEffect(async () => {
})

async function resolve() {
if (Array.isArray(props.ability)) {
const results = await Promise.all(props.ability.map((ability, index) => allows(ability, ...(props.args?.[index] ?? [] as any))))
return results.every(Boolean)
}

return await allows(props.ability, ...(props.args ?? [] as any))
}
</script>
Expand Down
9 changes: 7 additions & 2 deletions src/runtime/components/Cannot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Primitive } from './Primitive'
import { denies, ref, watchEffect } from '#imports'

const props = defineProps<{
ability: Ability
args?: BouncerArgs<Ability>
ability: Ability | Ability[]
args?: BouncerArgs<Ability> | BouncerArgs<Ability>[]
as?: string | Component
}>()

Expand All @@ -18,6 +18,11 @@ watchEffect(async () => {
})

async function resolve() {
if (Array.isArray(props.ability)) {
const results = await Promise.all(props.ability.map((ability, index) => denies(ability, ...(props.args?.[index] ?? [] as any))))
return results.every(Boolean)
}

return await denies(props.ability, ...(props.args ?? [] as any))
}
</script>
Expand Down
5 changes: 5 additions & 0 deletions test/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ describe('Components', async () => {

expect(await page.getByTestId('can-visible').isVisible()).toBeTruthy()
expect(await page.getByTestId('can-invisible').isVisible()).toBeFalsy()
expect(await page.getByTestId('can-multiple-visible').isVisible()).toBeTruthy()

expect(await page.getByTestId('as-can-visible').isVisible()).toBeTruthy()

expect(await page.getByTestId('cannot-invisible').isVisible()).toBeFalsy()
expect(await page.getByTestId('cannot-visible').isVisible()).toBeTruthy()
expect(await page.getByTestId('cannot-multiple-invisible').isVisible()).toBeFalsy()

expect(await page.getByTestId('bouncer-can-visible').isVisible()).toBeTruthy()
expect(await page.getByTestId('bouncer-cannot-invisible').isVisible()).toBeFalsy()

expect(await page.getByTestId('bouncer-can-invisible').isVisible()).toBeFalsy()
expect(await page.getByTestId('bouncer-cannot-visible').isVisible()).toBeTruthy()

expect(await page.getByTestId('bouncer-can-multiple-invisible').isVisible()).toBeFalsy()
expect(await page.getByTestId('bouncer-cannot-multiple-invisible').isVisible()).toBeFalsy()

await page.close()
})
})
32 changes: 32 additions & 0 deletions test/fixtures/components/app/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ const cannot = defineAbility(() => {
</div>
</Can>

<Can
:ability="[can, can]"
>
<div data-testid="can-multiple-visible">
Can Multiple Visible
</div>
</Can>

<Cannot
:ability="can"
>
Expand All @@ -52,6 +60,14 @@ const cannot = defineAbility(() => {
</div>
</Cannot>

<Cannot
:ability="[can, cannot]"
>
<div data-testid="cannot-multiple-invisible">
Cannot Multiple Visible
</div>
</Cannot>

<Bouncer
:ability="can"
>
Expand Down Expand Up @@ -83,5 +99,21 @@ const cannot = defineAbility(() => {
</div>
</template>
</Bouncer>

<Bouncer
:ability="[can, can]"
>
<template #can>
<div data-testid="bouncer-can-multiple-visible">
Bouncer Can Multiple Visible
</div>
</template>

<template #cannot>
<div data-testid="bouncer-cannot-multiple-invisible">
Bouncer Cannot Multiple Invisible
</div>
</template>
</Bouncer>
</div>
</template>