update
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Core exports
|
||||
export * from './types.js';
|
||||
export * from './response.js';
|
||||
export { SmartRequest, isUnixSocket, parseUnixSocketUrl } from './request.js';
|
||||
export { CoreRequest, isUnixSocket, parseUnixSocketUrl } from './request.js';
|
@@ -1,6 +1,6 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as types from './types.js';
|
||||
import { SmartResponse } from './response.js';
|
||||
import { CoreResponse } from './response.js';
|
||||
|
||||
// Keep-alive agents for connection pooling
|
||||
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
|
||||
*/
|
||||
@@ -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> {
|
||||
const incomingMessage = await this.executeCore();
|
||||
return new SmartResponse(incomingMessage, this.url);
|
||||
async fire(): Promise<CoreResponse> {
|
||||
const incomingMessage = await this.fireCore();
|
||||
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>();
|
||||
|
||||
// Parse URL
|
||||
@@ -88,8 +88,8 @@ export class SmartRequest {
|
||||
this.options.path = parsedUrl.path;
|
||||
|
||||
// Handle unix socket URLs
|
||||
if (SmartRequest.isUnixSocket(this.url)) {
|
||||
const { socketPath, path } = SmartRequest.parseUnixSocketUrl(this.options.path);
|
||||
if (CoreRequest.isUnixSocket(this.url)) {
|
||||
const { socketPath, path } = CoreRequest.parseUnixSocketUrl(this.options.path);
|
||||
this.options.socketPath = socketPath;
|
||||
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(
|
||||
url: string,
|
||||
options: types.ICoreRequestOptions = {}
|
||||
): Promise<SmartResponse> {
|
||||
const request = new SmartRequest(url, options);
|
||||
return request.execute();
|
||||
): Promise<CoreResponse> {
|
||||
const request = new CoreRequest(url, options);
|
||||
return request.fire();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience exports for backward compatibility
|
||||
*/
|
||||
export const isUnixSocket = SmartRequest.isUnixSocket;
|
||||
export const parseUnixSocketUrl = SmartRequest.parseUnixSocketUrl;
|
||||
export const isUnixSocket = CoreRequest.isUnixSocket;
|
||||
export const parseUnixSocketUrl = CoreRequest.parseUnixSocketUrl;
|
@@ -2,9 +2,9 @@ import * as plugins from './plugins.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 bodyBufferPromise: Promise<Buffer> | null = null;
|
||||
private consumed = false;
|
||||
|
@@ -72,8 +72,8 @@ export async function request(
|
||||
responseStreamArg = false,
|
||||
requestDataFunc?: (req: plugins.http.ClientRequest) => void
|
||||
): Promise<core.IExtendedIncomingMessage> {
|
||||
const smartRequest = new core.SmartRequest(urlArg, optionsArg, requestDataFunc);
|
||||
const stream = await smartRequest.executeCore();
|
||||
const coreRequest = new core.CoreRequest(urlArg, optionsArg, requestDataFunc);
|
||||
const stream = await coreRequest.fireCore();
|
||||
|
||||
if (responseStreamArg) {
|
||||
// For stream responses, just cast and return
|
||||
|
@@ -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';
|
||||
|
||||
/**
|
||||
* Creates a paginated response from a regular response
|
||||
*/
|
||||
export async function createPaginatedResponse<T>(
|
||||
response: SmartResponse<any>,
|
||||
response: CoreResponse<any>,
|
||||
paginationConfig: TPaginationConfig,
|
||||
queryParams: Record<string, string>,
|
||||
fetchNextPage: (params: Record<string, string>) => Promise<TPaginatedResponse<T>>
|
||||
|
@@ -2,7 +2,7 @@
|
||||
export { SmartRequestClient } from './smartrequestclient.js';
|
||||
|
||||
// Export response type from core
|
||||
export { SmartResponse } from '../core/index.js';
|
||||
export { CoreResponse } from '../core/index.js';
|
||||
|
||||
// Export types
|
||||
export type { HttpMethod, ResponseType, FormField, RetryConfig, TimeoutConfig } from './types/common.js';
|
||||
|
@@ -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 type { HttpMethod, ResponseType, FormField } from './types/common.js';
|
||||
@@ -223,35 +223,35 @@ export class SmartRequestClient<T = any> {
|
||||
/**
|
||||
* Make a GET request
|
||||
*/
|
||||
async get<R = T>(): Promise<SmartResponse<R>> {
|
||||
async get<R = T>(): Promise<CoreResponse<R>> {
|
||||
return this.execute<R>('GET');
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a POST request
|
||||
*/
|
||||
async post<R = T>(): Promise<SmartResponse<R>> {
|
||||
async post<R = T>(): Promise<CoreResponse<R>> {
|
||||
return this.execute<R>('POST');
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a PUT request
|
||||
*/
|
||||
async put<R = T>(): Promise<SmartResponse<R>> {
|
||||
async put<R = T>(): Promise<CoreResponse<R>> {
|
||||
return this.execute<R>('PUT');
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a DELETE request
|
||||
*/
|
||||
async delete<R = T>(): Promise<SmartResponse<R>> {
|
||||
async delete<R = T>(): Promise<CoreResponse<R>> {
|
||||
return this.execute<R>('DELETE');
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a PATCH request
|
||||
*/
|
||||
async patch<R = T>(): Promise<SmartResponse<R>> {
|
||||
async patch<R = T>(): Promise<CoreResponse<R>> {
|
||||
return this.execute<R>('PATCH');
|
||||
}
|
||||
|
||||
@@ -296,7 +296,7 @@ export class SmartRequestClient<T = any> {
|
||||
/**
|
||||
* 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) {
|
||||
this._options.method = method;
|
||||
}
|
||||
@@ -308,8 +308,8 @@ export class SmartRequestClient<T = any> {
|
||||
|
||||
for (let attempt = 0; attempt <= this._retries; attempt++) {
|
||||
try {
|
||||
const response = await SmartRequest.create(this._url, this._options);
|
||||
return response as SmartResponse<R>;
|
||||
const response = await CoreRequest.create(this._url, this._options);
|
||||
return response as CoreResponse<R>;
|
||||
} catch (error) {
|
||||
lastError = error as Error;
|
||||
|
||||
|
Reference in New Issue
Block a user