diff --git a/.eslintignore b/.eslintignore index 4e982747..25d162b6 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,5 +1,6 @@ # unconventional js /blueprints/*/files/ +**/*.d.ts # compiled output /dist/ diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 136bbaba..3003166b 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -12,6 +12,7 @@ module.exports = { parserOptions: { ecmaVersion: 'latest', sourceType: 'module', + requireConfigFile: false, babelOptions: { root: __dirname, }, diff --git a/package.json b/package.json index aa90abe0..fe34b028 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -26,6 +30,7 @@ "start": "rollup --config --watch" }, "dependencies": { + "@ember/test-waiters": "^3.1.0", "@embroider/addon-shim": "^1.8.7" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3fa984e4..154827cc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: dependencies: + '@ember/test-waiters': + specifier: ^3.1.0 + version: 3.1.0 '@embroider/addon-shim': specifier: ^1.8.7 version: 1.9.0 diff --git a/src/errors.d.ts b/src/errors.d.ts new file mode 100644 index 00000000..fd05e214 --- /dev/null +++ b/src/errors.d.ts @@ -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; diff --git a/src/errors.js b/src/errors.js new file mode 100644 index 00000000..cb85c7e8 --- /dev/null +++ b/src/errors.js @@ -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; +} diff --git a/src/utils/serialize-query-params.d.ts b/src/utils/serialize-query-params.d.ts new file mode 100644 index 00000000..687282e4 --- /dev/null +++ b/src/utils/serialize-query-params.d.ts @@ -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; diff --git a/src/utils/serialize-query-params.js b/src/utils/serialize-query-params.js new file mode 100644 index 00000000..bf046221 --- /dev/null +++ b/src/utils/serialize-query-params.js @@ -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;