Skip to content

Commit fdf32d0

Browse files
authored
fix(core): split http module to solve circular refs (#11141)
1 parent a63003b commit fdf32d0

File tree

16 files changed

+690
-584
lines changed

16 files changed

+690
-584
lines changed

apps/automated/src/http/http-tests.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ImageSource } from '@nativescript/core';
22
import * as TKUnit from '../tk-unit';
33
import * as http from '@nativescript/core/http';
44
import * as fs from '@nativescript/core/file-system';
5-
import { addHeader } from '@nativescript/core/http/http-request';
5+
import { requestInternal, addHeader, BaseHttpContent } from '@nativescript/core/http/http-request-internal';
66

77
export var test_getString_isDefined = function () {
88
TKUnit.assert(typeof http.getString !== 'undefined', 'Method http.getString() should be defined!');
@@ -329,6 +329,89 @@ export var test_request_requestShouldTimeout = function (done) {
329329
});
330330
};
331331

332+
export var test_requestInternal_responseStatusCodeShouldBeDefined = function (done) {
333+
requestInternal({ url: 'https://http-echo.nativescript.org/get', method: 'GET' }).then(
334+
function (response) {
335+
//// Argument (response) is HttpResponse!
336+
var statusCode = response.statusCode;
337+
try {
338+
TKUnit.assert(typeof statusCode !== 'undefined', 'response.statusCode should be defined!');
339+
done(null);
340+
} catch (err) {
341+
done(err);
342+
}
343+
},
344+
function (e) {
345+
//// Argument (e) is Error!
346+
done(e);
347+
},
348+
);
349+
};
350+
351+
export var test_requestInternal_responseContentShouldExposeNativeContentFunctions = function (done) {
352+
requestInternal({ url: 'https://http-echo.nativescript.org/get', method: 'GET' }).then(
353+
function (response) {
354+
try {
355+
TKUnit.assert(typeof response.content.toNativeImage === 'function' && typeof response.content.toNativeString === 'function', `response.content should expose native content functions!`);
356+
done(null);
357+
} catch (err) {
358+
done(err);
359+
}
360+
},
361+
function (e) {
362+
//// Argument (e) is Error!
363+
done(e);
364+
},
365+
);
366+
};
367+
368+
export var test_requestInternal_responseContentShouldExposeHandlerFunctions = function (done) {
369+
const responseHandler = {
370+
toDummy1: () => 'dummy1',
371+
toDummy2: () => 'dummy2',
372+
};
373+
374+
requestInternal({ url: 'https://http-echo.nativescript.org/get', method: 'GET' }, responseHandler).then(
375+
function (response) {
376+
try {
377+
TKUnit.assert(typeof response.content.toDummy1 === 'function' && typeof response.content.toDummy2 === 'function', `response.content should expose content handler functions!`);
378+
done(null);
379+
} catch (err) {
380+
done(err);
381+
}
382+
},
383+
function (e) {
384+
//// Argument (e) is Error!
385+
done(e);
386+
},
387+
);
388+
};
389+
390+
export var test_requestInternal_responseHandlerShouldBeAvailable = function (done) {
391+
const suffix = '-nsformatted';
392+
const responseHandler = {
393+
toFormattedString: function (this: BaseHttpContent) {
394+
return this.toNativeString() + suffix;
395+
},
396+
};
397+
398+
requestInternal({ url: 'https://http-echo.nativescript.org/get', method: 'GET' }, responseHandler).then(
399+
function (response) {
400+
const value = response.content.toFormattedString();
401+
try {
402+
TKUnit.assert(typeof value === 'string' && value.endsWith(suffix), `response.content.toFormattedString should return the response string appended with ${suffix} at the end!`);
403+
done(null);
404+
} catch (err) {
405+
done(err);
406+
}
407+
},
408+
function (e) {
409+
//// Argument (e) is Error!
410+
done(e);
411+
},
412+
);
413+
};
414+
332415
export var test_request_responseStatusCodeShouldBeDefined = function (done) {
333416
var result: http.HttpResponse;
334417

package-lock.json

Lines changed: 0 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/http/http-interfaces.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ImageSource } from '../image-source';
22
import type { File } from '../file-system';
3+
import type { BaseHttpContent } from './http-request-internal';
34

45
/**
56
* Provides options for the http requests.
@@ -39,7 +40,7 @@ export interface HttpRequestOptions {
3940
/**
4041
* Encapsulates HTTP-response information from an HTTP-request.
4142
*/
42-
export interface HttpResponse {
43+
export interface HttpResponse<T = HttpContent> {
4344
/**
4445
* Gets the response status code.
4546
*/
@@ -53,7 +54,7 @@ export interface HttpResponse {
5354
/**
5455
* Gets the response content.
5556
*/
56-
content?: HttpContent;
57+
content?: T;
5758
}
5859

5960
export type Headers = { [key: string]: string | string[] };
@@ -62,15 +63,8 @@ export enum HttpResponseEncoding {
6263
UTF8,
6364
GBK,
6465
}
65-
/**
66-
* Encapsulates the content of an HttpResponse.
67-
*/
68-
export interface HttpContent {
69-
/**
70-
* Gets the response body as raw data.
71-
*/
72-
raw: any;
7366

67+
export interface HttpContentHandler {
7468
/**
7569
* Gets the response body as ArrayBuffer
7670
*/
@@ -96,3 +90,8 @@ export interface HttpContent {
9690
*/
9791
toFile: (destinationFilePath?: string) => File;
9892
}
93+
94+
/**
95+
* Encapsulates the content of an HttpResponse.
96+
*/
97+
export interface HttpContent extends HttpContentHandler, BaseHttpContent {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Headers } from '../http-interfaces';
2+
3+
export function addHeader(headers: Headers, key: string, value: string): void {
4+
if (!headers[key]) {
5+
headers[key] = value;
6+
} else if (Array.isArray(headers[key])) {
7+
headers[key].push(value);
8+
} else {
9+
headers[key] = [headers[key], value];
10+
}
11+
}

0 commit comments

Comments
 (0)