@hapl/api-queries 0.1.73 → 0.1.74

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.
@@ -1,4 +1,15 @@
1
+ import { AxiosResponse, AxiosError } from 'axios';
1
2
  import { User, UserDirection, UserOffice } from '../../../types';
3
+ declare type ResultData = {
4
+ ids: number[];
5
+ byId: Record<string, Partial<User> & {
6
+ id: number;
7
+ }>;
8
+ meta: {
9
+ total: number;
10
+ };
11
+ };
12
+ declare type ResultError = string;
2
13
  export declare type FindPublishedExpertsParams = {
3
14
  filter?: {
4
15
  id?: number | number[];
@@ -15,23 +26,11 @@ export declare type FindPublishedExpertsParams = {
15
26
  type: 'id' | 'expert.scoring';
16
27
  };
17
28
  };
18
- export declare type FindPublishedExpertsData = {
19
- data: {
20
- ids: number[];
21
- byId: Record<string, Partial<User> & {
22
- id: number;
23
- }>;
24
- };
25
- meta: {
26
- total: number;
27
- };
28
- };
29
- export declare type FindPublishedExpertsError = {
30
- status: number;
31
- data: string;
32
- };
29
+ export declare type FindPublishedExpertsData = AxiosResponse<ResultData>;
30
+ export declare type FindPublishedExpertsError = AxiosError<ResultError>;
33
31
  export declare type FindPublishedExpertsConfig = {
34
32
  baseURL?: string;
35
33
  params: FindPublishedExpertsParams;
36
34
  };
37
35
  export declare function findPublishedExpertsRequest({ baseURL, params, }: FindPublishedExpertsConfig): Promise<FindPublishedExpertsData>;
36
+ export {};
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.1.73",
2
+ "version": "0.1.74",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -1,4 +1,4 @@
1
- import axios, { AxiosResponse, AxiosError } from 'axios';
1
+ import axios, { AxiosResponse, AxiosError, AxiosTransformer } from 'axios';
2
2
  import qs from 'qs';
3
3
  import { User, UserDirection, UserOffice } from '../../../types';
4
4
 
@@ -7,9 +7,11 @@ type SuccessData = {
7
7
  data: Partial<User> & { id: number }[];
8
8
  pageParams: { page: number; length: number };
9
9
  };
10
-
11
10
  type ErrorData = { success: false; data: { error: string } };
12
11
 
12
+ type ResultData = { ids: number[]; byId: Record<string, Partial<User> & { id: number }>; meta: { total: number } };
13
+ type ResultError = string;
14
+
13
15
  export type FindPublishedExpertsParams = {
14
16
  filter?: {
15
17
  id?: number | number[];
@@ -20,14 +22,8 @@ export type FindPublishedExpertsParams = {
20
22
  limits?: { page: number; count: number };
21
23
  sorting?: { direction: 'asc' | 'desc'; type: 'id' | 'expert.scoring' };
22
24
  };
23
-
24
- export type FindPublishedExpertsData = {
25
- data: { ids: number[]; byId: Record<string, Partial<User> & { id: number }> };
26
- meta: { total: number };
27
- };
28
-
29
- export type FindPublishedExpertsError = { status: number; data: string };
30
-
25
+ export type FindPublishedExpertsData = AxiosResponse<ResultData>;
26
+ export type FindPublishedExpertsError = AxiosError<ResultError>;
31
27
  export type FindPublishedExpertsConfig = { baseURL?: string; params: FindPublishedExpertsParams };
32
28
 
33
29
  export function findPublishedExpertsRequest({
@@ -40,27 +36,27 @@ export function findPublishedExpertsRequest({
40
36
  params,
41
37
  paramsSerializer: params => qs.stringify(params, { arrayFormat: 'brackets' }),
42
38
  headers: { Accept: 'application/json' },
39
+ transformResponse: [
40
+ ...(axios.defaults.transformResponse as AxiosTransformer[]),
41
+ (data: SuccessData | ErrorData): ResultData | ResultError => {
42
+ if (data.success) {
43
+ const ids: ResultData['ids'] = [];
44
+ const byId: ResultData['byId'] = {};
45
+
46
+ data.data.forEach(entity => {
47
+ byId[entity.id] = entity;
48
+ ids.push(entity.id);
49
+ });
50
+
51
+ return { ids, byId, meta: { total: data.pageParams.length } };
52
+ }
53
+
54
+ return data.data.error;
55
+ },
56
+ ],
43
57
  })
44
- .then(
45
- (res: AxiosResponse<SuccessData>): FindPublishedExpertsData => {
46
- const data: FindPublishedExpertsData = {
47
- data: { byId: {}, ids: [] },
48
- meta: { total: res.data.pageParams.length },
49
- };
50
-
51
- res.data.data.forEach(entity => {
52
- data.data.byId[entity.id] = entity;
53
- data.data.ids.push(entity.id);
54
- });
55
-
56
- return data;
57
- }
58
- )
59
- .catch((err: AxiosError<ErrorData>) => {
60
- const error: Error & Partial<FindPublishedExpertsError> = new Error(err.message);
61
- error.status = err.response?.status ?? 520;
62
- error.data = err.response?.data.data.error ?? 'Unknown Error';
63
-
64
- throw error;
58
+ .then((res: FindPublishedExpertsData) => res)
59
+ .catch((err: FindPublishedExpertsError) => {
60
+ throw err;
65
61
  });
66
62
  }