-
Notifications
You must be signed in to change notification settings - Fork 192
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
fix: paging parsing issues #728
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a5a992
updates URL parse to works with relative path
annguy3n 4379307
update total count header to be lower case
annguy3n 0798b5c
empty commit to retest
annguy3n 59f6843
Merge remote-tracking branch 'origin/main' into an/fix-parsing-error
jkaster fa7641b
fix: detect any casing of header
jkaster 7b791b6
added a comment for `new URL()`` base path
jkaster 4286ec7
Simplify `offset` and `limit` value extraction
jkaster 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 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 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 |
---|---|---|
|
@@ -33,7 +33,7 @@ import { | |
import { IAPIMethods } from './apiMethods' | ||
import { BaseTransport } from './baseTransport' | ||
|
||
export const LinkHeader = 'link' | ||
export const LinkHeader = 'Link' | ||
export const TotalCountHeader = 'X-Total-Count' | ||
|
||
/** | ||
|
@@ -257,7 +257,7 @@ export class Paging<TSuccess extends ILength, TError> | |
this.transport.observer = saved | ||
} | ||
if (Object.keys(raw).length === 0 || Object.keys(raw.headers).length === 0) | ||
throw new Error('No paging headers were found') | ||
return Promise.reject(new Error('No paging headers were found')) | ||
this.parse(raw) | ||
return this | ||
} | ||
|
@@ -362,21 +362,28 @@ export class Paging<TSuccess extends ILength, TError> | |
return result | ||
} | ||
|
||
static findHeader(raw: IRawResponse, name: string) { | ||
return ( | ||
raw.headers[name] || | ||
raw.headers[name.toLowerCase()] || | ||
raw.headers[name.toUpperCase()] | ||
Comment on lines
+367
to
+369
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. Recognizes all three reasonable casing options for the header |
||
) | ||
} | ||
|
||
parse(raw: IRawResponse): IPager<TSuccess, TError> { | ||
const req = new URL(raw.url) | ||
const params = req.searchParams | ||
const params = new URLSearchParams(raw.url) | ||
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. Handles full or relative urls |
||
this.limit = Paging.paramDefault(params.get('limit'), -1) | ||
this.offset = Paging.paramDefault( | ||
params.get('offset'), | ||
this.limit > 0 ? 0 : -1 | ||
) | ||
const linkHeader = raw.headers.link || raw.headers.Link || raw.headers.LINK | ||
const linkHeader = Paging.findHeader(raw, LinkHeader) | ||
if (linkHeader) { | ||
this.links = linkHeaderParser(linkHeader) | ||
} else { | ||
this.links = {} | ||
} | ||
const totalHeader = raw.headers[TotalCountHeader] | ||
const totalHeader = Paging.findHeader(raw, TotalCountHeader) | ||
if (totalHeader) { | ||
this.total = parseInt(totalHeader.trim(), 10) | ||
} else { | ||
|
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.
I feel like I see the capitalized form of http headers more often: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
also, server/client should consider them case-insensitive: https://stackoverflow.com/a/5259004/13802304
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, it's a problem with the headers handling in JS. I responded via chat that I think we need to support both the Title case and lowercase at least.