@fleetbase/ember-core 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ import { inject as service } from '@ember/service';
5
5
  import { storageFor } from 'ember-local-storage';
6
6
  import { get } from '@ember/object';
7
7
  import { isBlank } from '@ember/utils';
8
+ import { isArray } from '@ember/array';
8
9
  import { dasherize } from '@ember/string';
9
10
  import { pluralize } from 'ember-inflector';
10
11
  import { decompress as decompressJson } from 'compress-json';
@@ -14,7 +15,7 @@ import config from 'ember-get-config';
14
15
  if (isBlank(config.API.host)) {
15
16
  config.API.host = `${window.location.protocol}//${window.location.hostname}:8000`;
16
17
  }
17
-
18
+ const DEFAULT_ERROR_MESSAGE = 'Oops! Something went wrong. Please try again or contact support if the issue persists.';
18
19
  export default class ApplicationAdapter extends RESTAdapter {
19
20
  /**
20
21
  * Inject the `session` service
@@ -151,22 +152,22 @@ export default class ApplicationAdapter extends RESTAdapter {
151
152
  }
152
153
 
153
154
  /**
154
- * Handles the response from an AJAX request in an Ember application.
155
- *
156
- * @param {number} status - The HTTP status code of the response.
157
- * @param {object} headers - The headers of the response.
158
- * @param {object} payload - The payload of the response.
159
- * @return {Object | AdapterError} response - Returns a new `AdapterError` instance with detailed error information if the response is invalid; otherwise, it returns the result of the superclass's `handleResponse` method.
155
+ * Handles the response from an AJAX request.
156
+ * It decompresses the payload if needed, checks for error responses, and returns an AdapterError for error scenarios.
157
+ * For valid responses, the handling is delegated to the superclass's handleResponse method.
160
158
  *
161
- * This method first normalizes the error response and generates a detailed message.
162
- * It then checks if the response is invalid based on the status code. If invalid, it constructs an `AdapterError` with the normalized errors and detailed message.
163
- * For valid responses, it delegates the handling to the superclass's `handleResponse` method.
159
+ * @param {number} status - HTTP status code of the response.
160
+ * @param {object} headers - Response headers.
161
+ * @param {object} payload - Response payload.
162
+ * @param {object} requestData - Original request data.
163
+ * @return {Object | AdapterError} - The response object or an AdapterError in case of errors.
164
164
  */
165
- async handleResponse(status, headers, payload, requestData) {
166
- let decompressedPayload = this.decompressPayload(payload, headers);
167
- let errors = this.normalizeErrorResponse(status, headers, payload);
168
- if (this.isInvalid(status, headers, payload)) {
169
- return new AdapterError(errors);
165
+ handleResponse(status, headers, payload, requestData) {
166
+ const decompressedPayload = this.decompressPayload(payload, headers);
167
+ if (this.isErrorResponse(status, decompressedPayload)) {
168
+ const errors = this.getResponseErrors(decompressedPayload);
169
+ const errorMessage = this.getErrorMessage(errors);
170
+ return new AdapterError(errors, errorMessage);
170
171
  }
171
172
 
172
173
  return super.handleResponse(status, headers, decompressedPayload, requestData);
@@ -184,7 +185,7 @@ export default class ApplicationAdapter extends RESTAdapter {
184
185
  * @param {object} headers - The headers of the response, used to check if the payload is compressed.
185
186
  * @return {object} The decompressed payload if it was compressed, or the original payload otherwise.
186
187
  */
187
- async decompressPayload(payload, headers) {
188
+ decompressPayload(payload, headers) {
188
189
  // Check if the response is compressed
189
190
  if (headers['x-compressed-json'] === '1' || headers['x-compressed-json'] === 1) {
190
191
  // Decompress the payload
@@ -195,4 +196,38 @@ export default class ApplicationAdapter extends RESTAdapter {
195
196
 
196
197
  return payload;
197
198
  }
199
+
200
+ /**
201
+ * Extracts the error message from a list of errors.
202
+ * Returns a default error message if the provided list is empty or undefined.
203
+ *
204
+ * @param {Array} errors - Array of error messages or objects.
205
+ * @return {string} - The extracted error message.
206
+ */
207
+ getErrorMessage(errors = []) {
208
+ return errors[0] ? errors[0] : DEFAULT_ERROR_MESSAGE;
209
+ }
210
+
211
+ /**
212
+ * Extracts errors from a payload.
213
+ * Assumes the payload contains an `errors` array; returns a default error message otherwise.
214
+ *
215
+ * @param {object} payload - The response payload.
216
+ * @return {Array} - Array of extracted errors or a default error message.
217
+ */
218
+ getResponseErrors(payload) {
219
+ return isArray(payload.errors) ? payload.errors : [DEFAULT_ERROR_MESSAGE];
220
+ }
221
+
222
+ /**
223
+ * Determines if the response status indicates an error.
224
+ * Checks both the HTTP status code and the presence of errors in the payload.
225
+ *
226
+ * @param {number} status - The HTTP status code.
227
+ * @param {object} payload - The response payload.
228
+ * @return {boolean} - True if the response indicates an error, false otherwise.
229
+ */
230
+ isErrorResponse(status, payload) {
231
+ return (status >= 400 && status < 600) || (!isBlank(payload) && isArray(payload.errors));
232
+ }
198
233
  }
@@ -0,0 +1,33 @@
1
+ import getModelName from '@fleetbase/ember-core/utils/get-model-name';
2
+ import isModel from '@fleetbase/ember-core/utils/is-model';
3
+ import { camelize } from '@ember/string';
4
+
5
+ /**
6
+ * Applies context and dynamic arguments to a given component.
7
+ *
8
+ * @param {Component} component - The component to which context and arguments will be applied.
9
+ */
10
+ export default function applyContextComponentArguments(component) {
11
+ const { context, dynamicArgs = {} } = component.args;
12
+
13
+ // Apply context model if available
14
+ if (context && isModel(context)) {
15
+ const contextModelName = camelize(getModelName(context));
16
+ if (contextModelName) {
17
+ component[contextModelName] = context;
18
+ }
19
+ }
20
+
21
+ // Execute any apply callback present in dynamic arguments
22
+ const { applyCallback } = dynamicArgs;
23
+ if (typeof applyCallback === 'function') {
24
+ applyCallback(component);
25
+ }
26
+
27
+ // Apply other dynamic arguments to the component
28
+ for (const [key, value] of Object.entries(dynamicArgs)) {
29
+ if (key !== 'applyCallback') {
30
+ component[key] = value;
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,16 @@
1
+ export default function contextComponentCallback(component, name, ...params) {
2
+ let callbackInvoked = false;
3
+
4
+ if (typeof component.args[name] === 'function') {
5
+ component.args[name](...params);
6
+ callbackInvoked = true;
7
+ }
8
+
9
+ // now do for context options
10
+ if (typeof component.args.options === 'object' && typeof component.args.options[name] === 'function') {
11
+ component.args.options[name](...params);
12
+ callbackInvoked = true;
13
+ }
14
+
15
+ return callbackInvoked;
16
+ }
@@ -0,0 +1 @@
1
+ export { default } from '@fleetbase/ember-core/utils/apply-context-component-arguments';
@@ -0,0 +1 @@
1
+ export { default } from '@fleetbase/ember-core/utils/context-component-callback';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fleetbase/ember-core",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
5
5
  "keywords": [
6
6
  "fleetbase-core",