Skip to content

Commit

Permalink
fix: set version default value to null
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhengxu2018 committed Jul 14, 2024
1 parent 563a1b5 commit 6f0dce3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
12 changes: 6 additions & 6 deletions app/port/controller/ProxyCacheController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ export class ProxyCacheController extends AbstractController {
method: HTTPMethodEnum.GET,
path: `/-/proxy-cache/:fullname(${FULLNAME_REG_STRING})`,
})
async showProxyCaches(@HTTPParam() fullname: string) {
async showProxyCaches(@HTTPQuery() pageSize: Static<typeof QueryPageOptions>['pageSize'],
@HTTPQuery() pageIndex: Static<typeof QueryPageOptions>['pageIndex'], @HTTPParam() fullname: string) {
if (this.config.cnpmcore.syncMode !== SyncMode.proxy) {
throw new ForbiddenError('proxy mode is not enabled');
}
const result = await this.proxyCacheRepository.findProxyCaches(fullname);
if (result.length === 0) {
throw new NotFoundError();
}
return result;
return await this.proxyCacheRepository.listCachedFiles({
pageSize,
pageIndex,
}, fullname);
}

@HTTPMethod({
Expand Down
7 changes: 4 additions & 3 deletions app/repository/ProxyCacheRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ export class ProxyCacheRepository extends AbstractRepository {
return null;
}

// used by update & delete all cache
async findProxyCaches(fullname: string, version?: string) {
const models = version ? await this.ProxyCache.find({ fullname, version }) : await this.ProxyCache.find({ fullname });
return models;
}

async listCachedFiles(page: PageOptions): Promise<PageResult<ProxyCacheEntity>> {
async listCachedFiles(page: PageOptions, fullname?: string): Promise<PageResult<ProxyCacheEntity>> {
const { offset, limit } = EntityUtil.convertPageOptionsToLimitOption(page);
const count = await this.ProxyCache.find().count();
const models = await this.ProxyCache.find().offset(offset).limit(limit);
const count = fullname ? await this.ProxyCache.find({ fullname }).count() : await this.ProxyCache.find().count();
const models = fullname ? await this.ProxyCache.find({ fullname }).offset(offset).limit(limit) : await this.ProxyCache.find().offset(offset).limit(limit);
return {
count,
data: models.map(model => ModelConvertor.convertModelToEntity(model, ProxyCacheEntity)),
Expand Down
2 changes: 1 addition & 1 deletion app/repository/model/ProxyCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export class ProxyCache extends Bone {
filePath: string;

@Attribute(DataTypes.STRING(214))
version: string;
version?: string;

}
2 changes: 1 addition & 1 deletion sql/3.47.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE TABLE IF NOT EXISTS `proxy_caches` (
`gmt_create` datetime(3) NOT NULL COMMENT 'create time',
`gmt_modified` datetime(3) NOT NULL COMMENT 'modify time',
`fullname` varchar(214) NOT NULL DEFAULT '' COMMENT '@scope/package name',
`version` varchar(214) NULL DEFAULT '' COMMENT 'package version',
`version` varchar(214) COMMENT 'package version',
`file_type` varchar(30) NOT NULL DEFAULT '' COMMENT 'file type',
`file_path` varchar(512) NOT NULL DEFAULT '' COMMENT 'nfs file path',
PRIMARY KEY (`id`),
Expand Down
5 changes: 3 additions & 2 deletions test/port/controller/ProxyCacheController/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ describe('test/port/controller/PackageVersionFileController/listFiles.test.ts',
mock(app.config.cnpmcore, 'syncMode', SyncMode.proxy);
mock(app.config.cnpmcore, 'redirectNotFound', false);
const res = await app.httpRequest().get('/-/proxy-cache/foo-bar').expect(200);
assert(res.body.length === 2);
assert(res.body.count === 2);
});

it('should 404 when not found', async () => {
mock(app.config.cnpmcore, 'syncMode', SyncMode.proxy);
mock(app.config.cnpmcore, 'redirectNotFound', false);
await app.httpRequest().get('/-/proxy-cache/foo-bar-xxx').expect(404);
const res = await app.httpRequest().get('/-/proxy-cache/foo-bar-xxx').expect(200);
assert(res.body.count === 0);
});
});

Expand Down
8 changes: 4 additions & 4 deletions test/repository/ProxyCachePepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ describe('test/repository/ProxyCacheRepository.test.ts', () => {

it('remove work', async () => {
await proxyCacheRepository.removeProxyCache('foo-bar', DIST_NAMES.FULL_MANIFESTS);
const emptyRes = await proxyCacheRepository.listCachedFiles({});
assert.deepEqual(emptyRes.data, []);
const { count } = await proxyCacheRepository.listCachedFiles({});
assert.equal(count, 0);
});

it('truncate work', async () => {
Expand All @@ -63,8 +63,8 @@ describe('test/repository/ProxyCacheRepository.test.ts', () => {
fileType: DIST_NAMES.FULL_MANIFESTS,
}));
await proxyCacheRepository.truncateProxyCache();
const emptyRes = await proxyCacheRepository.listCachedFiles({});
assert.deepEqual(emptyRes.data, []);
const { count } = await proxyCacheRepository.listCachedFiles({});
assert.equal(count, 0);
});
});
});

0 comments on commit 6f0dce3

Please sign in to comment.