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>;
|
||||
}
|
Reference in New Issue
Block a user