@data-client/rest 0.7.6 → 0.8.0
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.
- package/dist/index.js +18 -76
- package/dist/index.umd.min.js +1 -1
- package/dist/next.js +0 -566
- package/legacy/NetworkError.js +2 -2
- package/legacy/RestEndpoint.js +16 -17
- package/legacy/RestEndpointTypes.js +1 -1
- package/legacy/createResource.js +2 -2
- package/legacy/hookifyResource.js +2 -2
- package/legacy/index.js +1 -2
- package/legacy/next/index.js +2 -4
- package/legacy/resourceTypes.js +1 -1
- package/lib/NetworkError.d.ts +1 -1
- package/lib/NetworkError.js +2 -2
- package/lib/RestEndpoint.d.ts +1 -1
- package/lib/RestEndpoint.js +16 -17
- package/lib/RestEndpointTypes.d.ts +15 -15
- package/lib/RestEndpointTypes.d.ts.map +1 -1
- package/lib/RestEndpointTypes.js +1 -1
- package/lib/createResource.d.ts +1 -1
- package/lib/createResource.js +2 -2
- package/lib/hookifyResource.d.ts +1 -1
- package/lib/hookifyResource.js +2 -2
- package/lib/index.d.ts +0 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -2
- package/lib/next/index.d.ts +0 -5
- package/lib/next/index.d.ts.map +1 -1
- package/lib/next/index.js +2 -4
- package/lib/resourceTypes.d.ts +13 -13
- package/lib/resourceTypes.d.ts.map +1 -1
- package/lib/resourceTypes.js +1 -1
- package/package.json +2 -2
- package/src/NetworkError.ts +1 -1
- package/src/RestEndpoint.d.ts +1 -1
- package/src/RestEndpoint.js +14 -13
- package/src/RestEndpointTypes.ts +15 -15
- package/src/__tests__/Resource.test.ts +10 -8
- package/src/__tests__/RestEndpoint.ts +55 -13
- package/src/__tests__/__snapshots__/RestEndpoint.ts.snap +9 -1
- package/src/__tests__/createResource.test.ts +16 -16
- package/src/__tests__/hookifyResource.ts +9 -9
- package/src/createResource.ts +1 -1
- package/src/hookifyResource.ts +1 -1
- package/src/index.ts +0 -1
- package/src/next/index.ts +0 -5
- package/src/resourceTypes.ts +13 -13
- package/ts4.0/NetworkError.d.ts +1 -1
- package/ts4.0/RestEndpoint.d.ts +1 -1
- package/ts4.0/RestEndpointTypes.d.ts +15 -15
- package/ts4.0/createResource.d.ts +1 -1
- package/ts4.0/hookifyResource.d.ts +1 -1
- package/ts4.0/index.d.ts +0 -1
- package/ts4.0/next/index.d.ts +0 -5
- package/ts4.0/resourceTypes.d.ts +13 -13
- package/ts4.1/NetworkError.d.ts +1 -1
- package/ts4.1/RestEndpoint.d.ts +1 -1
- package/ts4.1/RestEndpointTypes.d.ts +15 -15
- package/ts4.1/createResource.d.ts +1 -1
- package/ts4.1/hookifyResource.d.ts +1 -1
- package/ts4.1/index.d.ts +0 -1
- package/ts4.1/next/index.d.ts +0 -5
- package/ts4.1/resourceTypes.d.ts +13 -13
- package/legacy/getArrayPath.js +0 -19
- package/legacy/paginationUpdate.js +0 -36
- package/lib/getArrayPath.d.ts +0 -3
- package/lib/getArrayPath.d.ts.map +0 -1
- package/lib/getArrayPath.js +0 -19
- package/lib/paginationUpdate.d.ts +0 -8
- package/lib/paginationUpdate.d.ts.map +0 -1
- package/lib/paginationUpdate.js +0 -40
- package/src/__tests__/getArrayPath.test.ts +0 -30
- package/src/getArrayPath.ts +0 -20
- package/src/paginationUpdate.ts +0 -49
- package/ts4.0/getArrayPath.d.ts +0 -3
- package/ts4.0/paginationUpdate.d.ts +0 -10
- package/ts4.1/getArrayPath.d.ts +0 -3
- package/ts4.1/paginationUpdate.d.ts +0 -10
package/dist/index.js
CHANGED
|
@@ -67,7 +67,7 @@ function mapCollection(s, mapper) {
|
|
|
67
67
|
|
|
68
68
|
/** An error with a Rest Endpoint fetch
|
|
69
69
|
*
|
|
70
|
-
* @see https://
|
|
70
|
+
* @see https://dataclient.io/rest/api/NetworkError
|
|
71
71
|
*/
|
|
72
72
|
class NetworkError extends Error {
|
|
73
73
|
constructor(response) {
|
|
@@ -102,63 +102,6 @@ function createPaginationSchema(removeCursor, collection) {
|
|
|
102
102
|
});
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
function getArrayPath(s) {
|
|
106
|
-
if (typeof s !== 'object') return false;
|
|
107
|
-
if (s === undefined || s instanceof endpoint.schema.Array || Array.isArray(s)) {
|
|
108
|
-
return [];
|
|
109
|
-
}
|
|
110
|
-
const o = s instanceof endpoint.schema.Object ? s.schema : s;
|
|
111
|
-
for (const k in o) {
|
|
112
|
-
if (!o[k]) continue;
|
|
113
|
-
const path = getArrayPath(o[k]);
|
|
114
|
-
if (path !== false) {
|
|
115
|
-
// mutation is ok because there is only one path
|
|
116
|
-
path.unshift(k);
|
|
117
|
-
return path;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function paginationUpdate(endpoint, removeCursor) {
|
|
124
|
-
// infer path from schema. if schema is undefined assume array is top level
|
|
125
|
-
const path = getArrayPath(endpoint.schema);
|
|
126
|
-
if (path === false) throw new Error('schema has no array');
|
|
127
|
-
return (newPage, ...args) => ({
|
|
128
|
-
[endpoint.key(...removeCursor(...args))]: existing => {
|
|
129
|
-
const existingList = getIn(existing, path);
|
|
130
|
-
// using set so our lookups are O(1) in case this is large
|
|
131
|
-
const existingSet = new Set(existingList);
|
|
132
|
-
const addedList = getIn(newPage, path).filter(pk => !existingSet.has(pk));
|
|
133
|
-
const mergedResults = [...existingList, ...addedList];
|
|
134
|
-
return setIn(newPage, path, mergedResults);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
const getIn = (results, path) => {
|
|
139
|
-
let cur = results;
|
|
140
|
-
for (const p of path) {
|
|
141
|
-
if (!cur) return [];
|
|
142
|
-
cur = cur[p];
|
|
143
|
-
}
|
|
144
|
-
return cur || [];
|
|
145
|
-
};
|
|
146
|
-
const setIn = (results, path, values) => {
|
|
147
|
-
if (path.length === 0) return values;
|
|
148
|
-
const newResults = {
|
|
149
|
-
...results
|
|
150
|
-
};
|
|
151
|
-
let cur = newResults;
|
|
152
|
-
for (let i = 0; i < path.length - 1; i++) {
|
|
153
|
-
const p = path[i];
|
|
154
|
-
cur = cur[p] = {
|
|
155
|
-
...cur[p]
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
cur[path[path.length - 1]] = values;
|
|
159
|
-
return newResults;
|
|
160
|
-
};
|
|
161
|
-
|
|
162
105
|
function paramsToString(searchParams) {
|
|
163
106
|
const params = new URLSearchParams(searchParams);
|
|
164
107
|
params.sort();
|
|
@@ -201,7 +144,7 @@ function shortenPath(path) {
|
|
|
201
144
|
|
|
202
145
|
/** Simplifies endpoint definitions that follow REST patterns
|
|
203
146
|
*
|
|
204
|
-
* @see https://
|
|
147
|
+
* @see https://dataclient.io/rest/api/RestEndpoint
|
|
205
148
|
*/
|
|
206
149
|
var _hasBody = /*#__PURE__*/_classPrivateFieldLooseKey("hasBody");
|
|
207
150
|
class RestEndpoint extends endpoint.Endpoint {
|
|
@@ -318,7 +261,7 @@ class RestEndpoint extends endpoint.Endpoint {
|
|
|
318
261
|
See https://www.rfc-editor.org/rfc/rfc4627 for information on JSON responses
|
|
319
262
|
|
|
320
263
|
Using parsed JSON.
|
|
321
|
-
If text content was expected see https://
|
|
264
|
+
If text content was expected see https://dataclient.io/rest/api/RestEndpoint#parseResponse`;
|
|
322
265
|
}
|
|
323
266
|
} else {
|
|
324
267
|
error.message = `Unexpected html response for schema ${this.schema}
|
|
@@ -379,17 +322,14 @@ Response (first 300 characters): ${text.substring(0, 300)}`;
|
|
|
379
322
|
return createPaginationSchema(removeCursor, collection);
|
|
380
323
|
};
|
|
381
324
|
const newSchema = mapCollection(this.schema, createPaginatedSchema);
|
|
382
|
-
if (found)
|
|
383
|
-
|
|
384
|
-
return this.extend({
|
|
385
|
-
schema: newSchema,
|
|
386
|
-
key(...args) {
|
|
387
|
-
return sup.key.call(this, ...removeCursor(...args));
|
|
388
|
-
}
|
|
389
|
-
});
|
|
390
|
-
}
|
|
325
|
+
if (!found) throw new Error('Missing Collection');
|
|
326
|
+
const sup = this;
|
|
391
327
|
return this.extend({
|
|
392
|
-
|
|
328
|
+
schema: newSchema,
|
|
329
|
+
key(...args) {
|
|
330
|
+
return sup.key.call(this, ...removeCursor(...args));
|
|
331
|
+
},
|
|
332
|
+
name: this.name + '.getPage'
|
|
393
333
|
});
|
|
394
334
|
}
|
|
395
335
|
get getPage() {
|
|
@@ -398,19 +338,22 @@ Response (first 300 characters): ${text.substring(0, 300)}`;
|
|
|
398
338
|
get push() {
|
|
399
339
|
return this.extend({
|
|
400
340
|
method: 'POST',
|
|
401
|
-
schema: extractCollection(this.schema, s => s.push)
|
|
341
|
+
schema: extractCollection(this.schema, s => s.push),
|
|
342
|
+
name: this.name + '.create'
|
|
402
343
|
});
|
|
403
344
|
}
|
|
404
345
|
get unshift() {
|
|
405
346
|
return this.extend({
|
|
406
347
|
method: 'POST',
|
|
407
|
-
schema: extractCollection(this.schema, s => s.unshift)
|
|
348
|
+
schema: extractCollection(this.schema, s => s.unshift),
|
|
349
|
+
name: this.name + '.create'
|
|
408
350
|
});
|
|
409
351
|
}
|
|
410
352
|
get assign() {
|
|
411
353
|
return this.extend({
|
|
412
354
|
method: 'POST',
|
|
413
|
-
schema: extractCollection(this.schema, s => s.assign)
|
|
355
|
+
schema: extractCollection(this.schema, s => s.assign),
|
|
356
|
+
name: this.name + '.create'
|
|
414
357
|
});
|
|
415
358
|
}
|
|
416
359
|
}
|
|
@@ -429,7 +372,7 @@ const {
|
|
|
429
372
|
|
|
430
373
|
/** Creates collection of Endpoints for common operations on a given data/schema.
|
|
431
374
|
*
|
|
432
|
-
* @see https://
|
|
375
|
+
* @see https://dataclient.io/rest/api/createResource
|
|
433
376
|
*/
|
|
434
377
|
function createResource({
|
|
435
378
|
path,
|
|
@@ -567,7 +510,7 @@ function ensurePojo(body) {
|
|
|
567
510
|
/** Turns a collection of Endpoints (Resource) into a collection of hooks.
|
|
568
511
|
* This is useful for Endpoints that need hooks to prepare their fetch requests.
|
|
569
512
|
*
|
|
570
|
-
* @see https://
|
|
513
|
+
* @see https://dataclient.io/rest/api/hookifyResource
|
|
571
514
|
*/
|
|
572
515
|
function hookifyResource(resource, useRequestInit) {
|
|
573
516
|
const usingResource = {};
|
|
@@ -596,7 +539,6 @@ exports.NetworkError = NetworkError;
|
|
|
596
539
|
exports.RestEndpoint = RestEndpoint;
|
|
597
540
|
exports.createResource = createResource;
|
|
598
541
|
exports.hookifyResource = hookifyResource;
|
|
599
|
-
exports.paginationUpdate = paginationUpdate;
|
|
600
542
|
Object.keys(endpoint).forEach(function (k) {
|
|
601
543
|
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
602
544
|
enumerable: true,
|
package/dist/index.umd.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@data-client/endpoint"),require("path-to-regexp")):"function"==typeof define&&define.amd?define(["exports","@data-client/endpoint","path-to-regexp"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@data-client/endpoint"),require("path-to-regexp")):"function"==typeof define&&define.amd?define(["exports","@data-client/endpoint","path-to-regexp"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).dataClientRest={},t.endpoint,t.pathToRegexp)}(this,(function(t,e,n){"use strict";function s(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function r(t,e){return t(e={exports:{}},e.exports),e.exports}var i=s(r((function(t){t.exports=function(t){if(null==t)throw new TypeError("Cannot destructure "+t)},t.exports.__esModule=!0,t.exports.default=t.exports}))),o=s(r((function(t){t.exports=function(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t},t.exports.__esModule=!0,t.exports.default=t.exports}))),a=s(r((function(t){var e=0;t.exports=function(t){return"__private_"+e+++"_"+t},t.exports.__esModule=!0,t.exports.default=t.exports})));function c(t,n){if("object"!=typeof t||void 0===t||Array.isArray(t))return;if(t instanceof e.schema.Collection)return n(t);const s=Object.assign({},t instanceof e.schema.Object?t.schema:t);for(const t in s){if(!s[t])continue;const e=c(s[t],n);if(e)return e}}function h(t,n){if("object"!=typeof t||void 0===t)return t;if(t instanceof e.schema.Collection)return n(t);const s=Object.assign({},t instanceof e.schema.Object?t.schema:t);for(const t in s)s[t]&&(s[t]=h(s[t],n));return s}class u extends Error{constructor(t){super(t.statusText||`Network response not 'ok': ${t.status}`),this.name="NetworkError",this.status=t.status,this.response=t}}function p(t,e){const n=new Set(t),s=[...t];for(const t of e)n.has(t)||s.push(t);return s}const f=Object.create(null);const d=Object.create(null);const l=Object.prototype,g=Object.getPrototypeOf;var m=a("hasBody");class O extends e.Endpoint{constructor(t){var e;super(null!=(e=t.fetch)?e:async function(...t){const e=o(this,m)[m]&&t.length<2?{}:t[0]||{},n=o(this,m)[m]?t[t.length-1]:void 0;return this.fetchResponse(this.url(e),await this.getRequestInit(n)).then((t=>this.parseResponse(t))).then((e=>this.process(e,...t)))},t),Object.defineProperty(this,m,{writable:!0,value:void 0}),(!("sideEffect"in this)||"method"in t&&!("sideEffect"in t))&&(this.sideEffect="GET"!==t.method&&void 0!==t.method||void 0),void 0===this.method&&(this.method=this.sideEffect?"POST":"GET"),void 0===this.urlPrefix&&(this.urlPrefix=""),o(this,m)[m]=!("body"in this&&void 0===this.body||["GET","DELETE"].includes(this.method)),Object.defineProperty(this,"name",{get(){return"__name"in this?this.__name:this.urlPrefix+this.path}})}key(...t){return`${this.method} ${this.url(o(this,m)[m]&&t.length<2?{}:t[0]||{})}`}url(t={}){const e=((s=this.path)in f||(f[s]=n.compile(s,{encode:encodeURIComponent,validate:!1})),f[s])(t);var s;const r=function(t){return t in d||(d[t]=new Set(n.parse(t).map((t=>"string"==typeof t?t:`${t.name}`)))),d[t]}(this.path),i={};return Object.keys(t).forEach((e=>{r.has(e)||(i[e]=t[e])})),Object.keys(i).length?`${this.urlPrefix}${e}?${function(t){const e=new URLSearchParams(t);return e.sort(),e.toString()}(i)}`:`${this.urlPrefix}${e}`}getHeaders(t){return t}async getRequestInit(t){const e=null!==(n=t)&&"object"==typeof n&&g(n)===l;var n;e&&(t=JSON.stringify(t));const s=Object.assign({},this.requestInit,{method:this.method,signal:this.signal,body:t});return t&&!e||(s.headers=Object.assign({"Content-Type":"application/json"},s.headers)),s.headers=await this.getHeaders(s.headers),s}fetchResponse(t,e){return fetch(t,e).then((t=>{if(!t.ok)throw new u(t);return t})).catch((t=>{throw t instanceof TypeError&&(t.status=500),t}))}parseResponse(t){var e;return 204===t.status?Promise.resolve(null):null!=(e=t.headers.get("content-type"))&&e.includes("json")?t.json().catch((t=>{throw t.status=400,t})):t.text().then((e=>{if(["string","undefined"].includes(typeof this.schema)||null===this.schema||this.process!==O.prototype.process)return e;const n=new u(t);throw n.status=404,n.message=`Unexpected text response for schema ${this.schema}`,n}))}process(t){return t}errorPolicy(t){return t.status>=500?"soft":void 0}get pathRegex(){return n.pathToRegexp(this.path)}testKey(t){const e=this.method+" "+this.urlPrefix;if(!t.startsWith(e))return!1;let n=t.lastIndexOf("?");return-1===n&&(n=void 0),this.pathRegex.test(t.substring(e.length,n))}extend(t){class e extends this.constructor{}return Object.assign(e.prototype,this),new e(Object.assign({fetch:this.fetch},t))}paginated(t){if("string"==typeof t){const e=t;t=t=>{let n=Object.assign({},(i(t),t));return delete n[e],[n]}}let e=!1;const n=h(this.schema,(n=>(e=!0,function(t,e){return Object.create(e,{name:{value:`Pagination(${e.schema})`},merge:{value:p},pk:{value:function(n,s,r,i){return e.pk.call(this,n,s,r,t(...i))}}})}(t,n))));if(!e)throw new Error("Missing Collection");const s=this;return this.extend({schema:n,key(...e){return s.key.call(this,...t(...e))},name:this.name+".getPage"})}get getPage(){return this.paginated(this.paginationField)}get push(){return this.extend({method:"POST",schema:c(this.schema,(t=>t.push)),name:this.name+".create"})}get unshift(){return this.extend({method:"POST",schema:c(this.schema,(t=>t.unshift)),name:this.name+".create"})}get assign(){return this.extend({method:"POST",schema:c(this.schema,(t=>t.assign)),name:this.name+".create"})}}var b=s(r((function(t){t.exports=function(t,e){if(null==t)return{};var n,s,r={},i=Object.keys(t);for(s=0;s<i.length;s++)n=i[s],e.indexOf(n)>=0||(r[n]=t[n]);return r},t.exports.__esModule=!0,t.exports.default=t.exports})));const x=["path","schema","Endpoint","Collection","optimistic","paginationField"],{Invalidate:y,Collection:j}=e.schema;function w(t,e,n){return Object.assign({},e,E(n))}function v(t,e){return e}function E(t){return t instanceof FormData?Object.fromEntries(t.entries()):t}Object.hasOwn=Object.hasOwn||function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},t.NetworkError=u,t.RestEndpoint=O,t.createResource=function(t){let{path:n,schema:s,Endpoint:r=O,Collection:i=j,optimistic:o,paginationField:a}=t,c=b(t,x);const h=function(t){const e=t.lastIndexOf(":");if(-1===e)throw new Error("Resource path requires at least one :parameter");let n=t.substring(0,e);return"/"===n[n.length-1]&&(n=n.substring(0,n.length-1)),n}(n),u=t=>`${null==s?void 0:s.name}.${t}`;function p(t,e,n){t[e]=t[e].extend(n)}const f=Object.assign({},c),d=Object.assign({},c),l=new r(Object.assign({},c,{path:n,schema:s,name:u("get")}));var g;o&&(f.getOptimisticResponse=w,d.getOptimisticResponse=(g=l,function(t,n,s){const{data:r}=t.getResponse(g,n);if(!r)throw new e.AbortOptimistic;return Object.assign({},n,r,E(s))}));const m=new r(Object.assign({},f,{paginationField:a,path:h,schema:new i([s]),name:u("getList")}));return{get:l,getList:m,create:m.push.extend({name:u("create")}),update:new r(Object.assign({},f,{path:n,schema:s,method:"PUT",name:u("update")})),partialUpdate:new r(Object.assign({},d,{path:n,schema:s,method:"PATCH",name:u("partialUpdate")})),delete:new r(Object.assign({},f,{path:n,schema:s.process?new y(s):s,method:"DELETE",name:u("delete"),process:(t,e)=>t&&Object.keys(t).length?t:e,getOptimisticResponse:o?v:void 0})),extend(...t){if("string"==typeof t[0]){const[e,n]=t;if(e in this){const t=Object.assign({},this);return p(t,e,n),t}return Object.assign({},this,{[e]:this.get.extend(n)})}if("function"==typeof t[0]){const e=t[0](this);return Object.assign({},this,e)}const e=t[0],n=Object.assign({},this);for(const t in e)p(n,t,e[t]);return n}}},t.hookifyResource=function(t,e){const n={};return Object.keys(t).forEach((s=>{const r=t[s];var i;void 0!==r.extend&&(n[`use${i=s,i.charAt(0).toUpperCase()+i.slice(1)}`]=()=>{const t=e();return r.extend({requestInit:t})})})),n},Object.keys(e).forEach((function(n){"default"===n||t.hasOwnProperty(n)||Object.defineProperty(t,n,{enumerable:!0,get:function(){return e[n]}})})),Object.defineProperty(t,"__esModule",{value:!0})}));
|