| 17 | @Provide() |
| 18 | @Scope(ScopeEnum.Singleton) |
| 19 | export class COSServiceFactory extends ServiceFactory<COS> { |
| 20 | @Config('cos') |
| 21 | cosConfig; |
| 22 | |
| 23 | @Init() |
| 24 | async init() { |
| 25 | await this.initClients(this.cosConfig, { |
| 26 | concurrent: true, |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | @Logger('coreLogger') |
| 31 | logger; |
| 32 | |
| 33 | @Inject() |
| 34 | protected traceService: MidwayTraceService; |
| 35 | |
| 36 | @Config('cos.tracing.meta') |
| 37 | protected traceMetaResolver; |
| 38 | |
| 39 | @Config('cos.tracing.enable') |
| 40 | protected traceEnabled; |
| 41 | |
| 42 | @Config('cos.tracing.injector') |
| 43 | protected traceInjector; |
| 44 | |
| 45 | async createClient(config: COS.COSOptions): Promise<COS> { |
| 46 | const { customClientClass, ...otherConfig } = config as any; |
| 47 | if (customClientClass) { |
| 48 | const client = new customClientClass(otherConfig); |
| 49 | this.bindTraceContext(client as any); |
| 50 | return client; |
| 51 | } |
| 52 | |
| 53 | assert.ok( |
| 54 | config.SecretKey && config.SecretId, |
| 55 | '[@midwayjs/cos] secretId secretKey is required on config' |
| 56 | ); |
| 57 | this.logger.info('[midway:cos] init %s', config.SecretKey); |
| 58 | |
| 59 | const client = new COS(config); |
| 60 | this.bindTraceContext(client); |
| 61 | return client; |
| 62 | } |
| 63 | |
| 64 | protected bindTraceContext(client: COS) { |
| 65 | if (!client || !this.traceService) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | const rawRequest = (client as any).request?.bind(client); |
| 70 | if (!rawRequest) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | (client as any).request = async (...args) => { |
| 75 | if (typeof args[1] === 'function') { |
| 76 | return rawRequest(...args); |