Skip to content

Commit

Permalink
feat(client): introduce get & getBatch (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
oljekechoro authored Dec 2, 2020
1 parent b96ad0a commit e911a17
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 1 deletion.
10 changes: 9 additions & 1 deletion custom-typings/npm-registry-client/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ declare module 'npm-registry-client' {
message: string
}

type TCallback<T = any> = (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
Expand Down
59 changes: 59 additions & 0 deletions packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/client/src/main/ts/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Packument } from '@npm/types'

export interface IPackageParams {
packageName: string
version: string
Expand Down Expand Up @@ -29,6 +31,15 @@ export interface INpmRegClientWrapper {
params: Array<IPackageParams>,
skipErrors?: boolean
): Promise<any[]>

get(
packageName: string
): Promise<Packument>

getBatch(
packageNames: string[],
skipErrors?: boolean
): Promise<Packument[]>
}

export type TNpmRegClientAuth = {
Expand Down
21 changes: 21 additions & 0 deletions packages/client/src/main/ts/wrapper.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -63,6 +64,26 @@ export class NpmRegClientWrapper implements INpmRegClientWrapper {
return `${this.registryUrl}${packageName.replace('/', '%2F')}`
}

get(packageName: string): Promise<Packument> {
return new Promise<Packument>(
(resolve, reject) => {
try {
this.client.get(this.getPackageUrl(packageName), {}, (_, data) => resolve(data))
} catch (e) {
reject(e)
}
}
)
}

getBatch(packageNames: string[], skipErrors?: boolean): Promise<Packument[]> {
return NpmRegClientWrapper.performBatchActions(
packageNames,
(packageName) => this.get(packageName),
skipErrors
)
}

static performBatchActions(
params: Array<any>,
actionFactory: (...args: any[]) => Promise<any>,
Expand Down
26 changes: 26 additions & 0 deletions packages/client/src/test/ts/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDeprecatePackageParams & { verifier: Parameters<typeof prepareMocks>['1'] }> = [
{
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit e911a17

Please sign in to comment.