Closed magne4000 closed 2 years ago
How about PageContextInit & (({ httpResponse: HttpResponse } & PageContextAdded) | ({ httpResponse: null } & Partial<PageContextAdded>))
?
If an error throws early then PageContextAdded === {}
so we need Partial
.
which in both of my use cases (redirect and cookies) is not necessarily the case.
Can you elaborate the cookies case?
Can you elaborate the cookies case?
I'm curious.
How about
PageContextInit & (({ httpResponse: HttpResponse } & PageContextAdded) | ({ httpResponse: null } & Partial<PageContextAdded>))
?If an error throws early then
PageContextAdded === {}
so we needPartial
.
👍
Can you elaborate the cookies case?
I'm currently using onBeforeRender
server side to store Cookies (JWT encoded) to save some small state that represents a step or multiple steps in a user flow, e.g.:
onBeforeRender
to Platform XHow about
PageContextInit & (({ httpResponse: HttpResponse } & PageContextAdded) | ({ httpResponse: null } & Partial<PageContextAdded>))
?If an error throws early then
PageContextAdded === {}
so we needPartial
.
PageContextInit & (({ httpResponse: HttpResponse } & PageContextAdded) | ({ httpResponse: null } & (PageContextAdded | {})))
would perhaps be more accurate.
PageContextInit & (({ httpResponse: HttpResponse } & PageContextAdded) | ({ httpResponse: null } & (PageContextAdded | {})))
would perhaps be more accurate then
No because the error can also be thrown between two hooks. So the first hook succesfully added his additional pageContext
but not the second.
Objections? Let me know if not and I'll release the new type.
Hum indeed, but if a hook sets {a: number, b: number}
and another one {c: number}
, we lose accuracy in the definition. Partial
will force you to check a
and b
separately, but in the mean time it also doesn't force the developer to be aware of internal mecanism when he defines its types.
Using Partial
is safer in most cases IMO 👍 . I'll see if I come with another workaround to have better typing if the developer know what he/she's doing.
The best would be https://github.com/brillout/vite-plugin-ssr/issues/176 so that all types are automatically stiched together for the user.
You're right we loose accuracy but Partial<PageContextAddendum>
is still more accurate than {}
.
Let me release the new type. Last chance to object :-)
LGTM
Released in v0.3.20
.
First of all, thank you for this awesome lib (and really useful documentation)!
I was playing around with
createPageRenderer
(I'm using Typescript + Vercel + SolidJS) like so:Here, my
pageContext
should have custom fields that could be returned (likeredirectTo
as in your examples, orcookies
, that is responsible to set Cookies on headers).So I checked the
renderPage
typings:Good, there is a
PageContextAdded
type that I can use:This should have been enough I guess, but the right typing is not quite right IMO:
Looking closer,
PageContextAdded
is added to the return type only ifhttpResponse
is of typeHttpResponse
, which in both of my use cases (redirect and cookies) is not necessarily the case.Perhaps I'm doing this wrong? But if not, here is the typing I propose: