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: add policy composable #491

Merged
merged 1 commit into from
Mar 8, 2024
Merged

feat: add policy composable #491

merged 1 commit into from
Mar 8, 2024

Conversation

brc-dd
Copy link
Member

@brc-dd brc-dd commented Mar 7, 2024

closes #419

Usage:

import {
  type Policy,
  type PolicyResponse,
  type PolicyHelpers as SefirotPolicyHelpers,
  usePolicy as sefirotUsePolicy
} from 'sefirot/composables/Policy'

interface PolicyHelpers<Code extends string | undefined = undefined>
  extends SefirotPolicyHelpers<Code> {
  user: { admin: boolean }
}

function usePolicy<Code extends string | undefined = undefined>(
  fn: (helpers: PolicyHelpers<Code>) => PolicyResponse<Code>
): Policy<Code> {
  const user = { admin: true }

  return sefirotUsePolicy((helpers) => {
    return fn({ ...helpers, user })
  })
}


// ...

import { type Policy, usePolicy } from './Policy'

function canDoFoo(): Policy<'foo' | 'bar'> {
  return usePolicy(({ user, allow, deny }) => {
    return user.admin ? allow('foo') : deny('bar')
  })
}

function canDoBar(): Policy {
  return usePolicy(({ user, allow, deny }) => {
    return user.admin ? allow() : deny()
  })
}

// alternatively

import { usePolicy } from './Policy'

export function canDoFoo() {
  return usePolicy<'foo' | 'bar'>(({ user, allow, deny }) => {
    return user.admin ? allow('foo') : deny('bar')
  })
}

export function canDoBar() {
  return usePolicy(({ user, allow, deny }) => {
    return user.admin ? allow() : deny()
  })
}

Copy link

netlify bot commented Mar 7, 2024

Deploy Preview for sefirot-story ready!

Name Link
🔨 Latest commit 4189e03
🔍 Latest deploy log https://app.netlify.com/sites/sefirot-story/deploys/65e94681614420000814eb04
😎 Deploy Preview https://deploy-preview-491--sefirot-story.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Mar 7, 2024

Deploy Preview for sefirot-docs ready!

Name Link
🔨 Latest commit 4189e03
🔍 Latest deploy log https://app.netlify.com/sites/sefirot-docs/deploys/65e9468117dcdc000863bfdd
😎 Deploy Preview https://deploy-preview-491--sefirot-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@brc-dd brc-dd marked this pull request as draft March 7, 2024 04:49
@brc-dd brc-dd marked this pull request as ready for review March 7, 2024 04:50
@brc-dd brc-dd requested a review from kiaking March 7, 2024 04:50
Copy link
Member

@kiaking kiaking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Way cleaner than I though it would be 😳

@kiaking kiaking merged commit a26b9b3 into main Mar 8, 2024
9 checks passed
@kiaking kiaking deleted the feat/policy branch March 8, 2024 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Policy composable
2 participants