-
Notifications
You must be signed in to change notification settings - Fork 0
/
qiniu.ts
58 lines (52 loc) · 1.87 KB
/
qiniu.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
import { Region } from './region';
import { Adapter, RequestInfo, ResponseInfo } from './adapter';
import { Kodo } from './kodo';
import { S3 } from './s3';
import { NatureLanguage } from './uplog';
export const KODO_MODE = 'kodo';
export const S3_MODE = 's3';
export interface ModeOptions {
appName: string;
appVersion: string;
appNatureLanguage: NatureLanguage;
uplogBufferSize?: number;
requestCallback?: (request: RequestInfo) => void;
responseCallback?: (response: ResponseInfo) => void;
}
export class Qiniu {
private static readonly ADAPTERS: { [key: string]: typeof Adapter; } = {};
static register(modeName: string, adapter: any) {
Qiniu.ADAPTERS[modeName] = adapter;
}
constructor(
private readonly accessKey: string,
private readonly secretKey: string,
private readonly sessionToken?: string,
private readonly ucUrl?: string,
private readonly appendedUserAgent?: string,
private readonly regions: Region[] = [],
) {
}
mode(modeName: string, options?: ModeOptions): Adapter {
const adapter: any = Qiniu.ADAPTERS[modeName];
if (!adapter) {
throw new Error(`Invalid qiniu mode: ${modeName}`);
}
return new adapter({
accessKey: this.accessKey,
secretKey: this.secretKey,
sessionToken: this.sessionToken,
regions: this.regions,
ucUrl: this.ucUrl,
appendedUserAgent: this.appendedUserAgent,
appName: options?.appName,
appVersion: options?.appVersion,
appNatureLanguage: options?.appNatureLanguage,
uplogBufferSize: options?.uplogBufferSize,
requestCallback: options?.requestCallback,
responseCallback: options?.responseCallback,
});
}
}
Qiniu.register(KODO_MODE, Kodo);
Qiniu.register(S3_MODE, S3);