@aggiovato/yrest 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/index.js +23 -2
- package/dist/cli/index.mjs +23 -2
- package/dist/index.js +22 -1
- package/dist/index.mjs +22 -1
- package/package.json +3 -3
package/dist/cli/index.js
CHANGED
|
@@ -109,7 +109,7 @@ function registerInit(program2) {
|
|
|
109
109
|
}
|
|
110
110
|
(0, import_node_fs.writeFileSync)(target, templates[flags.sample], "utf8");
|
|
111
111
|
console.log(`Created ${flags.file} (sample: ${flags.sample})`);
|
|
112
|
-
console.log(`Run: yrest serve ${flags.file}`);
|
|
112
|
+
console.log(`Run: npx @aggiovato/yrest serve ${flags.file}`);
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
115
|
|
|
@@ -192,6 +192,17 @@ function patchItem(storage, resource, id, body) {
|
|
|
192
192
|
storage.persist();
|
|
193
193
|
return updated;
|
|
194
194
|
}
|
|
195
|
+
function filterByQuery(items, query) {
|
|
196
|
+
const filters = Object.entries(query).filter(([key]) => !key.startsWith("_"));
|
|
197
|
+
if (filters.length === 0) return items;
|
|
198
|
+
return items.filter(
|
|
199
|
+
(item) => filters.every(([key, value]) => item[key] !== void 0 && String(item[key]) === value)
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
function paginate(items, page, limit) {
|
|
203
|
+
const start = (page - 1) * limit;
|
|
204
|
+
return items.slice(start, start + limit);
|
|
205
|
+
}
|
|
195
206
|
function deleteItem(storage, resource, id) {
|
|
196
207
|
const collection = storage.getCollection(resource) ?? [];
|
|
197
208
|
const idx = findIndexById(collection, id);
|
|
@@ -204,7 +215,17 @@ function deleteItem(storage, resource, id) {
|
|
|
204
215
|
|
|
205
216
|
// src/router/routes/collection.routes.ts
|
|
206
217
|
function registerCollectionRoutes(server, storage, resource, prefix) {
|
|
207
|
-
server.get(prefix, () =>
|
|
218
|
+
server.get(prefix, (req, reply) => {
|
|
219
|
+
const collection = storage.getCollection(resource) ?? [];
|
|
220
|
+
const filtered = filterByQuery(collection, req.query);
|
|
221
|
+
const rawPage = req.query["_page"];
|
|
222
|
+
const rawLimit = req.query["_limit"];
|
|
223
|
+
if (!rawPage && !rawLimit) return filtered;
|
|
224
|
+
const page = Math.max(1, parseInt(rawPage ?? "1", 10) || 1);
|
|
225
|
+
const limit = Math.max(1, parseInt(rawLimit ?? "10", 10) || 10);
|
|
226
|
+
reply.header("X-Total-Count", String(filtered.length));
|
|
227
|
+
return paginate(filtered, page, limit);
|
|
228
|
+
});
|
|
208
229
|
server.post(prefix, (req, reply) => {
|
|
209
230
|
const item = createItem(storage, resource, req.body);
|
|
210
231
|
return reply.status(201).send(item);
|
package/dist/cli/index.mjs
CHANGED
|
@@ -86,7 +86,7 @@ function registerInit(program2) {
|
|
|
86
86
|
}
|
|
87
87
|
writeFileSync(target, templates[flags.sample], "utf8");
|
|
88
88
|
console.log(`Created ${flags.file} (sample: ${flags.sample})`);
|
|
89
|
-
console.log(`Run: yrest serve ${flags.file}`);
|
|
89
|
+
console.log(`Run: npx @aggiovato/yrest serve ${flags.file}`);
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
92
|
|
|
@@ -169,6 +169,17 @@ function patchItem(storage, resource, id, body) {
|
|
|
169
169
|
storage.persist();
|
|
170
170
|
return updated;
|
|
171
171
|
}
|
|
172
|
+
function filterByQuery(items, query) {
|
|
173
|
+
const filters = Object.entries(query).filter(([key]) => !key.startsWith("_"));
|
|
174
|
+
if (filters.length === 0) return items;
|
|
175
|
+
return items.filter(
|
|
176
|
+
(item) => filters.every(([key, value]) => item[key] !== void 0 && String(item[key]) === value)
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
function paginate(items, page, limit) {
|
|
180
|
+
const start = (page - 1) * limit;
|
|
181
|
+
return items.slice(start, start + limit);
|
|
182
|
+
}
|
|
172
183
|
function deleteItem(storage, resource, id) {
|
|
173
184
|
const collection = storage.getCollection(resource) ?? [];
|
|
174
185
|
const idx = findIndexById(collection, id);
|
|
@@ -181,7 +192,17 @@ function deleteItem(storage, resource, id) {
|
|
|
181
192
|
|
|
182
193
|
// src/router/routes/collection.routes.ts
|
|
183
194
|
function registerCollectionRoutes(server, storage, resource, prefix) {
|
|
184
|
-
server.get(prefix, () =>
|
|
195
|
+
server.get(prefix, (req, reply) => {
|
|
196
|
+
const collection = storage.getCollection(resource) ?? [];
|
|
197
|
+
const filtered = filterByQuery(collection, req.query);
|
|
198
|
+
const rawPage = req.query["_page"];
|
|
199
|
+
const rawLimit = req.query["_limit"];
|
|
200
|
+
if (!rawPage && !rawLimit) return filtered;
|
|
201
|
+
const page = Math.max(1, parseInt(rawPage ?? "1", 10) || 1);
|
|
202
|
+
const limit = Math.max(1, parseInt(rawLimit ?? "10", 10) || 10);
|
|
203
|
+
reply.header("X-Total-Count", String(filtered.length));
|
|
204
|
+
return paginate(filtered, page, limit);
|
|
205
|
+
});
|
|
185
206
|
server.post(prefix, (req, reply) => {
|
|
186
207
|
const item = createItem(storage, resource, req.body);
|
|
187
208
|
return reply.status(201).send(item);
|
package/dist/index.js
CHANGED
|
@@ -115,6 +115,17 @@ function patchItem(storage, resource, id, body) {
|
|
|
115
115
|
storage.persist();
|
|
116
116
|
return updated;
|
|
117
117
|
}
|
|
118
|
+
function filterByQuery(items, query) {
|
|
119
|
+
const filters = Object.entries(query).filter(([key]) => !key.startsWith("_"));
|
|
120
|
+
if (filters.length === 0) return items;
|
|
121
|
+
return items.filter(
|
|
122
|
+
(item) => filters.every(([key, value]) => item[key] !== void 0 && String(item[key]) === value)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
function paginate(items, page, limit) {
|
|
126
|
+
const start = (page - 1) * limit;
|
|
127
|
+
return items.slice(start, start + limit);
|
|
128
|
+
}
|
|
118
129
|
function deleteItem(storage, resource, id) {
|
|
119
130
|
const collection = storage.getCollection(resource) ?? [];
|
|
120
131
|
const idx = findIndexById(collection, id);
|
|
@@ -127,7 +138,17 @@ function deleteItem(storage, resource, id) {
|
|
|
127
138
|
|
|
128
139
|
// src/router/routes/collection.routes.ts
|
|
129
140
|
function registerCollectionRoutes(server, storage, resource, prefix) {
|
|
130
|
-
server.get(prefix, () =>
|
|
141
|
+
server.get(prefix, (req, reply) => {
|
|
142
|
+
const collection = storage.getCollection(resource) ?? [];
|
|
143
|
+
const filtered = filterByQuery(collection, req.query);
|
|
144
|
+
const rawPage = req.query["_page"];
|
|
145
|
+
const rawLimit = req.query["_limit"];
|
|
146
|
+
if (!rawPage && !rawLimit) return filtered;
|
|
147
|
+
const page = Math.max(1, parseInt(rawPage ?? "1", 10) || 1);
|
|
148
|
+
const limit = Math.max(1, parseInt(rawLimit ?? "10", 10) || 10);
|
|
149
|
+
reply.header("X-Total-Count", String(filtered.length));
|
|
150
|
+
return paginate(filtered, page, limit);
|
|
151
|
+
});
|
|
131
152
|
server.post(prefix, (req, reply) => {
|
|
132
153
|
const item = createItem(storage, resource, req.body);
|
|
133
154
|
return reply.status(201).send(item);
|
package/dist/index.mjs
CHANGED
|
@@ -77,6 +77,17 @@ function patchItem(storage, resource, id, body) {
|
|
|
77
77
|
storage.persist();
|
|
78
78
|
return updated;
|
|
79
79
|
}
|
|
80
|
+
function filterByQuery(items, query) {
|
|
81
|
+
const filters = Object.entries(query).filter(([key]) => !key.startsWith("_"));
|
|
82
|
+
if (filters.length === 0) return items;
|
|
83
|
+
return items.filter(
|
|
84
|
+
(item) => filters.every(([key, value]) => item[key] !== void 0 && String(item[key]) === value)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
function paginate(items, page, limit) {
|
|
88
|
+
const start = (page - 1) * limit;
|
|
89
|
+
return items.slice(start, start + limit);
|
|
90
|
+
}
|
|
80
91
|
function deleteItem(storage, resource, id) {
|
|
81
92
|
const collection = storage.getCollection(resource) ?? [];
|
|
82
93
|
const idx = findIndexById(collection, id);
|
|
@@ -89,7 +100,17 @@ function deleteItem(storage, resource, id) {
|
|
|
89
100
|
|
|
90
101
|
// src/router/routes/collection.routes.ts
|
|
91
102
|
function registerCollectionRoutes(server, storage, resource, prefix) {
|
|
92
|
-
server.get(prefix, () =>
|
|
103
|
+
server.get(prefix, (req, reply) => {
|
|
104
|
+
const collection = storage.getCollection(resource) ?? [];
|
|
105
|
+
const filtered = filterByQuery(collection, req.query);
|
|
106
|
+
const rawPage = req.query["_page"];
|
|
107
|
+
const rawLimit = req.query["_limit"];
|
|
108
|
+
if (!rawPage && !rawLimit) return filtered;
|
|
109
|
+
const page = Math.max(1, parseInt(rawPage ?? "1", 10) || 1);
|
|
110
|
+
const limit = Math.max(1, parseInt(rawLimit ?? "10", 10) || 10);
|
|
111
|
+
reply.header("X-Total-Count", String(filtered.length));
|
|
112
|
+
return paginate(filtered, page, limit);
|
|
113
|
+
});
|
|
93
114
|
server.post(prefix, (req, reply) => {
|
|
94
115
|
const item = createItem(storage, resource, req.body);
|
|
95
116
|
return reply.status(201).send(item);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aggiovato/yrest",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Zero-config REST API mock server powered by a YAML file",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"yaml",
|
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
"fastify"
|
|
12
12
|
],
|
|
13
13
|
"license": "MIT",
|
|
14
|
-
"author": "",
|
|
14
|
+
"author": "Aggiovato",
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
17
|
-
"url": ""
|
|
17
|
+
"url": "https://github.com/aggiovato/yaml-rest.git"
|
|
18
18
|
},
|
|
19
19
|
"main": "./dist/index.js",
|
|
20
20
|
"module": "./dist/index.mjs",
|