-
Notifications
You must be signed in to change notification settings - Fork 222
Conversation
@@ -0,0 +1,85 @@ | |||
# `@shopify/react-cookie` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just roughed this out for completeness, but will put some more ❤️ into this Readme
either before or immediately after merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of the stuff going on in the Cookie component looks weird / suspect, and I'd like some rationale for why we rebuilt all of this instead of just hooking up something like universal-cookie
to react-network
.
Overall this looks pretty close either way.
@TheMallen on doing our own thing vs third-party: this library allows us to provide a cookie solution for applications devs that uses consistent patters as with the other quilt packages that also require similar client/server interaction (self-serializer, react-effect, network, etc…) . Going this route also requires no server-side middleware and everything is confined to only this package (and really one component) rather than requiring orchestrated changes across Less important, there is only one viable third-party solution out there (the That said, I get that additional code is a liability (even given this library is pretty tiny) and understand the hesitation to introduce this package. |
429941c
to
b6b8c6d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this again I think we need this to depend on @shopify/react-network
or live as part of that if we want it to work holistically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few questions / changes but this is looking really nice now
f26304d
to
4246229
Compare
@TheMallen another look when you get a chance 🙇 Still doing a final comb through all the Readme, but otherwise all changes are in. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still feel a bit weird about where some of this is, @GoodForOneFare what do you think?
packages/react-cookie/src/server.ts
Outdated
Object.entries(cookies).forEach(([cookie, options]) => { | ||
const {value, ...cookieOptions} = options; | ||
|
||
ctx.cookies.set(cookie, value, cookieOptions as any); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add none
in a PR to https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cookies/index.d.ts#L91, please?
|
||
export {CookieContext} from './context'; | ||
|
||
export class BrowserCookieManager { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is consistent with the rest of Quilt, but I miss the lint rule for "this isn't really a class".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this could easily just be a constant object exported directly since it doesn't have any local state. Doesn't matter too much.
969324c
to
a2eb6aa
Compare
3b12aea
to
9bef59d
Compare
@TheMallen @GoodForOneFare All changes have been addressed. I also included:
The only way I was able to tophat this successfully was by building each library ( Thanks again for your continual feedback and patience! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nits but this looks great IMO.
|
||
export {CookieContext} from './context'; | ||
|
||
export class BrowserCookieManager { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this could easily just be a constant object exported directly since it doesn't have any local state. Doesn't matter too much.
manager.setCookie(key, {value, ...options}); | ||
}; | ||
|
||
return [cookie, setCookie]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I like the consistency with koa of doing set('name', null)
as long as it's documented
} | ||
|
||
it('provides a universal cookie manager for the server', () => { | ||
hasDocumentCookie.mockImplementation(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we might want to add a 'why' comment explaining why we have to mock this (so that we can test the server implementation despite jest always having a document.cookie defined)
<MockComponent cookie="foo" setValue={setValue} />, | ||
); | ||
|
||
wrapper.find('button')!.trigger('onClick'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this spacing is a bit weird IMO
</> | ||
); | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also document the test utilities we export?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did not 🎩 but it looks like this should work 👍 😅
manager.setCookie(key, {value, ...options}); | ||
}; | ||
|
||
return [cookie, setCookie]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it makes sense to follow our rule, I guess it's fine. I'm not 100% sure about how strict we about following it.
Is there any kind of type-hint we can provide like:
export const UNSET = '';
...
setCookie(name: string, value: string | CookieValue | UNSET)
Eww :/
198940d
to
ce89608
Compare
Description
This PR adds a@shopify/react-cookie
library that gathers cookies from both the server and client. There are three main components to this library, 1) A universal-provider that provides a set of initial cookies and instantiates 2) a manager. This manager provides common methods for getting/ setting cookies both internally, and in thedocument
. 3) Finally, there are 2 common React hooks for operating on the cookies from within a React component.Demo/ Tophat
You can view a demo here from this branch of @shopify/web-rails-proving-ground
If you want to tophat yourself, pull down this branch and also the
cookies
branch fromweb-rails-proving-ground
.dev up
in thecookies
branch ofweb-rails-proving-ground
dev run
in theweb-rails-proving-ground
repo.