refactor: circular deps part 14

This commit is contained in:
Nathan Walker
2025-07-10 15:47:29 -07:00
parent cebc78406b
commit e7ab426ee2
87 changed files with 2667 additions and 3403 deletions

View File

@ -0,0 +1,98 @@
import type { ImageSource } from '../image-source';
import type { File } from '../file-system';
/**
* Provides options for the http requests.
*/
export interface HttpRequestOptions {
/**
* Gets or sets the request url.
*/
url: string;
/**
* Gets or sets the request method.
*/
method: string;
/**
* Gets or sets the request headers in JSON format.
*/
headers?: any;
/**
* Gets or sets the request body.
*/
content?: string | FormData | ArrayBuffer | Uint8Array<ArrayBufferLike>;
/**
* Gets or sets the request timeout in milliseconds.
*/
timeout?: number;
/**
* Gets or sets whether to *not* follow server's redirection responses.
*/
dontFollowRedirects?: boolean;
}
/**
* Encapsulates HTTP-response information from an HTTP-request.
*/
export interface HttpResponse {
/**
* Gets the response status code.
*/
statusCode: number;
/**
* Gets the response headers.
*/
headers: Headers;
/**
* Gets the response content.
*/
content?: HttpContent;
}
export type Headers = { [key: string]: string | string[] };
export enum HttpResponseEncoding {
UTF8,
GBK,
}
/**
* Encapsulates the content of an HttpResponse.
*/
export interface HttpContent {
/**
* Gets the response body as raw data.
*/
raw: any;
/**
* Gets the response body as ArrayBuffer
*/
toArrayBuffer: () => ArrayBuffer;
/**
* Gets the response body as string.
*/
toString: (encoding?: HttpResponseEncoding) => string;
/**
* Gets the response body as JSON object.
*/
toJSON: (encoding?: HttpResponseEncoding) => any;
/**
* Gets the response body as ImageSource.
*/
toImage: () => Promise<ImageSource>;
/**
* Gets the response body as file.
*/
toFile: (destinationFilePath?: string) => File;
}

View File

@ -1,7 +1,6 @@
import * as fsModule from '../../file-system';
import { knownFolders, path } from '../../file-system';
export function getFilenameFromUrl(url: string) {
const fs: typeof fsModule = require('../../file-system');
const slashPos = url.lastIndexOf('/') + 1;
const questionMarkPos = url.lastIndexOf('?');
@ -12,7 +11,7 @@ export function getFilenameFromUrl(url: string) {
actualFileName = url.substring(slashPos);
}
const result = fs.path.join(fs.knownFolders.documents().path, actualFileName);
const result = path.join(knownFolders.documents().path, actualFileName);
return result;
}

View File

