Skip to content

Commit

Permalink
Remove hooks from datx-network to avoid react peer dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkoKukovec committed Nov 1, 2020
1 parent a3a7742 commit 26529ea
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 46 deletions.
4 changes: 1 addition & 3 deletions packages/datx-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@types/react": "^16.9.50",
"jest": "^26.4.2",
"mobx": "^6.0.0",
"react": "^16.13.1",
"rollup": "^2.28.2",
"rollup-plugin-exclude-dependencies-from-bundle": "^1.1.13",
"rollup-plugin-terser": "^7.0.2",
Expand All @@ -37,8 +36,7 @@
"typescript": "^4.0.3"
},
"peerDependencies": {
"mobx": "^4.2.0 || ^5.5.0 || ^6.0.0",
"react": "^16.8.0"
"mobx": "^4.2.0 || ^5.5.0 || ^6.0.0"
},
"scripts": {
"test": "jest --coverage",
Expand Down
43 changes: 0 additions & 43 deletions packages/datx-network/src/BaseRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PureModel } from 'datx';
import { useCallback, useEffect, useState } from 'react';

import { getDefaultConfig } from './defaults';
import { IConfigType } from './interfaces/IConfigType';
Expand All @@ -16,10 +15,6 @@ import { CachingStrategy } from './enums/CachingStrategy';
import { fetchInterceptor } from './interceptors/fetch';
import { body as bodyOperator } from './operators';

interface IHookOptions {
suspense?: boolean;
}

interface IRequestOptions {
method: HttpMethod;
url?: string;
Expand Down Expand Up @@ -144,44 +139,6 @@ export class BaseRequest<TModel extends PureModel = PureModel, TParams extends o
return interceptorChain(requestRef);
}

public useHook(
params?: TParams,
body?: any,
bodyType?: BodyType,
options?: IHookOptions,
): [Response<TModel> | null, boolean, string | Error | null] {
const [loader, setLoader] = useState<Promise<Response<TModel>> | null>(null);
const [value, setValue] = useState<Response<TModel> | null>(null);
const [error, setError] = useState<string | Error | null>(null);

const execute = useCallback(() => {
const loaderPromise = this.fetch(params, body, bodyType);
setLoader(loaderPromise);
setValue(null);
setError(null);

return loaderPromise
.then((response) => {
setValue(response);
setLoader(null);
})
.catch((error) => {
setError(error);
setLoader(null);
});
}, [this.fetch, params]);

useEffect(() => {
execute();
}, [execute]);

if (options?.suspense && loader) {
throw loader;
}

return [value, Boolean(loader), error];
}

public clone<TNewModel extends PureModel = TModel, TNewParams extends object = TParams>(
BaseRequestConstructor: typeof BaseRequest = this.constructor as typeof BaseRequest,
): BaseRequest<TNewModel, TNewParams> {
Expand Down

0 comments on commit 26529ea

Please sign in to comment.