diff --git a/src/api/endpoint/api/publish.ts b/src/api/endpoint/api/publish.ts index b41bff5c5..f88071298 100644 --- a/src/api/endpoint/api/publish.ts +++ b/src/api/endpoint/api/publish.ts @@ -216,8 +216,8 @@ export function publishPackage(storage: IStorageHandler, config: Config, auth: I try { const metadata = validateMetadata(req.body, packageName); - // treating deprecation as updating a package - if (req.params._rev || isRelatedToDeprecation(req.body)) { + // check _attachments to distinguish publish and deprecate + if (req.params._rev || (isRelatedToDeprecation(req.body) && _.isEmpty(req.body._attachments))) { debug('updating a new version for %o', packageName); // we check unpublish permissions, an update is basically remove versions const remote = req.remote_user; diff --git a/test/unit/modules/api/api.spec.ts b/test/unit/modules/api/api.spec.ts index bd47a7b74..6037689be 100644 --- a/test/unit/modules/api/api.spec.ts +++ b/test/unit/modules/api/api.spec.ts @@ -1073,6 +1073,28 @@ describe('endpoint unit test', () => { expect(res.body.versions['1.0.1'].deprecated).toEqual('get deprecated'); done(); }); + + test('should deprecate when publish new version with deprecate field', async (done) => { + await Promise.all([ + putPackage(request(app), `/${pkgName}`, generatePackageMetadata(pkgName, '2.0.0'), token), + putPackage(request(app), `/${pkgName}`, generatePackageMetadata(pkgName, '2.0.1'), token), + putPackage(request(app), `/${pkgName}`, generatePackageMetadata(pkgName, '2.0.2'), token) + ]); + + const pkg = generatePackageMetadata(pkgName, '2.0.3'); + pkg.versions['2.0.3'].deprecated = 'get deprecated'; + await putPackage(request(app), `/${encodeScopedUri(pkgName)}`, pkg, token); + + const [, res] = await getPackage(request(app), '', pkgName); + const versions = Object.keys(res.body.versions); + + expect(res.body.versions['2.0.3'].deprecated).toEqual('get deprecated'); + expect(versions).toContain('2.0.0'); + expect(versions).toContain('2.0.1'); + expect(versions).toContain('2.0.2'); + expect(versions).toContain('2.0.3'); + done(); + }); }); }); });