Create a fake REST API using json-schema-faker and json-server.
See this blog post for more details.
Install dependencies
npm i
Usage instructions
Simply run the start
script
npm run start
- runs all prepare scripts
- starts JSON REST server using generated DB and routes
start:db
starts the JSON REST server using DB loaded with the JSON DB datastart:routes
starts the JSON REST server using the generated routes and a DB loaded with the JSON DB data
prepare
generates schemas, routes and DB (json) from the Swagger API to prepare everything for the JSON REST server to work from
schemas:from-swagger
generate JSON schemas from Swagger API fileschemas:index
generate JSON schemas index file (.ts
)db:generate
generate JSON database from schemas (using faker and type definition of each schema property)routes:map
generate routes map, mapping swagget API routes to JSON REST server routesgraph:generate
generate graph of path and model (class/entity) graph for GraphViz
Generate JSON schema(s) from Swagger API yaml file in schemas
folder
npm run schemas:from-swagger
# ...
Note: You can add any Swagger 2.x compatible .yaml
or .yml
files to the /swagger
folder and each file will be used to generate schemas.
The schemas generated should have the following form:
export const listSchema = {
type: "array",
uniqueItems: true,
items: {
type: "object",
required: ["id", "name", ...],
properties: {
...
}
}
}
To generate an index
file that collects all the generated schemas into a JS object:
npm run schemas:index
The script will write a schemas/index.ts
file which can be referenced from the JS code, such as the script that generates fake data based on schema definitions and writes to DB.
export const schemas = {
affiliateProduct: require("affiliate-product.json"),
appSetting: require("app-setting.json"),
appStatus: require("app-status.json"),
autocompleteProductTag: require("autocomplete-product-tag.json"),
autocompleteUniqueId: require("autocomplete-unique-id.json"),
catalogChallengeListItem: require("catalog-challenge-list-item.json"),
catalogChallenges: require("catalog-challenges.json"),
catalogSeller: require("catalog-seller.json"),
catalogUserSearchResult: require("catalog-user-search-result.json"),
catalogVideoSearchResult: require("catalog-video-search-result.json"),
catalogVideos: require("catalog-videos.json"),
challengeTranslationPayload: require("challenge-translation-payload.json"),
challengeTranslations: require("challenge-translations.json"),
challenge: require("challenge.json")
// ...
};
npm run db:generate
Generates JSON database in db.json
Note: schema name must be pluralized, either via camelize
or dasherize
{
"affiliateProducts": [
{
"id": "6770b316-33b5-4e79-912e-471b8fddab91",
"network": "micheal.com",
"advertisingId": "80729",
"link": "sit est laboris",
"networkProductId": "54094",
"name": "Handmade Soft Bacon",
"description": "culpa ut rem",
"isInStock": false,
"imageUrl": "http://lorempixel.com/640/480",
"isActive": false,
"createdAt": "Mon Jun 11 2018 21:59:31 GMT+0200 (CEST)",
"globalProductIds": {},
"price": {}
}
]
}
Generate a routes.json
mapping file with custom routes, based on the Swagger API basePath:
entry concatenated with each path (key) entry under paths:
npm run routes:map
Routes with params such as '/v1/affiliate-products/{productId}'
will be translated to the form :<name>
such as /v1/affiliate-products/:productId
{
"/v1/affiliate-products": "/affiliateProducts",
"/v1/affiliate-products/:productId": "/affiliateProducts/:productId"
// ...
"/v2/purchase-requests": "/purchaseRequests",
"/v2/videos": "/paginatedVideoLists",
"/v2/videos/:videoId": "/videos/:videoId"
}
npm run prestart
The generator generates a JSON database in /server/db.json
You can start the JSON REST server using different configurations
Serve mock data from REST routes
npm run start:db
The JSON server serves the generated JSON database on a REST server on port 3000 by default
json-server --watch server/db.json --port 3000
To run server with custom routes
npm run start
Starts JSON server using custom routes defined in server/routes.json
GraphViz can be used with a CLI
You can render the GraphViz output via a webserver, using viz.js For a sample GraphViz rendering, see viz-js.com
To start the server
$ cd server/viz
$ yarn install
$ yarn build
# ...
$ graph:generate
will generated a new swagger-graph.gv
file in /server/viz
that is rendered by the web server.
The GraphViz output currently looks something like this:
digraph {
/affiliate-products -> AffiliateProductList [ bold ]
AffiliateProductList -> *PaginatedList [ filled ]
/affiliate-products/{productId} -> AffiliateProduct [ bold ]
/affiliate-products/{productId}/image -> AffiliateProduct [ bold ]
/auth/signup -> Auth [ bold ]
/auth -> Auth [ bold ]
/passwordReset -> Auth [ bold ]
/fbAuth -> fbAuth [ bold ]
/autocomplete/uniqueId -> autocompleteUniqueId [ bold ]
/autocomplete/productTag -> autocompleteProductTag [ bold ]
...
CatalogChallengeListItem -> *Product (associatedProducts) [ filled ]
Challenge -> *Product (associatedProducts) [ filled ]
Edit the toString()
method of the Graph
class in graph.ts
to change the GraphViz rendering.
Use pikturr to generate an UML diagram from a Swagger API spec
swagger-graph is an other Swagger graphing approach
We could perhaps let the JSF resolve the schemas when generating the DB
jsf.resolve(schema).then(() => {
// ...
});
We should add support for Open API 3.x
The following hard-coded url data is used for the video.json
and videos.json
schemas
{
"mediaUrl":
"https://d9w0wfiu0u1pg.cloudfront.net/bOFe2AHEOs/videos/DvyCq4rGZu/video-DvyCq4rGZu-568.m3u8",
"sourceMediaUrl":
"https://d9w0wfiu0u1pg.cloudfront.net/bOFe2AHEOs/videos/DvyCq4rGZu/74703ca4-6cf3-478e-9195-0bf8c74bd649.mp4",
"altMediaUrl":
"https://d9w0wfiu0u1pg.cloudfront.net/bOFe2AHEOs/videos/DvyCq4rGZu/video-DvyCq4rGZu-568-dash.mpd",
"posterImageUrl":
"https://d9w0wfiu0u1pg.cloudfront.net/bOFe2AHEOs/products/tFlhXqdVBd/2c54f5b7-0fe3-46fc-b9ad-02a56d154f0b.jpg",
"socialVideoUrl":
"https://d9w0wfiu0u1pg.cloudfront.net/bOFe2AHEOs/videos/DvyCq4rGZu/user-portrait-1528968430066.mp4",
"previewUrl":
"https://d9w0wfiu0u1pg.cloudfront.net/bOFe2AHEOs/videos/DvyCq4rGZu/user-preview-1528968423559.gif",
"altPreviewUrl":
"https://d9w0wfiu0u1pg.cloudfront.net/bOFe2AHEOs/videos/DvyCq4rGZu/fast-1528968423708.mp4",
"sharingImageUrl":
"https://d9w0wfiu0u1pg.cloudfront.net/bOFe2AHEOs/videos/DvyCq4rGZu/user-shareimage-1528968495470.jpg"
}
See TODO for a list of properties that need better schema generation (in src/generate/schemas/one.ts
). Please use the TODO
file to add properties you think could have better data/schema generation.
MIT