Skip to content

Commit af5d4b0

Browse files
committed
Refactoring, access-control, github:get-contributors method
1 parent 98bd960 commit af5d4b0

18 files changed

+145
-28
lines changed

README.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
# WebPurple VK-Provider
2-
[![Build Status](https://travis-ci.org/WebPurple/vk-provider.svg?branch=master)](https://travis-ci.org/WebPurple/vk-provider)
3-
[![License MIT](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/WebPurple/vk-provider/blob/master/LICENSE)
1+
# WebPurple Provider
2+
[![Build Status](https://travis-ci.org/WebPurple/provider.svg?branch=master)](https://travis-ci.org/WebPurple/provider)
3+
[![License MIT](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/WebPurple/provider/blob/master/LICENSE)
44

55

66
## Локальная работа
77
```sh
8-
git clone https://github.com/WebPurple/vk-provider.git
8+
git clone https://github.com/WebPurple/provider.git
99
npm install
1010
npm run dev
1111
```
1212

1313
## Методы
1414
### Albums
15-
Возвращает информацию о фотографиях в альбоме
15+
Возвращает информацию о фотографиях в альбоме ВКонтакте
1616
```
1717
GET /albums/:owner_id/:album_id/
18-
```
18+
```
19+
### Contributors
20+
Возвращает список "contributors" для репозитория GitHub
21+
```
22+
GET /contributors/:owner/:repo/
23+
```

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "vk-provider",
2+
"name": "webpurple-provider",
33
"version": "0.0.1",
4-
"description": "Requests provider for VK",
4+
"description": "Requests provider service",
55
"author": "Igor Fedyukin <[email protected]>",
66
"license": "MIT",
77
"scripts": {

src/app/app.controller.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { Get, Controller, Header, HttpCode } from '@nestjs/common';
1+
import { Get, Controller, HttpCode } from '@nestjs/common';
22

33
@Controller()
44
export class AppController {
55
constructor() {}
66

77
@Get()
8-
@HttpCode(301)
9-
@Header('Location', 'https://www.webpurple.net/')
8+
@HttpCode(200)
109
root(): string {
11-
return 'Hello world!';
10+
return 'WebPurple provider service is working...';
1211
}
1312
}

src/app/app.module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Module } from '@nestjs/common';
22
import { AppController } from './app.controller';
3-
import { AlbumsModule } from '../modules/albums/albums.module';
3+
import { AlbumsModule, ContributorsModule } from '../modules';
44

55
@Module({
6-
imports: [AlbumsModule],
6+
imports: [AlbumsModule, ContributorsModule],
77
controllers: [AppController],
88
providers: [],
99
})

src/config.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
export const config = {
22
port: process.env.PORT || 8080,
3-
apiVersion: '5.80',
4-
apiUrl: 'https://api.vk.com/method',
5-
serviceToken: process.env.SERVICE_TOKEN,
3+
access: {
4+
origins: ['webpurple.netlify.com', 'webpurple.net'],
5+
token: process.env.TOKEN,
6+
},
7+
vk: {
8+
apiVersion: '5.80',
9+
apiUrl: 'https://api.vk.com/method',
10+
serviceToken: process.env.VK_SERVICE_TOKEN,
11+
},
12+
github: {
13+
apiUrl: 'https://api.github.com',
14+
},
615
};

src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { NestFactory } from '@nestjs/core';
22
import { AppModule } from 'app/app.module';
33
import { config } from 'config';
4+
import { accessChecker } from './middlewares';
45

56
async function bootstrap() {
67
const app = await NestFactory.create(AppModule);
8+
app.use(accessChecker);
79
await app.listen(config.port);
810
}
911
bootstrap();

src/middlewares/access-checker.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { config } from '../config';
2+
3+
const { origins, token } = config.access;
4+
5+
const isAllowOrigin = origin => origin && origins.some(o => origin.includes(o));
6+
7+
export const accessChecker = (req, res, next) => {
8+
const origin = req.get('origin');
9+
const { token } = req.query;
10+
11+
if (isAllowOrigin(origin) || token === token) {
12+
res.set('Access-Control-Allow-Origin', '*');
13+
return next();
14+
}
15+
16+
res.status(403);
17+
res.send('Access denied!');
18+
};

src/middlewares/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { accessChecker } from './access-checker';

src/modules/albums/albums.controller.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { Get, Controller, Param, Header } from '@nestjs/common';
22
import { IGetPhotosParams } from './interfaces/GetPhoto';
33
import { GetPhotoDto } from '../common/dto/vk/get-photo.dto';
4-
import { VkService } from '../common/services/vk.service';
4+
import { VkService } from '../common/services';
55

66
@Controller('albums')
77
export class AlbumsController {
88
constructor(private readonly service: VkService) {}
99

1010
@Get(':owner_id/:album_id')
1111
@Header('Content-Type', 'application/json')
12-
@Header('Access-Control-Allow-Origin', 'http://webpurple.com')
1312
getPhotos(@Param() params: IGetPhotosParams): Promise<GetPhotoDto> {
1413
return this.service.getPhotos(params);
1514
}

src/modules/common/common.module.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Module } from '@nestjs/common';
2-
import { VkService } from './services/vk.service';
2+
import { VkService, GithubService } from './services';
33

44
@Module({
5-
providers: [VkService],
6-
exports: [VkService],
5+
providers: [VkService, GithubService],
6+
exports: [VkService, GithubService],
77
})
88
export class CommonModules {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export interface IContributors {
2+
avatar_url: string;
3+
contributions: number;
4+
events_url: string;
5+
followers_url: string;
6+
following_url: string;
7+
gists_url: string;
8+
gravatar_id: string;
9+
html_url: string;
10+
id: number;
11+
login: string;
12+
node_id: string;
13+
organizations_url: string;
14+
received_events_url: string;
15+
repos_url: string;
16+
site_admin: boolean;
17+
starred_url: string;
18+
subscriptions_url: string;
19+
type: string;
20+
url: string;
21+
}
22+
23+
export class GetContributorsDto {
24+
public contributors: Array<IContributors>;
25+
constructor(response) {
26+
this.contributors = Array.isArray(response) ? response : [];
27+
}
28+
29+
static of(response) {
30+
return new GetContributorsDto(response);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { config } from '../../../config';
3+
import { GetContributorsDto } from '../dto/github/get-contributors.dto';
4+
import axios from 'axios';
5+
6+
@Injectable()
7+
export class GithubService {
8+
private API_URL = config.github.apiUrl;
9+
10+
public getContributors(params: { owner: string; repo: string }): Promise<GetContributorsDto> {
11+
return axios
12+
.get(`${this.API_URL}/repos/${params.owner}/${params.repo}/contributors`)
13+
.then(response => response.data)
14+
.then(GetContributorsDto.of)
15+
.catch(e => {
16+
console.error(e);
17+
return null;
18+
});
19+
}
20+
}

src/modules/common/services/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { GithubService } from './github.service';
2+
export { VkService } from './vk.service';

src/modules/common/services/vk.service.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ import axios from 'axios';
55

66
@Injectable()
77
export class VkService {
8-
private SERVICE_TOKEN = config.serviceToken;
9-
private API_URL = config.apiUrl;
10-
private API_VERSION = config.apiVersion;
8+
private SERVICE_TOKEN = config.vk.serviceToken;
9+
private API_URL = config.vk.apiUrl;
10+
private API_VERSION = config.vk.apiVersion;
1111

1212
public getPhotos(params: { owner_id: string; album_id: string }): Promise<GetPhotoDto> {
1313
return axios
14-
.get(`${this.API_URL}/photos/get`, {
14+
.get(`${this.API_URL}/photos.get`, {
1515
params: {
1616
...params,
1717
access_token: this.SERVICE_TOKEN,
1818
v: this.API_VERSION,
1919
},
2020
})
2121
.then(response => response.data.response)
22+
.then(GetPhotoDto.of)
2223
.catch(e => {
2324
console.error(e);
2425
return null;
25-
})
26-
.then(GetPhotoDto.of);
26+
});
2727
}
2828
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Get, Controller, Param, Header } from '@nestjs/common';
2+
import { IGetContributorsParams } from './interfaces/GetContributors';
3+
import { GetContributorsDto } from '../common/dto/github/get-contributors.dto';
4+
import { GithubService } from '../common/services';
5+
6+
@Controller('contributors')
7+
export class ContributorsController {
8+
constructor(private readonly service: GithubService) {}
9+
10+
@Get(':owner/:repo')
11+
@Header('Content-Type', 'application/json')
12+
getContributors(@Param() params: IGetContributorsParams): Promise<GetContributorsDto> {
13+
return this.service.getContributors(params);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Module } from '@nestjs/common';
2+
import { ContributorsController } from './contributors.controller';
3+
import { CommonModules } from '../common/common.module';
4+
5+
@Module({
6+
imports: [CommonModules],
7+
controllers: [ContributorsController],
8+
})
9+
export class ContributorsModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IGetContributorsParams {
2+
owner: string;
3+
repo: string;
4+
}

src/modules/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { AlbumsModule } from './albums/albums.module';
2+
export { ContributorsModule } from './contributors/contributors.module';

0 commit comments

Comments
 (0)