Skip to content

Commit

Permalink
enhance: Improve legacy TypeScript compatibility (#2505)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Mar 21, 2023
1 parent 3612a25 commit 8bcaf1a
Show file tree
Hide file tree
Showing 19 changed files with 460 additions and 84 deletions.
2 changes: 2 additions & 0 deletions packages/endpoint/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
/dist
/legacy
/index.d.ts
/ts3.4
/ts4.0
/ts4.2
15 changes: 12 additions & 3 deletions packages/endpoint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
"unpkg": "dist/index.umd.min.js",
"types": "lib/index.d.ts",
"typesVersions": {
">=4.2": {
">=4.8": {
"": [
"lib/index.d.ts"
],
"*": [
"lib/index.d.ts"
]
},
">=4.2": {
"": [
"ts4.2/index.d.ts"
],
"*": [
"ts4.2/index.d.ts"
]
},
">=4.0": {
"": [
"ts4.0/index.d.ts"
Expand Down Expand Up @@ -51,6 +59,7 @@
"src",
"dist",
"lib",
"ts4.2",
"ts4.0",
"ts3.4",
"node.mjs",
Expand All @@ -64,9 +73,9 @@
"build:js:node": "BROWSERSLIST_ENV=node12 rollup -c && echo '{\"type\":\"commonjs\"}' > dist/package.json",
"build:js:browser": "BROWSERSLIST_ENV=legacy rollup -c",
"build:bundle": "run-s build:js:\\*",
"build:clean": "rimraf lib dist legacy ts3.4 ts4.0 *.tsbuildinfo",
"build:clean": "rimraf lib dist legacy ts3.4 ts4.0 ts4.2 *.tsbuildinfo",
"build": "yarn run build:lib && yarn run build:legacy:lib && yarn run build:bundle",
"build:legacy-types": "yarn run downlevel-dts lib ts3.4 && yarn run downlevel-dts lib ts4.0 --to=4.0 && copyfiles --up 1 ./src-4.0-types/**/*.d.ts ./ts3.4/ && copyfiles --up 1 ./src-4.0-types/**/*.d.ts ./ts4.0 && copyfiles --up 1 ./src-legacy-types/**/*.d.ts ./ts3.4/",
"build:legacy-types": "yarn run downlevel-dts lib ts3.4 && yarn run downlevel-dts lib ts4.0 --to=4.0 && yarn run downlevel-dts lib ts4.2 --to=4.2 && copyfiles --up 1 ./src-4.2-types/**/*.d.ts ./ts4.0/ && copyfiles --up 1 ./src-4.2-types/**/*.d.ts ./ts4.2 && copyfiles --up 1 ./src-4.0-types/**/*.d.ts ./ts3.4/ && copyfiles --up 1 ./src-4.0-types/**/*.d.ts ./ts4.0 && copyfiles --up 1 ./src-legacy-types/**/*.d.ts ./ts3.4/",
"dev": "yarn run build:lib -w",
"prepare": "yarn run build:lib",
"prepack": "yarn prepare",
Expand Down
217 changes: 217 additions & 0 deletions packages/endpoint/src-4.2-types/endpoint.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// relaxed constraints on call,apply,bind *this*

/* eslint-disable @typescript-eslint/ban-types */
import type { EndpointInterface, Schema } from './interface.js';
import type {
EndpointExtraOptions,
FetchFunction,
PartialArray,
} from './types.js';

export interface EndpointOptions<
F extends FetchFunction = FetchFunction,
S extends Schema | undefined = undefined,
M extends true | undefined = undefined,
> extends EndpointExtraOptions<F> {
key?: (...args: Parameters<F>) => string;
sideEffect?: M;
schema?: S;
[k: string]: any;
}

export interface EndpointExtendOptions<
F extends FetchFunction = FetchFunction,
S extends Schema | undefined = Schema | undefined,
M extends true | undefined = true | undefined,
> extends EndpointOptions<F, S, M> {
fetch?: FetchFunction;
}

export type ParamFromFetch<F> = F extends (
params: infer P,
body?: any,
) => Promise<any>
? P
: never;

type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;

export type KeyofEndpointInstance = keyof EndpointInstance<FetchFunction>;

export type ExtendedEndpoint<
O extends EndpointExtendOptions<F>,
E extends EndpointInstance<
FetchFunction,
Schema | undefined,
true | undefined
>,
F extends FetchFunction,
> = EndpointInstance<
'fetch' extends keyof O ? Exclude<O['fetch'], undefined> : E['fetch'],
'schema' extends keyof O ? O['schema'] : E['schema'],
'sideEffect' extends keyof O ? O['sideEffect'] : E['sideEffect']
> &
Omit<O, KeyofEndpointInstance> &
Omit<E, KeyofEndpointInstance>;

export function Make(...args: any[]): EndpointInstance<FetchFunction>;

/**
* Defines an async data source.
* @see https://resthooks.io/docs/api/Endpoint
*/
export interface EndpointInstance<
F extends (...args: any) => Promise<any> = FetchFunction,
S extends Schema | undefined = Schema | undefined,
M extends true | undefined = true | undefined,
> extends EndpointInstanceInterface<F, S, M> {
extend<
E extends EndpointInstance<
(...args: any) => Promise<any>,
Schema | undefined,
true | undefined
>,
O extends EndpointExtendOptions<F> &
Partial<Omit<E, keyof EndpointInstance<FetchFunction>>> &
Record<string, unknown>,
>(
this: E,
options: Readonly<O>,
): ExtendedEndpoint<typeof options, E, F>;
}

/**
* Defines an async data source.
* @see https://resthooks.io/docs/api/Endpoint
*/
export interface EndpointInstanceInterface<
F extends FetchFunction = FetchFunction,
S extends Schema | undefined = Schema | undefined,
M extends true | undefined = true | undefined,
> extends EndpointInterface<F, S, M> {
constructor: EndpointConstructor;

/**
* Calls the function, substituting the specified object for the this value of the function, and the specified array for the arguments of the function.
* @param thisArg The object to be used as the this object.
* @param argArray A set of arguments to be passed to the function.
*/
apply<E extends FetchFunction>(
this: E,
thisArg: any,
argArray?: Parameters<E>,
): ReturnType<E>;

/**
* Calls a method of an object, substituting another object for the current object.
* @param thisArg The object to be used as the current object.
* @param argArray A list of arguments to be passed to the method.
*/
call<E extends FetchFunction>(
this: E,
thisArg: any,
...argArray: Parameters<E>
): ReturnType<E>;

/**
* For a given function, creates a bound function that has the same body as the original function.
* The this object of the bound function is associated with the specified object, and has the specified initial parameters.
* @param thisArg An object to which the this keyword can refer inside the new function.
* @param argArray A list of arguments to be passed to the new function.
*/
bind<E extends FetchFunction, P extends PartialArray<Parameters<E>>>(
this: E,
thisArg: any,
...args: readonly [...P]
): EndpointInstance<
(...args: readonly [...RemoveArray<Parameters<E>, P>]) => ReturnType<E>,
S,
M
> &
Omit<E, keyof EndpointInstance<FetchFunction>>;

/** Returns a string representation of a function. */
toString(): string;

prototype: any;
readonly length: number;

// Non-standard extensions
arguments: any;
caller: F;

key(...args: Parameters<F>): string;

readonly sideEffect: M;

readonly schema: S;

fetch: F;

/* utilities */
/** @see https://resthooks.io/rest/api/Endpoint#testKey */
testKey(key: string): boolean;

/** The following is for compatibility with FetchShape */
/** @deprecated */
readonly type: M extends undefined
? 'read'
: IfAny<M, any, IfTypeScriptLooseNull<'read', 'mutate'>>;

/** @deprecated */
getFetchKey(...args: OnlyFirst<Parameters<F>>): string;
/** @deprecated */
options?: EndpointExtraOptions<F>;
}

interface EndpointConstructor {
new <
F extends (
this: EndpointInstance<FetchFunction> & E,
params?: any,
body?: any,
) => Promise<any>,
S extends Schema | undefined = undefined,
M extends true | undefined = undefined,
E extends Record<string, any> = {},
>(
fetchFunction: F,
options?: EndpointOptions<F, S, M> & E,
): EndpointInstance<F, S, M> & E;
readonly prototype: Function;
}
declare let Endpoint: EndpointConstructor;

export default Endpoint;

interface ExtendableEndpointConstructor {
new <
F extends (
this: EndpointInstanceInterface<FetchFunction> & E,
params?: any,
body?: any,
) => Promise<any>,
S extends Schema | undefined = undefined,
M extends true | undefined = undefined,
E extends Record<string, any> = {},
>(
RestFetch: F,
options?: Readonly<EndpointOptions<F, S, M>> & E,
): EndpointInstanceInterface<F, S, M> & E;
readonly prototype: Function;
}
export declare let ExtendableEndpoint: ExtendableEndpointConstructor;

type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
type IfTypeScriptLooseNull<Y, N> = 1 | undefined extends 1 ? Y : N;

type OnlyFirst<A extends unknown[]> = A extends [] ? [] : [A[0]];

type RemoveArray<Orig extends any[], Rem extends any[]> = Rem extends [
any,
...infer RestRem,
]
? Orig extends [any, ...infer RestOrig]
? RemoveArray<RestOrig, RestRem>
: never
: Orig;
7 changes: 4 additions & 3 deletions packages/endpoint/src-legacy-types/endpoint.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// had to remove all [...T], which included importing PartialArray from types
// also relaxed constraints on call,apply,bind *this*

/* eslint-disable @typescript-eslint/ban-types */
import { EndpointInterface, Schema } from './interface.js';
Expand Down Expand Up @@ -84,7 +85,7 @@ export interface EndpointInstanceInterface<
*/
apply<E extends FetchFunction>(
this: E,
thisArg: ThisParameterType<E>,
thisArg: any,
argArray?: Parameters<E>,
): ReturnType<E>;
/**
Expand All @@ -94,7 +95,7 @@ export interface EndpointInstanceInterface<
*/
call<E extends FetchFunction>(
this: E,
thisArg: ThisParameterType<E>,
thisArg: any,
...argArray: Parameters<E>
): ReturnType<E>;
/**
Expand All @@ -105,7 +106,7 @@ export interface EndpointInstanceInterface<
*/
bind<E extends FetchFunction, P extends Parameters<E>>(
this: E,
thisArg: ThisParameterType<E>,
thisArg: any,
...args: P
): EndpointInstance<() => ReturnType<E>, S, M> &
Pick<E, Exclude<keyof E, keyof EndpointInstance<FetchFunction>>>;
Expand Down
4 changes: 2 additions & 2 deletions packages/endpoint/src/endpoint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { EndpointInterface, Schema } from './interface.js';
import type {
EndpointExtraOptions,
FetchFunction,
PartialArray,
PartialParameters,
} from './types.js';

export interface EndpointOptions<
Expand Down Expand Up @@ -117,7 +117,7 @@ export interface EndpointInstanceInterface<
* @param thisArg An object to which the this keyword can refer inside the new function.
* @param argArray A list of arguments to be passed to the new function.
*/
bind<E extends FetchFunction, P extends PartialArray<Parameters<E>>>(
bind<E extends FetchFunction, P extends PartialParameters<E>>(
this: E,
thisArg: ThisParameterType<E>,
...args: readonly [...P]
Expand Down
7 changes: 7 additions & 0 deletions packages/endpoint/src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ export type PartialArray<A> = A extends []
: A extends (infer T)[]
? T[]
: never;

// workaround for https://github.com/microsoft/TypeScript/issues/29919
export type PartialParameters<T extends (...args: any[]) => any> = T extends (
...args: infer P
) => any
? Partial<P>
: never;
7 changes: 7 additions & 0 deletions packages/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ const articlesAscending = useCache(sortedArticles, { asc: true });
const articlesDescending = useCache(sortedArticles, { asc: false });
```

### TypeScript requirements

TypeScript is optional, but will only work with 4.0 or above. 4.1 is needed for stronger types as it
supports inferring argument types from the path templates.

Version 5.x can be used for older TypeScript versions.

### Prior Art

- [Backbone Model](https://backbonejs.org/#Model)
Expand Down
14 changes: 7 additions & 7 deletions packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
"unpkg": "dist/index.umd.min.js",
"types": "lib/index.d.ts",
"typesVersions": {
">=4.3": {
">=4.1": {
"": [
"lib/index.d.ts"
],
"*": [
"lib/index.d.ts"
]
},
">=4.1": {
">=4.0": {
"": [
"ts4.1/index.d.ts"
"ts4.0/index.d.ts"
],
"*": [
"ts4.1/index.d.ts"
"ts4.0/index.d.ts"
]
}
},
Expand All @@ -45,7 +45,7 @@
"lib",
"node.mjs",
"legacy",
"ts4.1",
"ts4.0",
"LICENSE",
"README.md"
],
Expand All @@ -55,8 +55,8 @@
"build:js:node": "BROWSERSLIST_ENV=node12 rollup -c",
"build:js:browser": "BROWSERSLIST_ENV=legacy rollup -c",
"build:bundle": "run-s build:js:\\* && echo '{\"type\":\"commonjs\"}' > dist/package.json",
"build:clean": "rimraf lib ts4.1 legacy dist *.tsbuildinfo",
"build:legacy-types": "yarn run downlevel-dts lib ts4.1 --to=4.1 && copyfiles --up 1 ./src-4.1-types/**/*.d.ts ./ts4.1",
"build:clean": "rimraf lib ts4.0 legacy dist *.tsbuildinfo",
"build:legacy-types": "yarn run downlevel-dts lib ts4.0 --to=4.0 && copyfiles --up 1 ./src-4.0-types/**/*.d.ts ./ts4.0",
"build": "yarn run build:lib && yarn run build:legacy:lib && yarn run build:bundle",
"dev": "yarn run build:lib -w",
"prepare": "yarn run build:lib",
Expand Down
Loading

0 comments on commit 8bcaf1a

Please sign in to comment.