update
This commit is contained in:
4
ts/core_base/index.ts
Normal file
4
ts/core_base/index.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// Core base exports - abstract classes and platform-agnostic types
|
||||||
|
export * from './types.js';
|
||||||
|
export * from './request.js';
|
||||||
|
export * from './response.js';
|
45
ts/core_base/request.ts
Normal file
45
ts/core_base/request.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import * as types from './types.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract Core Request class that defines the interface for all HTTP/HTTPS requests
|
||||||
|
*/
|
||||||
|
export abstract class CoreRequest<TOptions extends types.IAbstractRequestOptions = types.IAbstractRequestOptions, TResponse = any> {
|
||||||
|
/**
|
||||||
|
* Tests if a URL is a unix socket
|
||||||
|
*/
|
||||||
|
static isUnixSocket(url: string): boolean {
|
||||||
|
const unixRegex = /^(http:\/\/|https:\/\/|)unix:/;
|
||||||
|
return unixRegex.test(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses socket path and route from unix socket URL
|
||||||
|
*/
|
||||||
|
static parseUnixSocketUrl(url: string): { socketPath: string; path: string } {
|
||||||
|
const parseRegex = /(.*):(.*)/;
|
||||||
|
const result = parseRegex.exec(url);
|
||||||
|
return {
|
||||||
|
socketPath: result[1],
|
||||||
|
path: result[2],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected url: string;
|
||||||
|
protected options: TOptions;
|
||||||
|
|
||||||
|
constructor(url: string, options: TOptions) {
|
||||||
|
this.url = url;
|
||||||
|
this.options = options;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fire the request and return a response
|
||||||
|
*/
|
||||||
|
abstract fire(): Promise<TResponse>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fire the request and return the raw response (platform-specific)
|
||||||
|
*/
|
||||||
|
abstract fireCore(): Promise<any>;
|
||||||
|
|
||||||
|
}
|
40
ts/core_base/response.ts
Normal file
40
ts/core_base/response.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import * as types from './types.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract Core Response class that provides a fetch-like API
|
||||||
|
*/
|
||||||
|
export abstract class CoreResponse<T = any> implements types.IAbstractResponse<T> {
|
||||||
|
protected consumed = false;
|
||||||
|
|
||||||
|
// Public properties
|
||||||
|
public abstract readonly ok: boolean;
|
||||||
|
public abstract readonly status: number;
|
||||||
|
public abstract readonly statusText: string;
|
||||||
|
public abstract readonly headers: types.AbstractHeaders;
|
||||||
|
public abstract readonly url: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensures the body can only be consumed once
|
||||||
|
*/
|
||||||
|
protected ensureNotConsumed(): void {
|
||||||
|
if (this.consumed) {
|
||||||
|
throw new Error('Body has already been consumed');
|
||||||
|
}
|
||||||
|
this.consumed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse response as JSON
|
||||||
|
*/
|
||||||
|
abstract json(): Promise<T>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get response as text
|
||||||
|
*/
|
||||||
|
abstract text(): Promise<string>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get response as ArrayBuffer
|
||||||
|
*/
|
||||||
|
abstract arrayBuffer(): Promise<ArrayBuffer>;
|
||||||
|
}
|
62
ts/core_base/types.ts
Normal file
62
ts/core_base/types.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/**
|
||||||
|
* HTTP Methods supported
|
||||||
|
*/
|
||||||
|
export type THttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Response types supported
|
||||||
|
*/
|
||||||
|
export type ResponseType = 'json' | 'text' | 'binary' | 'stream';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Form field data for multipart/form-data requests
|
||||||
|
*/
|
||||||
|
export interface IFormField {
|
||||||
|
name: string;
|
||||||
|
value: string | Buffer;
|
||||||
|
filename?: string;
|
||||||
|
contentType?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* URL encoded form field
|
||||||
|
*/
|
||||||
|
export interface IUrlEncodedField {
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract request options - platform agnostic
|
||||||
|
*/
|
||||||
|
export interface IAbstractRequestOptions {
|
||||||
|
method?: THttpMethod | string; // Allow string for compatibility
|
||||||
|
headers?: any; // Allow any for platform compatibility
|
||||||
|
keepAlive?: boolean;
|
||||||
|
requestBody?: any;
|
||||||
|
queryParams?: { [key: string]: string };
|
||||||
|
timeout?: number;
|
||||||
|
hardDataCuttingTimeout?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract response headers - platform agnostic
|
||||||
|
*/
|
||||||
|
export type AbstractHeaders = Record<string, string | string[]>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract response interface - platform agnostic
|
||||||
|
*/
|
||||||
|
export interface IAbstractResponse<T = any> {
|
||||||
|
// Properties
|
||||||
|
ok: boolean;
|
||||||
|
status: number;
|
||||||
|
statusText: string;
|
||||||
|
headers: AbstractHeaders;
|
||||||
|
url: string;
|
||||||
|
|
||||||
|
// Methods
|
||||||
|
json(): Promise<T>;
|
||||||
|
text(): Promise<string>;
|
||||||
|
arrayBuffer(): Promise<ArrayBuffer>;
|
||||||
|
}
|
@@ -1,6 +1,7 @@
|
|||||||
import * as plugins from './plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
import * as types from './types.js';
|
import * as types from './types.js';
|
||||||
import { CoreResponse } from './response.js';
|
import { CoreResponse } from './response.js';
|
||||||
|
import { CoreRequest as AbstractCoreRequest } from '../core_base/request.js';
|
||||||
|
|
||||||
// Keep-alive agents for connection pooling
|
// Keep-alive agents for connection pooling
|
||||||
const httpAgent = new plugins.agentkeepalive.HttpAgent({
|
const httpAgent = new plugins.agentkeepalive.HttpAgent({
|
||||||
@@ -26,30 +27,9 @@ const httpsAgentKeepAliveFalse = new plugins.agentkeepalive.HttpsAgent({
|
|||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Core Request class that handles all HTTP/HTTPS requests
|
* Node.js implementation of Core Request class that handles all HTTP/HTTPS requests
|
||||||
*/
|
*/
|
||||||
export class CoreRequest {
|
export class CoreRequest extends AbstractCoreRequest<types.ICoreRequestOptions, CoreResponse> {
|
||||||
/**
|
|
||||||
* Tests if a URL is a unix socket
|
|
||||||
*/
|
|
||||||
static isUnixSocket(url: string): boolean {
|
|
||||||
const unixRegex = /^(http:\/\/|https:\/\/|)unix:/;
|
|
||||||
return unixRegex.test(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parses socket path and route from unix socket URL
|
|
||||||
*/
|
|
||||||
static parseUnixSocketUrl(url: string): { socketPath: string; path: string } {
|
|
||||||
const parseRegex = /(.*):(.*)/;
|
|
||||||
const result = parseRegex.exec(url);
|
|
||||||
return {
|
|
||||||
socketPath: result[1],
|
|
||||||
path: result[2],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
private url: string;
|
|
||||||
private options: types.ICoreRequestOptions;
|
|
||||||
private requestDataFunc: ((req: plugins.http.ClientRequest) => void) | null;
|
private requestDataFunc: ((req: plugins.http.ClientRequest) => void) | null;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -57,8 +37,7 @@ export class CoreRequest {
|
|||||||
options: types.ICoreRequestOptions = {},
|
options: types.ICoreRequestOptions = {},
|
||||||
requestDataFunc: ((req: plugins.http.ClientRequest) => void) | null = null
|
requestDataFunc: ((req: plugins.http.ClientRequest) => void) | null = null
|
||||||
) {
|
) {
|
||||||
this.url = url;
|
super(url, options);
|
||||||
this.options = options;
|
|
||||||
this.requestDataFunc = requestDataFunc;
|
this.requestDataFunc = requestDataFunc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,13 +1,13 @@
|
|||||||
import * as plugins from './plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
import * as types from './types.js';
|
import * as types from './types.js';
|
||||||
|
import { CoreResponse as AbstractCoreResponse } from '../core_base/response.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Core Response class that provides a fetch-like API
|
* Node.js implementation of Core Response class that provides a fetch-like API
|
||||||
*/
|
*/
|
||||||
export class CoreResponse<T = any> implements types.ICoreResponse<T> {
|
export class CoreResponse<T = any> extends AbstractCoreResponse<T> implements types.ICoreResponse<T> {
|
||||||
private incomingMessage: plugins.http.IncomingMessage;
|
private incomingMessage: plugins.http.IncomingMessage;
|
||||||
private bodyBufferPromise: Promise<Buffer> | null = null;
|
private bodyBufferPromise: Promise<Buffer> | null = null;
|
||||||
private consumed = false;
|
|
||||||
|
|
||||||
// Public properties
|
// Public properties
|
||||||
public readonly ok: boolean;
|
public readonly ok: boolean;
|
||||||
@@ -17,6 +17,7 @@ export class CoreResponse<T = any> implements types.ICoreResponse<T> {
|
|||||||
public readonly url: string;
|
public readonly url: string;
|
||||||
|
|
||||||
constructor(incomingMessage: plugins.http.IncomingMessage, url: string) {
|
constructor(incomingMessage: plugins.http.IncomingMessage, url: string) {
|
||||||
|
super();
|
||||||
this.incomingMessage = incomingMessage;
|
this.incomingMessage = incomingMessage;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.status = incomingMessage.statusCode || 0;
|
this.status = incomingMessage.statusCode || 0;
|
||||||
@@ -25,16 +26,6 @@ export class CoreResponse<T = any> implements types.ICoreResponse<T> {
|
|||||||
this.headers = incomingMessage.headers;
|
this.headers = incomingMessage.headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Ensures the body can only be consumed once
|
|
||||||
*/
|
|
||||||
private ensureNotConsumed(): void {
|
|
||||||
if (this.consumed) {
|
|
||||||
throw new Error('Body has already been consumed');
|
|
||||||
}
|
|
||||||
this.consumed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collects the body as a buffer
|
* Collects the body as a buffer
|
||||||
*/
|
*/
|
||||||
|
@@ -1,25 +1,19 @@
|
|||||||
import * as plugins from './plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
|
import * as baseTypes from '../core_base/types.js';
|
||||||
|
|
||||||
|
// Re-export base types
|
||||||
|
export * from '../core_base/types.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Core request options extending Node.js RequestOptions
|
* Core request options extending Node.js RequestOptions
|
||||||
*/
|
*/
|
||||||
export interface ICoreRequestOptions extends plugins.https.RequestOptions {
|
export interface ICoreRequestOptions extends plugins.https.RequestOptions, Omit<baseTypes.IAbstractRequestOptions, 'method' | 'headers'> {
|
||||||
keepAlive?: boolean;
|
keepAlive?: boolean;
|
||||||
requestBody?: any;
|
requestBody?: any;
|
||||||
queryParams?: { [key: string]: string };
|
queryParams?: { [key: string]: string };
|
||||||
hardDataCuttingTimeout?: number;
|
hardDataCuttingTimeout?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* HTTP Methods supported
|
|
||||||
*/
|
|
||||||
export type THttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response types supported
|
|
||||||
*/
|
|
||||||
export type ResponseType = 'json' | 'text' | 'binary' | 'stream';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extended IncomingMessage with body property (legacy compatibility)
|
* Extended IncomingMessage with body property (legacy compatibility)
|
||||||
*/
|
*/
|
||||||
@@ -28,27 +22,9 @@ export interface IExtendedIncomingMessage<T = any> extends plugins.http.Incoming
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Form field data for multipart/form-data requests
|
* Core response object that provides fetch-like API with Node.js specific methods
|
||||||
*/
|
*/
|
||||||
export interface IFormField {
|
export interface ICoreResponse<T = any> extends baseTypes.IAbstractResponse<T> {
|
||||||
name: string;
|
|
||||||
value: string | Buffer;
|
|
||||||
filename?: string;
|
|
||||||
contentType?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* URL encoded form field
|
|
||||||
*/
|
|
||||||
export interface IUrlEncodedField {
|
|
||||||
key: string;
|
|
||||||
value: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Core response object that provides fetch-like API
|
|
||||||
*/
|
|
||||||
export interface ICoreResponse<T = any> {
|
|
||||||
// Properties
|
// Properties
|
||||||
ok: boolean;
|
ok: boolean;
|
||||||
status: number;
|
status: number;
|
||||||
|
@@ -5,7 +5,7 @@ export * from './legacy/index.js';
|
|||||||
export * from './modern/index.js';
|
export * from './modern/index.js';
|
||||||
|
|
||||||
// Core exports for advanced usage
|
// Core exports for advanced usage
|
||||||
export { CoreResponse, type ICoreRequestOptions, type ICoreResponse } from './core/index.js';
|
export { CoreResponse, type ICoreRequestOptions, type ICoreResponse } from './core_node/index.js';
|
||||||
|
|
||||||
// Default export for easier importing
|
// Default export for easier importing
|
||||||
import { SmartRequestClient } from './modern/smartrequestclient.js';
|
import { SmartRequestClient } from './modern/smartrequestclient.js';
|
||||||
|
@@ -3,13 +3,13 @@
|
|||||||
* Maps legacy API to the new core module
|
* Maps legacy API to the new core module
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as core from '../core/index.js';
|
import * as core from '../core_node/index.js';
|
||||||
import * as plugins from '../core/plugins.js';
|
import * as plugins from '../core_node/plugins.js';
|
||||||
|
|
||||||
const smartpromise = plugins.smartpromise;
|
const smartpromise = plugins.smartpromise;
|
||||||
|
|
||||||
// Re-export types for backward compatibility
|
// Re-export types for backward compatibility
|
||||||
export { type IExtendedIncomingMessage } from '../core/types.js';
|
export { type IExtendedIncomingMessage } from '../core_node/types.js';
|
||||||
export interface ISmartRequestOptions extends core.ICoreRequestOptions {
|
export interface ISmartRequestOptions extends core.ICoreRequestOptions {
|
||||||
autoJsonParse?: boolean;
|
autoJsonParse?: boolean;
|
||||||
responseType?: 'json' | 'text' | 'binary' | 'stream';
|
responseType?: 'json' | 'text' | 'binary' | 'stream';
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { type CoreResponse } from '../../core/index.js';
|
import { type CoreResponse } from '../../core_node/index.js';
|
||||||
import { type TPaginationConfig, PaginationStrategy, type TPaginatedResponse } from '../types/pagination.js';
|
import { type TPaginationConfig, PaginationStrategy, type TPaginatedResponse } from '../types/pagination.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
export { SmartRequestClient } from './smartrequestclient.js';
|
export { SmartRequestClient } from './smartrequestclient.js';
|
||||||
|
|
||||||
// Export response type from core
|
// Export response type from core
|
||||||
export { CoreResponse } from '../core/index.js';
|
export { CoreResponse } from '../core_node/index.js';
|
||||||
|
|
||||||
// Export types
|
// Export types
|
||||||
export type { HttpMethod, ResponseType, FormField, RetryConfig, TimeoutConfig } from './types/common.js';
|
export type { HttpMethod, ResponseType, FormField, RetryConfig, TimeoutConfig } from './types/common.js';
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import { CoreRequest, CoreResponse, type ICoreRequestOptions } from '../core/index.js';
|
import { CoreRequest, CoreResponse, type ICoreRequestOptions } from '../core_node/index.js';
|
||||||
import * as plugins from '../core/plugins.js';
|
import * as plugins from '../core_node/plugins.js';
|
||||||
|
|
||||||
import type { HttpMethod, ResponseType, FormField } from './types/common.js';
|
import type { HttpMethod, ResponseType, FormField } from './types/common.js';
|
||||||
import {
|
import {
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { type CoreResponse } from '../../core/index.js';
|
import { type CoreResponse } from '../../core_node/index.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pagination strategy options
|
* Pagination strategy options
|
||||||
|
Reference in New Issue
Block a user