Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Nocobase
Commits
f1b8dc29
Commit
f1b8dc29
authored
3 years ago
by
chenos
Browse files
Options
Download
Email Patches
Plain Diff
fix: use request params merge
parent
cae11da7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/client/src/api-client/hooks/assign.ts
+118
-0
packages/client/src/api-client/hooks/assign.ts
packages/client/src/api-client/hooks/useRequest.ts
+12
-7
packages/client/src/api-client/hooks/useRequest.ts
with
130 additions
and
7 deletions
+130
-7
packages/client/src/api-client/hooks/assign.ts
0 → 100644
+
118
-
0
View file @
f1b8dc29
import
lodash
from
'
lodash
'
;
import
deepmerge
from
'
deepmerge
'
;
type
MergeStrategyType
=
'
merge
'
|
'
deepMerge
'
|
'
overwrite
'
|
'
andMerge
'
|
'
orMerge
'
|
'
intersect
'
|
'
union
'
;
type
MergeStrategyFunc
=
(
x
:
any
,
y
:
any
)
=>
any
;
export
type
MergeStrategy
=
MergeStrategyType
|
MergeStrategyFunc
;
export
interface
MergeStrategies
{
[
key
:
string
]:
MergeStrategy
;
}
export
default
function
isPlainObject
(
value
)
{
if
(
Object
.
prototype
.
toString
.
call
(
value
)
!==
'
[object Object]
'
)
{
return
false
;
}
const
prototype
=
Object
.
getPrototypeOf
(
value
);
return
prototype
===
null
||
prototype
===
Object
.
prototype
;
}
function
getEnumerableOwnPropertySymbols
(
target
:
any
):
any
[]
{
return
Object
.
getOwnPropertySymbols
?
Object
.
getOwnPropertySymbols
(
target
).
filter
((
symbol
)
=>
target
.
propertyIsEnumerable
(
symbol
))
:
[];
}
function
getKeys
(
target
:
any
)
{
return
Object
.
keys
(
target
).
concat
(
getEnumerableOwnPropertySymbols
(
target
));
}
export
const
mergeStrategies
=
new
Map
<
MergeStrategyType
,
MergeStrategyFunc
>
();
mergeStrategies
.
set
(
'
overwrite
'
,
(
_
,
y
)
=>
{
return
y
;
});
mergeStrategies
.
set
(
'
andMerge
'
,
(
x
,
y
)
=>
{
if
(
!
x
&&
!
y
)
{
return
;
}
if
(
!
x
)
{
return
y
;
}
if
(
!
y
)
{
return
x
;
}
return
{
$and
:
[
x
,
y
],
};
});
mergeStrategies
.
set
(
'
orMerge
'
,
(
x
,
y
)
=>
{
if
(
!
x
&&
!
y
)
{
return
;
}
if
(
!
x
)
{
return
y
;
}
if
(
!
y
)
{
return
x
;
}
return
{
$or
:
[
x
,
y
],
};
});
mergeStrategies
.
set
(
'
deepMerge
'
,
(
x
,
y
)
=>
{
return
isPlainObject
(
x
)
&&
isPlainObject
(
y
)
?
deepmerge
(
x
,
y
,
{
arrayMerge
:
(
x
,
y
)
=>
y
,
})
:
y
;
});
mergeStrategies
.
set
(
'
merge
'
,
(
x
,
y
)
=>
{
return
isPlainObject
(
x
)
&&
isPlainObject
(
y
)
?
Object
.
assign
(
x
,
y
)
:
y
;
});
mergeStrategies
.
set
(
'
union
'
,
(
x
,
y
)
=>
{
if
(
typeof
x
===
'
string
'
)
{
x
=
x
.
split
(
'
,
'
);
}
if
(
typeof
y
===
'
string
'
)
{
y
=
y
.
split
(
'
,
'
);
}
return
lodash
.
uniq
((
x
||
[]).
concat
(
y
||
[]));
});
mergeStrategies
.
set
(
'
intersect
'
,
(
x
,
y
)
=>
{
if
(
typeof
x
===
'
string
'
)
{
x
=
x
.
split
(
'
,
'
);
}
if
(
typeof
y
===
'
string
'
)
{
y
=
y
.
split
(
'
,
'
);
}
if
(
!
Array
.
isArray
(
x
)
||
x
.
length
===
0
)
{
return
y
||
[];
}
if
(
!
Array
.
isArray
(
y
)
||
y
.
length
===
0
)
{
return
x
||
[];
}
return
x
.
filter
((
v
)
=>
y
.
includes
(
v
));
});
export
function
assign
(
target
:
any
,
source
:
any
,
strategies
:
MergeStrategies
=
{})
{
getKeys
(
source
).
forEach
((
sourceKey
)
=>
{
const
strategy
=
strategies
[
sourceKey
];
let
func
=
mergeStrategies
.
get
(
'
deepMerge
'
);
if
(
typeof
strategy
===
'
function
'
)
{
func
=
strategy
;
}
else
if
(
typeof
strategy
===
'
string
'
&&
mergeStrategies
.
has
(
strategy
as
any
))
{
func
=
mergeStrategies
.
get
(
strategy
as
any
);
}
target
[
sourceKey
]
=
func
(
target
[
sourceKey
],
source
[
sourceKey
]);
});
return
target
;
}
This diff is collapsed.
Click to expand it.
packages/client/src/api-client/hooks/useRequest.ts
+
12
-
7
View file @
f1b8dc29
import
{
useContext
}
from
'
react
'
;
import
{
AxiosRequestConfig
}
from
'
axios
'
;
import
{
Options
}
from
'
ahooks/lib/useRequest/src/types
'
;
import
{
merge
}
from
'
@formily/shared
'
;
import
{
default
as
useReq
}
from
'
ahooks/lib/useRequest
'
;
import
{
Options
}
from
'
ahooks/lib/useRequest/src/types
'
;
import
{
AxiosRequestConfig
}
from
'
axios
'
;
import
cloneDeep
from
'
lodash/cloneDeep
'
;
import
{
useContext
}
from
'
react
'
;
import
{
APIClientContext
}
from
'
../context
'
;
import
{
assign
}
from
'
./assign
'
;
type
FunctionService
=
(...
args
:
any
[])
=>
Promise
<
any
>
;
...
...
@@ -21,14 +24,16 @@ export function useRequest<P>(
if
(
typeof
service
===
'
function
'
)
{
return
useReq
(
service
,
options
);
}
return
useReq
(
async
(
params
)
=>
{
return
useReq
(
async
(
params
=
{}
)
=>
{
const
{
resource
}
=
service
as
ResourceActionOptions
;
let
args
=
cloneDeep
(
service
);
if
(
resource
)
{
Object
.
assign
(
service
,
{
params
});
args
.
params
=
args
.
params
||
{};
assign
(
args
.
params
,
params
);
}
else
{
Object
.
assign
(
service
,
params
);
args
=
merge
(
args
,
params
);
}
const
response
=
await
api
.
request
(
service
);
const
response
=
await
api
.
request
(
args
);
return
response
?.
data
;
},
options
);
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help