35 |
36 | 37 | json-server-extensionjson-server is great for stub server usage 38 | but in my opinion there where some caveat that i tried to solve in this package
39 | 40 |41 |
42 | 43 | so what this package gives you-
44 |
- [x] splitting to static files - json-server can serve only single file but in medium/large applications it not ideal, by using this package you can split your json object to files 45 |
- [x] dynamic generation - with json server you can generate the whole file 46 | now you can create multiple generated objects decoupled each other and even combine 47 | static and generated files 48 |
51 |
52 | 53 | Examplefull example can be found here https://github.com/maty21/json-server-extension-example
54 | 55 |56 |
57 | 58 | init exampleconst jsonServer = require('json-server');
59 | const _jsonExtender = require('./jsonExtender');
60 |
61 | //options:
62 | //fullPath:fullpath for the combined object
63 | //generatedPath:the path where the generated files will be found
64 | //staticPath:the path where the static files will be found
65 | const jsonExtender = new _jsonExtender({filePath:'./db_extends.json',
66 | generatedPath:'./generated',
67 | staticPath:'./static'})
68 |
69 | //register accept array of generators or path to the generator scripts
70 | //const funcs = Object.keys(generators).map(key => generators[key])
71 | jsonExtender.register('../../../generators');
72 | jsonExtender.generate().then((data)=>{
73 | console.log(`wow ${data}`);
74 | var server = jsonServer.create()
75 | var router = jsonServer.router('./db_extends.json')
76 | var middlewares = jsonServer.defaults()
77 |
78 | server.use(middlewares)
79 | server.use(router)
80 | server.listen(4000, function () {
81 | console.log('JSON Server is running')
82 | }).catch((err) => {console.log(err)})
83 |
84 | });
87 |
88 | 89 | generator Exampleconst amount = 100;
90 | const func =next =>create => {
91 | const path = `feed/feedList.json`;
92 | const data = (amount)=> {
93 | let temp = [];
94 | for (let i = 0; i < amount; i++) {
95 | temp.push({
96 | id: `${i}N12134`,
97 | newNotificationCount: i * 3,
98 | isRead: (i % 2 == 0),
99 | isStarMark: (i % 4 == 0),
100 | iconType: "SocialNotifications",
101 | description: i + ": this is a new feed ",
102 | date: new Date(Date.now()).toLocaleString()
103 | }
104 | )
105 | }
106 | return temp;
107 | }
108 | create({data: {feed: data(amount)}, path: path})
109 | next(create);
110 |
111 | }
112 | module.exports = func;
115 |
116 | 117 | api118 |
119 | 120 | constructorconstructor({filePath:'string',generatedPath:'string, staticPath:'string'})
-
123 |
-
124 |
fullPath
- fullpath for the combined object
125 | -
126 |
generatedPath
- the path where the generated files will be founddefault 127 | : './generated'
128 |
129 | -
130 |
staticPath
- the path where the static files will be founddefault 131 | : './static'
132 |
133 |
136 |
137 | 138 | registerregister('path name') / register([...generator scripts])
-
141 |
-
142 |
register('path name')
- a path where the generators scripts will be found the package will insatiate the scripts automatically
143 | -
144 |
register([...generator scripts])
-array of your generators after requiring them manually
145 |
148 |
149 | 150 | generategenerate(isRun[default:true]) return promise
-
153 |
-
154 |
isRun
- there is ability to not generate the db.json each time good when you want to save the state after you close the process the promise will recive the same data so you will not have to change the code
155 | -
156 |
promise
157 | 158 |-
159 |
-
160 |
resolve
-{files:array of combined files, filePath:the combined file path }
161 | -
162 |
reject
- error
163 |
165 | -
160 |
168 |
169 | 170 | generatorconst func= next =>create => {}
- the generator should be initiated as follows first you will have to call for create this is sync function and the for next
-
173 |
-
174 |
create({data: {feed: generatedObject}, path: path})
175 | 176 |-
177 |
-
178 |
data
- the generated data where the name of the property will be the routing name in this casefeed
179 |
180 | -
181 |
path
- a relative path under the generated path that you set in the constructor where you wish to put the output
182 |
184 | -
178 |
-
185 |
next(create)
- just pass the create function there so it's reference will be passed in the pipeline
186 |