Giter Club home page Giter Club logo

currency-conversion's Introduction

Currency Conversion API

Tech Stack: Node.js, Typescript, Nest.js, MongoDB

Endpoints

POST/ currency -> create currencies

DELETE/ currency/{symbol} -> remove currencies

GET/ currency-conversion -> execute conversion between various currencies

Requirements

  • Node v16.13.2 or higher
  • Docker and docker compose

Run Application Locally

$ git clone
$ npm install
$ docker compose up mongodb

- this will start the database on local container
- use your preferred DBMS (ex.: MongoDB Compass) or command line to create a database called "currencies". It is not necessary to create any collection, this will happen automatically on application start.
-> create a .env file on project root. Copy values from .env.example file. The value for CONVERSION_API_KEY will be sent by email.
$ npm run start:dev

API Docs

[Swagger] http://localhost:300/api-docs

Tests

# unit tests
$ npm run test

# test coverage
$ npm run test:cov

Linter and formatting

# find problems
$ npm run lint

# find and fix problems automatically
$ npm run lint:fix

# format code
$ npm run format

Architecture design

  1. The endpoints are organized following Nest.js standard structure of Modules, Controllers and Services. The entry point module is the app.module that imports the other needed modules to make the endpoints available. This structure wraps all the endpoint dependencies inside a module and applies the singleton design pattern when creating objects (done by nest.js in the background).

  2. The src/core folder contains shareable resources that are used throughout the application and are not related to a specific functional requirement.

Error handling and logging

Errors are handled by a global nest interceptor created in src/core/request-interceptor/request.interceptor.ts and configured in src/main.ts. This interceptor catches any error thrown in the application and makes sure that is going to be returned in a standard format.

This same interceptor is also responsible for logging operation start, end and error with relevant data.

Input validation

All the input typings carry its own validations using class-validator decorators. The validations are triggered by a global nest.js validation pipe created in src/core/api-validation-pipe.ts and configured in src/main.

Folder Structure

The API design follows the basic Nest.js structure with modules, controllers and services. Clients are used to interact with external APIs. Nest.js interceptors are used to monitor the request flow, catch errors and log operations. Nest.js validation pipes are used to validate input to API endpoints.

This project was created from a boilerplate, made by the same author: hpsmatheus. The boilerplate contains error handling & logging structure, input validation structure, swagger config, linting config, pre-commit actions, basic test mocks and project configurations in general.

> src
  > clients                         (clients to interact with external APIs)
  > core                            (files that are used all over the API)
     > error                        (files to do the error handling and format API errors)
     > request-interceptor          (Nest.js interceptor to catch errors and do API logging)
     > api-validation.pipe.ts       (Nest.js pipe to validate DTOs)
     > swagger-response.ts          (abstraction to add and reuse swagger responses)

  > modules                         (the modules of the application separated by domain, ex.: currency)


  > typings                         (contains all the API typings)
  > main.ts                         (application entrypoint)

> test
  > integration                     (integration tests mocking db and external APIs)
  > mocks                           (mocks used all over the tests)
  > unit                            (unit tests)

Currency Conversion

This project uses CryptoCompare public API to execute conversions between currencies. It is necessary to have an API Key to perform operations against this api. This key will be send by e-mail and should be placed under the CONVERSION_API_KEY environment variable in the .env file.

Conversions can't be executed if the currency is not registered in the application, even if it's a real and valid currency.

Conversions with fictious currencies use a conversion to USD as an intermediate, for that it's necessary to fill quotationUSDToCurrency and quotationCurrencyToUSD fields when creating a new currency. The quotation values will be use in the calculation to find the corresponding value in the destination currency. In the feature this values can be automatically updated in the database.

currency-conversion's People

Contributors

hps-matheus avatar hpsmatheus avatar

Stargazers

automator hurb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.