/
manager.ts
69 lines (58 loc) 路 1.38 KB
/
manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {
Config,
Init,
Inject,
Logger,
Provide,
Scope,
ScopeEnum,
ServiceFactory,
delegateTargetPrototypeMethod,
MidwayCommonError,
} from '@midwayjs/core';
import * as assert from 'assert';
import * as COS from 'cos-nodejs-sdk-v5';
@Provide()
@Scope(ScopeEnum.Singleton)
export class COSServiceFactory extends ServiceFactory<COS> {
@Config('cos')
cosConfig;
@Init()
async init() {
await this.initClients(this.cosConfig);
}
@Logger('coreLogger')
logger;
async createClient(config: COS.COSOptions): Promise<COS> {
assert(
config.SecretKey && config.SecretId,
'[@midwayjs/cos] secretId secretKey is required on config'
);
this.logger.info('[midway:cos] init %s', config.SecretKey);
return new COS(config);
}
getName() {
return 'cos';
}
}
@Provide()
@Scope(ScopeEnum.Singleton)
export class COSService implements COS {
@Inject()
private serviceFactory: COSServiceFactory;
private instance: COS;
@Init()
async init() {
this.instance = this.serviceFactory.get(
this.serviceFactory.getDefaultClientName?.() || 'default'
);
if (!this.instance) {
throw new MidwayCommonError('cos default instance not found.');
}
}
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface COSService extends COS {
// empty
}
delegateTargetPrototypeMethod(COSService, [COS]);