Skip to content

Commit ba4ab96

Browse files
authored
Merge pull request #2 from Coreoz/feature/fix-return-type-of-rest-request
Feature/fix return type of rest request
2 parents 5649ecb + 226f3c3 commit ba4ab96

File tree

2 files changed

+110
-9
lines changed

2 files changed

+110
-9
lines changed

src/lib/promise/HttpPromise.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ export class HttpPromise<T> {
151151
*
152152
* @param thenFunction The code that will be executed after the `Promise` has been resolved.
153153
*/
154-
then<R = void>(thenFunction: PromiseFunction<T, R>): HttpPromise<R> {
154+
then<R = void>(thenFunction: PromiseFunction<T, R>): HttpPromise<Awaited<R>> {
155155
this.isThenAttached = true;
156156
this.promise = this.promise.then(safeThen(thenFunction, this.debugContext));
157-
return this as unknown as HttpPromise<R>;
157+
return this as unknown as HttpPromise<Awaited<R>>;
158158
}
159159

160160
/**
@@ -174,10 +174,10 @@ export class HttpPromise<T> {
174174
* @param catchFunction The code that will do something with the {@link HttpError}
175175
* and possibility recover the `Promise`.
176176
*/
177-
catch<R = void>(catchFunction: PromiseFunction<HttpError, R>): HttpPromise<R | T> {
177+
catch<R = void>(catchFunction: PromiseFunction<HttpError, R>): HttpPromise<Awaited<R>> {
178178
this.isCaughtAttached = true;
179179
this.promise = this.promise.catch(safeCatch(catchFunction, this.debugContext));
180-
return this as unknown as HttpPromise<R>;
180+
return this as unknown as HttpPromise<Awaited<R>>;
181181
}
182182

183183
/**

src/tests/FetchClient.test.ts

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { HttpRequest, HttpMethod } from 'simple-http-request-builder';
2-
import ApiHttpClient from './ApiHttpClient';
3-
import { genericError, networkError, timeoutError } from '../lib/client/HttpResponse';
1+
import { HttpMethod, HttpRequest } from 'simple-http-request-builder';
42
import { fetchClient } from '../lib/client/FetchClient';
5-
import { unwrapHttpPromise } from '../lib/promise/HttpPromise';
3+
import {
4+
genericError,
5+
HttpError,
6+
HttpResponse,
7+
networkError,
8+
timeoutError,
9+
toErrorResponsePromise,
10+
} from '../lib/client/HttpResponse';
11+
import { HttpPromise, unwrapHttpPromise } from '../lib/promise/HttpPromise';
12+
import ApiHttpClient from './ApiHttpClient';
613

714
const waitTimeout = (durationInMillis: number) => new Promise(
815
// eslint-disable-next-line no-promise-executor-return
@@ -14,6 +21,9 @@ const mockedFetchResponseStatus: ResponseInit = {
1421
status: 200,
1522
statusText: 'Ok',
1623
// headers
24+
headers: {
25+
'content-type': 'application/json',
26+
},
1727
};
1828
let mockedPromiseError: Error | undefined;
1929

@@ -28,6 +38,19 @@ const mockedFetch = (): Promise<Response> => {
2838
return Promise.resolve(new Response(mockedFetchResponseBody, mockedFetchResponseStatus));
2939
};
3040

41+
type MockUser = { id: number };
42+
43+
const executeGetUserRequest = (mockUser: MockUser): HttpPromise<MockUser> => {
44+
// Mock body
45+
setMockedBody(mockUser);
46+
// Add client
47+
const apiClient: ApiHttpClient = new ApiHttpClient();
48+
// Execute request
49+
return apiClient
50+
.restRequest<MockUser>(HttpMethod.GET, 'https://hostname/users')
51+
.execute();
52+
};
53+
3154
describe('Tests fetch client', () => {
3255
const oldFetch = global.fetch;
3356
global.fetch = mockedFetch;
@@ -77,7 +100,9 @@ describe('Tests fetch client', () => {
77100

78101
test('Check handler execution error rejects promise with genericError', async () => {
79102
const httpClientWithErrorHandler = (httpRequest: HttpRequest<unknown>) => unwrapHttpPromise(fetchClient(
80-
httpRequest, () => { throw new Error(); },
103+
httpRequest, () => {
104+
throw new Error();
105+
},
81106
));
82107
const response = new HttpRequest(
83108
httpClientWithErrorHandler,
@@ -87,4 +112,80 @@ describe('Tests fetch client', () => {
87112
).execute();
88113
await expect(response).rejects.toEqual(genericError);
89114
});
115+
116+
test('Check return type of rest request must be User', async () => {
117+
const mockFirstUser: MockUser = { id: 1 };
118+
const result: MockUser = await executeGetUserRequest(mockFirstUser);
119+
expect(result.id).toEqual(1);
120+
});
121+
122+
test('Check return type of then function must be unwrap in the next then call', async () => {
123+
// Mock body
124+
const mockFirstUser: MockUser = { id: 1 };
125+
const mockSecondUser: MockUser = { id: 2 };
126+
127+
// Add client
128+
// Execute request in request
129+
const result: MockUser = await executeGetUserRequest(mockFirstUser)
130+
.then(() => executeGetUserRequest(mockSecondUser));
131+
expect(result.id).toEqual(2);
132+
});
133+
134+
test('Check return type of catch function must be unwrap in the next then call', async () => {
135+
const httpClientWithErrorHandler = (
136+
httpRequest: HttpRequest<unknown>,
137+
): Promise<HttpResponse<MockUser>> => fetchClient(
138+
httpRequest,
139+
() => {
140+
const httpError: HttpError = {
141+
errorCode: 'INTERNAL_ERROR',
142+
};
143+
return toErrorResponsePromise<HttpError>(httpError);
144+
},
145+
);
146+
147+
const result: HttpError = await new HttpRequest<HttpPromise<MockUser>>(
148+
(httpRequest: HttpRequest<unknown>) => new HttpPromise<MockUser>(
149+
unwrapHttpPromise(httpClientWithErrorHandler(httpRequest)),
150+
httpRequest,
151+
),
152+
'http://localhost',
153+
HttpMethod.GET,
154+
'/users',
155+
)
156+
.execute()
157+
.catch((httpError: HttpError) => httpError);
158+
159+
expect(result.errorCode).toEqual('INTERNAL_ERROR');
160+
});
161+
162+
test('Check return type of catch function must be unwrap in the next then call', async () => {
163+
const mockFirstUser: MockUser = { id: 1 };
164+
165+
const httpClientWithErrorHandler = (
166+
httpRequest: HttpRequest<unknown>,
167+
): Promise<HttpResponse<MockUser>> => fetchClient(
168+
httpRequest,
169+
() => {
170+
const httpError: HttpError = {
171+
errorCode: 'INTERNAL_ERROR',
172+
};
173+
return toErrorResponsePromise<HttpError>(httpError);
174+
},
175+
);
176+
177+
const result: MockUser = await new HttpRequest<HttpPromise<MockUser>>(
178+
(httpRequest: HttpRequest<unknown>) => new HttpPromise<MockUser>(
179+
unwrapHttpPromise(httpClientWithErrorHandler(httpRequest)),
180+
httpRequest,
181+
),
182+
'http://localhost',
183+
HttpMethod.GET,
184+
'/users',
185+
)
186+
.execute()
187+
.catch(() => executeGetUserRequest(mockFirstUser));
188+
189+
expect(result.id).toEqual(1);
190+
});
90191
});

0 commit comments

Comments
 (0)