@ -1,17 +1,12 @@
// imported for definition purposes only
import * as httpModule from '../../http';
import * as imageSourceModule from '../../image-source';
import { ImageSource } from '../../image-source';
import { Screen } from '../../platform';
import * as fsModule from '../../file-system';
import { File } from '../../file-system';
import { HttpResponseEncoding } from '../http-interfaces';
import { getFilenameFromUrl } from './http-request-common';
import * as domainDebugger from '../../debugger';
export enum HttpResponseEncoding {
UTF8,
GBK,
}
function parseJSON(source: string): any {
const src = source.trim();
if (src.lastIndexOf(')') === src.length - 1) {
@ -24,19 +19,6 @@ function parseJSON(source: string): any {
let requestIdCounter = 0;
const pendingRequests = {};
let imageSource: typeof imageSourceModule;
function ensureImageSource() {
if (!imageSource) {
imageSource = require('../../image-source');
}
}
let fs: typeof fsModule;
function ensureFileSystem() {
if (!fs) {
fs = require('../../file-system');
}
}
let debugRequests: Map<number, { debugRequest: domainDebugger.domains.network.NetworkRequest; timestamp: number }>;
if (__DEV__) {
debugRequests = new Map();
@ -151,26 +133,22 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
return parseJSON(str);
},
toImage: () => {
ensureImageSource();
return new Promise<any>((resolveImage, rejectImage) => {
if (result.responseAsImage != null) {
resolveImage(new imageSource.ImageSource(result.responseAsImage));
resolveImage(new ImageSource(result.responseAsImage));
} else {
rejectImage(new Error('Response content may not be converted to an Image'));
}
});
},
toFile: (destinationFilePath: string) => {
ensureFileSystem();
if (!destinationFilePath) {
destinationFilePath = getFilenameFromUrl(callbacks.url);
}
let stream: java.io.FileOutputStream;
try {
// ensure destination path exists by creating any missing parent directories
const file = fs.File.fromPath(destinationFilePath);
const file = File.fromPath(destinationFilePath);
const javaFile = new java.io.File(destinationFilePath);
stream = new java.io.FileOutputStream(javaFile);

View File

@ -1,3 +1,3 @@
import { HttpRequestOptions, HttpResponse, Headers } from '..';
import { HttpRequestOptions, HttpResponse, Headers } from '../http-interfaces';
export const request: (options: HttpRequestOptions) => Promise<HttpResponse>;
export function addHeader(headers: Headers, key: string, value: string): void;

View File

@ -1,18 +1,12 @@
// imported for definition purposes only
import * as httpModule from '../../http';
import * as imageSourceModule from '../../image-source';
import * as fsModule from '../../file-system';
import { SDK_VERSION } from '../../utils/constants';
import { isRealDevice } from '../../utils';
import * as types from '../../utils/types';
import * as domainDebugger from '../../debugger';
import { getFilenameFromUrl } from './http-request-common';
export enum HttpResponseEncoding {
UTF8,
GBK,
}
import { ImageSource } from '../../image-source';
import { File } from '../../file-system';
import type { HttpRequestOptions, HttpResponse, Headers } from '../http-interfaces';
import { HttpResponseEncoding } from '../http-interfaces';
const currentDevice = UIDevice.currentDevice;
const device = currentDevice.userInterfaceIdiom === UIUserInterfaceIdiom.Phone ? 'Phone' : 'Pad';
@ -58,22 +52,8 @@ function ensureSessionNotFollowingRedirects() {
}
}
let imageSource: typeof imageSourceModule;
function ensureImageSource() {
if (!imageSource) {
imageSource = require('../../image-source');
}
}
let fs: typeof fsModule;
function ensureFileSystem() {
if (!fs) {
fs = require('../../file-system');
}
}
export function request(options: httpModule.HttpRequestOptions): Promise<httpModule.HttpResponse> {
return new Promise<httpModule.HttpResponse>((resolve, reject) => {
export function request(options: HttpRequestOptions): Promise<HttpResponse> {
return new Promise<HttpResponse>((resolve, reject) => {
if (!options.url) {
reject(new Error('Request url was empty.'));
@ -121,7 +101,7 @@ export function request(options: httpModule.HttpRequestOptions): Promise<httpMod
if (error) {
reject(new Error(error.localizedDescription));
} else {
const headers: httpModule.Headers = {};
const headers: Headers = {};
if (response && response.allHeaderFields) {
const headerFields = response.allHeaderFields;
@ -177,12 +157,10 @@ export function request(options: httpModule.HttpRequestOptions): Promise<httpMod
},
toJSON: (encoding?: any) => parseJSON(NSDataToString(data, encoding)),
toImage: () => {
ensureImageSource();
return new Promise((resolve, reject) => {
(<any>UIImage).tns_decodeImageWithDataCompletion(data, (image) => {
if (image) {
resolve(new imageSource.ImageSource(image));
resolve(new ImageSource(image));
} else {
reject(new Error('Response content may not be converted to an Image'));
}
@ -190,14 +168,12 @@ export function request(options: httpModule.HttpRequestOptions): Promise<httpMod
});
},
toFile: (destinationFilePath?: string) => {
ensureFileSystem();
if (!destinationFilePath) {
destinationFilePath = getFilenameFromUrl(options.url);
}
if (data instanceof NSData) {
// ensure destination path exists by creating any missing parent directories
const file = fs.File.fromPath(destinationFilePath);
const file = File.fromPath(destinationFilePath);
data.writeToFileAtomically(destinationFilePath, true);
@ -250,7 +226,7 @@ function NSDataToString(data: any, encoding?: HttpResponseEncoding): string {
return encodedString.toString();
}
export function addHeader(headers: httpModule.Headers, key: string, value: string): void {
export function addHeader(headers: Headers, key: string, value: string): void {
if (!headers[key]) {
headers[key] = value;
} else if (Array.isArray(headers[key])) {

View File

@ -7,3 +7,15 @@ export interface ImageSourceLike {
toBase64String(format: string, quality?: number): string;
// ... add other shared methods/properties as needed
}
// Circular dependency resolution handling (http <--> image-source)
let _getImage: (arg: any) => Promise<ImageSourceLike>;
export function getImageRequest(arg: any): Promise<ImageSourceLike> {
if (_getImage) {
return _getImage(arg);
}
return Promise.reject(new Error('No getImage request handler set.'));
}
export function setGetImageRequest(fn: (arg: any) => Promise<ImageSourceLike>) {
_getImage = fn;
}

View File

@ -1,6 +1,5 @@
// import { ImageSource } from '../image-source';
import type { ImageSourceLike } from './http-shared';
import { File } from '../file-system';
import type { ImageSource } from '../image-source';
export * from './http-interfaces';
/**
* Downloads the content from the specified URL as a string.
@ -30,13 +29,13 @@ export function getJSON<T>(options: HttpRequestOptions): Promise<T>;
* Downloads the content from the specified URL and attempts to decode it as an image.
* @param url The URL to request from.
*/
export function getImage(url: string): Promise<ImageSourceLike>;
export function getImage(url: string): Promise<ImageSource>;
/**
* Downloads the content from the specified URL and attempts to decode it as an image.
* @param options An object that specifies various request options.
*/
export function getImage(options: HttpRequestOptions): Promise<ImageSourceLike>;
export function getImage(options: HttpRequestOptions): Promise<ImageSource>;
/**
* Downloads the content from the specified URL and attempts to save it as file.
@ -158,10 +157,10 @@ export interface HttpContent {
/**
* Gets the response body as ImageSource.
*/
toImage: () => Promise<ImageSourceLike>;
toImage: () => Promise<ImageSource>;
/**
* Gets the response body as file.
*/
toFile: (destinationFilePath?: string) => File;
toFile: (destinationFilePath?: string) => any /* File */;
}

View File

@ -1,107 +1,11 @@
import type { ImageSourceLike } from './http-shared';
import { File } from '../file-system';
import * as httpRequest from './http-request';
import { setGetImageRequest, type ImageSourceLike } from './http-shared';
import { request } from './http-request';
export * from './http-request';
/**
* Provides options for the http requests.
*/
export interface HttpRequestOptions {
/**
* Gets or sets the request url.
*/
url: string;
/**
* Gets or sets the request method.
*/
method: string;
/**
* Gets or sets the request headers in JSON format.
*/
headers?: any;
/**
* Gets or sets the request body.
*/
content?: string | FormData | ArrayBuffer | Uint8Array<ArrayBufferLike>;
/**
* Gets or sets the request timeout in milliseconds.
*/
timeout?: number;
/**
* Gets or sets whether to *not* follow server's redirection responses.
*/
dontFollowRedirects?: boolean;
}
/**
* Encapsulates HTTP-response information from an HTTP-request.
*/
export interface HttpResponse {
/**
* Gets the response status code.
*/
statusCode: number;
/**
* Gets the response headers.
*/
headers: Headers;
/**
* Gets the response content.
*/
content?: HttpContent;
}
export type Headers = { [key: string]: string | string[] };
export enum HttpResponseEncoding {
UTF8,
GBK,
}
/**
* Encapsulates the content of an HttpResponse.
*/
export interface HttpContent {
/**
* Gets the response body as raw data.
*/
raw: any;
/**
* Gets the response body as ArrayBuffer
*/
toArrayBuffer: () => ArrayBuffer;
/**
* Gets the response body as string.
*/
toString: (encoding?: HttpResponseEncoding) => string;
/**
* Gets the response body as JSON object.
*/
toJSON: (encoding?: HttpResponseEncoding) => any;
/**
* Gets the response body as ImageSource.
*/
toImage: () => Promise<ImageSourceLike>;
/**
* Gets the response body as file.
*/
toFile: (destinationFilePath?: string) => File;
}
export * from './http-interfaces';
export function getString(arg: any): Promise<string> {
return new Promise<string>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const str = r.content.toString();
@ -117,7 +21,7 @@ export function getString(arg: any): Promise<string> {
export function getJSON<T>(arg: any): Promise<T> {
return new Promise<T>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const json = r.content.toJSON();
@ -133,7 +37,7 @@ export function getJSON<T>(arg: any): Promise<T> {
export function getImage(arg: any): Promise<ImageSourceLike> {
return new Promise<any>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
resolve(r.content.toImage());
@ -147,10 +51,11 @@ export function getImage(arg: any): Promise<ImageSourceLike> {
);
});
}
setGetImageRequest(getImage);
export function getFile(arg: any, destinationFilePath?: string): Promise<any> {
return new Promise<any>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const file = r.content.toFile(destinationFilePath);
@ -166,7 +71,7 @@ export function getFile(arg: any, destinationFilePath?: string): Promise<any> {
export function getBinary(arg: any): Promise<ArrayBuffer> {
return new Promise<ArrayBuffer>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const arrayBuffer = r.content.toArrayBuffer();