Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ctuanle/json-serve
A tool that allows you to create a testing api within less than a minute by serving a json file.
https://github.com/ctuanle/json-serve
fake-api json npm-package rest-api typescript
Last synced: about 2 months ago
JSON representation
A tool that allows you to create a testing api within less than a minute by serving a json file.
- Host: GitHub
- URL: https://github.com/ctuanle/json-serve
- Owner: ctuanle
- Created: 2022-07-09T21:58:53.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-10-24T08:40:58.000Z (about 2 years ago)
- Last Synced: 2023-10-11T19:46:03.160Z (about 1 year ago)
- Topics: fake-api, json, npm-package, rest-api, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@ctuanle/json-serve
- Size: 273 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## JSON-Serve
Lightweight, simple yet fast and useful tool that help you create a fake rest-api for your frontend by serving a json file.
## Installation
```shell
yarn add @ctuanle/json-serve --dev
```or
```shell
npm install @ctuanle/json-serve --save-dev
```## Demo
A [simple demo](./demo) on [fly.io](https://fly.io/). Here is the base api of my demo:
```shell
https://jss-demo.fly.dev/
```## Usage
```shell
yarn jss [json-path] [port] [other-options]
```| Options | Required | Default | Description |
| :---------- | :------: | :-------: | :-------------------------- |
| json-path | no | data.json | Path to your json file |
| port | no | 3000 | Port on which server run |
| --no-strict | no | false | Turn on no-strict mode |
| --readonly | no | false | Turn on readonly mode |
| --persist | no | false | Turn on save-change-to-disk |### Available methods
- GET
- POST
- PUT
- DELETE
- OPTIONS### No-strict mode
By default, you can only post/put/delete to array data. But in no-strict mode, these action are allowed with object type (key-value).
### Read-only mode
In this mode, only GET requests are allowed. Default is false.
### Persist
Save changes created by POST/PUT/DELETE to your json file. Default is false, so changes are kept only on memory and will be deleted when you turn server off.
### Example
You've create a data.json file:
```shell
yarn jss data.json 3000
```Or if you're too lazy and you don't specify a path, a prompt will appear and ask you if you want to create one with pre-defined data for you:
```shell
yarn jss
```You want to serve your json file and persist changes:
```shell
yarn jss data.json 3000 --persist
```## Details
If your json file contains this content:
```json
{
"method": [
{
"name": "GET"
},
{
"name": "POST"
}
],
"protocol": {
"1": "HTTP",
"2": "UDP"
}
}
```All available routes are:
```ts
/method
/method/[index]
/protocol
/protocol/[index]
```### GET
To get "protocol", you can go with `GET /protocol`.
Or to get all methods, go with `GET /method`.
Plus, with array data, you can filter it with query, for example, to get all method that name is "GET", `GET /method?name=GET`
### POST
With post request, you can update your json file.
If the target resources is an array, received data will be pushed into the array.
Ex: `POST /method` with body `{"name": "PUT"}` will turn above data into
```json
{
"method": [
{
"name": "GET"
},
{
"name": "POST"
},
{
"name": "PUT"
}
],
"protocol": {
"1": "HTTP",
"2": "UDP"
}
}
```Please note that if you edit json file manually while the server is running, edited data won't be seen by the server. In that case, restart the server.
### PUT
`PUT /protocol/0` with body `{"name": "PATCH"}` and `PUT /protocol/2` with body `"TCP"` will turn above data into:
```json
{
"method": [
{
"name": "PATCH"
},
{
"name": "POST"
},
{
"name": "PUT"
}
],
"protocol": {
"1": "HTTP",
"2": "TCP"
}
}
```#### DELETE
With DELETE requests, you can delete a specific data.
`DELETE /method/2` and `DELETE /protocol/2` will turn above data into:
```json
{
"method": [
{
"name": "PATCH"
},
{
"name": "POST"
},
null
],
"protocol": {
"1": "HTTP",
"2": null
}
}
```## Screenshots
Colorful console logging:
![Logging Console](./public/console.png 'Logging console')
Sample response:
![Server response](./public/sample.png 'Sample response')