Skip to content

Commit 3974a00

Browse files
committed
feat: Add PUT method support to BaseRestService and related services, including error handling
1 parent 5cca867 commit 3974a00

8 files changed

+68
-2
lines changed

packages/base/src/base-rest-service.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ describe('BaseRestService', () => {
99
async post<T = any>(endpoint: string, data: any): Promise<T> {
1010
return { endpoint, data } as T;
1111
}
12+
async put<T = any>(endpoint: string, data: any): Promise<T> {
13+
return { endpoint, data, method: 'PUT' } as T;
14+
}
1215
async delete<T = any>(endpoint: string): Promise<T> {
1316
return { endpoint } as T;
1417
}
@@ -21,6 +24,10 @@ describe('BaseRestService', () => {
2124
expect(service).toBeInstanceOf(TestRestService);
2225
expect(await service.get('/test')).toHaveProperty('endpoint', '/test');
2326
expect(await service.post('/test', { foo: 'bar' })).toHaveProperty('endpoint', '/test');
27+
expect(await service.put('/test', { foo: 'baz' })).toMatchObject({ endpoint: '/test', method: 'PUT' });
2428
expect(await service.delete('/test')).toHaveProperty('endpoint', '/test');
29+
expect(service.getConfig()).toEqual(config);
30+
const result = await service.execute(async () => 42);
31+
expect(result).toBe(42);
2532
});
2633
});

packages/base/src/base-rest-service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { BaseService, BaseServiceConfig } from './base-service';
32
import type { ApiResult } from '@mixcore/api';
43

@@ -34,4 +33,11 @@ export abstract class BaseRestService extends BaseService {
3433
* @param endpoint - API endpoint
3534
*/
3635
abstract delete(endpoint: string): Promise<ApiResult>;
36+
37+
/**
38+
* Abstract method for PUT requests (returns ApiResult)
39+
* @param endpoint - API endpoint
40+
* @param data - Data to put
41+
*/
42+
abstract put(endpoint: string, data: any): Promise<ApiResult>;
3743
}

packages/base/src/base-service.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* BaseService
43
* Abstract base class for Mixcore SDK services
@@ -20,6 +19,26 @@ export abstract class BaseService {
2019
this.config = config;
2120
}
2221

22+
/**
23+
* Returns the current configuration.
24+
*/
25+
getConfig(): BaseServiceConfig {
26+
return this.config;
27+
}
28+
29+
/**
30+
* Executes a function with error handling.
31+
* @param fn - Function to execute
32+
*/
33+
protected async execute<T>(fn: () => Promise<T>): Promise<T> {
34+
try {
35+
return await fn();
36+
} catch (error) {
37+
this.handleError(error);
38+
throw error;
39+
}
40+
}
41+
2342
/**
2443
* Abstract method for error handling
2544
* @param error - Error object

packages/database/src/post-rest-mvc-service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export class PostRestMvcService extends BaseRestService {
3535
throw new Error('DELETE method not implemented');
3636
}
3737

38+
async put<T = any>(endpoint: string = this.endpoint, data: any): Promise<T> {
39+
if (typeof this.config.put === 'function') {
40+
return this.config.put(endpoint, data);
41+
}
42+
throw new Error('PUT method not implemented');
43+
}
44+
3845
handleError(error: any): void {
3946
if (typeof this.config.handleError === 'function') {
4047
this.config.handleError(error);

packages/database/src/related-attribute-data-rest-form-service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ export class RelatedAttributeDataRestFormService extends BaseRestService {
3636
throw new Error('DELETE method not implemented');
3737
}
3838

39+
async put<T = any>(endpoint: string = this.endpoint, data: any): Promise<T> {
40+
if (typeof this.config.put === 'function') {
41+
return this.config.put(endpoint, data);
42+
}
43+
throw new Error('PUT method not implemented');
44+
}
45+
3946
handleError(error: any): void {
4047
if (typeof this.config.handleError === 'function') {
4148
this.config.handleError(error);

packages/database/src/related-attribute-data-rest-portal-service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ export class RelatedAttributeDataRestPortalService extends BaseRestService {
4545
return { isSucceed: false, errors: ['DELETE method not implemented'] };
4646
}
4747

48+
/**
49+
* PUT request for related attribute data (returns ApiResult)
50+
*/
51+
async put(endpoint: string = this.endpoint, data: any): Promise<ApiResult> {
52+
if (typeof this.config.put === 'function') {
53+
return this.config.put(endpoint, data);
54+
}
55+
return { isSucceed: false, errors: ['PUT method not implemented'] };
56+
}
57+
4858
handleError(error: any): void {
4959
if (typeof this.config.handleError === 'function') {
5060
this.config.handleError(error);

packages/database/src/related-attribute-set-rest-portal-service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ export class RelatedAttributeSetRestPortalService extends BaseRestService {
4545
return { isSucceed: false, errors: ['DELETE method not implemented'] };
4646
}
4747

48+
/**
49+
* PUT request for related attribute set (returns ApiResult)
50+
*/
51+
async put(endpoint: string = this.endpoint, data: any): Promise<ApiResult> {
52+
if (typeof this.config.put === 'function') {
53+
return this.config.put(endpoint, data);
54+
}
55+
return { isSucceed: false, errors: ['PUT method not implemented'] };
56+
}
57+
4858
handleError(error: any): void {
4959
if (typeof this.config.handleError === 'function') {
5060
this.config.handleError(error);

swagger.json

Whitespace-only changes.

0 commit comments

Comments
 (0)