Skip to content

Commit

Permalink
feat: serviceFactory support default client name (#2482)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Nov 17, 2022
1 parent 541be19 commit 170092f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/axios/src/serviceManager.ts
Expand Up @@ -58,7 +58,9 @@ export class HttpService implements AxiosHttpService {

@Init()
protected async init() {
this.instance = this.serviceFactory.get('default');
this.instance = this.serviceFactory.get(
this.serviceFactory.getDefaultClientName?.() || 'default'
);
if (!this.instance) {
throw new MidwayCommonError('axios default instance not found.');
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/common/serviceFactory.ts
Expand Up @@ -15,7 +15,6 @@ export abstract class ServiceFactory<T> {
options.clients = options.clients || {};
options.clients['default'] = options.clients['default'] || {};
extend(true, options.clients['default'], options.client);
delete options.client;
}

// multi client
Expand Down Expand Up @@ -58,4 +57,8 @@ export abstract class ServiceFactory<T> {
await this.destroyClient(value);
}
}

public getDefaultClientName(): string {
return this.options['defaultClientName'];
}
}
1 change: 1 addition & 0 deletions packages/core/src/interface.ts
Expand Up @@ -24,6 +24,7 @@ export type ServiceFactoryConfigOption<OPTIONS> = {
clients?: {
[key: string]: PowerPartial<OPTIONS>;
};
defaultClientName?: string;
};

export type DataSourceManagerConfigOption<OPTIONS> = {
Expand Down
9 changes: 9 additions & 0 deletions packages/core/test/common/serviceFactory.test.ts
Expand Up @@ -59,4 +59,13 @@ describe('test/common/serviceFactory.test.ts', () => {
});
expect(instance.get('default')).toBeDefined();
});

it('should test default name', async () => {
const instance = new TestServiceFactory();
await instance.initClients({
defaultClientName: 'abc',
clients: {}
})
expect(instance.getDefaultClientName()).toEqual('abc');
});
});
4 changes: 3 additions & 1 deletion packages/cos/src/manager.ts
Expand Up @@ -52,7 +52,9 @@ export class COSService implements COS {

@Init()
async init() {
this.instance = this.serviceFactory.get('default');
this.instance = this.serviceFactory.get(
this.serviceFactory.getDefaultClientName?.() || 'default'
);
if (!this.instance) {
throw new MidwayCommonError('cos default instance not found.');
}
Expand Down
8 changes: 6 additions & 2 deletions packages/oss/src/manager.ts
Expand Up @@ -78,7 +78,9 @@ export class OSSService implements OSS {

@Init()
async init() {
this.instance = this.serviceFactory.get('default');
this.instance = this.serviceFactory.get(
this.serviceFactory.getDefaultClientName?.() || 'default'
);
if (!this.instance) {
throw new MidwayCommonError('oss default instance not found.');
}
Expand All @@ -102,7 +104,9 @@ export class OSSSTSService implements OSS.STS {

@Init()
async init() {
this.instance = this.serviceFactory.get('default');
this.instance = this.serviceFactory.get(
this.serviceFactory.getDefaultClientName?.() || 'default'
);
if (!this.instance) {
throw new MidwayCommonError('oss sts default instance not found.');
}
Expand Down
4 changes: 3 additions & 1 deletion packages/redis/src/manager.ts
Expand Up @@ -112,7 +112,9 @@ export class RedisService implements Redis {

@Init()
async init() {
this.instance = this.serviceFactory.get('default');
this.instance = this.serviceFactory.get(
this.serviceFactory.getDefaultClientName?.() || 'default'
);
if (!this.instance) {
throw new MidwayCommonError('redis default instance not found.');
}
Expand Down
4 changes: 3 additions & 1 deletion packages/tablestore/src/manager.ts
Expand Up @@ -42,7 +42,9 @@ export class TableStoreService implements TableStoreClient {

@Init()
async init() {
this.instance = this.serviceFactory.get('default');
this.instance = this.serviceFactory.get(
this.serviceFactory.getDefaultClientName?.() || 'default'
);
if (!this.instance) {
throw new MidwayCommonError('TableStore default instance not found.');
}
Expand Down

0 comments on commit 170092f

Please sign in to comment.