Files
cnpmcore/app/common/adapter/binary/ApiBinary.ts
fengmk2 ffe723e65f refactor: use oxlint instead of eslint (#772)
say goodbye to eslint
2025-03-13 23:31:13 +08:00

47 lines
1.2 KiB
TypeScript

import { Inject, SingletonProto } from '@eggjs/tegg';
import type { EggAppConfig } from 'egg';
import { BinaryType } from '../../enum/Binary.js';
import type { FetchResult, BinaryItem } from './AbstractBinary.js';
import { AbstractBinary, 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 };
}
}