Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/graphql-editor/ts-api-faker

Micro api faker written in TypeScript which returns faked data from JSON queries
https://github.com/graphql-editor/ts-api-faker

api fake microservice node rest server typescript

Last synced: about 1 month ago
JSON representation

Micro api faker written in TypeScript which returns faked data from JSON queries

Awesome Lists containing this project

README

        

![](img/logo.jpg)

## What is it?

Minimal fake API server based on faker.js and unsplash.com

## Why should you use it?

To fake backend before its made and start frontend development just after project planning.

## How it works?

![](img/fakerserver.gif)

## How to use it?

After making post request with json argument

```json
[
{
"name": "name.firstName",
"surname": "name.lastName",
"mail": "internet.email",
"profilePhoto": "internet.avatar",
"animalPhoto": "image.dog"
},
"@repeat:1"
]
```

it returns

```json
[
{
"animalPhoto": "https://source.unsplash.com/200x200/?cat",
"profilePhoto": "https://s3.amazonaws.com/uifaces/faces/twitter/gu5taf/128.jpg",
"mail": "[email protected]",
"surname": "Schaefer",
"name": "Madelynn"
},
{
"animalPhoto": "https://source.unsplash.com/200x200/?dog",
"profilePhoto": "https://s3.amazonaws.com/uifaces/faces/twitter/Elt_n/128.jpg",
"mail": "[email protected]",
"surname": "Herzog",
"name": "Lempi"
}
]
```

Ant these 2 photos looks like

![](https://source.unsplash.com/200x200/?cat)

![](https://source.unsplash.com/200x200/?dog)

### Directives are also available for use:
`@key` - use the name from the key to generate a value from, usage:
```
{
"name": "@key"
}

returns, ex:

{
"name": ""
}
```
`@repeat` - only for lists, replicates the object in a specified number of times, usage:
```
[
{
"name": "name.firstName"
},
"@repeat:1"
]

returns, ex:
[
{
"name": "Alice"
},
{
"name": "Dennor"
}
]
```
`@static` - determines that the generated value is consistent for all objects, usage:
```
[
{
"name": "@static:name.firstName", (or name.firstname@static)
"surname": "name.lastName"
},
"@repeat:1"
]

returns, ex:
[
{
"name": "Connor"
"surname": "Json"
},
{
"name": "Connor",
"surname": "Kowalsky"
}
]
```
`@settings` - generation settings.
- data - source of static data for `@data` directive
- definitions - reusable fake definitions for `@use` direective
- root - if `true`, value under rootValue is treated as output root

`@data` - directive for static data

`@use` - takes reusable definitions from `@settings

complete example:
```
{
"@settings": {
"data": {
"name": "Michal"
},
"definitions": {
"photo": {
"dateTaken": "@static:date.pas",
"url": "image.girl.640.480",
"placeLat": "addrss.latitu",
"placeLong": "address.longitude"
}
},
"root": true
},
"out": [
{
"commerce": "@key",
"name": "@data:name",
"address": {
"street": "address.streetName",
"city": "adess.secondyAddss"
},
"date": "date.past",
"avatar": "shpe.circ",
"photos": [
"1@repeat,@use:photo"
]
},
"@repeat:1"
]
}

returns, ex:

[
{
"commerce": "Generic Steel Ball",
"name": "Michal",
"address": {
"street": "Bins Street",
"city": "Apt. 373"
},
"date": "2020-02-04T12:36:22.679Z",
"avatar": "",
"photos": [
{
"dateTaken": "2016-01-06T18:53:55.060Z",
"url": "https://source.unsplash.com/640x480/?girl",
"placeLat": "33.2601",
"placeLong": "144.7978"
},
{
"dateTaken": "2016-01-06T18:53:55.060Z",
"url": "https://source.unsplash.com/640x480/?girl",
"placeLat": "1.3206",
"placeLong": "128.1190"
}
]
},
{
"commerce": "604.00",
"name": "Michal",
"address": {
"street": "Ophelia Manors",
"city": "Apt. 499"
},
"date": "2019-11-15T08:59:17.824Z",
"avatar": "",
"photos": [
{
"dateTaken": "2016-01-06T18:53:55.060Z",
"url": "https://source.unsplash.com/640x480/?girl",
"placeLat": "-83.2634",
"placeLong": "-36.3521"
},
{
"dateTaken": "2016-01-06T18:53:55.060Z",
"url": "https://source.unsplash.com/640x480/?girl",
"placeLat": "22.2511",
"placeLong": "2.0486"
}
]
}
]
```

### Some examples of arguments to JSON

| Key | Result |
| --- | :---: |
| address.zipCode | 73318 |
| commerce.department | Toys |
| company.companyName | Grant Group |
| database.type | text |
| date.future | 2016-12-12T14:50:49.361Z |
| lorem.words | itaque nostrum molestiae |

Learn more about arguments that can be passed in JSON in [Faker.js Repo](https://github.com/marak/Faker.js/).

## Installation

Clone this repo and run

```
npm install
npm run build
```

## Run development server

```
npm run dev
```

## Run production server

```
npm run start
```

or using docker image

```
docker run -d -p 3000:3000 slothking/ts-api-faker
```

## Build

```
npm run build
```

## Contribute

Feel free to contact us and contribute. [email protected]

1. fork this repo
2. Create your feature branch: git checkout -b feature-name
3. Commit your changes: git commit -am 'Add some feature'
4. Push to the branch: git push origin my-new-feature
5. Submit a pull request