├── .prettierrc ├── nest-cli.json ├── tsconfig.build.json ├── test ├── jest-e2e.json └── app.e2e-spec.ts ├── src ├── main.ts ├── auth │ ├── dto │ │ ├── refreshAccessToken.dto.ts │ │ └── accessToken.dto.ts │ ├── auth.module.ts │ ├── auth.service.spec.ts │ ├── auth.controller.spec.ts │ ├── auth.controller.ts │ └── auth.service.ts ├── oauth │ ├── dto │ │ ├── authorize.dto.ts │ │ └── accessToken.dto.ts │ ├── oauth.module.ts │ ├── oauth.service.spec.ts │ ├── oauth.controller.spec.ts │ ├── oauth.controller.ts │ └── oauth.service.ts ├── app.module.ts ├── user │ ├── user.module.ts │ ├── user.service.spec.ts │ ├── user.controller.spec.ts │ ├── user.controller.ts │ └── user.service.ts └── media │ ├── media.module.ts │ ├── media.service.spec.ts │ ├── media.controller.spec.ts │ ├── media.controller.ts │ └── media.service.ts ├── tsconfig.json ├── .gitignore ├── .eslintrc.js ├── package.json └── README.md /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(3000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /src/auth/dto/refreshAccessToken.dto.ts: -------------------------------------------------------------------------------- 1 | import { IsNotEmpty } from 'class-validator'; 2 | 3 | export class RefreshAccessTokenDto { 4 | 5 | @IsNotEmpty() 6 | grant_type: string; 7 | 8 | @IsNotEmpty() 9 | access_token: string; 10 | } 11 | -------------------------------------------------------------------------------- /src/auth/dto/accessToken.dto.ts: -------------------------------------------------------------------------------- 1 | import { IsNotEmpty } from 'class-validator'; 2 | 3 | export class AccessTokenDto { 4 | @IsNotEmpty() 5 | client_secret: string; 6 | 7 | @IsNotEmpty() 8 | grant_type: string; 9 | 10 | @IsNotEmpty() 11 | access_token: string; 12 | } 13 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "target": "es2017", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./", 12 | "incremental": true 13 | }, 14 | "exclude": ["node_modules", "dist"] 15 | } 16 | -------------------------------------------------------------------------------- /src/oauth/dto/authorize.dto.ts: -------------------------------------------------------------------------------- 1 | import { IsNotEmpty, IsNumberString, IsUrl } from 'class-validator'; 2 | 3 | export class AuthorizeDto { 4 | @IsNotEmpty() 5 | @IsNumberString() 6 | client_id: string; 7 | 8 | @IsUrl() 9 | @IsNotEmpty() 10 | redirect_uri: string; 11 | 12 | @IsNotEmpty() 13 | response_type: string; 14 | 15 | @IsNotEmpty() 16 | scope: string; 17 | 18 | state: string; 19 | } 20 | -------------------------------------------------------------------------------- /src/oauth/dto/accessToken.dto.ts: -------------------------------------------------------------------------------- 1 | import { IsNotEmpty, IsNumberString, IsUrl } from 'class-validator'; 2 | 3 | export class AccessTokenDto { 4 | @IsNotEmpty() 5 | @IsNumberString() 6 | client_id: string; 7 | 8 | @IsNotEmpty() 9 | client_secret: string; 10 | 11 | @IsNotEmpty() 12 | code: string; 13 | 14 | @IsNotEmpty() 15 | grant_type: string; 16 | 17 | @IsUrl() 18 | @IsNotEmpty() 19 | redirect_uri: string; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { ConfigModule } from '@nestjs/config'; 3 | 4 | import { OAuthModule } from './oauth/oauth.module'; 5 | import { AuthModule } from './auth/auth.module'; 6 | import { MediaModule } from './media/media.module'; 7 | import { UserModule } from './user/user.module'; 8 | 9 | 10 | @Module({ 11 | imports: [OAuthModule, AuthModule, MediaModule, UserModule, ConfigModule.forRoot()], 12 | }) 13 | export class AppModule {} 14 | -------------------------------------------------------------------------------- /src/auth/auth.module.ts: -------------------------------------------------------------------------------- 1 | import { Module, HttpModule } from '@nestjs/common'; 2 | import { AuthService } from './auth.service'; 3 | import { AuthController } from './auth.controller'; 4 | 5 | @Module({ 6 | imports: [ 7 | HttpModule.register({ 8 | timeout: 5000, 9 | maxRedirects: 5, 10 | baseURL: 'https://graph.instagram.com/', 11 | }), 12 | ], 13 | providers: [AuthService], 14 | controllers: [AuthController], 15 | }) 16 | export class AuthModule {} 17 | -------------------------------------------------------------------------------- /src/user/user.module.ts: -------------------------------------------------------------------------------- 1 | import { Module, HttpModule } from '@nestjs/common'; 2 | import { UserController } from './user.controller'; 3 | import { UserService } from './user.service'; 4 | 5 | @Module({ 6 | imports: [ 7 | HttpModule.register({ 8 | timeout: 5000, 9 | maxRedirects: 5, 10 | baseURL: 'https://graph.instagram.com/', 11 | }), 12 | ], 13 | controllers: [UserController], 14 | providers: [UserService] 15 | }) 16 | export class UserModule {} 17 | -------------------------------------------------------------------------------- /src/media/media.module.ts: -------------------------------------------------------------------------------- 1 | import { Module, HttpModule } from '@nestjs/common'; 2 | import { MediaController } from './media.controller'; 3 | import { MediaService } from './media.service'; 4 | 5 | @Module({ 6 | imports: [ 7 | HttpModule.register({ 8 | timeout: 5000, 9 | maxRedirects: 5, 10 | baseURL: 'https://graph.instagram.com/', 11 | }), 12 | ], 13 | controllers: [MediaController], 14 | providers: [MediaService] 15 | }) 16 | export class MediaModule {} 17 | -------------------------------------------------------------------------------- /src/oauth/oauth.module.ts: -------------------------------------------------------------------------------- 1 | import { Module, HttpModule } from '@nestjs/common'; 2 | import { OAuthController } from './oauth.controller'; 3 | import { OAuthService } from './oauth.service'; 4 | 5 | @Module({ 6 | imports: [ 7 | HttpModule.register({ 8 | timeout: 5000, 9 | maxRedirects: 5, 10 | baseURL: 'https://api.instagram.com/oauth' 11 | }) 12 | ], 13 | controllers: [OAuthController], 14 | providers: [OAuthService] 15 | }) 16 | export class OAuthModule {} 17 | -------------------------------------------------------------------------------- /src/auth/auth.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AuthService } from './auth.service'; 3 | 4 | describe('AuthService', () => { 5 | let service: AuthService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [AuthService], 10 | }).compile(); 11 | 12 | service = module.get(AuthService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/user/user.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { UserService } from './user.service'; 3 | 4 | describe('UserService', () => { 5 | let service: UserService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [UserService], 10 | }).compile(); 11 | 12 | service = module.get(UserService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | lerna-debug.log* 12 | 13 | # OS 14 | .DS_Store 15 | 16 | # Tests 17 | /coverage 18 | /.nyc_output 19 | 20 | # IDEs and editors 21 | /.idea 22 | .project 23 | .classpath 24 | .c9/ 25 | *.launch 26 | .settings/ 27 | *.sublime-workspace 28 | 29 | # IDE - VSCode 30 | .vscode/* 31 | !.vscode/settings.json 32 | !.vscode/tasks.json 33 | !.vscode/launch.json 34 | !.vscode/extensions.json 35 | 36 | 37 | .env -------------------------------------------------------------------------------- /src/oauth/oauth.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AuthService } from './oauth.service'; 3 | 4 | describe('AuthService', () => { 5 | let service: AuthService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [AuthService], 10 | }).compile(); 11 | 12 | service = module.get(AuthService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/media/media.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { MediaService } from './media.service'; 3 | 4 | describe('MediaService', () => { 5 | let service: MediaService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [MediaService], 10 | }).compile(); 11 | 12 | service = module.get(MediaService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/auth/auth.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AuthController } from './auth.controller'; 3 | 4 | describe('Auth Controller', () => { 5 | let controller: AuthController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [AuthController], 10 | }).compile(); 11 | 12 | controller = module.get(AuthController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/user/user.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { UserController } from './user.controller'; 3 | 4 | describe('User Controller', () => { 5 | let controller: UserController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [UserController], 10 | }).compile(); 11 | 12 | controller = module.get(UserController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/oauth/oauth.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AuthController } from './oauth.controller'; 3 | 4 | describe('oauth Controller', () => { 5 | let controller: AuthController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [AuthController], 10 | }).compile(); 11 | 12 | controller = module.get(AuthController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/media/media.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { MediaController } from './media.controller'; 3 | 4 | describe('Media Controller', () => { 5 | let controller: MediaController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [MediaController], 10 | }).compile(); 11 | 12 | controller = module.get(MediaController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/eslint-recommended', 10 | 'plugin:@typescript-eslint/recommended', 11 | 'prettier', 12 | 'prettier/@typescript-eslint', 13 | ], 14 | root: true, 15 | env: { 16 | node: true, 17 | jest: true, 18 | }, 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /src/media/media.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Param, Get, ParseIntPipe } from '@nestjs/common'; 2 | import { AxiosResponse } from 'axios'; 3 | 4 | import { MediaService } from './media.service'; 5 | 6 | @Controller('media') 7 | export class MediaController { 8 | constructor(private mediaService: MediaService) {} 9 | 10 | @Get('/:mediaId') 11 | getMediaById( 12 | @Param('mediaId', ParseIntPipe) mediaId: number, 13 | ): Promise { 14 | return this.mediaService.getMediaById(mediaId); 15 | } 16 | 17 | @Get('/:mediaId/children') 18 | getChildrenOfMediaById( 19 | @Param('mediaId', ParseIntPipe) mediaId: number, 20 | ): Promise { 21 | return this.mediaService.getChildrenOfMediaById(mediaId); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/user/user.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common'; 2 | import { AxiosResponse } from 'axios'; 3 | 4 | import { UserService } from './user.service'; 5 | 6 | @Controller('user') 7 | export class UserController { 8 | constructor(private userService: UserService) {} 9 | 10 | @Get('/me') 11 | getMe(): Promise { 12 | return this.userService.getMe(); 13 | } 14 | 15 | @Get('/:userId') 16 | getUserById( 17 | @Param('userId') userId: string, 18 | ): Promise { 19 | return this.userService.getUserById(userId); 20 | } 21 | 22 | @Get('/:userId/media') 23 | getMediaByUserId( 24 | @Param('userId') userId: string, 25 | ): Promise { 26 | return this.userService.getMediaByUserId(userId); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/oauth/oauth.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get, Query, ValidationPipe, Post, UsePipes, Body } from '@nestjs/common'; 2 | import { AxiosResponse } from 'axios'; 3 | import { OAuthService } from './oauth.service'; 4 | import { AuthorizeDto } from './dto/authorize.dto'; 5 | import { AccessTokenDto } from './dto/accessToken.dto'; 6 | 7 | @Controller('oauth') 8 | export class OAuthController { 9 | constructor(private oauthService: OAuthService) {} 10 | 11 | @Get('/authorize') 12 | authorize( 13 | @Query(ValidationPipe) authorizeDto: AuthorizeDto, 14 | ): Promise { 15 | return this.oauthService.authorize(authorizeDto); 16 | } 17 | 18 | @Post('/access_token') 19 | @UsePipes(ValidationPipe) 20 | accessToken( 21 | @Body() accessTokenDto: AccessTokenDto 22 | ): Promise { 23 | return this.oauthService.accessToken(accessTokenDto) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/auth/auth.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get, Query, ValidationPipe } from '@nestjs/common'; 2 | import { AxiosResponse } from 'axios'; 3 | 4 | import { AuthService } from './auth.service'; 5 | import { AccessTokenDto } from './dto/accessToken.dto'; 6 | import { RefreshAccessTokenDto } from './dto/refreshAccessToken.dto'; 7 | 8 | @Controller('auth') 9 | export class AuthController { 10 | constructor(private authService: AuthService) {} 11 | 12 | @Get('/access_token') 13 | accessToken( 14 | @Query(ValidationPipe) accessTokenDto: AccessTokenDto, 15 | ): Promise { 16 | return this.authService.accessToken(accessTokenDto); 17 | } 18 | 19 | 20 | @Get('/refresh_access_token') 21 | refreshAccessToken( 22 | @Query(ValidationPipe) refreshAccessTokenDto: RefreshAccessTokenDto, 23 | ): Promise { 24 | return this.authService.refreshAccessToken(refreshAccessTokenDto); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/oauth/oauth.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, HttpService, HttpException } from '@nestjs/common'; 2 | import { AxiosResponse } from 'axios'; 3 | 4 | import { AuthorizeDto } from './dto/authorize.dto'; 5 | import { AccessTokenDto } from './dto/accessToken.dto'; 6 | 7 | @Injectable() 8 | export class OAuthService { 9 | constructor(private httpService: HttpService) {} 10 | 11 | async authorize(authorizeDto: AuthorizeDto): Promise { 12 | try { 13 | const response = await this.httpService 14 | .get('/authorize', { 15 | params: authorizeDto, 16 | }) 17 | .toPromise(); 18 | return response.data; 19 | } catch (error) { 20 | const { status, statusText } = error.response; 21 | throw new HttpException(statusText, status); 22 | } 23 | } 24 | 25 | async accessToken(accessTokenDto: AccessTokenDto): Promise { 26 | try { 27 | const response = await this.httpService 28 | .post('/access_token', { 29 | data: accessTokenDto, 30 | }) 31 | .toPromise(); 32 | return response.data; 33 | } catch (error) { 34 | const { status, statusText } = error.response; 35 | throw new HttpException(statusText, status); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/auth/auth.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, HttpService, HttpException } from '@nestjs/common'; 2 | import { AxiosResponse } from 'axios'; 3 | 4 | import { AccessTokenDto } from './dto/accessToken.dto'; 5 | import { RefreshAccessTokenDto } from './dto/refreshAccessToken.dto'; 6 | 7 | @Injectable() 8 | export class AuthService { 9 | constructor(private httpService: HttpService) {} 10 | 11 | async accessToken(accessTokenDto: AccessTokenDto): Promise { 12 | try { 13 | const response = await this.httpService 14 | .get('/access_token', { 15 | params: accessTokenDto, 16 | }) 17 | .toPromise(); 18 | return response.data; 19 | } catch (error) { 20 | const { status, statusText } = error.response; 21 | throw new HttpException(statusText, status); 22 | } 23 | } 24 | 25 | async refreshAccessToken( 26 | refreshAccessTokenDto: RefreshAccessTokenDto, 27 | ): Promise { 28 | try { 29 | const response = await this.httpService 30 | .get('/refresh_access_token', { 31 | params: refreshAccessTokenDto, 32 | }) 33 | .toPromise(); 34 | return response.data; 35 | } catch (error) { 36 | const { status, statusText } = error.response; 37 | throw new HttpException(statusText, status); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/media/media.service.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable @typescript-eslint/camelcase */ 2 | import { Injectable, HttpService, HttpException } from '@nestjs/common'; 3 | import { AxiosResponse } from 'axios'; 4 | 5 | @Injectable() 6 | export class MediaService { 7 | constructor(private httpService: HttpService) {} 8 | 9 | async getMediaById(mediaId): Promise { 10 | try { 11 | const response = await this.httpService 12 | .get(`/${mediaId}`, { 13 | params: { 14 | access_token: process.env.ACCESS_TOKEN, 15 | fields: 16 | 'caption,id,media_type,media_url,permalink,thumbnail_url,timestamp,username', 17 | }, 18 | }) 19 | .toPromise(); 20 | return response.data; 21 | } catch (error) { 22 | const { status, statusText } = error.response; 23 | throw new HttpException(statusText, status); 24 | } 25 | } 26 | 27 | async getChildrenOfMediaById(mediaId): Promise { 28 | try { 29 | const response = await this.httpService 30 | .get(`/${mediaId}/children`, { 31 | params: { 32 | access_token: process.env.ACCESS_TOKEN, 33 | fields: 34 | 'caption,id,media_type,media_url,permalink,thumbnail_url,timestamp,username', 35 | }, 36 | }) 37 | .toPromise(); 38 | return response.data; 39 | } catch (error) { 40 | const { status, statusText } = error.response; 41 | throw new HttpException(statusText, status); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/user/user.service.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable @typescript-eslint/camelcase */ 2 | import { Injectable, HttpService, HttpException } from '@nestjs/common'; 3 | import { AxiosResponse } from 'axios'; 4 | 5 | @Injectable() 6 | export class UserService { 7 | constructor(private httpService: HttpService) {} 8 | 9 | async getMe(): Promise { 10 | try { 11 | const response = await this.httpService 12 | .get('/me', { 13 | params: { 14 | access_token: process.env.ACCESS_TOKEN, 15 | fields: 'account_type,id,media_count,username', 16 | }, 17 | }) 18 | .toPromise(); 19 | return response.data; 20 | } catch (error) { 21 | const { status, statusText } = error.response; 22 | throw new HttpException(statusText, status); 23 | } 24 | } 25 | 26 | async getUserById(userId): Promise { 27 | try { 28 | const response = await this.httpService 29 | .get(`/${userId}`, { 30 | params: { 31 | access_token: process.env.ACCESS_TOKEN, 32 | fields: 'account_type,id,media_count,username', 33 | }, 34 | }) 35 | .toPromise(); 36 | return response.data; 37 | } catch (error) { 38 | const { status, statusText } = error.response; 39 | throw new HttpException(statusText, status); 40 | } 41 | } 42 | 43 | async getMediaByUserId(userId): Promise { 44 | try { 45 | const response = await this.httpService 46 | .get(`${userId}/media`, { 47 | params: { 48 | access_token: process.env.ACCESS_TOKEN, 49 | fields: 50 | 'caption,id,media_type,media_url,permalink,thumbnail_url,timestamp,username', 51 | }, 52 | }) 53 | .toPromise(); 54 | return response.data; 55 | } catch (error) { 56 | const { status, statusText } = error.response; 57 | throw new HttpException(statusText, status); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nestjs-instagram-api", 3 | "version": "0.0.1", 4 | "description": "NestJS Backend to call Instagram API", 5 | "author": "Teresa Romero", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^7.0.0", 25 | "@nestjs/config": "^0.4.1", 26 | "@nestjs/core": "^7.0.0", 27 | "@nestjs/platform-express": "^7.0.0", 28 | "axios": "^0.19.2", 29 | "class-transformer": "^0.2.3", 30 | "class-validator": "^0.12.2", 31 | "reflect-metadata": "^0.1.13", 32 | "rimraf": "^3.0.2", 33 | "rxjs": "^6.5.4" 34 | }, 35 | "devDependencies": { 36 | "@nestjs/cli": "^7.0.0", 37 | "@nestjs/schematics": "^7.0.0", 38 | "@nestjs/testing": "^7.0.0", 39 | "@types/express": "^4.17.3", 40 | "@types/jest": "25.1.4", 41 | "@types/node": "^13.9.1", 42 | "@types/supertest": "^2.0.8", 43 | "@typescript-eslint/eslint-plugin": "^2.23.0", 44 | "@typescript-eslint/parser": "^2.23.0", 45 | "eslint": "^6.8.0", 46 | "eslint-config-prettier": "^6.10.0", 47 | "eslint-plugin-import": "^2.20.1", 48 | "jest": "^25.1.0", 49 | "prettier": "^1.19.1", 50 | "supertest": "^4.0.2", 51 | "ts-jest": "25.2.1", 52 | "ts-loader": "^6.2.1", 53 | "ts-node": "^8.6.2", 54 | "tsconfig-paths": "^3.9.0", 55 | "typescript": "^3.7.4" 56 | }, 57 | "jest": { 58 | "moduleFileExtensions": [ 59 | "js", 60 | "json", 61 | "ts" 62 | ], 63 | "rootDir": "src", 64 | "testRegex": ".spec.ts$", 65 | "transform": { 66 | "^.+\\.(t|j)s$": "ts-jest" 67 | }, 68 | "coverageDirectory": "../coverage", 69 | "testEnvironment": "node" 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master 6 | [travis-url]: https://travis-ci.org/nestjs/nest 7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux 8 | [linux-url]: https://travis-ci.org/nestjs/nest 9 | 10 |

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

11 |

12 | NPM Version 13 | Package License 14 | NPM Downloads 15 | Travis 16 | Linux 17 | Coverage 18 | Gitter 19 | Backers on Open Collective 20 | Sponsors on Open Collective 21 | 22 | 23 |

24 | 26 | 27 | ## Description 28 | 29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 30 | 31 | ## Installation 32 | 33 | ```bash 34 | $ npm install 35 | ``` 36 | 37 | ## Running the app 38 | 39 | ```bash 40 | # development 41 | $ npm run start 42 | 43 | # watch mode 44 | $ npm run start:dev 45 | 46 | # production mode 47 | $ npm run start:prod 48 | ``` 49 | 50 | ## Test 51 | 52 | ```bash 53 | # unit tests 54 | $ npm run test 55 | 56 | # e2e tests 57 | $ npm run test:e2e 58 | 59 | # test coverage 60 | $ npm run test:cov 61 | ``` 62 | 63 | ## Support 64 | 65 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). 66 | 67 | ## Stay in touch 68 | 69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 70 | - Website - [https://nestjs.com](https://nestjs.com/) 71 | - Twitter - [@nestframework](https://twitter.com/nestframework) 72 | 73 | ## License 74 | 75 | Nest is [MIT licensed](LICENSE). 76 | --------------------------------------------------------------------------------