-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathApiBinary.ts
37 lines (34 loc) · 1.12 KB
/
ApiBinary.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
import { Inject, SingletonProto } from '@eggjs/tegg';
import { EggAppConfig } from 'egg';
import { BinaryType } from '../../enum/Binary.js';
import { AbstractBinary, FetchResult, BinaryItem, BinaryAdapter } from './AbstractBinary.js';
@SingletonProto()
@BinaryAdapter(BinaryType.Api)
export class ApiBinary extends AbstractBinary {
@Inject()
private readonly config: EggAppConfig;
async initFetch() {
// do nothing
return;
}
async fetch(dir: string, binaryName: string): Promise<FetchResult | undefined> {
const apiUrl = this.config.cnpmcore.syncBinaryFromAPISource || `${this.config.cnpmcore.sourceRegistry}/-/binary`;
const url = `${apiUrl}/${binaryName}${dir}`;
const data = await this.requestJSON(url);
if (!Array.isArray(data)) {
this.logger.warn('[ApiBinary.fetch:response-data-not-array] data: %j', data);
return;
}
const items: BinaryItem[] = [];
for (const item of data) {
items.push({
name: item.name,
isDir: item.type === 'dir',
url: item.url,
size: item.size || '-',
date: item.date,
});
}
return { items };
}
}