-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add errors and serialize-query-params
- Loading branch information
1 parent
a3cbc17
commit 5b91d51
Showing
8 changed files
with
165 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# unconventional js | ||
/blueprints/*/files/ | ||
**/*.d.ts | ||
|
||
# compiled output | ||
/dist/ | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Checks if the given response represents an unauthorized request error | ||
*/ | ||
export function isUnauthorizedResponse(response: Response): boolean; | ||
|
||
/** | ||
* Checks if the given response represents a forbidden request error | ||
*/ | ||
export function isForbiddenResponse(response: Response): boolean; | ||
|
||
/** | ||
* Checks if the given response represents an invalid request error | ||
*/ | ||
export function isInvalidResponse(response: Response): boolean; | ||
|
||
/** | ||
* Checks if the given response represents a bad request error | ||
*/ | ||
export function isBadRequestResponse(response: Response): boolean; | ||
|
||
/** | ||
* Checks if the given response represents a "not found" error | ||
*/ | ||
export function isNotFoundResponse(response: Response): boolean; | ||
|
||
/** | ||
* Checks if the given response represents a "gone" error | ||
*/ | ||
export function isGoneResponse(response: Response): boolean; | ||
|
||
/** | ||
* Checks if the given error is an "abort" error | ||
*/ | ||
export function isAbortError(error: DOMException): boolean; | ||
|
||
/** | ||
* Checks if the given response represents a conflict error | ||
*/ | ||
export function isConflictResponse(response: Response): boolean; | ||
|
||
/** | ||
* Checks if the given response represents a server error | ||
*/ | ||
export function isServerErrorResponse(response: Response): boolean; |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export function isUnauthorizedResponse(response) { | ||
return response.status === 401; | ||
} | ||
|
||
export function isForbiddenResponse(response) { | ||
return response.status === 403; | ||
} | ||
|
||
export function isInvalidResponse(response) { | ||
return response.status === 422; | ||
} | ||
|
||
export function isBadRequestResponse(response) { | ||
return response.status === 400; | ||
} | ||
|
||
export function isNotFoundResponse(response) { | ||
return response.status === 404; | ||
} | ||
|
||
export function isGoneResponse(response) { | ||
return response.status === 410; | ||
} | ||
|
||
export function isAbortError(error) { | ||
return error.name == 'AbortError'; | ||
} | ||
|
||
export function isConflictResponse(response) { | ||
return response.status === 409; | ||
} | ||
|
||
export function isServerErrorResponse(response) { | ||
return response.status >= 500 && response.status < 600; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Helper function that turns the data/body of a request into a query param string. | ||
* This is directly copied from jQuery.param. | ||
*/ | ||
export function serializeQueryParams( | ||
queryParamsObject: object | string, | ||
): string; | ||
|
||
export default serializeQueryParams; |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const RBRACKET = /\[\]$/; | ||
|
||
function isPlainObject(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Object]'; | ||
} | ||
|
||
/** | ||
* Helper function that turns the data/body of a request into a query param string. | ||
* This is directly copied from jQuery.param. | ||
*/ | ||
export function serializeQueryParams(queryParamsObject) { | ||
var s = []; | ||
|
||
function buildParams(prefix, obj) { | ||
var i, len, key; | ||
|
||
if (prefix) { | ||
if (Array.isArray(obj)) { | ||
for (i = 0, len = obj.length; i < len; i++) { | ||
if (RBRACKET.test(prefix)) { | ||
add(s, prefix, obj[i]); | ||
} else { | ||
buildParams( | ||
prefix + '[' + (typeof obj[i] === 'object' ? i : '') + ']', | ||
obj[i], | ||
); | ||
} | ||
} | ||
} else if (isPlainObject(obj)) { | ||
for (key in obj) { | ||
buildParams(prefix + '[' + key + ']', obj[key]); | ||
} | ||
} else { | ||
add(s, prefix, obj); | ||
} | ||
} else if (Array.isArray(obj)) { | ||
for (i = 0, len = obj.length; i < len; i++) { | ||
add(s, obj[i].name, obj[i].value); | ||
} | ||
} else { | ||
for (key in obj) { | ||
buildParams(key, obj[key]); | ||
} | ||
} | ||
return s; | ||
} | ||
|
||
return buildParams('', queryParamsObject).join('&').replace(/%20/g, '+'); | ||
} | ||
|
||
/** | ||
* Part of the `serializeQueryParams` helper function. | ||
*/ | ||
function add(s, k, v) { | ||
// Strip out keys with undefined value and replace null values with | ||
// empty strings (mimics jQuery.ajax) | ||
if (v === undefined) { | ||
return; | ||
} else if (v === null) { | ||
v = ''; | ||
} | ||
|
||
v = typeof v === 'function' ? v() : v; | ||
s[s.length] = `${encodeURIComponent(k)}=${encodeURIComponent(v)}`; | ||
} | ||
|
||
export default serializeQueryParams; |