<repo_name>Volodymyr-Roiukk/rs-pet-shop-backend<file_sep>/product-service/constants.js
const PRODUCTS_API_PATH = 'http://products-test1.s3-website-eu-west-1.amazonaws.com/';

module.exports = {PRODUCTS_API_PATH};<file_sep>/product-service/tests/getProductsList.test.js
const {getProductsList} = require('../handlers/getProductsList');

const defaultProducts = [
  {
    id: 1,
    title: "Royal Canin Sterilised",
    description: "The best premium food for your cats",
    price: 19.99
  },
  {
    id: 2,
    title: "Royal Canin British Shorthair",
    description: "The best premium food for your cats",
    price: 14.99
  },
  {
    id: 3,
    title: "Royal Canin Sensible 33",
    description: "The best premium food for your cats",
    price: 9.99
  },
  {
    id: 4,
    title: "Royal Canin Kitten",
    description: "The best premium food for your cats",
    price: 9.99
  },
  {
    id: 5,
    title: "Royal Canin Indoor 27",
    description: "The best premium food for your cats",
    price: 19.99
  },
  {
    id: 6,
    title: "Royal Canin Hair & Skin Care",
    description: "The best premium food for your cats",
    price: 19.99
  },
  {
    id: 7,
    title: "Royal Canin Urinary Care",
    description: "The best premium food for your cats",
    price: 14.99
  },
  {
    id: 8,
    title: "Royal Canin Babycat",
    description: "The best premium food for your cats",
    price: 14.99
  },
  {
    id: 9,
    title: "Royal Canin Hairball Care",
    description: "The best premium food for your cats",
    price: 19.99
  },
  {
    id: 10,
    title: "Royal Canin Exigent Savour Sensation",
    description: "The best premium food for your cats",
    price: 20.99
  }
];

describe('getProductsList handler tests', () => {
  test('getProductsList should return response with status code 200', async () => {
    const {statusCode} = await getProductsList();
    
    expect(statusCode).toBe(200);
  });

  test('getProductsList should return response with products list', async () => {
    const {body} = await getProductsList();
    const products = JSON.parse(body);
    
    expect(products).toEqual(defaultProducts);
  });
});<file_sep>/README.md
# rs-pet-shop-backend

## Task-3

### [getProductsList](https://7hirlqmvvi.execute-api.eu-west-1.amazonaws.com/dev/products) - Get all products.
### [getProductsById](https://7hirlqmvvi.execute-api.eu-west-1.amazonaws.com/dev/products/1) - Get products by ID.
### [Frontend App](https://d1k2kpbj5227x1.cloudfront.net) - Here you can see that my FE app is synchronized with my API.

### [Frontend PR](https://github.com/Volodymyr-Roiukk/rs-pet-shop-frontend/pull/2)
### [Backend PR](https://github.com/Volodymyr-Roiukk/rs-pet-shop-backend/pull/1)

#### Service was done and Frontend synchronized with my API
#### Optional scope: All done without SWAGGER

#### Product properties:
##### - Id
##### - title
##### - description
##### - price
<file_sep>/product-service/index.js
import {getProductsList} from './handlers/getProductsList'
import {getProductsById} from './handlers/getProductsById'

export {getProductsList, getProductsById};
<file_sep>/product-service/webpack.config.js
const slsw = require('serverless-webpack');

module.exports = {
  entry: './index.js',
  target: 'node',
  entry: slsw.lib.entries
};
<file_sep>/product-service/handlers/getProductsList.js
import fetch from 'node-fetch';
import {PRODUCTS_API_PATH} from '../constants'

export const getProductsList = async () => {
  try {
    const response = await fetch(PRODUCTS_API_PATH);
    const products = await response.json();

    return {
      statusCode: 200,
      headers: {
        "Access-Control-Allow-Origin" : "*", // Required for CORS support to work
        "Access-Control-Allow-Credentials" : true 
      },
      body: JSON.stringify(products),
    }
  } catch (e) {
    console.error(`Error: ${e}`);
    
    return {
      statusCode: 404,
      headers: {
        "Access-Control-Allow-Origin" : "*",
        "Access-Control-Allow-Credentials" : true 
      },
      body: JSON.stringify({message: `Something went wrong: ${e.message}`}),
    }
  }
};<file_sep>/product-service/tests/getProductsById.test.js
const {getProductsById} = require('../handlers/getProductsById');

describe('getProductsById handler tests', () => {
  test('getProductsById should return response with status code 200', async () => {
    const event = {pathParameters: {productId: 2}};
    const {statusCode} = await getProductsById(event);
    
    expect(statusCode).toBe(200);
  });

  test('getProductsById should return response with only one product array', async () => {
    const event = {pathParameters: {productId: 7}};
    const {body} = await getProductsById(event);
    const products = JSON.parse(body);
    
    expect(products.length).toBe(1);
  });

  test('getProductsById should return response with status code 404 if product not found', async () => {
    const event = {pathParameters: {productId: -1}};
    const {statusCode} = await getProductsById(event);
    
    expect(statusCode).toBe(404);
  });

  test('getProductsById should return response with error message "Product not found"', async () => {
    const expError = {message: "Product is not found"};
    const event = {pathParameters: {productId: -1}};
    const {body} = await getProductsById(event);
    const error = JSON.parse(body);
    
    expect(error).toEqual(expError);
  });
});