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 @cerbos/react package #876

Merged
merged 17 commits into from
Mar 25, 2024
Merged

Conversation

hasanayan
Copy link
Contributor

This is an initial/draft PR to agree on some of the concepts. It's lacking documentation and testing and some areas that are subject to change.

I am opening this to be able to discuss the what @cerbos/react should offer and some other issues on the environment (linter rules etc)

@hasanayan hasanayan requested a review from haines March 19, 2024 13:05
packages/react/src/cerbos-provider.tsx Show resolved Hide resolved
packages/react/package.json Outdated Show resolved Hide resolved
packages/react/src/use-check-resource.ts Outdated Show resolved Hide resolved
@hasanayan hasanayan marked this pull request as draft March 19, 2024 13:11
packages/react/src/use-check-resource.ts Outdated Show resolved Hide resolved
packages/react/package.json Outdated Show resolved Hide resolved
packages/react/src/use-check-resource.ts Outdated Show resolved Hide resolved
packages/react/src/use-check-resource.ts Outdated Show resolved Hide resolved
packages/react/src/cerbos-provider.tsx Outdated Show resolved Hide resolved
packages/react/src/use-check-resource.ts Outdated Show resolved Hide resolved
packages/react/src/use-check-resource.ts Outdated Show resolved Hide resolved
packages/react/package.json Outdated Show resolved Hide resolved
.eslintrc.yaml Outdated Show resolved Hide resolved
@hasanayan hasanayan changed the title @cerbos/react initial implementation feat: @cerbos/react initial implementation Mar 21, 2024
@hasanayan hasanayan changed the title feat: @cerbos/react initial implementation feat: Init @cerbos/react package Mar 21, 2024
@hasanayan
Copy link
Contributor Author

@haines where do you think we should test this package? in packages/test with the rest of the packages? We will need to extend the test config with jsdom and @vitejs/plugin-react but I believe that should be OK. Do you have any other opinion on this?

@haines
Copy link
Member

haines commented Mar 21, 2024

We will need to extend the test config with jsdom and @vitejs/plugin-react but I believe that should be OK.

Yeah, that's fine. Although it's maybe less convenient than colocating the tests with the source, the rationale for keeping the tests in a separate package is that it forces us to only test against the publicly-exposed API of the other packages, and it ensures that we don't accidentally ship the tests in the packages we publish to npm.

packages/react/src/cerbos-provider.tsx Outdated Show resolved Hide resolved
packages/react/src/use-cerbos-request.ts Outdated Show resolved Hide resolved
packages/react/src/cerbos-provider.tsx Outdated Show resolved Hide resolved
@hasanayan
Copy link
Contributor Author

@haines I think docs and the actual code is ready for final review, I'm moving on to testing

.gitignore Outdated Show resolved Hide resolved
Copy link
Member

@haines haines left a comment

Choose a reason for hiding this comment

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

Code looks great. I've made some minor suggestions for the docs on hasanayan#2. We'll also need to add a changelog but we should do that after #18 lands because I changed the format 🙈

@hasanayan hasanayan force-pushed the feat/init-react-sdk branch 4 times, most recently from ba27f95 to c3c7375 Compare March 24, 2024 21:24
@hasanayan
Copy link
Contributor Author

hasanayan commented Mar 24, 2024

@haines added tests with a placeholder test for testing request cancellation, I'll wait until you merge your PR and I'll add the test.

I also added changelog.yml file but I am not sure it's correct. Btw I don't know what should be our initial version, an alpha or beta maybe? For now it's set as 0.0.1 in package.json

Also, apparently my brain is still in weekend-low-power mode 😄, I somehow thought your docs PR was actually a direct commit (you don't even have access, I know ^^) and I force pushed and removed it so I manually took those changes in 🤦🏻 But anyway I saved you a rebase 😄

@hasanayan hasanayan marked this pull request as ready for review March 24, 2024 21:25
hasanayan and others added 7 commits March 25, 2024 12:56
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Andrew Haines <haines@cerbos.dev>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Andrew Haines <haines@cerbos.dev>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
@haines haines changed the title feat: Init @cerbos/react package feat: Add @cerbos/react package Mar 25, 2024
Copy link
Member

@haines haines left a comment

Choose a reason for hiding this comment

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

This is looking great!

packages/react/src/index.ts Show resolved Hide resolved
packages/react/changelog.yaml Outdated Show resolved Hide resolved
packages/react/changelog.yaml Outdated Show resolved Hide resolved
packages/react/changelog.yaml Outdated Show resolved Hide resolved
packages/react/package.json Outdated Show resolved Hide resolved
packages/react/package.json Outdated Show resolved Hide resolved
private/test/src/react/use-check-resource.test.tsx Outdated Show resolved Hide resolved
private/test/src/react/use-check-resource.test.tsx Outdated Show resolved Hide resolved
Signed-off-by: Hasan Ayan <hasanayan@me.com>
Signed-off-by: Hasan Ayan <hasanayan@me.com>
@hasanayan
Copy link
Contributor Author

hasanayan commented Mar 25, 2024

@haines all done but; I broke my back trying to get React to complain about setState call after unmounting during tests (without if (!abortController.signal.aborted) check in the catch block) so that I could test that doesn't happen. Unfortunately I could not succeed. But I am testing that we always pass an AbortSignal with the requests and the signal is aborted on unmount.

Signed-off-by: Hasan Ayan <hasanayan@me.com>
packages/react/src/use-cerbos-request.ts Outdated Show resolved Hide resolved
Comment on lines +230 to +231
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(abortSignal!.aborted).toBe(false);
Copy link
Member

Choose a reason for hiding this comment

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

Minor but here and below you could probably avoid the non-null assertion because the test will still fail if it is undefined:

Suggested change
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(abortSignal!.aborted).toBe(false);
expect(abortSignal?.aborted).toBe(false);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought the same but no-unsafe-assignment rule is not happy about that (no configuration options). I chose this one to suppress

Copy link
Member

Choose a reason for hiding this comment

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

Ah, ok... weird!

@haines
Copy link
Member

haines commented Mar 25, 2024

I broke my back trying to get React to complain about setState call after unmounting during tests (without if (!abortController.signal.aborted) check in the catch block) so that I could test that happens. Unfortunately I could succeed. But I am testing that we always pass an AbortSignal with the requests and the signal is aborted on unmount.

Yeah, sounds painful - I think we have good test coverage of the abort behaviour between the tests I wrote for the client and the one you have added here, so I'm happy enough with that!

Signed-off-by: Hasan Ayan <hasanayan@me.com>
@hasanayan hasanayan requested a review from haines March 25, 2024 19:35
Copy link
Member

@haines haines left a comment

Choose a reason for hiding this comment

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

Awesome!

@hasanayan hasanayan merged commit 9f44179 into cerbos:main Mar 25, 2024
27 checks passed
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.

2 participants