feat(docs): Enhance documentation and tests with modern API usage examples and migration guide
This commit is contained in:
@@ -2,14 +2,14 @@ import { type ISmartRequestOptions } from '../legacy/smartrequest.interfaces.js'
|
||||
import { request, type IExtendedIncomingMessage } from '../legacy/smartrequest.request.js';
|
||||
import * as plugins from '../legacy/smartrequest.plugins.js';
|
||||
|
||||
import type { HttpMethod, ResponseType, RetryConfig, TimeoutConfig, FormField } from './types/common.js';
|
||||
import type {
|
||||
TPaginationConfig,
|
||||
PaginationStrategy,
|
||||
OffsetPaginationConfig,
|
||||
CursorPaginationConfig,
|
||||
CustomPaginationConfig,
|
||||
TPaginatedResponse
|
||||
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';
|
||||
|
||||
@@ -65,7 +65,7 @@ export class SmartRequestClient<T = any> {
|
||||
*/
|
||||
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, {
|
||||
@@ -76,16 +76,16 @@ export class SmartRequestClient<T = any> {
|
||||
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;
|
||||
}
|
||||
@@ -149,11 +149,11 @@ export class SmartRequestClient<T = any> {
|
||||
*/
|
||||
responseType(type: ResponseType): this {
|
||||
this._responseType = type;
|
||||
|
||||
|
||||
if (type === 'binary' || type === 'stream') {
|
||||
this._options.autoJsonParse = false;
|
||||
}
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -177,13 +177,13 @@ export class SmartRequestClient<T = any> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -264,24 +264,24 @@ export class SmartRequestClient<T = any> {
|
||||
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 createPaginatedResponse<ItemType>(
|
||||
response,
|
||||
this._paginationConfig,
|
||||
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>();
|
||||
}
|
||||
);
|
||||
@@ -304,28 +304,28 @@ export class SmartRequestClient<T = any> {
|
||||
}
|
||||
|
||||
this._options.queryParams = this._queryParams;
|
||||
|
||||
|
||||
// Handle retry logic
|
||||
let lastError: Error;
|
||||
|
||||
|
||||
for (let attempt = 0; attempt <= this._retries; attempt++) {
|
||||
try {
|
||||
if (this._responseType === 'stream') {
|
||||
return await request(this._url, this._options, true) as IExtendedIncomingMessage<R>;
|
||||
} else if (this._responseType === 'binary') {
|
||||
const response = await request(this._url, this._options, true);
|
||||
|
||||
|
||||
// Handle binary response
|
||||
const dataPromise = plugins.smartpromise.defer<Buffer>();
|
||||
const chunks: Buffer[] = [];
|
||||
|
||||
|
||||
response.on('data', (chunk: Buffer) => chunks.push(chunk));
|
||||
response.on('end', () => {
|
||||
const buffer = Buffer.concat(chunks);
|
||||
(response as IExtendedIncomingMessage<R>).body = buffer as any;
|
||||
dataPromise.resolve();
|
||||
});
|
||||
|
||||
|
||||
await dataPromise.promise;
|
||||
return response as IExtendedIncomingMessage<R>;
|
||||
} else {
|
||||
@@ -334,17 +334,17 @@ export class SmartRequestClient<T = any> {
|
||||
}
|
||||
} 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