Skip to content

Commit

Permalink
chore: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fatihky committed Jan 25, 2025
1 parent bd02662 commit 7593cb8
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 65 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export class PolicyContext {
}

export class PostPolicy extends PunditPolicy<PolicyContext, Post, PostActions> {
constructor() {
super(Post);
}

authorize(context, post, action) {
switch (action) {
case "create":
Expand All @@ -113,6 +117,10 @@ export class PostPolicy extends PunditPolicy<PolicyContext, Post, PostActions> {
}

export class UserPolicy extends PunditPolicy<PolicyContext, User, UserActions> {
constructor() {
super(User);
}

authorize(context, post, action) {
switch (action) {
case "create":
Expand All @@ -125,7 +133,7 @@ export class UserPolicy extends PunditPolicy<PolicyContext, User, UserActions> {
}

filter(ctx) {
// modify context
// modify context or return some filter object...
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "MIT",
"description": "",
"dependencies": {
"pundit-ts": "^0.3.0"
"pundit-ts": "^0.4.1"
},
"devDependencies": {
"@types/node": "^22.10.2",
Expand Down
10 changes: 5 additions & 5 deletions examples/blog/pnpm-lock.yaml

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

18 changes: 5 additions & 13 deletions examples/blog/src/policies/post.policy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { PunditPolicy } from "../../../../index";
import { PunditPolicy } from "pundit-ts";
import { Post } from "../models";
import { PolicyContext } from "./policy-context";

export type PostActions = "create" | "publish" | "unpublish" | "update";

export class PostPolicy extends PunditPolicy<PolicyContext, Post, PostActions> {
constructor() {
super(Post);
}

authorize(context: PolicyContext, post: Post, action: PostActions): boolean {
switch (action) {
case "create":
Expand All @@ -17,18 +21,6 @@ export class PostPolicy extends PunditPolicy<PolicyContext, Post, PostActions> {

async filter(context: PolicyContext): Promise<void> {}

handlesAction(action: unknown): action is PostActions {
return action === "create" || action === "update";
}

handlesModel(object: unknown): object is Post {
return object instanceof Post;
}

handlesModelConstructor(cons: unknown): cons is new () => Post {
return cons === Post;
}

private isAdminOrAuthor(context: PolicyContext, post: Post): boolean {
return (
context.actor?.isAdmin || post.author.username === context.actor?.username
Expand Down
20 changes: 5 additions & 15 deletions examples/blog/src/policies/user.policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { PolicyContext } from "./policy-context";

export type UserActions = "create";

export class UserPolicy
implements PunditPolicy<PolicyContext, User, UserActions>
{
export class UserPolicy extends PunditPolicy<PolicyContext, User, UserActions> {
constructor() {
super(User);
}

authorize(
_context: PolicyContext,
_object: User,
Expand All @@ -16,16 +18,4 @@ export class UserPolicy
}

async filter(context: PolicyContext): Promise<void> {}

handlesAction(action: unknown): action is UserActions {
return action === "create";
}

handlesModel(object: unknown): object is User {
return object instanceof User;
}

handlesModelConstructor(cons: unknown): cons is new () => User {
return cons === User;
}
}
2 changes: 1 addition & 1 deletion examples/prisma-blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
},
"dependencies": {
"@prisma/client": "6.0.1",
"pundit-ts": "^0.3.0"
"pundit-ts": "^0.4.1"
}
}
10 changes: 5 additions & 5 deletions examples/prisma-blog/pnpm-lock.yaml

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

16 changes: 4 additions & 12 deletions examples/prisma-blog/src/policies/post.policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export class PostPolicy extends PunditPolicy<
PostActions,
Prisma.PostFindManyArgs
> {
constructor() {
super(Post);
}

authorize(context: PolicyContext, post: Post, action: PostActions): boolean {
switch (action) {
case "create":
Expand All @@ -28,16 +32,4 @@ export class PostPolicy extends PunditPolicy<
where: { authorId: context.actor?.id },
} satisfies Prisma.PostFindManyArgs;
}

handlesAction(action: unknown): action is PostActions {
return action === "create" || action === "update";
}

handlesModel(object: unknown): object is Post {
return true; // cannot perform instanceof check here
}

handlesModelConstructor(cons: unknown): cons is new () => Post {
return cons === Post;
}
}
16 changes: 4 additions & 12 deletions examples/prisma-blog/src/policies/user.policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { PolicyContext } from "./policy-context";
export type UserActions = "create";

export class UserPolicy extends PunditPolicy<PolicyContext, User, UserActions> {
constructor() {
super(User);
}

authorize(
_context: PolicyContext,
_user: User,
Expand All @@ -14,16 +18,4 @@ export class UserPolicy extends PunditPolicy<PolicyContext, User, UserActions> {
}

async filter(context: PolicyContext): Promise<void> {}

handlesModelConstructor(cons: unknown): cons is new () => User {
return cons === User;
}

handlesAction(action: unknown): action is UserActions {
return action === "create";
}

handlesModel(object: User): object is User {
return true; // cannot perform instanceof check here, because User is just a type, not a class
}
}

0 comments on commit 7593cb8

Please sign in to comment.