|
| 1 | +// @flow |
| 2 | + |
| 3 | +export const ensureIsObject = ({ |
| 4 | + data, |
| 5 | + endpointName, |
| 6 | +}: {| |
| 7 | + data: any, |
| 8 | + endpointName: string, |
| 9 | +|}): any => { |
| 10 | + if (!data || typeof data !== 'object') { |
| 11 | + throw new Error( |
| 12 | + `Invalid response from endpoint ${endpointName}, was expecting an object.` |
| 13 | + ); |
| 14 | + } |
| 15 | + return data; |
| 16 | +}; |
| 17 | + |
| 18 | +export const ensureIsObjectOrNull = ({ |
| 19 | + data, |
| 20 | + endpointName, |
| 21 | +}: {| |
| 22 | + data: any, |
| 23 | + endpointName: string, |
| 24 | +|}): any => { |
| 25 | + if (data !== null && (!data || typeof data !== 'object')) { |
| 26 | + throw new Error( |
| 27 | + `Invalid response from endpoint ${endpointName}, was expecting an object or null.` |
| 28 | + ); |
| 29 | + } |
| 30 | + return data; |
| 31 | +}; |
| 32 | + |
| 33 | +export const ensureObjectHasProperty = ({ |
| 34 | + data, |
| 35 | + propertyName, |
| 36 | + endpointName, |
| 37 | +}: {| |
| 38 | + data: any, |
| 39 | + propertyName: string, |
| 40 | + endpointName: string, |
| 41 | +|}): any => { |
| 42 | + if (!data || typeof data !== 'object') { |
| 43 | + throw new Error( |
| 44 | + `Invalid response from endpoint ${endpointName}, was expecting an object.` |
| 45 | + ); |
| 46 | + } |
| 47 | + if (data[propertyName] === undefined) { |
| 48 | + throw new Error( |
| 49 | + `Invalid response from endpoint ${endpointName}, was expecting an object with a ${propertyName} field.` |
| 50 | + ); |
| 51 | + } |
| 52 | + return data; |
| 53 | +}; |
| 54 | + |
| 55 | +export const ensureIsNullOrObjectHasProperty = ({ |
| 56 | + data, |
| 57 | + propertyName, |
| 58 | + endpointName, |
| 59 | +}: {| |
| 60 | + data: any, |
| 61 | + propertyName: string, |
| 62 | + endpointName: string, |
| 63 | +|}): any => { |
| 64 | + if (data !== null) { |
| 65 | + if (!data || typeof data !== 'object') { |
| 66 | + throw new Error( |
| 67 | + `Invalid response from endpoint ${endpointName}, was expecting an object or null.` |
| 68 | + ); |
| 69 | + } |
| 70 | + if (data[propertyName] === undefined) { |
| 71 | + throw new Error( |
| 72 | + `Invalid response from endpoint ${endpointName}, was expecting an object with a ${propertyName} field.` |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + return data; |
| 77 | +}; |
| 78 | + |
| 79 | +export const ensureIsArray = ({ |
| 80 | + data, |
| 81 | + endpointName, |
| 82 | +}: {| |
| 83 | + data: any, |
| 84 | + endpointName: string, |
| 85 | +|}): Array<any> => { |
| 86 | + if (!Array.isArray(data)) { |
| 87 | + throw new Error( |
| 88 | + `Invalid response from endpoint ${endpointName}, was expecting an array.` |
| 89 | + ); |
| 90 | + } |
| 91 | + return data; |
| 92 | +}; |
| 93 | + |
| 94 | +export const ensureIsArrayOrNull = ({ |
| 95 | + data, |
| 96 | + endpointName, |
| 97 | +}: {| |
| 98 | + data: any, |
| 99 | + endpointName: string, |
| 100 | +|}): Array<any> | null => { |
| 101 | + if (data !== null && !Array.isArray(data)) { |
| 102 | + throw new Error( |
| 103 | + `Invalid response from endpoint ${endpointName}, was expecting an array or null.` |
| 104 | + ); |
| 105 | + } |
| 106 | + return data; |
| 107 | +}; |
| 108 | + |
| 109 | +export const ensureIsString = ({ |
| 110 | + data, |
| 111 | + endpointName, |
| 112 | +}: {| |
| 113 | + data: any, |
| 114 | + endpointName: string, |
| 115 | +|}): string => { |
| 116 | + if (typeof data !== 'string') { |
| 117 | + throw new Error( |
| 118 | + `Invalid response from endpoint ${endpointName}, was expecting a string.` |
| 119 | + ); |
| 120 | + } |
| 121 | + return data; |
| 122 | +}; |
| 123 | + |
| 124 | +export const ensureIsObjectWithPropertyOfType = ({ |
| 125 | + data, |
| 126 | + propertyName, |
| 127 | + propertyType, |
| 128 | + endpointName, |
| 129 | +}: {| |
| 130 | + data: any, |
| 131 | + propertyName: string, |
| 132 | + propertyType: 'string' | 'number' | 'boolean' | 'object' | 'array', |
| 133 | + endpointName: string, |
| 134 | +|}): any => { |
| 135 | + if (!data || typeof data !== 'object') { |
| 136 | + throw new Error( |
| 137 | + `Invalid response from endpoint ${endpointName}, was expecting an object.` |
| 138 | + ); |
| 139 | + } |
| 140 | + if (data[propertyName] === undefined) { |
| 141 | + throw new Error( |
| 142 | + `Invalid response from endpoint ${endpointName}, was expecting an object with a ${propertyName} field.` |
| 143 | + ); |
| 144 | + } |
| 145 | + const propertyValue = data[propertyName]; |
| 146 | + let isValidType = false; |
| 147 | + if (propertyType === 'array') { |
| 148 | + isValidType = Array.isArray(propertyValue); |
| 149 | + } else { |
| 150 | + isValidType = typeof propertyValue === propertyType; |
| 151 | + } |
| 152 | + if (!isValidType) { |
| 153 | + throw new Error( |
| 154 | + `Invalid response from endpoint ${endpointName}, was expecting an object with a ${propertyName} field of type ${propertyType}.` |
| 155 | + ); |
| 156 | + } |
| 157 | + return data; |
| 158 | +}; |
0 commit comments