Skip to content

Commit d9fa8d9

Browse files
committed
feat: Make axiosInstance optional and conditionally initialize it based on customFetch presence.
1 parent f665b26 commit d9fa8d9

2 files changed

Lines changed: 31 additions & 40 deletions

File tree

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@forgebase/sdk",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"private": false,
55
"type": "module",
66
"main": "./dist/index.cjs",

packages/sdk/src/database/client/client.ts

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class DatabaseSDK<
207207
CreatedSchema extends Record<string, any> = any,
208208
> {
209209
private baseUrl: string;
210-
private axiosInstance: AxiosInstance;
210+
private axiosInstance?: AxiosInstance;
211211
private customFetch?: typeof fetch;
212212

213213
/**
@@ -239,18 +239,20 @@ export class DatabaseSDK<
239239
}
240240

241241
// Use the provided axios instance or create a new one
242-
if (axiosInstance) {
243-
this.axiosInstance = axiosInstance;
244-
} else {
245-
this.axiosInstance = axios.create({
246-
baseURL: this.baseUrl,
247-
withCredentials: true,
248-
...axiosConfig,
249-
});
242+
if (!this.customFetch) {
243+
if (axiosInstance) {
244+
this.axiosInstance = axiosInstance;
245+
} else {
246+
this.axiosInstance = axios.create({
247+
baseURL: this.baseUrl,
248+
withCredentials: true,
249+
...axiosConfig,
250+
});
250251

251-
// Apply auth interceptors if provided
252-
if (authInterceptors) {
253-
this.applyAuthInterceptors(authInterceptors);
252+
// Apply auth interceptors if provided
253+
if (authInterceptors) {
254+
this.applyAuthInterceptors(authInterceptors);
255+
}
254256
}
255257
}
256258
}
@@ -271,7 +273,7 @@ export class DatabaseSDK<
271273
this.baseUrl = baseUrl.replace(/\/$/, ''); // Remove trailing slash if present
272274

273275
// Update the axios instance baseURL if it was created by this class
274-
if (this.axiosInstance.defaults.baseURL) {
276+
if (this.axiosInstance?.defaults.baseURL) {
275277
this.axiosInstance.defaults.baseURL = this.baseUrl;
276278
}
277279
}
@@ -280,7 +282,7 @@ export class DatabaseSDK<
280282
* Get the axios instance used for API requests
281283
* @returns The axios instance
282284
*/
283-
getAxiosInstance(): AxiosInstance {
285+
getAxiosInstance(): AxiosInstance | undefined {
284286
return this.axiosInstance;
285287
}
286288

@@ -303,25 +305,14 @@ export class DatabaseSDK<
303305
): Promise<any> {
304306
if (!this.customFetch) return null;
305307

306-
const { headers, signal, withCredentials } = axiosConfig;
307-
308308
const fetchOptions: RequestInit = {
309309
method,
310310
headers: {
311311
'Content-Type': 'application/json',
312-
...(headers as any),
313312
},
314313
body: payload ? JSON.stringify(payload) : undefined,
315314
};
316315

317-
if (withCredentials) {
318-
fetchOptions.credentials = 'include';
319-
}
320-
321-
if (signal) {
322-
fetchOptions.signal = signal as AbortSignal;
323-
}
324-
325316
const response = await this.customFetch(url, fetchOptions);
326317
const data = await response.json();
327318

@@ -341,10 +332,10 @@ export class DatabaseSDK<
341332
*/
342333
applyAuthInterceptors(authInterceptors: AuthInterceptors): void {
343334
// Add request interceptor
344-
this.axiosInstance.interceptors.request.use(authInterceptors.request);
335+
this.axiosInstance?.interceptors.request.use(authInterceptors.request);
345336

346337
// Add response interceptors
347-
this.axiosInstance.interceptors.response.use(
338+
this.axiosInstance?.interceptors.response.use(
348339
authInterceptors.response.onFulfilled,
349340
authInterceptors.response.onRejected,
350341
);
@@ -388,14 +379,14 @@ export class DatabaseSDK<
388379
};
389380
}
390381

391-
const response = await this.axiosInstance.post<ApiResponse<T>>(
382+
const response = await this.axiosInstance?.post<ApiResponse<T>>(
392383
`/query/${tableName}`,
393384
payload,
394385
axiosConfig,
395386
);
396387

397388
return {
398-
records: response.data as T[],
389+
records: response?.data as T[],
399390
params: params,
400391
message: 'Records fetched successfully',
401392
error: undefined,
@@ -443,13 +434,13 @@ export class DatabaseSDK<
443434
};
444435
}
445436

446-
const response = await this.axiosInstance.post<ApiResponse<T>>(
437+
const response = await this.axiosInstance?.post<ApiResponse<T>>(
447438
`/create/${tableName}`,
448439
payload,
449440
axiosConfig,
450441
);
451442
return {
452-
records: [response.data as T],
443+
records: [response?.data as T],
453444
message: 'Record created successfully',
454445
error: undefined,
455446
};
@@ -495,13 +486,13 @@ export class DatabaseSDK<
495486
};
496487
}
497488

498-
const response = await this.axiosInstance.put<ApiResponse<T>>(
489+
const response = await this.axiosInstance?.put<ApiResponse<T>>(
499490
`/update/${tableName}/${id}`,
500491
payload,
501492
axiosConfig,
502493
);
503494
return {
504-
records: [response.data as T],
495+
records: [response?.data as T],
505496
message: 'Record updated successfully',
506497
error: undefined,
507498
};
@@ -551,15 +542,15 @@ export class DatabaseSDK<
551542
};
552543
}
553544

554-
const response = await this.axiosInstance.post<ApiResponse<never>>(
545+
const response = await this.axiosInstance?.post<ApiResponse<never>>(
555546
`/update/${tableName}`,
556547
payload,
557548
axiosConfig,
558549
);
559550
return {
560551
message: 'Records updated successfully',
561552
error: undefined,
562-
records: response.data as T[],
553+
records: response?.data as T[],
563554
};
564555
} catch (error) {
565556
if (axios.isAxiosError(error)) {
@@ -599,15 +590,15 @@ export class DatabaseSDK<
599590
};
600591
}
601592

602-
const response = await this.axiosInstance.post<ApiResponse<never>>(
593+
const response = await this.axiosInstance?.post<ApiResponse<never>>(
603594
`/del/${tableName}/${id}`,
604595
{},
605596
axiosConfig,
606597
);
607598
return {
608599
message: 'Record deleted successfully',
609600
error: undefined,
610-
records: response.data as any[],
601+
records: response?.data as any[],
611602
};
612603
} catch (error) {
613604
if (axios.isAxiosError(error)) {
@@ -653,15 +644,15 @@ export class DatabaseSDK<
653644
};
654645
}
655646

656-
const response = await this.axiosInstance.post<ApiResponse<never>>(
647+
const response = await this.axiosInstance?.post<ApiResponse<never>>(
657648
`/del/${tableName}`,
658649
payload,
659650
axiosConfig,
660651
);
661652
return {
662653
message: 'Record deleted successfully',
663654
error: undefined,
664-
records: response.data as any[],
655+
records: response?.data as any[],
665656
};
666657
} catch (error) {
667658
if (axios.isAxiosError(error)) {

0 commit comments

Comments
 (0)