This commit is contained in:
2025-07-28 14:45:47 +00:00
parent 2cded974a8
commit d627bc870e
7 changed files with 34 additions and 34 deletions

View File

@@ -1,4 +1,4 @@
// Core exports // Core exports
export * from './types.js'; export * from './types.js';
export * from './response.js'; export * from './response.js';
export { SmartRequest, isUnixSocket, parseUnixSocketUrl } from './request.js'; export { CoreRequest, isUnixSocket, parseUnixSocketUrl } from './request.js';

View File

@@ -1,6 +1,6 @@
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 { SmartResponse } from './response.js'; import { CoreResponse } from './response.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,9 +26,9 @@ const httpsAgentKeepAliveFalse = new plugins.agentkeepalive.HttpsAgent({
}); });
/** /**
* Modern Request class that handles all HTTP/HTTPS requests * Core Request class that handles all HTTP/HTTPS requests
*/ */
export class SmartRequest { export class CoreRequest {
/** /**
* Tests if a URL is a unix socket * Tests if a URL is a unix socket
*/ */
@@ -63,17 +63,17 @@ export class SmartRequest {
} }
/** /**
* Execute the request and return a SmartResponse * Fire the request and return a CoreResponse
*/ */
async execute(): Promise<SmartResponse> { async fire(): Promise<CoreResponse> {
const incomingMessage = await this.executeCore(); const incomingMessage = await this.fireCore();
return new SmartResponse(incomingMessage, this.url); return new CoreResponse(incomingMessage, this.url);
} }
/** /**
* Execute the request and return the raw IncomingMessage * Fire the request and return the raw IncomingMessage
*/ */
async executeCore(): Promise<plugins.http.IncomingMessage> { async fireCore(): Promise<plugins.http.IncomingMessage> {
const done = plugins.smartpromise.defer<plugins.http.IncomingMessage>(); const done = plugins.smartpromise.defer<plugins.http.IncomingMessage>();
// Parse URL // Parse URL
@@ -88,8 +88,8 @@ export class SmartRequest {
this.options.path = parsedUrl.path; this.options.path = parsedUrl.path;
// Handle unix socket URLs // Handle unix socket URLs
if (SmartRequest.isUnixSocket(this.url)) { if (CoreRequest.isUnixSocket(this.url)) {
const { socketPath, path } = SmartRequest.parseUnixSocketUrl(this.options.path); const { socketPath, path } = CoreRequest.parseUnixSocketUrl(this.options.path);
this.options.socketPath = socketPath; this.options.socketPath = socketPath;
this.options.path = path; this.options.path = path;
} }
@@ -166,19 +166,19 @@ export class SmartRequest {
} }
/** /**
* Static factory method to create and execute a request * Static factory method to create and fire a request
*/ */
static async create( static async create(
url: string, url: string,
options: types.ICoreRequestOptions = {} options: types.ICoreRequestOptions = {}
): Promise<SmartResponse> { ): Promise<CoreResponse> {
const request = new SmartRequest(url, options); const request = new CoreRequest(url, options);
return request.execute(); return request.fire();
} }
} }
/** /**
* Convenience exports for backward compatibility * Convenience exports for backward compatibility
*/ */
export const isUnixSocket = SmartRequest.isUnixSocket; export const isUnixSocket = CoreRequest.isUnixSocket;
export const parseUnixSocketUrl = SmartRequest.parseUnixSocketUrl; export const parseUnixSocketUrl = CoreRequest.parseUnixSocketUrl;

View File

@@ -2,9 +2,9 @@ import * as plugins from './plugins.js';
import * as types from './types.js'; import * as types from './types.js';
/** /**
* Modern Response class that provides a fetch-like API * Core Response class that provides a fetch-like API
*/ */
export class SmartResponse<T = any> implements types.ICoreResponse<T> { export class CoreResponse<T = any> 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; private consumed = false;

View File

@@ -72,8 +72,8 @@ export async function request(
responseStreamArg = false, responseStreamArg = false,
requestDataFunc?: (req: plugins.http.ClientRequest) => void requestDataFunc?: (req: plugins.http.ClientRequest) => void
): Promise<core.IExtendedIncomingMessage> { ): Promise<core.IExtendedIncomingMessage> {
const smartRequest = new core.SmartRequest(urlArg, optionsArg, requestDataFunc); const coreRequest = new core.CoreRequest(urlArg, optionsArg, requestDataFunc);
const stream = await smartRequest.executeCore(); const stream = await coreRequest.fireCore();
if (responseStreamArg) { if (responseStreamArg) {
// For stream responses, just cast and return // For stream responses, just cast and return

View File

@@ -1,11 +1,11 @@
import { type SmartResponse } from '../../core/index.js'; import { type CoreResponse } from '../../core/index.js';
import { type TPaginationConfig, PaginationStrategy, type TPaginatedResponse } from '../types/pagination.js'; import { type TPaginationConfig, PaginationStrategy, type TPaginatedResponse } from '../types/pagination.js';
/** /**
* Creates a paginated response from a regular response * Creates a paginated response from a regular response
*/ */
export async function createPaginatedResponse<T>( export async function createPaginatedResponse<T>(
response: SmartResponse<any>, response: CoreResponse<any>,
paginationConfig: TPaginationConfig, paginationConfig: TPaginationConfig,
queryParams: Record<string, string>, queryParams: Record<string, string>,
fetchNextPage: (params: Record<string, string>) => Promise<TPaginatedResponse<T>> fetchNextPage: (params: Record<string, string>) => Promise<TPaginatedResponse<T>>

View File

@@ -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 { SmartResponse } from '../core/index.js'; export { CoreResponse } from '../core/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';

View File

@@ -1,4 +1,4 @@
import { SmartRequest, SmartResponse, type ICoreRequestOptions } from '../core/index.js'; import { CoreRequest, CoreResponse, type ICoreRequestOptions } from '../core/index.js';
import * as plugins from '../core/plugins.js'; import * as plugins from '../core/plugins.js';
import type { HttpMethod, ResponseType, FormField } from './types/common.js'; import type { HttpMethod, ResponseType, FormField } from './types/common.js';
@@ -223,35 +223,35 @@ export class SmartRequestClient<T = any> {
/** /**
* Make a GET request * Make a GET request
*/ */
async get<R = T>(): Promise<SmartResponse<R>> { async get<R = T>(): Promise<CoreResponse<R>> {
return this.execute<R>('GET'); return this.execute<R>('GET');
} }
/** /**
* Make a POST request * Make a POST request
*/ */
async post<R = T>(): Promise<SmartResponse<R>> { async post<R = T>(): Promise<CoreResponse<R>> {
return this.execute<R>('POST'); return this.execute<R>('POST');
} }
/** /**
* Make a PUT request * Make a PUT request
*/ */
async put<R = T>(): Promise<SmartResponse<R>> { async put<R = T>(): Promise<CoreResponse<R>> {
return this.execute<R>('PUT'); return this.execute<R>('PUT');
} }
/** /**
* Make a DELETE request * Make a DELETE request
*/ */
async delete<R = T>(): Promise<SmartResponse<R>> { async delete<R = T>(): Promise<CoreResponse<R>> {
return this.execute<R>('DELETE'); return this.execute<R>('DELETE');
} }
/** /**
* Make a PATCH request * Make a PATCH request
*/ */
async patch<R = T>(): Promise<SmartResponse<R>> { async patch<R = T>(): Promise<CoreResponse<R>> {
return this.execute<R>('PATCH'); return this.execute<R>('PATCH');
} }
@@ -296,7 +296,7 @@ export class SmartRequestClient<T = any> {
/** /**
* Execute the HTTP request * Execute the HTTP request
*/ */
private async execute<R = T>(method?: HttpMethod): Promise<SmartResponse<R>> { private async execute<R = T>(method?: HttpMethod): Promise<CoreResponse<R>> {
if (method) { if (method) {
this._options.method = method; this._options.method = method;
} }
@@ -308,8 +308,8 @@ export class SmartRequestClient<T = any> {
for (let attempt = 0; attempt <= this._retries; attempt++) { for (let attempt = 0; attempt <= this._retries; attempt++) {
try { try {
const response = await SmartRequest.create(this._url, this._options); const response = await CoreRequest.create(this._url, this._options);
return response as SmartResponse<R>; return response as CoreResponse<R>;
} catch (error) { } catch (error) {
lastError = error as Error; lastError = error as Error;