Skip to content

Commit

Permalink
Add errors and serialize-query-params
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Nov 5, 2024
1 parent a3cbc17 commit 5b91d51
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# unconventional js
/blueprints/*/files/
**/*.d.ts

# compiled output
/dist/
Expand Down
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
requireConfigFile: false,
babelOptions: {
root: __dirname,
},
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"types": "./src/index.d.ts",
"default": "./src/index.js"
},
"./*": {
"types": "./src/*.d.ts",
"default": "./src/*.js"
},
"./addon-main.js": "./addon-main.cjs"
},
"files": [
Expand All @@ -26,6 +30,7 @@
"start": "rollup --config --watch"
},
"dependencies": {
"@ember/test-waiters": "^3.1.0",
"@embroider/addon-shim": "^1.8.7"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions src/errors.d.ts
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;
35 changes: 35 additions & 0 deletions src/errors.js
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;
}
9 changes: 9 additions & 0 deletions src/utils/serialize-query-params.d.ts
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;
67 changes: 67 additions & 0 deletions src/utils/serialize-query-params.js
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;

0 comments on commit 5b91d51

Please sign in to comment.