A backend API service to get the countries details using RestCountries API.
- User signup and signin for authentication to access APIs.
- API authentication using JWT token.
- Global Error handling middleware.
- Custom Error handlers for different use cases.
# Clone the repository
git clone https://github.com/neerajkumar161/rest-countries
# Change to the project directory
cd rest-countries
# Install dependencies, use
pnpm install
To run the application, use node v18.16.1
, pnpm v8.7.5
, and typescript v5.2.2
. You can check the versions in package.json
file as well.
# To compile the typescript files, make sure there should be no error in compile time
npm run compile
On second terminal, while first opened
# Start Node.js server for listening requests
npm start
The following section lists and describes the available API endpoints, including details about request methods, request payloads, and response formats.
{
"username": "neerajkumar",
"password": "testpassword"
}
{
"message": "User registered!",
"data": null,
"code": 200
}
{
"errors":[
{
"message":"Username already exists! Try another one!"
}
]
}
curl -X POST http://localhost:5500/user/signup -H "Content-Type: application/json" -d '{
"username": "neerajkumar",
"password": "tester"
}'
{
"username": "neerajkumar",
"password": "testpassword"
}
{
"message": "User signed in succesfully!",
"data": {
"username": "neerajkumar",
"token": "user-generated-token"
},
"code": 200
}
{
"errors":[
{
"message":"Invalid credentials!"
}
]
}
curl -X POST http://localhost:5500/user/signin -H "Content-Type: application/json" -d '{
"username": "neerajkumar",
"password": "tester"
}'
Authorization: auth-token // user auth token received in signin api
{
"message": "Country Details fetched!",
"data": [
{
"name": {},
"currencies": {},
"region": "",
"otherKeys": "values"
}
],
"code": 200
}
{
"errors":[
{
"message":"Invalid auth token!" // or "Auth token is required!"
}
]
}
curl -X GET http://localhost:5500/countries/india -H "Authorization: auth-token"
-
Note: Since, population and area will return single country, therefore sorting and pagination will not work on these two filters, use only language filter to use pagination. if no filter is passed, it will retreive all countries with pagination
- language: hin | eng | tam | jpa // use Alpha -3 codes here is the list.
- population: 1380004385 // population of the country
- area: 3287590.0 // area of the country
-
- sortBy: population | area
- orderBy: asc | desc
- pageSize: number
- pageNumber: number
Authorization: auth-token // user auth token received in signin api
{
"message": "All Countries fetched",
"data": {
"countries": {
"name": {},
"currencies": {},
"region": "",
"otherKeys": "values"
},
"currentPage": 1,
"totalPages": 25,
"totalItems": 250
},
"code": 200
}
{
"errors":[
{
"message":"Invalide auth token!" // or "Auth token is required!"
}
]
}
curl -X GET 'http://localhost:5500/countries?language=hin&sortBy=area&orderBy=asc&pageSize=10&pageNumber=1' -H "Authorization: auth-token"
- When some error occured in server
{
"message": "Something went wrong!"
}
- When Request Body, Params and Quey validation fails
[
{"message":"Validation error!",
"fields":
{
"key": "string[]"
}
}
]
This project is licensed under the MIT License. See the LICENSE file for details.