An open API service indexing awesome lists of open source software.

https://github.com/dimpiax/sh-market-api

Second-hand market API
https://github.com/dimpiax/sh-market-api

babel eslint flow nodejs

Last synced: 2 months ago
JSON representation

Second-hand market API

Awesome Lists containing this project

README

          

# Second-hand market API
#### prototype

Second-hand goods market API.

API has next endpoints:
* register
* auth
* tags
* advertisements

## Running
After project cloning and installing dependencies, you have to run server by:
`npm start`.

In **development** environment doesn't use cluster mode.

In **production** environment, use only **builded** package. You need to installed **pm2** in your work environment and run `pm2 start index.js -i 0` script. It will start server in cluster mode, using available CPUs.

## Mongo Shell script
MongoDB shell version must be >= 3.4.2

Go to `scripts/database/mongodb/` directory and execute there the command `mongo install_bd.js`.

## Error messages
System has feature to humanize error messages, there is hardcoded default messages model, it uses if no-one translation found. If error is not described anywhere, it has format `region:type`.

External document located at `assets/docs/errorMessages.json`, which has strict structure to translate errors relatively to language. If defined language at `user` or at query is absent, system uses `en` as default.

Here is something special to know: messages are support template literals. I.e.:
`Email (${email}) or password (${passwd}) are wrong.`, where ${key} - is passed value through error.

## Example
Request examples in curl.

Query:

+ lang: error message language priority
+ token: for `adverts` request

### register
###### Register new user
```curl
curl -XPOST http://localhost:3000/apiv1/register?lang=es -H 'Content-Type: application/json' -d '{ "name": "dimpiax", "email": "dimpiax@gmail.com", "passwd": "root", "lang": "UA" }'
```

### auth
###### Auth user
Returns `AUTH_TOKEN`
```curl
curl -XPOST http://localhost:3000/apiv1/auth -H 'Content-Type: application/json' -d '{ "email": "dimpiax@gmail.com", "passwd": "root" }'
```

### tags
###### Get existed unique tags
```curl
curl -XGET http://localhost:3000/apiv1/adverts/tags?lang=es\&token=AUTH_TOKEN
```

### advertisements
###### Get advertisements
Query has additional keys:

Documents includes in response that satisfy conditions:

+ name *(*string*)*: starts by given string
+ price *(*string | double*)*: partly or whole range for `price` property
+ toSell *(*boolean*)*: advertisement type
+ tag *(*string*)*: value that exist in `tags` array property
+ limit *(*number*)*: limit documents count in response
+ start *(*number*)*: show page relatively to limit count. I.e.: start is **5** and limit is **10**, documents will be shown from *50* index.

Documents can be sorted:

+ sort *(*string*)*: ascending sorting by set specific property

#### Requests:

+ paging, sorting, price interval, tag
```curl
curl -XGET http://localhost:3000/apiv1/adverts?token=AUTH_TOKEN\&start=0\&limit=2\&sort=toSell\&price=10-230.15\&tag=motor
```
+ advertisement type
```curl
curl -XGET http://localhost:3000/apiv1/adverts?token=AUTH_TOKEN\&toSell=false
```

### Technical information
Project implemented on JavaScript, using the latest ES features, dated on the latest commit.

Its development environment based on: Babel, Flow, ESLint.