From e911a1768719ea73339c37d5301c859e169c0deb Mon Sep 17 00:00:00 2001 From: Choro Abdymanapov Date: Wed, 2 Dec 2020 18:08:30 +0300 Subject: [PATCH] feat(client): introduce get & getBatch (#7) --- custom-typings/npm-registry-client/index.d.ts | 10 +++- packages/client/README.md | 59 +++++++++++++++++++ packages/client/package.json | 1 + packages/client/src/main/ts/interfaces.ts | 11 ++++ packages/client/src/main/ts/wrapper.ts | 21 +++++++ packages/client/src/test/ts/wrapper.ts | 26 ++++++++ yarn.lock | 5 ++ 7 files changed, 132 insertions(+), 1 deletion(-) diff --git a/custom-typings/npm-registry-client/index.d.ts b/custom-typings/npm-registry-client/index.d.ts index 3cb757f..3ac0af2 100644 --- a/custom-typings/npm-registry-client/index.d.ts +++ b/custom-typings/npm-registry-client/index.d.ts @@ -11,11 +11,19 @@ declare module 'npm-registry-client' { message: string } + type TCallback = (error: any, data: T, raw: any, res: any) => void + class RegClient { deprecate( uri: string, params: TPackage & { auth: TAuth }, - cb: (error: any, data: any, raw: any, res: any) => void + cb: TCallback + ): void + + get( + uri: string, + params: any, + cb: TCallback ): void } export = RegClient diff --git a/packages/client/README.md b/packages/client/README.md index a9d6920..8bcbcc4 100644 --- a/packages/client/README.md +++ b/packages/client/README.md @@ -36,6 +36,65 @@ const wrapperWithCustomClientInstance = new NpmRegClientWrapper( ) ``` +## Get info about package +```typescript +wrapper.get('@types/react') + .then(console.log) +/* +{ + "_id": "foo", + "_rev": "12-116b13c5b065b01fc85f3b4034414b2c", + "name": "foo", + "dist-tags": { + "latest": "1.2.0" + }, + "versions": { + "1.1.0": { + ... +} +*/ +``` +## Get info about packages by list +```typescript +const packageNames = [ + 'foo', + 'bar', + 'baz' +] + +wrapper.getBatch(packageNames) + .then(console.log) +/* +[ + { + "_id": "foo", + "_rev": "12-12345678123456781234567812345678", + "name": "foo", + "dist-tags": { + "latest": "1.2.0" + }, + "versions": { + "1.1.0": { + ... + }, + { + "_id": "bar", + "_rev": "12-12345678123456781234567812345679", + "name": "bar", + "dist-tags": { + "latest": "1.2.0" + }, + "versions": { + "1.1.0": { + ... + }, + ... +] +*/ +wrapper.getBatch(packageNames, true) // if you want to ignore errors when executing a batch actions + +``` + ## Deprecate package version by given range and with given message ```typescript wrapper.deprecate('foo', '<1.2.0', 'foo <1.2.0 contains critical bugs') diff --git a/packages/client/package.json b/packages/client/package.json index 9531c31..a2b14ba 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -47,6 +47,7 @@ "tslib": "^2.0.3" }, "devDependencies": { + "@npm/types": "^1.0.1", "@qiwi/npm-batch-client-infra": "1.0.0", "nock": "^13.0.5" }, diff --git a/packages/client/src/main/ts/interfaces.ts b/packages/client/src/main/ts/interfaces.ts index ec4490e..d7ba14a 100644 --- a/packages/client/src/main/ts/interfaces.ts +++ b/packages/client/src/main/ts/interfaces.ts @@ -1,3 +1,5 @@ +import { Packument } from '@npm/types' + export interface IPackageParams { packageName: string version: string @@ -29,6 +31,15 @@ export interface INpmRegClientWrapper { params: Array, skipErrors?: boolean ): Promise + + get( + packageName: string + ): Promise + + getBatch( + packageNames: string[], + skipErrors?: boolean + ): Promise } export type TNpmRegClientAuth = { diff --git a/packages/client/src/main/ts/wrapper.ts b/packages/client/src/main/ts/wrapper.ts index 0ff0e3a..4a85492 100644 --- a/packages/client/src/main/ts/wrapper.ts +++ b/packages/client/src/main/ts/wrapper.ts @@ -1,6 +1,7 @@ import RegClient from 'npm-registry-client' import { IDeprecatePackageParams, INpmRegClientWrapper, IPackageParams,TNpmRegClientAuth } from './interfaces' +import { Packument } from '@npm/types' export class NpmRegClientWrapper implements INpmRegClientWrapper { client: RegClient @@ -63,6 +64,26 @@ export class NpmRegClientWrapper implements INpmRegClientWrapper { return `${this.registryUrl}${packageName.replace('/', '%2F')}` } + get(packageName: string): Promise { + return new Promise( + (resolve, reject) => { + try { + this.client.get(this.getPackageUrl(packageName), {}, (_, data) => resolve(data)) + } catch (e) { + reject(e) + } + } + ) + } + + getBatch(packageNames: string[], skipErrors?: boolean): Promise { + return NpmRegClientWrapper.performBatchActions( + packageNames, + (packageName) => this.get(packageName), + skipErrors + ) + } + static performBatchActions( params: Array, actionFactory: (...args: any[]) => Promise, diff --git a/packages/client/src/test/ts/wrapper.ts b/packages/client/src/test/ts/wrapper.ts index d394366..783aaa8 100644 --- a/packages/client/src/test/ts/wrapper.ts +++ b/packages/client/src/test/ts/wrapper.ts @@ -47,6 +47,32 @@ describe('NpmRegClientWrapper', () => { return wrapper.deprecate('foo', '*', 'foo') }) + test('get calls REST API', async () => { + const packageName = 'foo' + const mock = nock(url) + .get(`/${packageName}`) + .reply(200, packageInfo) + await expect(wrapper.get(packageName)).resolves.toMatchObject(packageInfo) + expect(mock.isDone()).toEqual(true) + }) + + test('getBatch', async () => { + const packageNames = [ + 'foo', + 'bar', + 'baz' + ] + + const mocks = packageNames.map( + packageName => nock(url) + .get(`/${packageName}`) + .reply(200, packageInfo) + ) + + await expect(wrapper.getBatch(packageNames)).resolves.toMatchObject(new Array(3).fill(packageInfo)) + expect(mocks.filter(mock => mock.isDone())).toHaveLength(packageNames.length) + }) + test('deprecateBatch makes batch calls to REST API', async () => { const params: Array['1'] }> = [ { diff --git a/yarn.lock b/yarn.lock index 65d0445..94d5f09 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1346,6 +1346,11 @@ "@nodelib/fs.scandir" "2.1.3" fastq "^1.6.0" +"@npm/types@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@npm/types/-/types-1.0.1.tgz#6b378d8ef427ddaf2efa967b4530de793dbc2de2" + integrity sha512-+tTwymYjZrm7s5KPcS6Abq2l1wVlsk0Jxx4RWMMlC9BePNK4BGMaXUKWphdi7xAYJNA+lwqIqFK6hcNrMu/HFg== + "@octokit/auth-token@^2.4.0": version "2.4.3" resolved "https://registry.yarnpkg.com/@octokit/auth-token/-/auth-token-2.4.3.tgz#b868b5f2366533a7e62933eaa1181a8924228cc4"