-
Notifications
You must be signed in to change notification settings - Fork 167
Update session timeout poll response handling to omit timeout on inactive session #8521
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d5d0f3a
Update session timeout spec to actually exercise timeout
aduth 17cbd3a
Handle 401 response from server for session status
aduth 83dc5f3
Unset timeout in status response if inactive
aduth 675f619
Add test case for 401 server response
aduth bb25f1c
Offset by timeout_in
aduth 766b961
Use current main behavior for forcing timeout
aduth 8e290b1
Handle unauthorized response consistently
aduth ba964f1
changelog: Bug Fixes, Session Timeout, Improve consistency of session…
aduth d1e9f83
Upgrade TypeScript to resolve typecheck errors
aduth 0b0efb5
Explicitly list generic parameter variations in overload
aduth fd2d313
Update timeout type to reflect current active endpoint
aduth 874f068
Revert "Update timeout type to reflect current active endpoint"
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,82 @@ | ||
| import { request } from '@18f/identity-request'; | ||
|
|
||
| export interface SessionStatusResponse { | ||
| export interface SessionLiveStatusResponse { | ||
| /** | ||
| * Whether the session is still active. | ||
| */ | ||
| live: boolean; | ||
| live: true; | ||
|
|
||
| /** | ||
| * ISO8601-formatted date string for session timeout. | ||
| */ | ||
| timeout: string; | ||
| } | ||
|
|
||
| export interface SessionStatus { | ||
| export interface SessionTimedOutStatusResponse { | ||
| /** | ||
| * Whether the session is still active. | ||
| */ | ||
| isLive: boolean; | ||
| live: false; | ||
|
|
||
| /** | ||
| * ISO8601-formatted date string for session timeout. | ||
| */ | ||
| timeout: string; | ||
| timeout: null; | ||
| } | ||
|
|
||
| type SessionStatusResponse = SessionLiveStatusResponse | SessionTimedOutStatusResponse; | ||
|
|
||
| interface SessionLiveStatus { | ||
| /** | ||
| * Whether the session is still active. | ||
| */ | ||
| isLive: true; | ||
|
|
||
| /** | ||
| * ISO8601-formatted date string for session timeout. | ||
| */ | ||
| timeout: Date; | ||
| } | ||
|
|
||
| interface SessionTimedOutStatus { | ||
| /** | ||
| * Whether the session is still active. | ||
| */ | ||
| isLive: false; | ||
|
|
||
| /** | ||
| * ISO8601-formatted date string for session timeout. | ||
| */ | ||
| timeout?: undefined; | ||
| } | ||
|
|
||
| export type SessionStatus = SessionLiveStatus | SessionTimedOutStatus; | ||
|
|
||
| export const STATUS_API_ENDPOINT = '/active'; | ||
| export const KEEP_ALIVE_API_ENDPOINT = '/sessions/keepalive'; | ||
|
|
||
| const mapSessionStatusResponse = ({ live, timeout }: SessionStatusResponse): SessionStatus => ({ | ||
| isLive: live, | ||
| function mapSessionStatusResponse<R extends SessionLiveStatusResponse>( | ||
| response: R, | ||
| ): SessionLiveStatus; | ||
| function mapSessionStatusResponse<R extends SessionTimedOutStatusResponse>( | ||
| response: R, | ||
| ): SessionTimedOutStatus; | ||
| function mapSessionStatusResponse<R extends SessionStatusResponse>({ | ||
| live, | ||
| timeout, | ||
| }); | ||
| }: R): SessionLiveStatus | SessionTimedOutStatus { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The types here were giving me trouble where I was hoping TypeScript would be a bit smarter about resolving between |
||
| return live ? { isLive: true, timeout: new Date(timeout) } : { isLive: false }; | ||
| } | ||
|
|
||
| /** | ||
| * Request the current session status. Returns a promise resolving to the current session status. | ||
| * | ||
| * @return A promise resolving to the current session status | ||
| */ | ||
| export const requestSessionStatus = (): Promise<SessionStatus> => | ||
| request<SessionStatusResponse>(STATUS_API_ENDPOINT).then(mapSessionStatusResponse); | ||
| request<SessionStatusResponse>(STATUS_API_ENDPOINT) | ||
| .catch(() => ({ live: false, timeout: null })) | ||
|
aduth marked this conversation as resolved.
Outdated
|
||
| .then(mapSessionStatusResponse); | ||
|
|
||
| /** | ||
| * Request that the current session be kept alive. Returns a promise resolving to the updated | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
how come have both
SessionStatusandSessionStatusResponse? I feel like having the union type is useful for the actual TS code, since the code is already mapping responses from the server explicitly, having the union type forSessionStatusResponseseems like it doesn't give us much?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.
It was probably useful when I was trying different implementations to get the type narrowing to cooperate with different generic forms. I suspect I left it mostly as a nice symmetry between the "input" and "output" types. Ultimately though, yeah, it's unused, and if anything the overloaded function generic using it would be clearer by just spelling out the union of overloaded forms explicitly.
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 if I was writing this I'd probably collapse the "raw" server types into a singular one, and use the union type for the one passed around in TS code
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.
Hm, I think I may have initially misunderstood the suggestion. But after digging in, both interpretations lead me to think that maybe it's worth keeping as-is:
SessionStatusResponseas a union ofSessionLiveStatusResponse | SessionTimedOutStatusResponse, while keeping the latter types, this makes sense for the overloaded method signature, but makes therequestgeneric parameter below a little less expressive.SessionStatusResponseas the interface and update property types to reflect all forms (live: boolean; timeout: string | null), this makes TypeScript unable to do type narrowing in the logic of the mapping function, specifically on calling theDateconstructor with a possibly-nulltimeout(screenshot).As a compromise, I'd suggest keeping the union type for the
requestcalls, and updating the overloaded signature to re-reference the individual response types, to reflect the overloaded forms. See 0b0efb5.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 I had intended the "flatten SessionStatusResponse as the interface and update property types to reflect all forms" approach, but the screenshot makes a good case for keeping. Thanks for trying!