Skip to content

Commit c35a2d7

Browse files
committed
feat: Refactor ApiService implementation, enhance type imports, and update service configurations
1 parent 1b98ef7 commit c35a2d7

File tree

6 files changed

+67
-24
lines changed

6 files changed

+67
-24
lines changed

packages/api/src/api-services.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type ApiServiceHook = {
1717
onResponse?: (res: Response, req: RequestInit & { url: string }) => void | Promise<void>;
1818
};
1919

20-
export class ApiService {
20+
export class ApiService implements ApiService {
2121
private config: ApiServiceConfig;
2222
private hooks: ApiServiceHook[] = [];
2323

@@ -89,7 +89,7 @@ export class ApiService {
8989
}
9090
return { isSucceed: true, data: respData, status: res.status };
9191
} catch (err) {
92-
return { isSucceed: false, errors: [(err as Error).message] };
92+
throw err;
9393
}
9494
}
9595

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
// SDK bootstrap entrypoint for Mixcore JavaScript SDK
22
// Usage: import { createMixcoreSdk } from '@mixcore/apis';
33
import { ApiService } from './api-services';
4-
import { UserServices } from '@mixcore/user';
5-
import { TemplateService } from '@mixcore/template';
6-
import { FileServices } from '@mixcore/shared';
7-
import { FileServicesPortal } from '@mixcore/file';
8-
import { ConfigurationServices } from '@mixcore/config';
9-
// ...import other domain services as needed
4+
import type {
5+
ApiServiceConfig,
6+
UserServices,
7+
TemplateService,
8+
FileServices,
9+
ConfigurationServices
10+
} from '@mixcore/shared';
1011

11-
export interface MixcoreSdkConfig {
12-
apiBaseUrl: string;
13-
apiKey?: string;
14-
[key: string]: any;
12+
export interface MixcoreSdkConfig extends ApiServiceConfig {}
13+
14+
export interface MixcoreSdkOptions {
15+
user: UserServices;
16+
template: TemplateService;
17+
file: FileServices;
18+
config: ConfigurationServices;
19+
// ...add other domain services here
1520
}
1621

17-
export function createMixcoreSdk(config: MixcoreSdkConfig) {
22+
export function createMixcoreSdk(
23+
config: MixcoreSdkConfig,
24+
services: MixcoreSdkOptions
25+
) {
1826
const api = new ApiService(config);
1927

2028
return {
2129
api,
22-
user: new UserServices(api),
23-
template: new TemplateService(api),
24-
file: new FileServicesPortal(api),
25-
config: new ConfigurationServices(api),
26-
// ...add other domain services here
30+
...services
2731
};
2832
}

packages/file/src/file-services.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { ApiService } from '@mixcore/api';
2-
import type { FileServices } from '@mixcore/shared';
1+
import type { ApiService, FileServices } from '@mixcore/shared';
32

43
export class FileServicesPortal implements FileServices {
54
private api: ApiService;

packages/shared/src/api-types.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,47 @@
33
*
44
* Contains types that need to be shared across packages to avoid circular dependencies
55
*/
6+
7+
export interface ApiServiceConfig {
8+
apiBaseUrl: string;
9+
apiKey?: string;
10+
}
11+
12+
export interface ApiService {
13+
config: ApiServiceConfig;
14+
request<T = any>(options: {
15+
url: string;
16+
method?: string;
17+
body?: any;
18+
headers?: Record<string, string>;
19+
}): Promise<T>;
20+
get<T = any>(url: string): Promise<T>;
21+
post<T = any>(url: string, body?: any): Promise<T>;
22+
}
23+
24+
export interface UserServices {
25+
constructor(api: ApiService): void;
26+
// Add user service methods here
27+
}
28+
29+
export interface TemplateService {
30+
constructor(api: ApiService): void;
31+
// Add template service methods here
32+
}
33+
34+
export interface ConfigurationServices {
35+
constructor(api: ApiService): void;
36+
// Add config service methods here
37+
}
38+
39+
export interface FileServices {
40+
getFile(folder: string, filename: string): Promise<any>;
41+
initFile(type: string): Promise<any>;
42+
getFiles(request: any): Promise<any>;
43+
removeFile(fullPath: string): Promise<any>;
44+
saveFile(file: any): Promise<any>;
45+
uploadFile(file: File, folder: string): Promise<any>;
46+
}
647
export interface ApiServiceConfig {
748
apiBaseUrl: string;
849
apiKey?: string;
@@ -17,6 +58,8 @@ export interface ApiService {
1758
body?: any;
1859
headers?: Record<string, string>;
1960
}): Promise<T>;
61+
get<T = any>(url: string): Promise<T>;
62+
post<T = any>(url: string, body?: any): Promise<T>;
2063
}
2164

2265
export interface ApiResult {

packages/shared/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11

22
export * from './api-types';
3-
export { ApiService } from './api-types';
4-
export * from './file-types';
53
export * from './common-services';
64
export * from './crypto-services';
7-
export * from './file-services';
85
export * from './global-settings-services';
96
export * from './gpay-services';
107
export * from './modal-nav-metas-service';

packages/template/src/template-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiService } from '@mixcore/api';
1+
import type { ApiService } from '@mixcore/shared';
22

33
export class TemplateService {
44
private api: ApiService;

0 commit comments

Comments
 (0)