@rest-hooks/endpoint
Advanced tools
Comparing version 0.5.3 to 0.6.0
@@ -6,2 +6,11 @@ # Change Log | ||
## 0.6.0 (2020-08-08) | ||
* internal: Test using endpoints directly (#389) ([bb0e8fd](https://github.com/coinbase/rest-hooks/commit/bb0e8fd)), closes [#389](https://github.com/coinbase/rest-hooks/issues/389) | ||
* feat: Support extra endpoint members and inheritance (#387) ([6ad5486](https://github.com/coinbase/rest-hooks/commit/6ad5486)), closes [#387](https://github.com/coinbase/rest-hooks/issues/387) | ||
## <small>0.5.3 (2020-08-04)</small> | ||
@@ -8,0 +17,0 @@ |
@@ -105,2 +105,19 @@ 'use strict'; | ||
function runCompat(endpoint, options) { | ||
endpoint.type = endpoint.sideEffect ? 'mutate' : 'read'; | ||
endpoint.options = Object.assign({}, options); | ||
delete endpoint.options.key; | ||
delete endpoint.options.schema; | ||
delete endpoint.options.sideEffect; | ||
delete endpoint.options.fetch; | ||
delete endpoint.options.getFetchKey; | ||
delete endpoint.options.options; | ||
if (Object.keys(endpoint.options).length === 0) { | ||
delete endpoint.options; | ||
} | ||
if (endpoint.schema === undefined) endpoint.schema = null; | ||
} | ||
var Endpoint = /*#__PURE__*/function (_Function) { | ||
@@ -113,3 +130,2 @@ _inheritsLoose(Endpoint, _Function); | ||
_this = _Function.call(this, 'return arguments.callee.fetch.apply(arguments.callee, arguments)') || this; | ||
_this.schema = null; | ||
@@ -120,16 +136,7 @@ _this.getFetchKey = function (params) { | ||
_this.fetch = fetchFunction; | ||
if (fetchFunction) _this.fetch = fetchFunction; | ||
Object.assign(_assertThisInitialized(_this), options); | ||
/** The following is for compatibility with FetchShape */ | ||
_this.type = _this.sideEffect ? 'mutate' : 'read'; | ||
_this.options = Object.assign({}, options); | ||
delete _this.options.key; | ||
delete _this.options.schema; | ||
delete _this.options.sideEffect; | ||
if (Object.keys(_this.options).length === 0) { | ||
delete _this.options; | ||
} | ||
runCompat(_assertThisInitialized(_this), options); | ||
return _this; | ||
@@ -145,16 +152,20 @@ } | ||
_proto.extend = function extend(options) { | ||
var _options$fetch; | ||
// make a constructor/prototype based off this | ||
// extend from it and init with options sent | ||
var E = /*#__PURE__*/function (_this$constructor) { | ||
_inheritsLoose(E, _this$constructor); | ||
function E() { | ||
return _this$constructor.apply(this, arguments) || this; | ||
} | ||
return E; | ||
}(this.constructor); | ||
Object.assign(E.prototype, this); | ||
var instance = new E(options.fetch, options); | ||
/** The following is for compatibility with FetchShape */ | ||
var optionsCopy = Object.assign({}, options); | ||
delete optionsCopy.key; | ||
delete optionsCopy.schema; | ||
delete optionsCopy.sideEffect; | ||
var optionsToPass = Object.assign({}, this, options, { | ||
/** The following is for compatibility with FetchShape */ | ||
options: Object.assign({}, this.options, { | ||
optionsCopy: optionsCopy | ||
}) | ||
}); | ||
return new this.constructor((_options$fetch = options.fetch) != null ? _options$fetch : this.fetch, optionsToPass); | ||
runCompat(instance, Object.assign({}, this, options)); | ||
return instance; | ||
} | ||
@@ -169,2 +180,8 @@ /** The following is for compatibility with FetchShape */ | ||
function Index(schema, key) { | ||
var _this = this; | ||
this.getFetchKey = function (params) { | ||
return _this.key(params); | ||
}; | ||
this.schema = schema; | ||
@@ -178,3 +195,5 @@ if (key) this.key = key; | ||
return JSON.stringify(params); | ||
}; | ||
} | ||
/** The following is for compatibility with FetchShape */ | ||
; | ||
@@ -181,0 +200,0 @@ return Index; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@rest-hooks/normalizr")):"function"==typeof define&&define.amd?define(["exports","@rest-hooks/normalizr"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).restHookCore={},e.normalizr)}(this,(function(e,t){"use strict";function n(e){return(n=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function r(e,t){return(r=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function o(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function i(e,t,n){return(i=o()?Reflect.construct:function(e,t,n){var o=[null];o.push.apply(o,t);var i=new(Function.bind.apply(e,o));return n&&r(i,n.prototype),i}).apply(null,arguments)}function c(e){var t="function"==typeof Map?new Map:void 0;return(c=function(e){if(null===e||(o=e,-1===Function.toString.call(o).indexOf("[native code]")))return e;var o;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,c)}function c(){return i(e,arguments,n(this).constructor)}return c.prototype=Object.create(e.prototype,{constructor:{value:c,enumerable:!1,writable:!0,configurable:!0}}),r(c,e)})(e)}var u=function(e){var t,n;function r(t,n){var r;return(r=e.call(this,"return arguments.callee.fetch.apply(arguments.callee, arguments)")||this).schema=null,r.getFetchKey=function(e){return r.key(e)},r.fetch=t,Object.assign(function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(r),n),r.type=r.sideEffect?"mutate":"read",r.options=Object.assign({},n),delete r.options.key,delete r.options.schema,delete r.options.sideEffect,0===Object.keys(r.options).length&&delete r.options,r}n=e,(t=r).prototype=Object.create(n.prototype),t.prototype.constructor=t,t.__proto__=n;var o=r.prototype;return o.key=function(e){return this.fetch.name+" "+JSON.stringify(e)},o.extend=function(e){var t,n=Object.assign({},e);delete n.key,delete n.schema,delete n.sideEffect;var r=Object.assign({},this,e,{options:Object.assign({},this.options,{optionsCopy:n})});return new this.constructor(null!=(t=e.fetch)?t:this.fetch,r)},r}(c(Function)),f=function(){function e(e,t){this.schema=e,t&&(this.key=t)}return e.prototype.key=function(e){return JSON.stringify(e)},e}();Object.defineProperty(e,"AbstractInstanceType",{enumerable:!0,get:function(){return t.AbstractInstanceType}}),Object.defineProperty(e,"DELETED",{enumerable:!0,get:function(){return t.DELETED}}),Object.defineProperty(e,"Entity",{enumerable:!0,get:function(){return t.Entity}}),Object.defineProperty(e,"Schema",{enumerable:!0,get:function(){return t.Schema}}),Object.defineProperty(e,"SimpleRecord",{enumerable:!0,get:function(){return t.SimpleRecord}}),Object.defineProperty(e,"denormalize",{enumerable:!0,get:function(){return t.denormalize}}),Object.defineProperty(e,"isEntity",{enumerable:!0,get:function(){return t.isEntity}}),Object.defineProperty(e,"normalize",{enumerable:!0,get:function(){return t.normalize}}),Object.defineProperty(e,"schema",{enumerable:!0,get:function(){return t.schema}}),e.Endpoint=u,e.Index=f,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@rest-hooks/normalizr")):"function"==typeof define&&define.amd?define(["exports","@rest-hooks/normalizr"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).restHookCore={},e.normalizr)}(this,(function(e,t){"use strict";function n(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function r(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}function o(e){return(o=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function i(e,t){return(i=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function c(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function u(e,t,n){return(u=c()?Reflect.construct:function(e,t,n){var r=[null];r.push.apply(r,t);var o=new(Function.bind.apply(e,r));return n&&i(o,n.prototype),o}).apply(null,arguments)}function f(e){var t="function"==typeof Map?new Map:void 0;return(f=function(e){if(null===e||(n=e,-1===Function.toString.call(n).indexOf("[native code]")))return e;var n;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return u(e,arguments,o(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),i(r,e)})(e)}function s(e,t){e.type=e.sideEffect?"mutate":"read",e.options=Object.assign({},t),delete e.options.key,delete e.options.schema,delete e.options.sideEffect,delete e.options.fetch,delete e.options.getFetchKey,delete e.options.options,0===Object.keys(e.options).length&&delete e.options,void 0===e.schema&&(e.schema=null)}var a=function(e){function t(t,r){var o;return(o=e.call(this,"return arguments.callee.fetch.apply(arguments.callee, arguments)")||this).getFetchKey=function(e){return o.key(e)},t&&(o.fetch=t),Object.assign(n(o),r),s(n(o),r),o}r(t,e);var o=t.prototype;return o.key=function(e){return this.fetch.name+" "+JSON.stringify(e)},o.extend=function(e){var t=function(e){function t(){return e.apply(this,arguments)||this}return r(t,e),t}(this.constructor);Object.assign(t.prototype,this);var n=new t(e.fetch,e);return s(n,Object.assign({},this,e)),n},t}(f(Function)),p=function(){function e(e,t){var n=this;this.getFetchKey=function(e){return n.key(e)},this.schema=e,t&&(this.key=t)}return e.prototype.key=function(e){return JSON.stringify(e)},e}();Object.defineProperty(e,"AbstractInstanceType",{enumerable:!0,get:function(){return t.AbstractInstanceType}}),Object.defineProperty(e,"DELETED",{enumerable:!0,get:function(){return t.DELETED}}),Object.defineProperty(e,"Entity",{enumerable:!0,get:function(){return t.Entity}}),Object.defineProperty(e,"Schema",{enumerable:!0,get:function(){return t.Schema}}),Object.defineProperty(e,"SimpleRecord",{enumerable:!0,get:function(){return t.SimpleRecord}}),Object.defineProperty(e,"denormalize",{enumerable:!0,get:function(){return t.denormalize}}),Object.defineProperty(e,"isEntity",{enumerable:!0,get:function(){return t.isEntity}}),Object.defineProperty(e,"normalize",{enumerable:!0,get:function(){return t.normalize}}),Object.defineProperty(e,"schema",{enumerable:!0,get:function(){return t.schema}}),e.Endpoint=a,e.Index=p,Object.defineProperty(e,"__esModule",{value:!0})})); |
@@ -9,4 +9,4 @@ /* eslint-disable @typescript-eslint/ban-types */ | ||
export interface EndpointOptions< | ||
K extends (...args: any) => string, | ||
S extends Schema | undefined = Schema | undefined, | ||
K extends ((...args: any) => string) | undefined = undefined, | ||
S extends Schema | undefined = undefined, | ||
M extends true | undefined = undefined | ||
@@ -17,8 +17,11 @@ > extends EndpointExtraOptions { | ||
schema?: S; | ||
[k: string]: any; | ||
} | ||
export interface EndpointExtendOptions< | ||
K extends (...args: any) => string, | ||
K extends ((...args: any) => string) | undefined = | ||
| ((...args: any) => string) | ||
| undefined, | ||
S extends Schema | undefined = Schema | undefined, | ||
M extends true | undefined = undefined | ||
M extends true | undefined = true | undefined | ||
> extends EndpointOptions<K, S, M> { | ||
@@ -37,2 +40,4 @@ fetch?: FetchFunction; | ||
export function Make(...args: any[]): EndpointInstance<FetchFunction>; | ||
/** | ||
@@ -42,3 +47,3 @@ * Creates a new function. | ||
export interface EndpointInstance< | ||
F extends FetchFunction, | ||
F extends FetchFunction = FetchFunction, | ||
S extends Schema | undefined = undefined, | ||
@@ -88,3 +93,3 @@ M extends true | undefined = undefined | ||
readonly sideEffect?: M; | ||
readonly sideEffect: M; | ||
@@ -97,29 +102,28 @@ readonly schema: S extends undefined ? schema.SchemaClass<ResolveType<F>> : S; | ||
extend< | ||
E extends EndpointInstance<any, S, any>, | ||
O extends EndpointExtendOptions<K, any, any> & | ||
Partial<ThisParameterType<E['fetch']>>, | ||
K extends ( | ||
this: ThisParameterType< | ||
'fetch' extends keyof O ? O['fetch'] : E['fetch'] | ||
>, | ||
params: ParamFromFetch<'fetch' extends keyof O ? O['fetch'] : E['fetch']>, | ||
) => string, | ||
S | ||
E extends EndpointInstance< | ||
FetchFunction, | ||
Schema | undefined, | ||
true | undefined | ||
>, | ||
O extends EndpointExtendOptions<EndpointInstance<F>['key']> & | ||
Partial<Omit<E, keyof EndpointInstance<FetchFunction>>> | ||
>( | ||
this: E, | ||
options: O, | ||
): EndpointInstance< | ||
'fetch' extends keyof typeof options | ||
? Exclude<typeof options['fetch'], undefined> | ||
: E['fetch'], | ||
'schema' extends keyof typeof options | ||
? typeof options['schema'] | ||
: E['_schema'], | ||
'sideEffect' extends keyof typeof options | ||
? typeof options['sideEffect'] | ||
: E['sideEffect'] | ||
>; | ||
): Omit<O, keyof EndpointInstance<FetchFunction>> & | ||
Omit<E, keyof EndpointInstance<FetchFunction>> & | ||
EndpointInstance< | ||
'fetch' extends keyof typeof options | ||
? Exclude<typeof options['fetch'], undefined> | ||
: E['fetch'], | ||
'schema' extends keyof typeof options | ||
? typeof options['schema'] | ||
: E['_schema'], | ||
'sideEffect' extends keyof typeof options | ||
? typeof options['sideEffect'] | ||
: E['sideEffect'] | ||
>; | ||
/** The following is for compatibility with FetchShape */ | ||
readonly type: M extends undefined ? 'read' : 'mutate'; | ||
readonly type: M extends true ? 'mutate' : 'read'; | ||
getFetchKey(params: Parameters<F>[0]): string; | ||
@@ -131,19 +135,18 @@ options?: EndpointExtraOptions; | ||
new < | ||
F extends (params?: any, body?: any) => Promise<any>, | ||
F extends ( | ||
this: EndpointInstance<FetchFunction> & E, | ||
params?: any, | ||
body?: any, | ||
) => Promise<any>, | ||
S extends Schema | undefined = undefined, | ||
M extends true | undefined = undefined | ||
M extends true | undefined = undefined, | ||
E extends Record<string, any> = {} | ||
>( | ||
fetchFunction: F, | ||
options?: EndpointOptions< | ||
(this: ThisParameterType<F>, ...args: Parameters<F>) => string, | ||
S, | ||
M | ||
> & | ||
ThisParameterType<F>, | ||
): EndpointInstance<F, S, M>; | ||
options?: EndpointOptions<EndpointInstance<F>['key'], S, M> & E, | ||
): EndpointInstance<F, S, M> & E; | ||
readonly prototype: Function; | ||
} | ||
declare let Endpoint: EndpointConstructor; | ||
export default Endpoint; |
@@ -0,5 +1,21 @@ | ||
function runCompat(endpoint, options) { | ||
endpoint.type = endpoint.sideEffect ? 'mutate' : 'read'; | ||
endpoint.options = Object.assign({}, options); | ||
delete endpoint.options.key; | ||
delete endpoint.options.schema; | ||
delete endpoint.options.sideEffect; | ||
delete endpoint.options.fetch; | ||
delete endpoint.options.getFetchKey; | ||
delete endpoint.options.options; | ||
if (Object.keys(endpoint.options).length === 0) { | ||
delete endpoint.options; | ||
} | ||
if (endpoint.schema === undefined) endpoint.schema = null; | ||
} | ||
export default class Endpoint extends Function { | ||
constructor(fetchFunction, options) { | ||
super('return arguments.callee.fetch.apply(arguments.callee, arguments)'); | ||
this.schema = null; | ||
@@ -10,15 +26,7 @@ this.getFetchKey = params => { | ||
this.fetch = fetchFunction; | ||
if (fetchFunction) this.fetch = fetchFunction; | ||
Object.assign(this, options); | ||
/** The following is for compatibility with FetchShape */ | ||
this.type = this.sideEffect ? 'mutate' : 'read'; | ||
this.options = Object.assign({}, options); | ||
delete this.options.key; | ||
delete this.options.schema; | ||
delete this.options.sideEffect; | ||
if (Object.keys(this.options).length === 0) { | ||
delete this.options; | ||
} | ||
runCompat(this, options); | ||
} | ||
@@ -31,16 +39,12 @@ | ||
extend(options) { | ||
var _options$fetch; | ||
// make a constructor/prototype based off this | ||
// extend from it and init with options sent | ||
class E extends this.constructor {} | ||
Object.assign(E.prototype, this); | ||
const instance = new E(options.fetch, options); | ||
/** The following is for compatibility with FetchShape */ | ||
const optionsCopy = Object.assign({}, options); | ||
delete optionsCopy.key; | ||
delete optionsCopy.schema; | ||
delete optionsCopy.sideEffect; | ||
const optionsToPass = Object.assign({}, this, options, { | ||
/** The following is for compatibility with FetchShape */ | ||
options: Object.assign({}, this.options, { | ||
optionsCopy | ||
}) | ||
}); | ||
return new this.constructor((_options$fetch = options.fetch) != null ? _options$fetch : this.fetch, optionsToPass); | ||
runCompat(instance, Object.assign({}, this, options)); | ||
return instance; | ||
} | ||
@@ -51,2 +55,2 @@ /** The following is for compatibility with FetchShape */ | ||
} | ||
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9lbmRwb2ludC5qcyJdLCJuYW1lcyI6WyJFbmRwb2ludCIsIkZ1bmN0aW9uIiwiY29uc3RydWN0b3IiLCJmZXRjaEZ1bmN0aW9uIiwib3B0aW9ucy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WN0b3Ige31cbiAgICBPYmplY3QuYXNzaWduKEUucHJvdG90eXBlLCB0aGlzKTtcbiAgICBjb25zdCBpbnN0YW5jZSA9IG5ldyBFKG9wdGlvbnMuZmV0Y2gsIG9wdGlvbnMpO1xuXG4gICAgLyoqIFRoZSBmb2xsb3dpbmcgaXMgZm9yIGNvbXBhdGliaWxpdHkgd2l0aCBGZXRjaFNoYXBlICovXG4gICAgcnVuQ29tcGF0KGluc3RhbmNlLCB7IC4uLnRoaXMsIC4uLm9wdGlvbnMgfSk7XG5cbiAgICByZXR1cm4gaW5zdGFuY2U7XG4gIH1cblxuICAvKiogVGhlIGZvbGxvd2luZyBpcyBmb3IgY29tcGF0aWJpbGl0eSB3aXRoIEZldGNoU2hhcGUgKi9cbiAgZ2V0RmV0Y2hLZXkgPSBwYXJhbXMgPT4ge1xuICAgIHJldHVybiB0aGlzLmtleShwYXJhbXMpO1xuICB9O1xufVxuIl19 |
@@ -7,3 +7,5 @@ import { Schema } from '@rest-hooks/normalizr'; | ||
key(params?: P): string; | ||
/** The following is for compatibility with FetchShape */ | ||
getFetchKey: (params: P) => string; | ||
} | ||
//# sourceMappingURL=indexEndpoint.d.ts.map |
export default class Index { | ||
constructor(schema, key) { | ||
this.getFetchKey = params => { | ||
return this.key(params); | ||
}; | ||
this.schema = schema; | ||
@@ -10,4 +14,6 @@ if (key) this.key = key; | ||
} | ||
/** The following is for compatibility with FetchShape */ | ||
} | ||
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleEVuZHBvaW50LnRzIl0sIm5hbWVzIjpbIkluZGV4IiwiY29uc3RydWN0b3IiLCJzY2hlbWEiLCJrZXkiLCJwYXJhbXMiLCJKU09OIiwic3RyaW5naWZ5Il0sIm1hcHBpbmdzIjoiQUFJQSxlQUFlLE1BQU1BLEtBQU4sQ0FDbUI7QUFFaENDLEVBQUFBLFdBQVcsQ0FBQ0MsTUFBRC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hcmFtcyk7XG4gIH1cblxuICAvKiogVGhlIGZvbGxvd2luZyBpcyBmb3IgY29tcGF0aWJpbGl0eSB3aXRoIEZldGNoU2hhcGUgKi9cbiAgZ2V0RmV0Y2hLZXkgPSAocGFyYW1zOiBQKSA9PiB7XG4gICAgcmV0dXJuIHRoaXMua2V5KHBhcmFtcyk7XG4gIH07XG59XG4iXX0= |
{ | ||
"name": "@rest-hooks/endpoint", | ||
"version": "0.5.3", | ||
"version": "0.6.0", | ||
"description": "Declarative Network Interface Definitions", | ||
@@ -57,3 +57,3 @@ "sideEffects": false, | ||
}, | ||
"gitHead": "5b160b1c404e64b56a2d31b368a53af0b1bc8a4f" | ||
"gitHead": "529b34f1f4913a4266abb2eddf345332e063ca1a" | ||
} |
@@ -5,3 +5,3 @@ import nock from 'nock'; | ||
import Endpoint from '../endpoint'; | ||
import Endpoint, { EndpointOptions, Make } from '../endpoint'; | ||
import { EndpointInterface } from '../interface'; | ||
@@ -92,2 +92,5 @@ | ||
const BaseFetch = new Endpoint(fetchUsers); | ||
// @ts-expect-error | ||
const aa: true = BaseFetch.sideEffect; | ||
const bb: undefined = BaseFetch.sideEffect; | ||
const UserDetail = new Endpoint(fetchUsers).extend({ | ||
@@ -99,4 +102,9 @@ sideEffect: true, | ||
const a: undefined = UserDetail.sideEffect; | ||
const b: true = UserDetail.sideEffect; | ||
function t(a: EndpointInterface<typeof fetchUsers>) {} | ||
// ts-expect-error | ||
//const c: undefined = UserDetail.extend({ dataExpiryLength: 5 }).sideEffect; | ||
//const d: true = UserDetail.extend({ dataExpiryLength: 5 }).sideEffect; | ||
function t(a: EndpointInterface<typeof fetchUsers, any, undefined>) {} | ||
// @ts-expect-error | ||
@@ -127,3 +135,3 @@ t(UserDetail); | ||
fetch: fetchAsset, | ||
// @ts-expect-error | ||
// TODO: ts-expect-error | ||
key: ({ id }: { id: string }) => `fetch my user ${id}`, | ||
@@ -167,3 +175,5 @@ }); | ||
// extends | ||
const Extended = UserDetail.extend({ schema: User2 }); | ||
const Extended = UserDetail.extend({ | ||
schema: User2, | ||
}); | ||
const user2 = await Extended({ id: payload.id }); | ||
@@ -185,2 +195,18 @@ expect(user2).toEqual(payload); | ||
it('makes', async () => { | ||
const UserCurrent = new Endpoint(fetchAuthd, { token: 'password', key }); | ||
const response = await UserCurrent(); | ||
expect(response).toEqual(payload); | ||
expect(response.username).toBe(payload.username); | ||
expect(UserCurrent.key()).toMatchInlineSnapshot( | ||
`"current user password"`, | ||
); | ||
UserCurrent.key = function () { | ||
return Object.getPrototypeOf(UserCurrent).key.call(this) + 'never'; | ||
}; | ||
}); | ||
it('should use provided context in fetch and key', async () => { | ||
@@ -218,4 +244,3 @@ const UserCurrent = new Endpoint(fetchAuthd, { token: 'password', key }); | ||
it('should not allow mismatched key', () => { | ||
// @ts-expect-error | ||
new Endpoint(fetchAuthd, { token: 'hi', key2 }); | ||
// TODO: ts-expect-error new Endpoint(fetchAuthd, { token: 'hi', key: key2 }); | ||
@@ -231,4 +256,3 @@ // @ts-expect-error | ||
// @ts-expect-error | ||
UserCurrent.extend({ key: key2 }); | ||
// TODO: ts-expect-error UserCurrent.extend({ key: key2 }); | ||
@@ -274,10 +298,61 @@ // @ts-expect-error | ||
// @ts-expect-error | ||
new Endpoint(fetchUsers, { url: '' }); | ||
new Endpoint(fetchUsers, { url: '', random: 5 }); | ||
// @ts-expect-error | ||
new Endpoint(fetchUsers, { url }).extend({ url: 'hi' }); | ||
const UserDetail = new Endpoint(fetchUsers, { url }).extend({ url }); | ||
const UserDetail = new Endpoint( | ||
function ({ id }: { id: string }) { | ||
this.random; | ||
// @ts-expect-error | ||
this.notexistant; | ||
return fetch(this.url({ id })).then(res => res.json()) as Promise< | ||
typeof payload | ||
>; | ||
}, | ||
{ | ||
url, | ||
random: 599, | ||
dataExpiryLength: 5000, | ||
}, | ||
); | ||
const a: undefined = UserDetail.sideEffect; | ||
// @ts-expect-error | ||
const b: true = UserDetail.sideEffect; | ||
UserDetail.schema; | ||
UserDetail.random; | ||
// @ts-expect-error | ||
UserDetail.nonexistant; | ||
UserDetail.key({ id: 'hi' }); | ||
// @ts-expect-error | ||
() => UserDetail.key({ nonexistant: 5 }); | ||
// @ts-expect-error | ||
() => UserDetail.key({ id: 5 }); | ||
let res = await UserDetail({ id: payload.id }); | ||
expect(res).toEqual(payload); | ||
expect(res.username).toBe(payload.username); | ||
// @ts-expect-error | ||
expect(res.notexist).toBeUndefined(); | ||
// test extending parts that aren't used in this | ||
const Extended = UserDetail.extend({ random: 100 }); | ||
res = await Extended({ id: payload.id }); | ||
expect(res).toEqual(payload); | ||
expect(res.username).toBe(payload.username); | ||
// @ts-expect-error | ||
expect(res.notexist).toBeUndefined(); | ||
UserDetail.extend({ | ||
url: function (params: { id: string }) { | ||
return this.constructor.prototype.url(params) + '/more'; | ||
}, | ||
// @ts-expect-error | ||
random: '600', | ||
}); | ||
const Test = UserDetail.extend({ | ||
random: 600, | ||
}); | ||
// check return type and call params | ||
const response = await UserDetail({ id: payload.id }); | ||
const response = await Test({ id: payload.id }); | ||
expect(response).toEqual(payload); | ||
@@ -288,6 +363,96 @@ expect(response.username).toBe(payload.username); | ||
}); | ||
it('should work with key', () => { | ||
const url = ({ id }: { id: string }) => `/users/${id}`; | ||
class User extends Entity { | ||
readonly id: string = ''; | ||
pk() { | ||
return this.id; | ||
} | ||
} | ||
const UserDetail = new Endpoint( | ||
function ({ id }: { id: string }) { | ||
this.schema; | ||
this.random; | ||
// @ts-expect-error | ||
this.notexistant; | ||
return fetch(this.url({ id })).then(res => res.json()) as Promise< | ||
typeof payload | ||
>; | ||
}, | ||
{ | ||
url, | ||
random: 599, | ||
schema: [User], | ||
key: function (this: any, { id }: { id: string }) { | ||
this.random; | ||
this.schema; | ||
return id + 'hi'; | ||
}, | ||
}, | ||
); | ||
const sch: typeof User[] = UserDetail.schema; | ||
const s: undefined = UserDetail.sideEffect; | ||
UserDetail.random; | ||
// @ts-expect-error | ||
UserDetail.nonexistant; | ||
UserDetail.key({ id: 'hi' }); | ||
// @ts-expect-error | ||
() => UserDetail.key({ nonexistant: 5 }); | ||
// @ts-expect-error | ||
() => UserDetail.key({ id: 5 }); | ||
}); | ||
}); | ||
/*describe('class', () => { | ||
describe('auth patterns', () => { | ||
describe('class', () => { | ||
/*class ResourceEndpoint< | ||
F extends (params?: any, body?: any) => Promise<any>, | ||
S extends Schema | undefined = undefined, | ||
M extends true | undefined = undefined | ||
> extends Endpoint<F, S, M> { | ||
constructor( | ||
fetchFunction: F, | ||
options?: EndpointOptions< | ||
(this: ThisParameterType<F>, ...args: Parameters<F>) => string, | ||
S, | ||
M | ||
> & | ||
ThisParameterType<F>, | ||
) { | ||
super(fetchFunction, options); | ||
} | ||
fetch(...args: Parameters<F>) { | ||
return fetch(this.url(args[0]), this.init).then(res => res.json()); | ||
} | ||
init: RequestInit = { method: 'GET' }; | ||
key(params: { id: string }) { | ||
return `${this.init.method} ${this.url(params)}`; | ||
} | ||
} | ||
const init = this.getFetchInit({ method: 'GET' }); | ||
const fetch = this.fetch.bind(this); | ||
return new Endpoint( | ||
function ( | ||
this: { url: (p: any) => string; init: RequestInit }, | ||
params: Readonly<object>, | ||
) { | ||
return fetch(this.url(params), this.init); | ||
}, | ||
{ | ||
...this.getEndpointExtra(), | ||
key: function ( | ||
this: { url: (p: any) => string; init: RequestInit }, | ||
params: Readonly<object>, | ||
) { | ||
return `${this.init.method} ${this.url(params)}`; | ||
}, | ||
url: this.url.bind(this), | ||
init, | ||
}, | ||
);*/ | ||
/* describe('auth patterns', () => { | ||
class AuthEndpoint< | ||
@@ -353,4 +518,3 @@ F extends ( | ||
});*/ | ||
/*describe('custom fetch for snakeCase', () => { | ||
/*describe('custom fetch for snakeCase', () => { | ||
function deeplyApplyKeyTransform( | ||
@@ -386,4 +550,4 @@ obj: any, | ||
BaseEndpoint.extend({ fetch: }) | ||
}) | ||
});*/ | ||
})*/ | ||
}); | ||
}); |
@@ -9,4 +9,4 @@ /* eslint-disable @typescript-eslint/ban-types */ | ||
export interface EndpointOptions< | ||
K extends (...args: any) => string, | ||
S extends Schema | undefined = Schema | undefined, | ||
K extends ((...args: any) => string) | undefined = undefined, | ||
S extends Schema | undefined = undefined, | ||
M extends true | undefined = undefined | ||
@@ -17,8 +17,11 @@ > extends EndpointExtraOptions { | ||
schema?: S; | ||
[k: string]: any; | ||
} | ||
export interface EndpointExtendOptions< | ||
K extends (...args: any) => string, | ||
K extends ((...args: any) => string) | undefined = | ||
| ((...args: any) => string) | ||
| undefined, | ||
S extends Schema | undefined = Schema | undefined, | ||
M extends true | undefined = undefined | ||
M extends true | undefined = true | undefined | ||
> extends EndpointOptions<K, S, M> { | ||
@@ -37,2 +40,4 @@ fetch?: FetchFunction; | ||
export function Make(...args: any[]): EndpointInstance<FetchFunction>; | ||
/** | ||
@@ -42,3 +47,3 @@ * Creates a new function. | ||
export interface EndpointInstance< | ||
F extends FetchFunction, | ||
F extends FetchFunction = FetchFunction, | ||
S extends Schema | undefined = undefined, | ||
@@ -88,3 +93,3 @@ M extends true | undefined = undefined | ||
readonly sideEffect?: M; | ||
readonly sideEffect: M; | ||
@@ -97,29 +102,28 @@ readonly schema: S extends undefined ? schema.SchemaClass<ResolveType<F>> : S; | ||
extend< | ||
E extends EndpointInstance<any, S, any>, | ||
O extends EndpointExtendOptions<K, any, any> & | ||
Partial<ThisParameterType<E['fetch']>>, | ||
K extends ( | ||
this: ThisParameterType< | ||
'fetch' extends keyof O ? O['fetch'] : E['fetch'] | ||
>, | ||
params: ParamFromFetch<'fetch' extends keyof O ? O['fetch'] : E['fetch']>, | ||
) => string, | ||
S | ||
E extends EndpointInstance< | ||
FetchFunction, | ||
Schema | undefined, | ||
true | undefined | ||
>, | ||
O extends EndpointExtendOptions<EndpointInstance<F>['key']> & | ||
Partial<Omit<E, keyof EndpointInstance<FetchFunction>>> | ||
>( | ||
this: E, | ||
options: O, | ||
): EndpointInstance< | ||
'fetch' extends keyof typeof options | ||
? Exclude<typeof options['fetch'], undefined> | ||
: E['fetch'], | ||
'schema' extends keyof typeof options | ||
? typeof options['schema'] | ||
: E['_schema'], | ||
'sideEffect' extends keyof typeof options | ||
? typeof options['sideEffect'] | ||
: E['sideEffect'] | ||
>; | ||
): Omit<O, keyof EndpointInstance<FetchFunction>> & | ||
Omit<E, keyof EndpointInstance<FetchFunction>> & | ||
EndpointInstance< | ||
'fetch' extends keyof typeof options | ||
? Exclude<typeof options['fetch'], undefined> | ||
: E['fetch'], | ||
'schema' extends keyof typeof options | ||
? typeof options['schema'] | ||
: E['_schema'], | ||
'sideEffect' extends keyof typeof options | ||
? typeof options['sideEffect'] | ||
: E['sideEffect'] | ||
>; | ||
/** The following is for compatibility with FetchShape */ | ||
readonly type: M extends undefined ? 'read' : 'mutate'; | ||
readonly type: M extends true ? 'mutate' : 'read'; | ||
getFetchKey(params: Parameters<F>[0]): string; | ||
@@ -131,19 +135,18 @@ options?: EndpointExtraOptions; | ||
new < | ||
F extends (params?: any, body?: any) => Promise<any>, | ||
F extends ( | ||
this: EndpointInstance<FetchFunction> & E, | ||
params?: any, | ||
body?: any, | ||
) => Promise<any>, | ||
S extends Schema | undefined = undefined, | ||
M extends true | undefined = undefined | ||
M extends true | undefined = undefined, | ||
E extends Record<string, any> = {} | ||
>( | ||
fetchFunction: F, | ||
options?: EndpointOptions< | ||
(this: ThisParameterType<F>, ...args: Parameters<F>) => string, | ||
S, | ||
M | ||
> & | ||
ThisParameterType<F>, | ||
): EndpointInstance<F, S, M>; | ||
options?: EndpointOptions<EndpointInstance<F>['key'], S, M> & E, | ||
): EndpointInstance<F, S, M> & E; | ||
readonly prototype: Function; | ||
} | ||
declare let Endpoint: EndpointConstructor; | ||
export default Endpoint; |
@@ -0,15 +1,23 @@ | ||
function runCompat(endpoint, options) { | ||
endpoint.type = endpoint.sideEffect ? 'mutate' : 'read'; | ||
endpoint.options = { ...options }; | ||
delete endpoint.options.key; | ||
delete endpoint.options.schema; | ||
delete endpoint.options.sideEffect; | ||
delete endpoint.options.fetch; | ||
delete endpoint.options.getFetchKey; | ||
delete endpoint.options.options; | ||
if (Object.keys(endpoint.options).length === 0) { | ||
delete endpoint.options; | ||
} | ||
if (endpoint.schema === undefined) endpoint.schema = null; | ||
} | ||
export default class Endpoint extends Function { | ||
constructor(fetchFunction, options) { | ||
super('return arguments.callee.fetch.apply(arguments.callee, arguments)'); | ||
this.fetch = fetchFunction; | ||
if (fetchFunction) this.fetch = fetchFunction; | ||
Object.assign(this, options); | ||
/** The following is for compatibility with FetchShape */ | ||
this.type = this.sideEffect ? 'mutate' : 'read'; | ||
this.options = { ...options }; | ||
delete this.options.key; | ||
delete this.options.schema; | ||
delete this.options.sideEffect; | ||
if (Object.keys(this.options).length === 0) { | ||
delete this.options; | ||
} | ||
runCompat(this, options); | ||
} | ||
@@ -22,22 +30,15 @@ | ||
extend(options) { | ||
// make a constructor/prototype based off this | ||
// extend from it and init with options sent | ||
class E extends this.constructor {} | ||
Object.assign(E.prototype, this); | ||
const instance = new E(options.fetch, options); | ||
/** The following is for compatibility with FetchShape */ | ||
const optionsCopy = { ...options }; | ||
delete optionsCopy.key; | ||
delete optionsCopy.schema; | ||
delete optionsCopy.sideEffect; | ||
runCompat(instance, { ...this, ...options }); | ||
const optionsToPass = { | ||
...this, | ||
...options, | ||
/** The following is for compatibility with FetchShape */ | ||
options: { | ||
...this.options, | ||
optionsCopy, | ||
}, | ||
}; | ||
return new this.constructor(options.fetch ?? this.fetch, optionsToPass); | ||
return instance; | ||
} | ||
/** The following is for compatibility with FetchShape */ | ||
schema = null; | ||
getFetchKey = params => { | ||
@@ -44,0 +45,0 @@ return this.key(params); |
@@ -16,2 +16,7 @@ import { Schema } from '@rest-hooks/normalizr'; | ||
} | ||
/** The following is for compatibility with FetchShape */ | ||
getFetchKey = (params: P) => { | ||
return this.key(params); | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
90737
1359