update
This commit is contained in:
330
ts/client/smartrequestclient.ts
Normal file
330
ts/client/smartrequestclient.ts
Normal file
@@ -0,0 +1,330 @@
|
||||
import { CoreRequest, CoreResponse } from '../core/index.js';
|
||||
import * as plugins from '../core_node/plugins.js';
|
||||
import type { IAbstractRequestOptions } from '../core_base/types.js';
|
||||
|
||||
import type { HttpMethod, ResponseType, FormField } from './types/common.js';
|
||||
import {
|
||||
type TPaginationConfig,
|
||||
PaginationStrategy,
|
||||
type OffsetPaginationConfig,
|
||||
type CursorPaginationConfig,
|
||||
type CustomPaginationConfig,
|
||||
type TPaginatedResponse
|
||||
} from './types/pagination.js';
|
||||
import { createPaginatedResponse } from './features/pagination.js';
|
||||
|
||||
/**
|
||||
* Modern fluent client for making HTTP requests
|
||||
*/
|
||||
export class SmartRequestClient<T = any> {
|
||||
private _url: string;
|
||||
private _options: IAbstractRequestOptions = {};
|
||||
private _retries: number = 0;
|
||||
private _queryParams: Record<string, string> = {};
|
||||
private _paginationConfig?: TPaginationConfig;
|
||||
|
||||
/**
|
||||
* Create a new SmartRequestClient instance
|
||||
*/
|
||||
static create<T = any>(): SmartRequestClient<T> {
|
||||
return new SmartRequestClient<T>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the URL for the request
|
||||
*/
|
||||
url(url: string): this {
|
||||
this._url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the HTTP method
|
||||
*/
|
||||
method(method: HttpMethod): this {
|
||||
this._options.method = method;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set JSON body for the request
|
||||
*/
|
||||
json(data: any): this {
|
||||
if (!this._options.headers) {
|
||||
this._options.headers = {};
|
||||
}
|
||||
this._options.headers['Content-Type'] = 'application/json';
|
||||
this._options.requestBody = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set form data for the request
|
||||
*/
|
||||
formData(data: FormField[]): this {
|
||||
const form = new plugins.formData();
|
||||
|
||||
for (const item of data) {
|
||||
if (Buffer.isBuffer(item.value)) {
|
||||
form.append(item.name, item.value, {
|
||||
filename: item.filename || 'file',
|
||||
contentType: item.contentType || 'application/octet-stream'
|
||||
});
|
||||
} else {
|
||||
form.append(item.name, item.value);
|
||||
}
|
||||
}
|
||||
|
||||
if (!this._options.headers) {
|
||||
this._options.headers = {};
|
||||
}
|
||||
|
||||
this._options.headers = {
|
||||
...this._options.headers,
|
||||
...form.getHeaders()
|
||||
};
|
||||
|
||||
this._options.requestBody = form;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set request timeout in milliseconds
|
||||
*/
|
||||
timeout(ms: number): this {
|
||||
this._options.timeout = ms;
|
||||
this._options.hardDataCuttingTimeout = ms;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set number of retry attempts
|
||||
*/
|
||||
retry(count: number): this {
|
||||
this._retries = count;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set HTTP headers
|
||||
*/
|
||||
headers(headers: Record<string, string>): this {
|
||||
if (!this._options.headers) {
|
||||
this._options.headers = {};
|
||||
}
|
||||
this._options.headers = {
|
||||
...this._options.headers,
|
||||
...headers
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a single HTTP header
|
||||
*/
|
||||
header(name: string, value: string): this {
|
||||
if (!this._options.headers) {
|
||||
this._options.headers = {};
|
||||
}
|
||||
this._options.headers[name] = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set query parameters
|
||||
*/
|
||||
query(params: Record<string, string>): this {
|
||||
this._queryParams = {
|
||||
...this._queryParams,
|
||||
...params
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Accept header to indicate what content type is expected
|
||||
*/
|
||||
accept(type: ResponseType): this {
|
||||
// Map response types to Accept header values
|
||||
const acceptHeaders: Record<ResponseType, string> = {
|
||||
'json': 'application/json',
|
||||
'text': 'text/plain',
|
||||
'binary': 'application/octet-stream',
|
||||
'stream': '*/*'
|
||||
};
|
||||
|
||||
return this.header('Accept', acceptHeaders[type]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure pagination for requests
|
||||
*/
|
||||
pagination(config: TPaginationConfig): this {
|
||||
this._paginationConfig = config;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure offset-based pagination (page & limit)
|
||||
*/
|
||||
withOffsetPagination(config: Omit<OffsetPaginationConfig, 'strategy'> = {}): this {
|
||||
this._paginationConfig = {
|
||||
strategy: PaginationStrategy.OFFSET,
|
||||
pageParam: config.pageParam || 'page',
|
||||
limitParam: config.limitParam || 'limit',
|
||||
startPage: config.startPage || 1,
|
||||
pageSize: config.pageSize || 20,
|
||||
totalPath: config.totalPath || 'total'
|
||||
};
|
||||
|
||||
// Add initial pagination parameters
|
||||
this.query({
|
||||
[this._paginationConfig.pageParam]: String(this._paginationConfig.startPage),
|
||||
[this._paginationConfig.limitParam]: String(this._paginationConfig.pageSize)
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure cursor-based pagination
|
||||
*/
|
||||
withCursorPagination(config: Omit<CursorPaginationConfig, 'strategy'> = {}): this {
|
||||
this._paginationConfig = {
|
||||
strategy: PaginationStrategy.CURSOR,
|
||||
cursorParam: config.cursorParam || 'cursor',
|
||||
cursorPath: config.cursorPath || 'nextCursor',
|
||||
hasMorePath: config.hasMorePath || 'hasMore'
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure Link header-based pagination
|
||||
*/
|
||||
withLinkPagination(): this {
|
||||
this._paginationConfig = {
|
||||
strategy: PaginationStrategy.LINK_HEADER
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure custom pagination
|
||||
*/
|
||||
withCustomPagination(config: Omit<CustomPaginationConfig, 'strategy'>): this {
|
||||
this._paginationConfig = {
|
||||
strategy: PaginationStrategy.CUSTOM,
|
||||
hasNextPage: config.hasNextPage,
|
||||
getNextPageParams: config.getNextPageParams
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a GET request
|
||||
*/
|
||||
async get<R = T>(): Promise<CoreResponse<R>> {
|
||||
return this.execute<R>('GET');
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a POST request
|
||||
*/
|
||||
async post<R = T>(): Promise<CoreResponse<R>> {
|
||||
return this.execute<R>('POST');
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a PUT request
|
||||
*/
|
||||
async put<R = T>(): Promise<CoreResponse<R>> {
|
||||
return this.execute<R>('PUT');
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a DELETE request
|
||||
*/
|
||||
async delete<R = T>(): Promise<CoreResponse<R>> {
|
||||
return this.execute<R>('DELETE');
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a PATCH request
|
||||
*/
|
||||
async patch<R = T>(): Promise<CoreResponse<R>> {
|
||||
return this.execute<R>('PATCH');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get paginated response
|
||||
*/
|
||||
async getPaginated<ItemType = T>(): Promise<TPaginatedResponse<ItemType>> {
|
||||
if (!this._paginationConfig) {
|
||||
throw new Error('Pagination not configured. Call one of the pagination methods first.');
|
||||
}
|
||||
|
||||
// Default to GET if no method specified
|
||||
if (!this._options.method) {
|
||||
this._options.method = 'GET';
|
||||
}
|
||||
|
||||
const response = await this.execute();
|
||||
|
||||
return await createPaginatedResponse<ItemType>(
|
||||
response,
|
||||
this._paginationConfig,
|
||||
this._queryParams,
|
||||
(nextPageParams) => {
|
||||
// Create a new client with the same configuration but updated query params
|
||||
const nextClient = new SmartRequestClient<ItemType>();
|
||||
Object.assign(nextClient, this);
|
||||
nextClient._queryParams = nextPageParams;
|
||||
|
||||
return nextClient.getPaginated<ItemType>();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all pages at once (use with caution for large datasets)
|
||||
*/
|
||||
async getAllPages<ItemType = T>(): Promise<ItemType[]> {
|
||||
const firstPage = await this.getPaginated<ItemType>();
|
||||
return firstPage.getAllPages();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the HTTP request
|
||||
*/
|
||||
private async execute<R = T>(method?: HttpMethod): Promise<CoreResponse<R>> {
|
||||
if (method) {
|
||||
this._options.method = method;
|
||||
}
|
||||
|
||||
this._options.queryParams = this._queryParams;
|
||||
|
||||
// Handle retry logic
|
||||
let lastError: Error;
|
||||
|
||||
for (let attempt = 0; attempt <= this._retries; attempt++) {
|
||||
try {
|
||||
const response = await CoreRequest.create(this._url, this._options);
|
||||
return response as CoreResponse<R>;
|
||||
} catch (error) {
|
||||
lastError = error as Error;
|
||||
|
||||
// If this is the last attempt, throw the error
|
||||
if (attempt === this._retries) {
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
// Otherwise, wait before retrying
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
}
|
||||
}
|
||||
|
||||
// This should never be reached due to the throw in the loop above
|
||||
throw lastError;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user