This commit is contained in:
2025-07-28 22:37:36 +00:00
parent bc99aa3569
commit eb2ccd8d9f
21 changed files with 228 additions and 99 deletions

View File

@@ -1,17 +1,18 @@
import { type CoreResponse } from '../../core_node/index.js';
import { type CoreResponse } from '../../core/index.js';
import type { ICoreResponse } from '../../core_base/types.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: CoreResponse<any>,
response: ICoreResponse<any>,
paginationConfig: TPaginationConfig,
queryParams: Record<string, string>,
fetchNextPage: (params: Record<string, string>) => Promise<TPaginatedResponse<T>>
): Promise<TPaginatedResponse<T>> {
// Parse response body first
const body = await response.json();
const body = await response.json() as any;
// Default to response.body for items if response is JSON
let items: T[] = Array.isArray(body)

View File

@@ -2,7 +2,7 @@
export { SmartRequestClient } from './smartrequestclient.js';
// Export response type from core
export { CoreResponse } from '../core_node/index.js';
export { CoreResponse } from '../core/index.js';
// Export types
export type { HttpMethod, ResponseType, FormField, RetryConfig, TimeoutConfig } from './types/common.js';

6
ts/client/plugins.ts Normal file
View File

@@ -0,0 +1,6 @@
// plugins for client module
import FormData from 'form-data';
export {
FormData as formData
};

View File

@@ -1,6 +1,7 @@
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 { ICoreResponse } from '../core_base/types.js';
import * as plugins from './plugins.js';
import type { ICoreRequestOptions } from '../core_base/types.js';
import type { HttpMethod, ResponseType, FormField } from './types/common.js';
import {
@@ -18,7 +19,7 @@ import { createPaginatedResponse } from './features/pagination.js';
*/
export class SmartRequestClient<T = any> {
private _url: string;
private _options: IAbstractRequestOptions = {};
private _options: ICoreRequestOptions = {};
private _retries: number = 0;
private _queryParams: Record<string, string> = {};
private _paginationConfig?: TPaginationConfig;
@@ -224,35 +225,35 @@ export class SmartRequestClient<T = any> {
/**
* Make a GET request
*/
async get<R = T>(): Promise<CoreResponse<R>> {
async get<R = T>(): Promise<ICoreResponse<R>> {
return this.execute<R>('GET');
}
/**
* Make a POST request
*/
async post<R = T>(): Promise<CoreResponse<R>> {
async post<R = T>(): Promise<ICoreResponse<R>> {
return this.execute<R>('POST');
}
/**
* Make a PUT request
*/
async put<R = T>(): Promise<CoreResponse<R>> {
async put<R = T>(): Promise<ICoreResponse<R>> {
return this.execute<R>('PUT');
}
/**
* Make a DELETE request
*/
async delete<R = T>(): Promise<CoreResponse<R>> {
async delete<R = T>(): Promise<ICoreResponse<R>> {
return this.execute<R>('DELETE');
}
/**
* Make a PATCH request
*/
async patch<R = T>(): Promise<CoreResponse<R>> {
async patch<R = T>(): Promise<ICoreResponse<R>> {
return this.execute<R>('PATCH');
}
@@ -297,7 +298,7 @@ export class SmartRequestClient<T = any> {
/**
* Execute the HTTP request
*/
private async execute<R = T>(method?: HttpMethod): Promise<CoreResponse<R>> {
private async execute<R = T>(method?: HttpMethod): Promise<ICoreResponse<R>> {
if (method) {
this._options.method = method;
}
@@ -309,8 +310,9 @@ export class SmartRequestClient<T = any> {
for (let attempt = 0; attempt <= this._retries; attempt++) {
try {
const response = await CoreRequest.create(this._url, this._options);
return response as CoreResponse<R>;
const request = new CoreRequest(this._url, this._options as any);
const response = await request.fire();
return response as ICoreResponse<R>;
} catch (error) {
lastError = error as Error;

View File

@@ -1,4 +1,5 @@
import { type CoreResponse } from '../../core_node/index.js';
import { type CoreResponse } from '../../core/index.js';
import type { ICoreResponse } from '../../core_base/types.js';
/**
* Pagination strategy options
@@ -45,8 +46,8 @@ export interface LinkPaginationConfig {
*/
export interface CustomPaginationConfig {
strategy: PaginationStrategy.CUSTOM;
hasNextPage: (response: CoreResponse<any>) => boolean;
getNextPageParams: (response: CoreResponse<any>, currentParams: Record<string, string>) => Record<string, string>;
hasNextPage: (response: ICoreResponse<any>) => boolean;
getNextPageParams: (response: ICoreResponse<any>, currentParams: Record<string, string>) => Record<string, string>;
}
/**
@@ -62,5 +63,5 @@ export interface TPaginatedResponse<T> {
hasNextPage: boolean; // Whether there are more pages
getNextPage: () => Promise<TPaginatedResponse<T>>; // Function to get the next page
getAllPages: () => Promise<T[]>; // Function to get all remaining pages and combine
response: CoreResponse<any>; // Original response
response: ICoreResponse<any>; // Original response
}