avvir 0.4.17 → 0.4.19

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.
@@ -2,8 +2,9 @@ import _ from "underscore";
2
2
  import checkFetchStatus from "./check_fetch_status";
3
3
  import Config from "../config";
4
4
 
5
- const makeErrorsPrettyForFunction = (actionName, action, displayErrorMessage) => {
6
- return (...argumentList) => {
5
+ type ObjectWithMethodProperties = Record<string, (...args: any) => any>
6
+ const makeErrorsPrettyForFunction = <Class extends ObjectWithMethodProperties, Key extends keyof Class>(actionName: Key, action: Class[Key], displayErrorMessage: boolean): Class[Key] => {
7
+ return ((...argumentList) => {
7
8
  return action(...argumentList)
8
9
  .then((response) => checkFetchStatus(response, displayErrorMessage))
9
10
  .catch((error) => {
@@ -11,27 +12,34 @@ const makeErrorsPrettyForFunction = (actionName, action, displayErrorMessage) =>
11
12
  error,
12
13
  action: actionName,
13
14
  arguments: argumentList
14
- })
15
- })
15
+ });
16
+ });
17
+ }) as Class[Key];
18
+ };
16
19
 
17
- }
18
- }
19
- const getFunctionNames = (clazz) => {
20
+ type MethodProperties<T> = keyof { [P in keyof T as T[P] extends (...args: any) => any ? P : never]: any }
21
+ const getFunctionNames = <Class>(clazz: Class): MethodProperties<Class>[] => {
20
22
  let builtinProperties = ["length", "constructor", "name", "prototype"];
21
23
  // TODO it might make sense to check the type of each property
22
- return _.without(Object.getOwnPropertyNames(clazz), ...builtinProperties)
24
+ return _.without(Object.getOwnPropertyNames(clazz), ...builtinProperties) as MethodProperties<Class>[];
25
+ };
26
+
27
+ type Options<Class> = {
28
+ exclude?: MethodProperties<Class>[],
29
+ overrideErrorMessage?: MethodProperties<Class>[]
23
30
  }
24
31
 
25
- const makeErrorsPretty = (apiClass, options = {exclude: [], overrideErrorMessage: []}) => {
32
+ const makeErrorsPretty = <Class>(apiClass: Class, options: Options<Class> = { exclude: [], overrideErrorMessage: [] }) => {
26
33
  let functionNames = getFunctionNames(apiClass);
27
34
  _.forEach(functionNames, (functionName) => {
28
35
  let isExcluded = options.exclude && options.exclude.includes(functionName);
29
36
  let displayErrorMessage = !options.overrideErrorMessage || !options.overrideErrorMessage.includes(functionName);
30
37
  if (!isExcluded) {
38
+ // @ts-ignore
31
39
  apiClass[functionName] = makeErrorsPrettyForFunction(functionName, apiClass[functionName], displayErrorMessage);
32
40
  }
33
41
  });
34
42
  return apiClass;
35
- }
43
+ };
36
44
 
37
45
  export default makeErrorsPretty;