@data-client/rest 0.1.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/LICENSE +201 -0
- package/README.md +110 -0
- package/dist/index.js +561 -0
- package/dist/index.umd.min.js +1 -0
- package/dist/next.js +520 -0
- package/dist/package.json +1 -0
- package/legacy/NetworkError.js +13 -0
- package/legacy/OptionsToFunction.js +2 -0
- package/legacy/RestEndpoint.js +225 -0
- package/legacy/RestEndpointTypeHelp.js +2 -0
- package/legacy/RestHelpers.js +33 -0
- package/legacy/createResource.js +88 -0
- package/legacy/extractCollection.js +14 -0
- package/legacy/getArrayPath.js +19 -0
- package/legacy/hookifyResource.js +24 -0
- package/legacy/index.js +10 -0
- package/legacy/mapCollection.js +14 -0
- package/legacy/next/OptionsToFunction.js +2 -0
- package/legacy/next/RestEndpoint.js +225 -0
- package/legacy/next/createResource.js +86 -0
- package/legacy/next/index.js +4 -0
- package/legacy/next/resourceTypes.js +2 -0
- package/legacy/paginatedCollections.js +29 -0
- package/legacy/paginationUpdate.js +36 -0
- package/legacy/paramsToString.js +6 -0
- package/legacy/paramsToString.native.js +9 -0
- package/legacy/pathTypes.js +2 -0
- package/legacy/test-fixtures.js +111 -0
- package/legacy/utiltypes.js +2 -0
- package/lib/NetworkError.d.ts +11 -0
- package/lib/NetworkError.d.ts.map +1 -0
- package/lib/NetworkError.js +13 -0
- package/lib/OptionsToFunction.d.ts +7 -0
- package/lib/OptionsToFunction.d.ts.map +1 -0
- package/lib/OptionsToFunction.js +2 -0
- package/lib/RestEndpoint.d.ts +548 -0
- package/lib/RestEndpoint.js +229 -0
- package/lib/RestEndpointTypeHelp.d.ts +10 -0
- package/lib/RestEndpointTypeHelp.d.ts.map +1 -0
- package/lib/RestEndpointTypeHelp.js +2 -0
- package/lib/RestHelpers.d.ts +7 -0
- package/lib/RestHelpers.d.ts.map +1 -0
- package/lib/RestHelpers.js +33 -0
- package/lib/createResource.d.ts +66 -0
- package/lib/createResource.d.ts.map +1 -0
- package/lib/createResource.js +104 -0
- package/lib/extractCollection.d.ts +11 -0
- package/lib/extractCollection.d.ts.map +1 -0
- package/lib/extractCollection.js +16 -0
- package/lib/getArrayPath.d.ts +3 -0
- package/lib/getArrayPath.d.ts.map +1 -0
- package/lib/getArrayPath.js +19 -0
- package/lib/hookifyResource.d.ts +14 -0
- package/lib/hookifyResource.d.ts.map +1 -0
- package/lib/hookifyResource.js +24 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +10 -0
- package/lib/mapCollection.d.ts +16 -0
- package/lib/mapCollection.d.ts.map +1 -0
- package/lib/mapCollection.js +16 -0
- package/lib/next/OptionsToFunction.d.ts +7 -0
- package/lib/next/OptionsToFunction.d.ts.map +1 -0
- package/lib/next/OptionsToFunction.js +2 -0
- package/lib/next/RestEndpoint.d.ts +542 -0
- package/lib/next/RestEndpoint.js +229 -0
- package/lib/next/createResource.d.ts +86 -0
- package/lib/next/createResource.d.ts.map +1 -0
- package/lib/next/createResource.js +100 -0
- package/lib/next/index.d.ts +6 -0
- package/lib/next/index.d.ts.map +1 -0
- package/lib/next/index.js +4 -0
- package/lib/next/resourceTypes.d.ts +31 -0
- package/lib/next/resourceTypes.d.ts.map +1 -0
- package/lib/next/resourceTypes.js +2 -0
- package/lib/paginatedCollections.d.ts +3 -0
- package/lib/paginatedCollections.d.ts.map +1 -0
- package/lib/paginatedCollections.js +29 -0
- package/lib/paginationUpdate.d.ts +8 -0
- package/lib/paginationUpdate.d.ts.map +1 -0
- package/lib/paginationUpdate.js +40 -0
- package/lib/paramsToString.d.ts +2 -0
- package/lib/paramsToString.d.ts.map +1 -0
- package/lib/paramsToString.js +6 -0
- package/lib/paramsToString.native.d.ts +2 -0
- package/lib/paramsToString.native.d.ts.map +1 -0
- package/lib/paramsToString.native.js +9 -0
- package/lib/pathTypes.d.ts +20 -0
- package/lib/pathTypes.d.ts.map +1 -0
- package/lib/pathTypes.js +2 -0
- package/lib/test-fixtures.d.ts +141 -0
- package/lib/test-fixtures.d.ts.map +1 -0
- package/lib/test-fixtures.js +111 -0
- package/lib/utiltypes.d.ts +21 -0
- package/lib/utiltypes.d.ts.map +1 -0
- package/lib/utiltypes.js +2 -0
- package/node.mjs +1 -0
- package/package.json +121 -0
- package/src/NetworkError.ts +17 -0
- package/src/OptionsToFunction.ts +42 -0
- package/src/RestEndpoint.d.ts +548 -0
- package/src/RestEndpoint.js +262 -0
- package/src/RestEndpointTypeHelp.ts +20 -0
- package/src/RestHelpers.ts +48 -0
- package/src/__tests__/Resource.test.ts +317 -0
- package/src/__tests__/RestEndpoint.ts +1304 -0
- package/src/__tests__/__snapshots__/RestEndpoint.ts.snap +18 -0
- package/src/__tests__/__snapshots__/createResource.test.ts.snap +5 -0
- package/src/__tests__/createResource.test.ts +583 -0
- package/src/__tests__/getArrayPath.test.ts +30 -0
- package/src/__tests__/hookifyResource.ts +197 -0
- package/src/__tests__/types.test.ts +309 -0
- package/src/createResource.ts +172 -0
- package/src/extractCollection.ts +34 -0
- package/src/getArrayPath.ts +20 -0
- package/src/hookifyResource.ts +36 -0
- package/src/index.ts +42 -0
- package/src/mapCollection.ts +54 -0
- package/src/next/OptionsToFunction.ts +41 -0
- package/src/next/RestEndpoint.d.ts +542 -0
- package/src/next/RestEndpoint.js +262 -0
- package/src/next/__tests__/RestEndpoint.ts +1333 -0
- package/src/next/__tests__/__snapshots__/RestEndpoint.ts.snap +18 -0
- package/src/next/__tests__/types.test.ts +429 -0
- package/src/next/createResource.ts +194 -0
- package/src/next/index.ts +5 -0
- package/src/next/resourceTypes.ts +32 -0
- package/src/package.json +1 -0
- package/src/paginatedCollections.ts +32 -0
- package/src/paginationUpdate.ts +49 -0
- package/src/paramsToString.native.ts +10 -0
- package/src/paramsToString.ts +7 -0
- package/src/pathTypes.ts +58 -0
- package/src/test-fixtures.ts +128 -0
- package/src/utiltypes.ts +25 -0
- package/ts4.0/NetworkError.d.ts +11 -0
- package/ts4.0/OptionsToFunction.d.ts +16 -0
- package/ts4.0/RestEndpoint.d.ts +240 -0
- package/ts4.0/RestEndpointTypeHelp.d.ts +10 -0
- package/ts4.0/RestHelpers.d.ts +7 -0
- package/ts4.0/createResource.d.ts +66 -0
- package/ts4.0/extractCollection.d.ts +11 -0
- package/ts4.0/getArrayPath.d.ts +3 -0
- package/ts4.0/hookifyResource.d.ts +19 -0
- package/ts4.0/index.d.ts +12 -0
- package/ts4.0/mapCollection.d.ts +16 -0
- package/ts4.0/next/OptionsToFunction.d.ts +16 -0
- package/ts4.0/next/RestEndpoint.d.ts +226 -0
- package/ts4.0/next/createResource.d.ts +94 -0
- package/ts4.0/next/index.d.ts +6 -0
- package/ts4.0/next/resourceTypes.d.ts +31 -0
- package/ts4.0/paginatedCollections.d.ts +3 -0
- package/ts4.0/paginationUpdate.d.ts +10 -0
- package/ts4.0/paramsToString.d.ts +2 -0
- package/ts4.0/paramsToString.native.d.ts +2 -0
- package/ts4.0/pathTypes.d.ts +20 -0
- package/ts4.0/test-fixtures.d.ts +141 -0
- package/ts4.0/utiltypes.d.ts +5 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Turns a collection of Endpoints (Resource) into a collection of hooks.
|
|
2
|
+
* This is useful for Endpoints that need hooks to prepare their fetch requests.
|
|
3
|
+
*
|
|
4
|
+
* @see https://resthooks.io/rest/api/hookifyResource
|
|
5
|
+
*/
|
|
6
|
+
export default function hookifyResource(resource, useRequestInit) {
|
|
7
|
+
const usingResource = {};
|
|
8
|
+
Object.keys(resource).forEach(key => {
|
|
9
|
+
const endpoint = resource[key];
|
|
10
|
+
if (endpoint.extend !== undefined) usingResource[`use${capitalizeFirstLetter(key)}`] = () => {
|
|
11
|
+
// this is false positive due to the dynamic nature of assignment
|
|
12
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
13
|
+
const requestInit = useRequestInit();
|
|
14
|
+
return endpoint.extend({
|
|
15
|
+
requestInit
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
return usingResource;
|
|
20
|
+
}
|
|
21
|
+
function capitalizeFirstLetter(s) {
|
|
22
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJob29raWZ5UmVzb3VyY2UiLCJyZXNvdXJjZSIsInVzZVJlcXVlc3RJbml0IiwidXNpbmdSZXNvdXJjZSIsIk9iamVjdCIsImtleXMiLCJmb3JFYWNoIiwia2V5IiwiZW5kcG9pbnQiLCJleHRlbmQiLCJ1bmRlZmluZWQiLCJjYXBpdGFsaXplRmlyc3RMZXR0ZXIiLCJyZXF1ZXN0SW5pdCIsInMiLCJjaGFyQXQiLCJ0b1VwcGVyQ2FzZSIsInNsaWNlIl0sInNvdXJjZXMiOlsiLi4vc3JjL2hvb2tpZnlSZXNvdXJjZS50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgeyBFbmRwb2ludEludGVyZmFjZSB9IGZyb20gJ0BkYXRhLWNsaWVudC9lbmRwb2ludCc7XG5cbmV4cG9ydCBpbnRlcmZhY2UgSG9va2FibGVFbmRwb2ludEludGVyZmFjZSBleHRlbmRzIEVuZHBvaW50SW50ZXJmYWNlIHtcbiAgZXh0ZW5kKC4uLmFyZ3M6IGFueSk6IEhvb2thYmxlRW5kcG9pbnRJbnRlcmZhY2U7XG59XG5cbi8qKiBUdXJucyBhIGNvbGxlY3Rpb24gb2YgRW5kcG9pbnRzIChSZXNvdXJjZSkgaW50byBhIGNvbGxlY3Rpb24gb2YgaG9va3MuXG4gKiBUaGlzIGlzIHVzZWZ1bCBmb3IgRW5kcG9pbnRzIHRoYXQgbmVlZCBob29rcyB0byBwcmVwYXJlIHRoZWlyIGZldGNoIHJlcXVlc3RzLlxuICpcbiAqIEBzZWUgaHR0cHM6Ly9yZXN0aG9va3MuaW8vcmVzdC9hcGkvaG9va2l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+1,12 @@
|
|
|
1
|
+
export { default as RestEndpoint } from './RestEndpoint.js';
|
|
2
|
+
export type { GetEndpoint, MutateEndpoint, NewGetEndpoint, NewMutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, } from './RestEndpoint.js';
|
|
3
|
+
export { default as createResource } from './createResource.js';
|
|
4
|
+
export type { Resource } from './createResource.js';
|
|
5
|
+
export { default as hookifyResource } from './hookifyResource.js';
|
|
6
|
+
export type { HookResource, HookableEndpointInterface, } from './hookifyResource.js';
|
|
7
|
+
export { default as NetworkError } from './NetworkError.js';
|
|
8
|
+
export { default as paginationUpdate } from './paginationUpdate.js';
|
|
9
|
+
export type { OptionsToFunction } from './OptionsToFunction.js';
|
|
10
|
+
export type { ShortenPath, PathArgs, PathKeys, KeysToArgs, PathArgsAndSearch, } from './pathTypes.js';
|
|
11
|
+
export * from '@data-client/endpoint';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAC5D,YAAY,EACV,WAAW,EACX,cAAc,EACd,cAAc,EACd,iBAAiB,EACjB,QAAQ,EACR,YAAY,EACZ,QAAQ,EACR,WAAW,EACX,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,iBAAiB,EACjB,8BAA8B,EAC9B,WAAW,EACX,uBAAuB,GACxB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAChE,YAAY,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAClE,YAAY,EACV,YAAY,EACZ,yBAAyB,GAC1B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACpE,YAAY,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChE,YAAY,EACV,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,UAAU,EACV,iBAAiB,GAClB,MAAM,gBAAgB,CAAC;AAExB,cAAc,uBAAuB,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Object.hasOwn = Object.hasOwn || /* istanbul ignore next */function hasOwn(it, key) {
|
|
2
|
+
return Object.prototype.hasOwnProperty.call(it, key);
|
|
3
|
+
};
|
|
4
|
+
export { default as RestEndpoint } from './RestEndpoint.js';
|
|
5
|
+
export { default as createResource } from './createResource.js';
|
|
6
|
+
export { default as hookifyResource } from './hookifyResource.js';
|
|
7
|
+
export { default as NetworkError } from './NetworkError.js';
|
|
8
|
+
export { default as paginationUpdate } from './paginationUpdate.js';
|
|
9
|
+
export * from '@data-client/endpoint';
|
|
10
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJPYmplY3QiLCJoYXNPd24iLCJpdCIsImtleSIsInByb3RvdHlwZSIsImhhc093blByb3BlcnR5IiwiY2FsbCIsImRlZmF1bHQiLCJSZXN0RW5kcG9pbnQiLCJjcmVhdGVSZXNvdXJjZSIsImhvb2tpZnlSZXNvdXJjZSIsIk5ldHdvcmtFcnJvciIsInBhZ2luYXRpb25VcGRhdGUiXSwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwic291cmNlc0NvbnRlbnQiOlsiT2JqZWN0Lmhhc093biA9XG4gIE9iamVjdC5oYXNPd24gfHxcbiAgLyogaXN0YW5idWwgaWdub3JlIG5leHQgKi8gZn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|
|
2
|
+
export default function mapCollection<M extends <C extends schema.Collection>(collection: C) => any, S extends Schema | undefined>(s: S, mapper: M): S extends schema.Collection ? ReturnType<typeof mapper<S>> : S extends schema.Object<infer T> ? {
|
|
3
|
+
[K in keyof T]: T[K] extends Schema ? typeof mapCollection<M, T[K]> : T[K];
|
|
4
|
+
} : S extends {
|
|
5
|
+
[K: string]: any;
|
|
6
|
+
} ? {
|
|
7
|
+
[K in keyof S]: S[K] extends Schema ? typeof mapCollection<M, S[K]> : S[K];
|
|
8
|
+
} : S;
|
|
9
|
+
type MapCollection<M extends <C extends schema.Collection>(collection: C) => any, S extends Schema | undefined> = S extends schema.Collection ? ReturnType<M> : S extends schema.Object<infer T> ? MapCollection<M, T> : S extends {
|
|
10
|
+
[K: string]: any;
|
|
11
|
+
} ? MapObject<M, S> : never;
|
|
12
|
+
export type MapObject<M extends (collection: schema.Collection) => any, S extends Record<string, any>> = {
|
|
13
|
+
[K in keyof S]: S[K] extends Schema ? MapCollection<M, S[K]> : S[K];
|
|
14
|
+
};
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=mapCollection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapCollection.d.ts","sourceRoot":"","sources":["../src/mapCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAEvD,MAAM,CAAC,OAAO,UAAU,aAAa,CACnC,CAAC,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,UAAU,EAAE,UAAU,EAAE,CAAC,KAAK,GAAG,EAC7D,CAAC,SAAS,MAAM,GAAG,SAAS,EAE5B,CAAC,EAAE,CAAC,EACJ,MAAM,EAAE,CAAC,GACR,CAAC,SAAS,MAAM,CAAC,UAAU,GAC1B,UAAU,CAAC,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC,GAC5B,CAAC,SAAS,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,C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|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { schema } from '@data-client/endpoint';
|
|
2
|
+
export default function mapCollection(s, mapper) {
|
|
3
|
+
if (typeof s !== 'object' || s === undefined) return s;
|
|
4
|
+
if (s instanceof schema.Collection) {
|
|
5
|
+
return mapper(s);
|
|
6
|
+
}
|
|
7
|
+
const objCopy = {
|
|
8
|
+
...(s instanceof schema.Object ? s.schema : s)
|
|
9
|
+
};
|
|
10
|
+
for (const k in objCopy) {
|
|
11
|
+
if (!objCopy[k]) continue;
|
|
12
|
+
objCopy[k] = mapCollection(objCopy[k], mapper);
|
|
13
|
+
}
|
|
14
|
+
return objCopy;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJzY2hlbWEiLCJtYXBDb2xsZWN0aW9uIiwicyIsIm1hcHBlciIsInVuZGVmaW5lZCIsIkNvbGxlY3Rpb24iLCJvYmpDb3B5IiwiT2JqZWN0IiwiayJdLCJzb3VyY2VzIjpbIi4uL3NyYy9tYXBDb2xsZWN0aW9uLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IFNjaGVtYSwgc2NoZW1hIH0gZnJvbSAnQGRhdGEtY2xpZW50L2VuZHBvaW50JztcblxuZXhwb3J0IGRlZmF1bHQgZnVuY3Rpb24gbWFwQ29sbGVjdGlvbjxcbiAgTSBleHRlbmRzIDxDIGV4dGVuZHMgc2NoZW1hLkNvbGxlY3Rpb24+KGNvbGxlY3Rpb246IEMpID0+IGFueSxcbiAgUyBleHRlbmRzIFNjaGVtYSB8IHVuZGVmaW5lZCxcbj4oXG4gIHM6IFMsXG4gIG1hcHBlcjogTSxcbik6IFMgZXh0ZW5kcyBzY2hlbWEuQ29sbGVjdGlvblxuICA/IFJldHVyblR5cGU8dHlwZW9mIG1hcHBlcjxTPj5cbiAgOiBTIGV4dGVuZHMgc2NoZW1hLk9iamVjdDxpbmZlciBUPlxu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8sQ0FBQ0UsQ0FBQyxDQUFDLEVBQUU7SUFDakJGLE9BQU8sQ0FBQ0UsQ0FBQyxDQUFDLEdBQUdQLGFBQWEsQ0FBQ0ssT0FBTyxDQUFDRSxDQUFDLENBQUMsRUFBRUwsTUFBTSxDQUFRO0VBQ3ZEO0VBQ0EsT0FBT0csT0FBTztBQUNoQiJ9
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FetchFunction, ResolveType } from '@data-client/endpoint';
|
|
2
|
+
import { PartialRestGenerics, RestInstanceBase, RestFetch } from './RestEndpoint.js';
|
|
3
|
+
import { PathArgs } from '../pathTypes.js';
|
|
4
|
+
export type OptionsToFunction<O extends PartialRestGenerics, E extends RestInstanceBase & {
|
|
5
|
+
body?: any;
|
|
6
|
+
}, F extends FetchFunction> = 'path' extends keyof O ? RestFetch<'searchParams' extends keyof O ? O['searchParams'] & PathArgs<Exclude<O['path'], undefined>> : PathArgs<Exclude<O['path'], undefined>>, 'body' extends keyof O ? O['body'] : E['body'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>> : 'body' extends keyof O ? RestFetch<'searchParams' extends keyof O ? O['searchParams'] & PathArgs<Exclude<E['path'], undefined>> : PathArgs<Exclude<E['path'], undefined>>, O['body'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>> : 'searchParams' extends keyof O ? RestFetch<O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, E['body'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>> : (this: ThisParameterType<F>, ...args: Parameters<F>) => Promise<O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>>;
|
|
7
|
+
//# sourceMappingURL=OptionsToFunction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OptionsToFunction.d.ts","sourceRoot":"","sources":["../../src/next/OptionsToFunction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC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VkPj4sXG4gICAgICBFWydib2R5J10sXG4gICAgICBPWydwcm9jZXNzJ10gZXh0ZW5kcyB7fSA/IFJldHVyblR5cGU8T1sncHJvY2VzcyddPiA6IFJlc29sdmVUeXBlPEY+XG4gICAgPlxuICA6IChcbiAgICAgIHRoaXM6IFRoaXNQYXJhbWV0ZXJUeXBlPEY+LFxuICAgICAgLi4uYXJnczogUGFyYW1ldGVyczxGPlxuICAgICkgPT4gUHJvbWlzZTxcbiAgICAgIE9bJ3Byb2Nlc3MnXSBleHRlbmRzIHt9ID8gUmV0dXJuVHlwZTxPWydwcm9jZXNzJ10+IDogUmVzb2x2ZVR5cGU8Rj5cbiAgICA+O1xuIl0sIm1hcHBpbmdzIjoiIn0=
|
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
+
import type {
|
|
3
|
+
EndpointExtraOptions,
|
|
4
|
+
EndpointInstanceInterface,
|
|
5
|
+
Schema,
|
|
6
|
+
FetchFunction,
|
|
7
|
+
ResolveType,
|
|
8
|
+
} from '@data-client/endpoint';
|
|
9
|
+
|
|
10
|
+
import { OptionsToFunction } from './OptionsToFunction.js';
|
|
11
|
+
import type { ExtractCollection } from '../extractCollection.js';
|
|
12
|
+
import { PathArgs } from '../pathTypes.js';
|
|
13
|
+
import { EndpointUpdateFunction } from '../RestEndpointTypeHelp.js';
|
|
14
|
+
|
|
15
|
+
export interface RestInstanceBase<
|
|
16
|
+
F extends FetchFunction = FetchFunction,
|
|
17
|
+
S extends Schema | undefined = any,
|
|
18
|
+
M extends true | undefined = true | undefined,
|
|
19
|
+
O extends {
|
|
20
|
+
path: string;
|
|
21
|
+
body?: any;
|
|
22
|
+
searchParams?: any;
|
|
23
|
+
} = { path: string },
|
|
24
|
+
> extends EndpointInstanceInterface<F, S, M> {
|
|
25
|
+
readonly body?: 'body' extends keyof O ? O['body'] : any;
|
|
26
|
+
readonly searchParams?: 'searchParams' extends keyof O
|
|
27
|
+
? O['searchParams']
|
|
28
|
+
: // unknown is identity with '&' type operator
|
|
29
|
+
unknown;
|
|
30
|
+
|
|
31
|
+
/** Pattern to construct url based on Url Parameters
|
|
32
|
+
* @see https://resthooks.io/rest/api/RestEndpoint#path
|
|
33
|
+
*/
|
|
34
|
+
readonly path: O['path'];
|
|
35
|
+
/** Prepended to all urls
|
|
36
|
+
* @see https://resthooks.io/rest/api/RestEndpoint#urlPrefix
|
|
37
|
+
*/
|
|
38
|
+
readonly urlPrefix: string;
|
|
39
|
+
readonly requestInit: RequestInit;
|
|
40
|
+
readonly method: string;
|
|
41
|
+
readonly signal: AbortSignal | undefined;
|
|
42
|
+
|
|
43
|
+
/* fetch lifecycles */
|
|
44
|
+
/* before-fetch */
|
|
45
|
+
url(...args: Parameters<F>): string;
|
|
46
|
+
/** @see https://resthooks.io/rest/api/RestEndpoint#getRequestInit */
|
|
47
|
+
getRequestInit(
|
|
48
|
+
this: any,
|
|
49
|
+
body?: RequestInit['body'] | Record<string, unknown>,
|
|
50
|
+
): Promise<RequestInit> | RequestInit;
|
|
51
|
+
/** @see https://resthooks.io/rest/api/RestEndpoint#getHeaders */
|
|
52
|
+
getHeaders(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
|
|
53
|
+
/* after-fetch */
|
|
54
|
+
/** @see https://resthooks.io/rest/api/RestEndpoint#fetchResponse */
|
|
55
|
+
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
56
|
+
/** @see https://resthooks.io/rest/api/RestEndpoint#parseResponse */
|
|
57
|
+
parseResponse(response: Response): Promise<any>;
|
|
58
|
+
/** @see https://resthooks.io/rest/api/RestEndpoint#process */
|
|
59
|
+
process(value: any, ...args: Parameters<F>): ResolveType<F>;
|
|
60
|
+
|
|
61
|
+
/* utilities */
|
|
62
|
+
/** @see https://resthooks.io/rest/api/RestEndpoint#testKey */
|
|
63
|
+
testKey(key: string): boolean;
|
|
64
|
+
|
|
65
|
+
/* extenders */
|
|
66
|
+
// TODO: figure out better way than wrapping whole options in Readonly<> + making O extend from {}
|
|
67
|
+
// this is just a hack to handle when no members of PartialRestGenerics are present
|
|
68
|
+
// Note: Using overloading (like paginated did) struggles because typescript does not have a clear way of distinguishing one
|
|
69
|
+
// should be used from the other (due to same problem with every member being partial)
|
|
70
|
+
extend<E extends RestInstanceBase, O extends PartialRestGenerics | {}>(
|
|
71
|
+
this: E,
|
|
72
|
+
options: Readonly<RestEndpointExtendOptions<O, E, F> & O>,
|
|
73
|
+
): RestExtendedEndpoint<O, E>;
|
|
74
|
+
paginated<
|
|
75
|
+
E extends RestInstanceBase<FetchFunction, any, undefined>,
|
|
76
|
+
A extends any[],
|
|
77
|
+
>(
|
|
78
|
+
this: E,
|
|
79
|
+
removeCursor: (...args: A) => readonly [...Parameters<E>],
|
|
80
|
+
): PaginationEndpoint<E, A>;
|
|
81
|
+
paginated<
|
|
82
|
+
E extends RestInstanceBase<FetchFunction, any, undefined>,
|
|
83
|
+
C extends string,
|
|
84
|
+
>(
|
|
85
|
+
this: E,
|
|
86
|
+
cursorField: C,
|
|
87
|
+
): PaginationFieldEndpoint<E, C>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface RestInstance<
|
|
91
|
+
F extends FetchFunction = FetchFunction,
|
|
92
|
+
S extends Schema | undefined = any,
|
|
93
|
+
M extends true | undefined = true | undefined,
|
|
94
|
+
O extends {
|
|
95
|
+
path: string;
|
|
96
|
+
body?: any;
|
|
97
|
+
searchParams?: any;
|
|
98
|
+
} = { path: string },
|
|
99
|
+
> extends RestInstanceBase<F, S, M, O> {
|
|
100
|
+
push: AddEndpoint<F, S, O>;
|
|
101
|
+
unshift: AddEndpoint<F, S, O>;
|
|
102
|
+
assign: AddEndpoint<F, S, O>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type RestEndpointExtendOptions<
|
|
106
|
+
O extends PartialRestGenerics,
|
|
107
|
+
E extends RestInstanceBase,
|
|
108
|
+
F extends FetchFunction,
|
|
109
|
+
> = RestEndpointOptions<
|
|
110
|
+
OptionsToFunction<O, E, F>,
|
|
111
|
+
'schema' extends keyof O
|
|
112
|
+
? Extract<O['schema'], Schema | undefined>
|
|
113
|
+
: E['schema']
|
|
114
|
+
> &
|
|
115
|
+
Partial<Omit<E, KeyofRestEndpoint | keyof PartialRestGenerics>>;
|
|
116
|
+
|
|
117
|
+
type OptionsToRestEndpoint<
|
|
118
|
+
O extends PartialRestGenerics,
|
|
119
|
+
E extends RestInstanceBase & { body?: any },
|
|
120
|
+
F extends FetchFunction,
|
|
121
|
+
> = 'path' extends keyof O
|
|
122
|
+
? RestType<
|
|
123
|
+
'searchParams' extends keyof O
|
|
124
|
+
? O['searchParams'] extends undefined
|
|
125
|
+
? PathArgs<Exclude<O['path'], undefined>>
|
|
126
|
+
: O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
|
|
127
|
+
: PathArgs<Exclude<O['path'], undefined>>,
|
|
128
|
+
'body' extends keyof O ? O['body'] : E['body'],
|
|
129
|
+
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
130
|
+
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
131
|
+
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
|
|
132
|
+
{
|
|
133
|
+
path: Exclude<O['path'], undefined>;
|
|
134
|
+
body: 'body' extends keyof O ? O['body'] : E['body'];
|
|
135
|
+
searchParams: 'searchParams' extends keyof O
|
|
136
|
+
? O['searchParams']
|
|
137
|
+
: E['searchParams'];
|
|
138
|
+
}
|
|
139
|
+
>
|
|
140
|
+
: 'body' extends keyof O
|
|
141
|
+
? RestType<
|
|
142
|
+
'searchParams' extends keyof O
|
|
143
|
+
? O['searchParams'] extends undefined
|
|
144
|
+
? PathArgs<Exclude<O['path'], undefined>>
|
|
145
|
+
: O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>
|
|
146
|
+
: PathArgs<Exclude<E['path'], undefined>>,
|
|
147
|
+
O['body'],
|
|
148
|
+
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
149
|
+
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
150
|
+
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
|
|
151
|
+
{
|
|
152
|
+
path: E['path'];
|
|
153
|
+
body: O['body'];
|
|
154
|
+
searchParams: 'searchParams' extends keyof O
|
|
155
|
+
? O['searchParams']
|
|
156
|
+
: E['searchParams'];
|
|
157
|
+
}
|
|
158
|
+
>
|
|
159
|
+
: 'searchParams' extends keyof O
|
|
160
|
+
? RestType<
|
|
161
|
+
O['searchParams'] extends undefined
|
|
162
|
+
? PathArgs<Exclude<O['path'], undefined>>
|
|
163
|
+
: O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>,
|
|
164
|
+
E['body'],
|
|
165
|
+
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
166
|
+
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
167
|
+
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
|
|
168
|
+
{
|
|
169
|
+
path: E['path'];
|
|
170
|
+
body: E['body'];
|
|
171
|
+
searchParams: O['searchParams'];
|
|
172
|
+
}
|
|
173
|
+
>
|
|
174
|
+
: RestInstance<
|
|
175
|
+
F,
|
|
176
|
+
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
177
|
+
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
178
|
+
{
|
|
179
|
+
path: 'path' extends keyof O
|
|
180
|
+
? Exclude<O['path'], undefined>
|
|
181
|
+
: E['path'];
|
|
182
|
+
body: 'body' extends keyof O ? O['body'] : E['body'];
|
|
183
|
+
searchParams: 'searchParams' extends keyof O
|
|
184
|
+
? O['searchParams']
|
|
185
|
+
: E['searchParams'];
|
|
186
|
+
}
|
|
187
|
+
>;
|
|
188
|
+
|
|
189
|
+
export type RestExtendedEndpoint<
|
|
190
|
+
O extends PartialRestGenerics,
|
|
191
|
+
E extends RestInstanceBase,
|
|
192
|
+
> = OptionsToRestEndpoint<
|
|
193
|
+
O,
|
|
194
|
+
E,
|
|
195
|
+
RestInstance<
|
|
196
|
+
(
|
|
197
|
+
...args: Parameters<E>
|
|
198
|
+
) => O['process'] extends {}
|
|
199
|
+
? Promise<ReturnType<O['process']>>
|
|
200
|
+
: ReturnType<E>,
|
|
201
|
+
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
202
|
+
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect']
|
|
203
|
+
>
|
|
204
|
+
> &
|
|
205
|
+
Omit<O, KeyofRestEndpoint> &
|
|
206
|
+
Omit<E, KeyofRestEndpoint | keyof O>;
|
|
207
|
+
|
|
208
|
+
export interface PartialRestGenerics {
|
|
209
|
+
readonly path?: string;
|
|
210
|
+
readonly schema?: Schema | undefined;
|
|
211
|
+
readonly method?: string;
|
|
212
|
+
/** Only used for types */
|
|
213
|
+
readonly body?: any;
|
|
214
|
+
/** Only used for types */
|
|
215
|
+
readonly searchParams?: any;
|
|
216
|
+
/** @see https://resthooks.io/rest/api/RestEndpoint#process */
|
|
217
|
+
process?(value: any, ...args: any): any;
|
|
218
|
+
}
|
|
219
|
+
export interface RestGenerics extends PartialRestGenerics {
|
|
220
|
+
readonly path: string;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export type PaginationEndpoint<
|
|
224
|
+
E extends RestInstanceBase,
|
|
225
|
+
A extends any[],
|
|
226
|
+
> = RestInstance<
|
|
227
|
+
ParamFetchNoBody<A[0], ResolveType<E>>,
|
|
228
|
+
E['schema'],
|
|
229
|
+
E['sideEffect'],
|
|
230
|
+
Pick<E, 'path' | 'searchParams' | 'body'> & {
|
|
231
|
+
searchParams: Omit<A[0], keyof PathArgs<E['path']>>;
|
|
232
|
+
}
|
|
233
|
+
>;
|
|
234
|
+
export type PaginationFieldEndpoint<
|
|
235
|
+
E extends RestInstanceBase,
|
|
236
|
+
C extends string,
|
|
237
|
+
> = RestInstance<
|
|
238
|
+
ParamFetchNoBody<
|
|
239
|
+
{ [K in C]: string | number | boolean } & E['searchParams'] &
|
|
240
|
+
PathArgs<Exclude<E['path'], undefined>>,
|
|
241
|
+
ResolveType<E>
|
|
242
|
+
>,
|
|
243
|
+
E['schema'],
|
|
244
|
+
E['sideEffect'],
|
|
245
|
+
Pick<E, 'path' | 'searchParams' | 'body'> & {
|
|
246
|
+
searchParams: { [K in C]: string | number | boolean } & E['searchParams'];
|
|
247
|
+
}
|
|
248
|
+
>;
|
|
249
|
+
export type AddEndpoint<
|
|
250
|
+
F extends FetchFunction = FetchFunction,
|
|
251
|
+
S extends Schema | undefined = any,
|
|
252
|
+
O extends {
|
|
253
|
+
path: string;
|
|
254
|
+
body?: any;
|
|
255
|
+
searchParams?: any;
|
|
256
|
+
} = { path: string },
|
|
257
|
+
> = RestInstanceBase<
|
|
258
|
+
RestFetch<
|
|
259
|
+
'searchParams' extends keyof O
|
|
260
|
+
? O['searchParams'] extends undefined
|
|
261
|
+
? PathArgs<Exclude<O['path'], undefined>>
|
|
262
|
+
: O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
|
|
263
|
+
: PathArgs<Exclude<O['path'], undefined>>,
|
|
264
|
+
any,
|
|
265
|
+
ReturnType<F>
|
|
266
|
+
>,
|
|
267
|
+
ExtractCollection<S>,
|
|
268
|
+
true,
|
|
269
|
+
Omit<O, 'body'>
|
|
270
|
+
> & { method: 'POST' };
|
|
271
|
+
|
|
272
|
+
export type BodyDefault<O extends RestGenerics> = 'body' extends keyof O
|
|
273
|
+
? O['body']
|
|
274
|
+
: O['method'] extends 'POST' | 'PUT' | 'PATCH'
|
|
275
|
+
? Record<string, unknown> | FormData
|
|
276
|
+
: undefined;
|
|
277
|
+
|
|
278
|
+
type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O
|
|
279
|
+
? O
|
|
280
|
+
: O['method'] extends 'POST' | 'PUT' | 'PATCH'
|
|
281
|
+
? O & { body: any }
|
|
282
|
+
: O & { body: undefined };
|
|
283
|
+
|
|
284
|
+
export interface RestEndpointOptions<
|
|
285
|
+
F extends FetchFunction = FetchFunction,
|
|
286
|
+
S extends Schema | undefined = undefined,
|
|
287
|
+
> extends EndpointExtraOptions<F> {
|
|
288
|
+
urlPrefix?: string;
|
|
289
|
+
requestInit?: RequestInit;
|
|
290
|
+
getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
|
|
291
|
+
getRequestInit?(body: any): Promise<RequestInit> | RequestInit;
|
|
292
|
+
fetchResponse?(input: RequestInfo, init: RequestInit): Promise<any>;
|
|
293
|
+
parseResponse?(response: Response): Promise<any>;
|
|
294
|
+
|
|
295
|
+
sideEffect?: true | undefined;
|
|
296
|
+
name?: string;
|
|
297
|
+
signal?: AbortSignal;
|
|
298
|
+
fetch?: F;
|
|
299
|
+
key?(...args: Parameters<F>): string;
|
|
300
|
+
url?(...args: Parameters<F>): string;
|
|
301
|
+
update?: EndpointUpdateFunction<F, S>;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export type RestEndpointConstructorOptions<O extends RestGenerics = any> =
|
|
305
|
+
RestEndpointOptions<
|
|
306
|
+
RestFetch<
|
|
307
|
+
'searchParams' extends keyof O
|
|
308
|
+
? O['searchParams'] extends undefined
|
|
309
|
+
? PathArgs<O['path']>
|
|
310
|
+
: O['searchParams'] & PathArgs<O['path']>
|
|
311
|
+
: PathArgs<O['path']>,
|
|
312
|
+
BodyDefault<O>,
|
|
313
|
+
O['process'] extends {}
|
|
314
|
+
? ReturnType<O['process']>
|
|
315
|
+
: any /*Denormalize<O['schema']>*/
|
|
316
|
+
>,
|
|
317
|
+
O['schema']
|
|
318
|
+
>;
|
|
319
|
+
|
|
320
|
+
export interface RestEndpointConstructor {
|
|
321
|
+
new <O extends RestGenerics = any>({
|
|
322
|
+
method,
|
|
323
|
+
sideEffect,
|
|
324
|
+
name,
|
|
325
|
+
...options
|
|
326
|
+
}: RestEndpointConstructorOptions<O> & Readonly<O>): RestInstance<
|
|
327
|
+
RestFetch<
|
|
328
|
+
'searchParams' extends keyof O
|
|
329
|
+
? O['searchParams'] extends undefined
|
|
330
|
+
? PathArgs<O['path']>
|
|
331
|
+
: O['searchParams'] & PathArgs<O['path']>
|
|
332
|
+
: PathArgs<O['path']>,
|
|
333
|
+
BodyDefault<O>,
|
|
334
|
+
O['process'] extends {}
|
|
335
|
+
? ReturnType<O['process']>
|
|
336
|
+
: any /*Denormalize<O['schema']>*/
|
|
337
|
+
>,
|
|
338
|
+
'schema' extends keyof O ? O['schema'] : undefined,
|
|
339
|
+
MethodToSide<O['method']>,
|
|
340
|
+
OptionsBodyDefault<O>
|
|
341
|
+
>;
|
|
342
|
+
readonly prototype: RestInstanceBase;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Simplifies endpoint definitions that follow REST patterns
|
|
346
|
+
*
|
|
347
|
+
* @see https://resthooks.io/rest/api/RestEndpoint
|
|
348
|
+
*/
|
|
349
|
+
export declare let RestEndpoint: RestEndpointConstructor;
|
|
350
|
+
export default RestEndpoint;
|
|
351
|
+
|
|
352
|
+
export type MethodToSide<M> = M extends string
|
|
353
|
+
? M extends 'GET'
|
|
354
|
+
? undefined
|
|
355
|
+
: true
|
|
356
|
+
: undefined;
|
|
357
|
+
|
|
358
|
+
/** RestEndpoint types simplified */
|
|
359
|
+
export type RestType<
|
|
360
|
+
UrlParams = any,
|
|
361
|
+
Body = any,
|
|
362
|
+
S extends Schema | undefined = Schema | undefined,
|
|
363
|
+
M extends true | undefined = true | undefined,
|
|
364
|
+
R = any,
|
|
365
|
+
O extends {
|
|
366
|
+
path: string;
|
|
367
|
+
body?: any;
|
|
368
|
+
searchParams?: any;
|
|
369
|
+
} = { path: string },
|
|
370
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
371
|
+
> = IfTypeScriptLooseNull<
|
|
372
|
+
RestInstance<
|
|
373
|
+
keyof UrlParams extends never
|
|
374
|
+
? (this: EndpointInstanceInterface, body?: Body) => Promise<R>
|
|
375
|
+
: // even with loose null, this will only be true when all members are optional
|
|
376
|
+
{} extends UrlParams
|
|
377
|
+
?
|
|
378
|
+
| ((this: EndpointInstanceInterface, body?: Body) => Promise<R>)
|
|
379
|
+
| ((
|
|
380
|
+
this: EndpointInstanceInterface,
|
|
381
|
+
params: UrlParams,
|
|
382
|
+
body?: Body,
|
|
383
|
+
) => Promise<R>)
|
|
384
|
+
: (
|
|
385
|
+
this: EndpointInstanceInterface,
|
|
386
|
+
params: UrlParams,
|
|
387
|
+
body?: Body,
|
|
388
|
+
) => Promise<R>,
|
|
389
|
+
S,
|
|
390
|
+
M,
|
|
391
|
+
O
|
|
392
|
+
>,
|
|
393
|
+
Body extends {}
|
|
394
|
+
? RestTypeWithBody<UrlParams, S, M, Body, R, O>
|
|
395
|
+
: RestTypeNoBody<UrlParams, S, M, R, O>
|
|
396
|
+
>;
|
|
397
|
+
|
|
398
|
+
export type RestTypeWithBody<
|
|
399
|
+
UrlParams = any,
|
|
400
|
+
S extends Schema | undefined = Schema | undefined,
|
|
401
|
+
M extends true | undefined = true | undefined,
|
|
402
|
+
Body = any,
|
|
403
|
+
R = any /*Denormalize<S>*/,
|
|
404
|
+
O extends {
|
|
405
|
+
path: string;
|
|
406
|
+
body?: any;
|
|
407
|
+
searchParams?: any;
|
|
408
|
+
} = { path: string; body: any },
|
|
409
|
+
> = RestInstance<ParamFetchWithBody<UrlParams, Body, R>, S, M, O>;
|
|
410
|
+
|
|
411
|
+
export type RestTypeNoBody<
|
|
412
|
+
UrlParams = any,
|
|
413
|
+
S extends Schema | undefined = Schema | undefined,
|
|
414
|
+
M extends true | undefined = true | undefined,
|
|
415
|
+
R = any /*Denormalize<S>*/,
|
|
416
|
+
O extends {
|
|
417
|
+
path: string;
|
|
418
|
+
body?: undefined;
|
|
419
|
+
searchParams?: any;
|
|
420
|
+
} = { path: string; body: undefined },
|
|
421
|
+
> = RestInstance<ParamFetchNoBody<UrlParams, R>, S, M, O>;
|
|
422
|
+
|
|
423
|
+
/** Simple parameters, and body fetch functions */
|
|
424
|
+
export type RestFetch<
|
|
425
|
+
UrlParams,
|
|
426
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
427
|
+
Body = {},
|
|
428
|
+
Resolve = any,
|
|
429
|
+
> = IfTypeScriptLooseNull<
|
|
430
|
+
| ParamFetchNoBody<UrlParams, Resolve>
|
|
431
|
+
| ParamFetchWithBody<UrlParams, Body, Resolve>,
|
|
432
|
+
Body extends {}
|
|
433
|
+
? ParamFetchWithBody<UrlParams, Body, Resolve>
|
|
434
|
+
: ParamFetchNoBody<UrlParams, Resolve>
|
|
435
|
+
>;
|
|
436
|
+
|
|
437
|
+
export type ParamFetchWithBody<P, B = {}, R = any> =
|
|
438
|
+
// we must always allow undefined in a union and give it a type without params
|
|
439
|
+
P extends undefined
|
|
440
|
+
? (this: EndpointInstanceInterface, body: B) => Promise<R>
|
|
441
|
+
: // even with loose null, this will only be true when all members are optional
|
|
442
|
+
{} extends P
|
|
443
|
+
? // this safely handles PathArgs with no members that results in a simple `unknown` type
|
|
444
|
+
keyof P extends never
|
|
445
|
+
? (this: EndpointInstanceInterface, body: B) => Promise<R>
|
|
446
|
+
:
|
|
447
|
+
| ((
|
|
448
|
+
this: EndpointInstanceInterface,
|
|
449
|
+
params: P,
|
|
450
|
+
body: B,
|
|
451
|
+
) => Promise<R>)
|
|
452
|
+
| ((this: EndpointInstanceInterface, body: B) => Promise<R>)
|
|
453
|
+
: (this: EndpointInstanceInterface, params: P, body: B) => Promise<R>;
|
|
454
|
+
|
|
455
|
+
export type ParamFetchNoBody<P, R = any> =
|
|
456
|
+
// we must always allow undefined in a union and give it a type without params
|
|
457
|
+
P extends undefined
|
|
458
|
+
? (this: EndpointInstanceInterface) => Promise<R>
|
|
459
|
+
: // even with loose null, this will only be true when all members are optional
|
|
460
|
+
{} extends P
|
|
461
|
+
? // this safely handles PathArgs with no members that results in a simple `unknown` type
|
|
462
|
+
keyof P extends never
|
|
463
|
+
? (this: EndpointInstanceInterface) => Promise<R>
|
|
464
|
+
:
|
|
465
|
+
| ((this: EndpointInstanceInterface, params: P) => Promise<R>)
|
|
466
|
+
| ((this: EndpointInstanceInterface) => Promise<R>)
|
|
467
|
+
: (this: EndpointInstanceInterface, params: P) => Promise<R>;
|
|
468
|
+
|
|
469
|
+
type IfTypeScriptLooseNull<Y, N> = 1 | undefined extends 1 ? Y : N;
|
|
470
|
+
|
|
471
|
+
export type KeyofRestEndpoint = keyof RestInstance;
|
|
472
|
+
|
|
473
|
+
export type FromFallBack<K extends keyof E, O, E> = K extends keyof O
|
|
474
|
+
? O[K]
|
|
475
|
+
: E[K];
|
|
476
|
+
|
|
477
|
+
export type FetchMutate<
|
|
478
|
+
A extends readonly any[] = // eslint-disable-next-line @typescript-eslint/ban-types
|
|
479
|
+
| [any, {}]
|
|
480
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
481
|
+
| [{}],
|
|
482
|
+
R = any,
|
|
483
|
+
> = (this: RestInstance, ...args: A) => Promise<R>;
|
|
484
|
+
|
|
485
|
+
export type FetchGet<A extends readonly any[] = [any], R = any> = (
|
|
486
|
+
this: RestInstance,
|
|
487
|
+
...args: A
|
|
488
|
+
) => Promise<R>;
|
|
489
|
+
|
|
490
|
+
export type Defaults<O, D> = {
|
|
491
|
+
[K in keyof O | keyof D]: K extends keyof O
|
|
492
|
+
? Exclude<O[K], undefined>
|
|
493
|
+
: D[Extract<K, keyof D>];
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
export type GetEndpoint<
|
|
497
|
+
O extends {
|
|
498
|
+
readonly path: string;
|
|
499
|
+
readonly schema: Schema;
|
|
500
|
+
/** Only used for types */
|
|
501
|
+
readonly searchParams?: any;
|
|
502
|
+
} = {
|
|
503
|
+
path: string;
|
|
504
|
+
schema: Schema;
|
|
505
|
+
},
|
|
506
|
+
> = RestTypeNoBody<
|
|
507
|
+
'searchParams' extends keyof O
|
|
508
|
+
? O['searchParams'] extends undefined
|
|
509
|
+
? PathArgs<O['path']>
|
|
510
|
+
: O['searchParams'] & PathArgs<O['path']>
|
|
511
|
+
: PathArgs<O['path']>,
|
|
512
|
+
O['schema'],
|
|
513
|
+
undefined,
|
|
514
|
+
any,
|
|
515
|
+
O & { body: undefined }
|
|
516
|
+
>;
|
|
517
|
+
|
|
518
|
+
export type MutateEndpoint<
|
|
519
|
+
O extends {
|
|
520
|
+
readonly path: string;
|
|
521
|
+
readonly schema: Schema;
|
|
522
|
+
/** Only used for types */
|
|
523
|
+
readonly searchParams?: any;
|
|
524
|
+
/** Only used for types */
|
|
525
|
+
readonly body?: any;
|
|
526
|
+
} = {
|
|
527
|
+
path: string;
|
|
528
|
+
body: any;
|
|
529
|
+
schema: Schema;
|
|
530
|
+
},
|
|
531
|
+
> = RestTypeWithBody<
|
|
532
|
+
'searchParams' extends keyof O
|
|
533
|
+
? O['searchParams'] extends undefined
|
|
534
|
+
? PathArgs<O['path']>
|
|
535
|
+
: O['searchParams'] & PathArgs<O['path']>
|
|
536
|
+
: PathArgs<O['path']>,
|
|
537
|
+
O['schema'],
|
|
538
|
+
true,
|
|
539
|
+
O['body'],
|
|
540
|
+
any,
|
|
541
|
+
O
|
|
542
|
+
>;
|