@performant-software/shared-components 0.5.2 → 0.5.5

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.
@@ -0,0 +1,30 @@
1
+ // @flow
2
+
3
+ import _ from 'underscore';
4
+ import BaseTransform from './BaseTransform';
5
+ import StringUtils from '../utils/String';
6
+
7
+ /**
8
+ * Class for handling transforming records for PUT/POST requests. This class transforms objects in FormData. This
9
+ * class is useful if your model contains binary data to be uploaded.
10
+ */
11
+ class FormDataTransform extends BaseTransform {
12
+ /**
13
+ * Converts the passed records to a formData object to be sent on PUT/POST requests.
14
+ *
15
+ * @param record
16
+ *
17
+ * @returns {FormData}
18
+ */
19
+ toPayload(record: any) {
20
+ const formData = new FormData();
21
+
22
+ _.each(this.getPayloadKeys(), (key) => {
23
+ formData.append(`${this.getParameterName()}[${key}]`, StringUtils.toString(record[key]));
24
+ });
25
+
26
+ return formData;
27
+ }
28
+ }
29
+
30
+ export default FormDataTransform;
@@ -0,0 +1,63 @@
1
+ // @flow
2
+
3
+ import _ from 'underscore';
4
+ import StringUtils from '../utils/String';
5
+
6
+ /**
7
+ * Class for handling transforming nested attributes of a parent object. This class will handle transforming the
8
+ * object into a payload to be sent to a POST/PUT request on an API. This class currently supports transforming into
9
+ * a plain Javascript object or a FormData object.
10
+ */
11
+ class NestedAttributesTransform {
12
+ /**
13
+ * Constructs a new BaseTransform object. This constructor should never be used directly.
14
+ */
15
+ constructor() {
16
+ if (this.constructor === NestedAttributesTransform) {
17
+ throw new TypeError('Abstract class "NestedAttributesTransform" cannot be instantiated directly.');
18
+ }
19
+ }
20
+
21
+ /**
22
+ * Returns the array of payload keys.
23
+ *
24
+ * @returns {*[]}
25
+ */
26
+ getPayloadKeys() {
27
+ // Implemented in sub-class.
28
+ return [];
29
+ }
30
+
31
+ /**
32
+ * Appends the passed record's collection to the form data.
33
+ *
34
+ * @param formData
35
+ * @param prefix
36
+ * @param record
37
+ * @param collection
38
+ */
39
+ toFormData(formData: FormData, prefix: string, record: any, collection: string) {
40
+ _.each(record[collection], (item, index) => {
41
+ _.each(this.getPayloadKeys(), (key) => {
42
+ formData.append(`${prefix}[${collection}][${index}][${key}]`, StringUtils.toString(item[key]));
43
+ });
44
+ });
45
+ }
46
+
47
+ /**
48
+ * Transforms the passed record's collection to a payload object.
49
+ *
50
+ * @param record
51
+ * @param collection
52
+ *
53
+ * @returns {{[p: string]: *}}
54
+ */
55
+ toPayload(record: any, collection: string) {
56
+ return {
57
+ [collection]: _.map(record[collection],
58
+ (item, index) => ({ ..._.pick(item, this.getPayloadKeys()), order: index }))
59
+ };
60
+ }
61
+ }
62
+
63
+ export default NestedAttributesTransform;
@@ -0,0 +1,35 @@
1
+ // @flow
2
+
3
+ import NestedAttributesTransform from './NestedAttributesTransform';
4
+
5
+ /**
6
+ * Class for transforming reference codes nested objects for PUT/POST requests.
7
+ */
8
+ class ReferenceCodes extends NestedAttributesTransform {
9
+ /**
10
+ * Returns the reference codes payload keys.
11
+ *
12
+ * @returns {string[]}
13
+ */
14
+ getPayloadKeys(): Array<string> {
15
+ return [
16
+ 'id',
17
+ 'name',
18
+ '_destroy'
19
+ ];
20
+ }
21
+
22
+ /**
23
+ * Calls the super method with a default value for the collection name.
24
+ *
25
+ * @param record
26
+ * @param collection
27
+ *
28
+ * @returns {{[p: string]: *}}
29
+ */
30
+ toPayload(record: any, collection: string = 'reference_codes') {
31
+ return super.toPayload(record, collection);
32
+ }
33
+ }
34
+
35
+ export default new ReferenceCodes();
@@ -0,0 +1,55 @@
1
+ // @flow
2
+
3
+ import _ from 'underscore';
4
+ import BaseTransform from './BaseTransform';
5
+ import ReferenceCodes from './ReferenceCodes';
6
+
7
+ type ReferenceTableType = {
8
+ id: number,
9
+ name: string,
10
+ key: string
11
+ };
12
+
13
+ /**
14
+ * Class for transforming reference table objects for PUT/POST requests.
15
+ */
16
+ class ReferenceTable extends BaseTransform {
17
+ /**
18
+ * Returns the reference table parameter name.
19
+ *
20
+ * @returns {string}
21
+ */
22
+ getParameterName() {
23
+ return 'reference_table';
24
+ }
25
+
26
+ /**
27
+ * Returns the reference table payload keys.
28
+ *
29
+ * @returns {string[]}
30
+ */
31
+ getPayloadKeys() {
32
+ return [
33
+ 'name',
34
+ 'key'
35
+ ];
36
+ }
37
+
38
+ /**
39
+ * Converts the passed reference table object to JSON for PUT/POST requests.
40
+ *
41
+ * @param referenceTable
42
+ *
43
+ * @returns {*}
44
+ */
45
+ toPayload(referenceTable: ReferenceTableType) {
46
+ return {
47
+ reference_table: {
48
+ ..._.pick(referenceTable, this.getPayloadKeys()),
49
+ ...ReferenceCodes.toPayload(referenceTable)
50
+ }
51
+ };
52
+ }
53
+ }
54
+
55
+ export default new ReferenceTable();
@@ -1,14 +0,0 @@
1
- // @flow
2
-
3
- /**
4
- * Returns true if passed value is a promise (i.e, has a .then method)
5
- *
6
- * @param *
7
- *
8
- * @returns {boolean}
9
- */
10
- const isPromise = (value: any) => !!value && typeof value === 'object' && typeof value.then === 'function';
11
-
12
- export default {
13
- isPromise
14
- };