@selkirk-systems/fetch 1.5.0 → 1.5.2

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.
@@ -87,8 +87,8 @@ export async function fetchPagedURL(url, callback, options) {
87
87
  results = resultsArray[resultsArray.length - 1];
88
88
  await invokeCallbacks(cachedCallbacks, callback);
89
89
  ret = sortByProp(ret, "id");
90
- ret = ret.reduce((acc, value) => acc.concat(value.items), []);
91
90
  }
91
+ ret = ret.reduce((acc, value) => acc.concat(value.items), []);
92
92
  return [null, ret, {
93
93
  ...results,
94
94
  page: data.page.totalPages
@@ -100,6 +100,43 @@ async function invokeCallbacks(array, callback) {
100
100
  return callback(a.results, a.items);
101
101
  });
102
102
  }
103
+
104
+ /**
105
+ * @typedef {Object} Status
106
+ * @prop {number} [code=200] HTML status code
107
+ * @prop {string} [text=''] any important message, typical in errors
108
+ * @prop {boolean} [isAbort=false] was the request aborted, usually if called in quick succession
109
+ */
110
+
111
+ /**
112
+ * @typedef {Object} FetchResponse
113
+ * @prop {Object} [request=null] the generated request, null if pulled from cache
114
+ * @prop {Status} [status] current status of the request
115
+ * @prop {Object} [data] json data from the request
116
+ * @prop {Response} [response] response from the request
117
+ */
118
+
119
+ /**
120
+ * Creates a standard response obj, the fetch library will always return this structure.
121
+ * If you need to mock a response return this object.
122
+ * @param {FetchResponse} props
123
+ * @returns {FetchResponse}
124
+ */
125
+ export const createResponse = props => {
126
+ const response = new Response(props.data && JSON.stringify(props.data));
127
+ return {
128
+ request: null,
129
+ response: response,
130
+ data: null,
131
+ ...props,
132
+ status: {
133
+ code: 200,
134
+ text: '',
135
+ isAbort: false
136
+ },
137
+ ...props.status
138
+ };
139
+ };
103
140
  function getEmbedded(data) {
104
141
  return data._embedded[[Object.keys(data._embedded)[0]]];
105
142
  }
@@ -122,15 +122,14 @@ export async function fetchPagedURL( url, callback, options ) {
122
122
 
123
123
  ret = sortByProp( ret, "id" );
124
124
 
125
- ret = ret.reduce( ( acc, value ) => acc.concat( value.items ), [] );
126
-
127
125
  }
128
126
 
129
-
127
+ ret = ret.reduce( ( acc, value ) => acc.concat( value.items ), [] );
130
128
  return [null, ret, { ...results, page: data.page.totalPages }];
131
129
 
132
130
  }
133
131
 
132
+
134
133
  async function invokeCallbacks( array, callback ) {
135
134
 
136
135
  array = sortByProp( array, "index" );
@@ -141,6 +140,45 @@ async function invokeCallbacks( array, callback ) {
141
140
 
142
141
  }
143
142
 
143
+ /**
144
+ * @typedef {Object} Status
145
+ * @prop {number} [code=200] HTML status code
146
+ * @prop {string} [text=''] any important message, typical in errors
147
+ * @prop {boolean} [isAbort=false] was the request aborted, usually if called in quick succession
148
+ */
149
+
150
+ /**
151
+ * @typedef {Object} FetchResponse
152
+ * @prop {Object} [request=null] the generated request, null if pulled from cache
153
+ * @prop {Status} [status] current status of the request
154
+ * @prop {Object} [data] json data from the request
155
+ * @prop {Response} [response] response from the request
156
+ */
157
+
158
+ /**
159
+ * Creates a standard response obj, the fetch library will always return this structure.
160
+ * If you need to mock a response return this object.
161
+ * @param {FetchResponse} props
162
+ * @returns {FetchResponse}
163
+ */
164
+ export const createResponse = ( props ) => {
165
+
166
+ const response = new Response( props.data && JSON.stringify( props.data ) );
167
+
168
+ return {
169
+ request: null,
170
+ response: response,
171
+ data: null,
172
+ ...props,
173
+ status: {
174
+ code: 200,
175
+ text: '',
176
+ isAbort: false
177
+ },
178
+ ...props.status
179
+ }
180
+ }
181
+
144
182
 
145
183
  function getEmbedded( data ) {
146
184
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@selkirk-systems/fetch",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "description": "Abortable fetch library",
5
5
  "keywords": [],
6
6
  "author": "Marcos Bernal <mbernal@selkirksystems.com>",
@@ -36,5 +36,5 @@
36
36
  "peerDependencies": {
37
37
  "@selkirk-systems/state-management": ">=1.0.0"
38
38
  },
39
- "gitHead": "3f425f25b96a2239bea972a184391ceb709f64bb"
39
+ "gitHead": "97254eb48b612c80120c1285278c8bda7c5b8874"
40
40
  }