@tmlmobilidade/utils 20250728.1548.33 → 20250728.2156.56

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,7 +1,13 @@
1
- export interface HttpResponse<T> {
2
- data: null | T;
3
- error: null | string;
4
- status: number;
1
+ export declare class HttpResponse<T> {
2
+ readonly data: null | T;
3
+ readonly error: null | string;
4
+ readonly isOk?: () => boolean;
5
+ readonly statusCode: number;
6
+ constructor({ data, error, statusCode }: {
7
+ data: null | T;
8
+ error: null | string;
9
+ statusCode: number;
10
+ });
5
11
  }
6
12
  export type WithPagination<T> = T & {
7
13
  pagination: {
package/dist/src/http.js CHANGED
@@ -1,4 +1,16 @@
1
1
  import { HttpException } from '@tmlmobilidade/lib';
2
+ export class HttpResponse {
3
+ data;
4
+ error;
5
+ isOk;
6
+ statusCode;
7
+ constructor({ data, error, statusCode }) {
8
+ this.data = data;
9
+ this.error = error;
10
+ this.statusCode = statusCode;
11
+ this.isOk = () => statusCode >= 200 && statusCode < 300;
12
+ }
13
+ }
2
14
  /**
3
15
  * Fetches data from a URL with configurable HTTP method, body, headers, and options.
4
16
  * @param url - The URL to fetch from
@@ -33,26 +45,25 @@ export async function fetchData(url, method = 'GET', body, headers = {}, options
33
45
  ...options,
34
46
  });
35
47
  const data = await response.json();
36
- if (!response.ok) {
37
- const errorData = data;
38
- return {
48
+ if (!response.ok || data.error) {
49
+ return new HttpResponse({
39
50
  data: null,
40
- error: errorData.message || 'An error occurred',
41
- status: response.status,
42
- };
51
+ error: data.error,
52
+ statusCode: response.status,
53
+ });
43
54
  }
44
- return {
45
- data: data,
55
+ return new HttpResponse({
56
+ data: data.data,
46
57
  error: null,
47
- status: response.status,
48
- };
58
+ statusCode: response.status,
59
+ });
49
60
  }
50
61
  catch (error) {
51
- return {
62
+ return new HttpResponse({
52
63
  data: null,
53
64
  error: error instanceof Error ? error.message : 'Network error',
54
- status: 500,
55
- };
65
+ statusCode: 500,
66
+ });
56
67
  }
57
68
  }
58
69
  /**
@@ -76,25 +87,25 @@ export async function multipartFetch(url, formData) {
76
87
  method: 'POST',
77
88
  });
78
89
  const data = await response.json();
79
- if (!response.ok) {
80
- return {
90
+ if (!response.ok || data.error) {
91
+ return new HttpResponse({
81
92
  data: null,
82
- error: data.message || 'An error occurred',
83
- status: response.status,
84
- };
93
+ error: data.error,
94
+ statusCode: response.status,
95
+ });
85
96
  }
86
- return {
87
- data: data,
97
+ return new HttpResponse({
98
+ data: data.data,
88
99
  error: null,
89
- status: response.status,
90
- };
100
+ statusCode: response.status,
101
+ });
91
102
  }
92
103
  catch (error) {
93
- return {
104
+ return new HttpResponse({
94
105
  data: null,
95
106
  error: error instanceof Error ? error.message : 'Network error',
96
- status: 500,
97
- };
107
+ statusCode: 500,
108
+ });
98
109
  }
99
110
  }
100
111
  /**
@@ -125,7 +136,7 @@ export const swrFetcher = async (url) => {
125
136
  const res = await fetch(url, { credentials: 'include' });
126
137
  const data = await res.json();
127
138
  if (!res.ok) {
128
- throw new HttpException(res.status, data.message);
139
+ throw new HttpException(res.status, data.error ?? 'An error occurred');
129
140
  }
130
141
  return data;
131
142
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/utils",
3
- "version": "20250728.1548.33",
3
+ "version": "20250728.2156.56",
4
4
  "author": "João de Vasconcelos & Jusi Monteiro",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "homepage": "https://github.com/tmlmobilidade/services#readme",