vite-plugin-json-server 0.1.0 → 0.1.2
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/index.cjs +83 -8
- package/dist/index.d.ts +20 -15
- package/dist/index.js +83 -8
- package/package.json +18 -16
package/dist/index.cjs
CHANGED
|
@@ -35,11 +35,77 @@ __export(src_exports, {
|
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(src_exports);
|
|
37
37
|
var import_json_server = __toESM(require("json-server"), 1);
|
|
38
|
-
var
|
|
39
|
-
var
|
|
40
|
-
var import_load = __toESM(require("json-server/lib/cli/utils/load"), 1);
|
|
38
|
+
var import_path2 = require("path");
|
|
39
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
40
|
+
var import_load = __toESM(require("json-server/lib/cli/utils/load.js"), 1);
|
|
41
41
|
var import_connect_pause = __toESM(require("connect-pause"), 1);
|
|
42
|
-
|
|
42
|
+
|
|
43
|
+
// src/model.ts
|
|
44
|
+
var import_path = require("path");
|
|
45
|
+
var import_is = __toESM(require("json-server/lib/cli/utils/is.js"), 1);
|
|
46
|
+
|
|
47
|
+
// src/query.ts
|
|
48
|
+
var identity = (query) => query;
|
|
49
|
+
var applyApiV1 = (query) => {
|
|
50
|
+
const output = new URLSearchParams();
|
|
51
|
+
const filter = query.get("filter");
|
|
52
|
+
if (filter) {
|
|
53
|
+
try {
|
|
54
|
+
const attrs = JSON.parse(filter);
|
|
55
|
+
Object.entries(attrs).forEach(([attr, value]) => {
|
|
56
|
+
output.set(attr, String(value));
|
|
57
|
+
});
|
|
58
|
+
} catch (e) {
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const pager = query.get("pager");
|
|
62
|
+
if (pager) {
|
|
63
|
+
try {
|
|
64
|
+
const [page, perPage] = JSON.parse(pager);
|
|
65
|
+
if (page !== void 0)
|
|
66
|
+
output.set("_page", String(page));
|
|
67
|
+
if (perPage !== void 0)
|
|
68
|
+
output.set("_limit", String(perPage));
|
|
69
|
+
} catch (e) {
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const sort = query.get("sort");
|
|
73
|
+
if (sort) {
|
|
74
|
+
try {
|
|
75
|
+
const columns = JSON.parse(sort);
|
|
76
|
+
if (columns.length) {
|
|
77
|
+
output.set("_sort", columns.map(([col]) => col).join(","));
|
|
78
|
+
output.set("_order", columns.map(([, order]) => order).join(","));
|
|
79
|
+
}
|
|
80
|
+
} catch (e) {
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
query.forEach((value, key) => {
|
|
84
|
+
if (!["filter", "pager", "sort"].includes(key)) {
|
|
85
|
+
output.set(key, value);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return output;
|
|
89
|
+
};
|
|
90
|
+
var resolveQueryMode = (mode) => {
|
|
91
|
+
if (mode === "default")
|
|
92
|
+
return identity;
|
|
93
|
+
if (mode === "apiV1")
|
|
94
|
+
return applyApiV1;
|
|
95
|
+
return mode;
|
|
96
|
+
};
|
|
97
|
+
var createQueryAdapter = (transform) => (req, _res, next) => {
|
|
98
|
+
var _a;
|
|
99
|
+
const [path, search = ""] = ((_a = req.url) != null ? _a : "").split("?");
|
|
100
|
+
const output = transform(new URLSearchParams(search));
|
|
101
|
+
const query = output.toString();
|
|
102
|
+
req.url = query ? `${path}?${query}` : path;
|
|
103
|
+
req.query = Object.fromEntries(output.entries());
|
|
104
|
+
next();
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// src/model.ts
|
|
108
|
+
var ensureConfig = (pluginOptions) => {
|
|
43
109
|
const {
|
|
44
110
|
apiPath = "/api",
|
|
45
111
|
profile = "",
|
|
@@ -56,7 +122,8 @@ var assertPluginOptions = (pluginOptions) => {
|
|
|
56
122
|
delay = 10,
|
|
57
123
|
id = "_id",
|
|
58
124
|
foreignKeySuffix = "_id",
|
|
59
|
-
quiet = false
|
|
125
|
+
quiet = false,
|
|
126
|
+
queryMode = "default"
|
|
60
127
|
} = pluginOptions;
|
|
61
128
|
return {
|
|
62
129
|
apiPath,
|
|
@@ -74,11 +141,14 @@ var assertPluginOptions = (pluginOptions) => {
|
|
|
74
141
|
delay,
|
|
75
142
|
id,
|
|
76
143
|
foreignKeySuffix,
|
|
77
|
-
quiet
|
|
144
|
+
quiet,
|
|
145
|
+
queryMode: resolveQueryMode(queryMode)
|
|
78
146
|
};
|
|
79
147
|
};
|
|
148
|
+
|
|
149
|
+
// src/index.ts
|
|
80
150
|
var pluginJsonServer = (userOptions = {}) => {
|
|
81
|
-
const opts =
|
|
151
|
+
const opts = ensureConfig(userOptions);
|
|
82
152
|
const createServer = async (config) => {
|
|
83
153
|
const { root, logger } = config;
|
|
84
154
|
try {
|
|
@@ -87,7 +157,7 @@ var pluginJsonServer = (userOptions = {}) => {
|
|
|
87
157
|
foreignKeySuffix: opts.foreignKeySuffix
|
|
88
158
|
};
|
|
89
159
|
const defaultsOpts = {
|
|
90
|
-
static: (0,
|
|
160
|
+
static: (0, import_path2.join)(root, opts.static),
|
|
91
161
|
logger: !opts.quiet,
|
|
92
162
|
readOnly: opts.readOnly,
|
|
93
163
|
noCors: opts.noCors,
|
|
@@ -104,6 +174,7 @@ var pluginJsonServer = (userOptions = {}) => {
|
|
|
104
174
|
logger.info(" Delay: " + opts.delay);
|
|
105
175
|
app.use((0, import_connect_pause.default)(opts.delay));
|
|
106
176
|
}
|
|
177
|
+
app.use(createQueryAdapter(opts.queryMode));
|
|
107
178
|
app.use(router);
|
|
108
179
|
return app;
|
|
109
180
|
} catch (error) {
|
|
@@ -115,6 +186,10 @@ var pluginJsonServer = (userOptions = {}) => {
|
|
|
115
186
|
name: "vite-plugin-json-server",
|
|
116
187
|
configureServer: async (devServer) => {
|
|
117
188
|
const { config, watcher } = devServer;
|
|
189
|
+
const dir = (0, import_path2.join)(config.root, opts.profile);
|
|
190
|
+
if (!import_fs.default.existsSync(dir)) {
|
|
191
|
+
import_fs.default.mkdirSync(dir, { recursive: true });
|
|
192
|
+
}
|
|
118
193
|
let app = await createServer(config);
|
|
119
194
|
if (!opts.unwatch) {
|
|
120
195
|
const files = [opts.source];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
2
|
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
3
|
+
type QueryTransform = (query: URLSearchParams) => URLSearchParams;
|
|
4
|
+
type QueryMode = "default" | "apiV1" | QueryTransform;
|
|
5
|
+
|
|
6
|
+
type PluginOptions = {
|
|
7
|
+
source: string;
|
|
8
|
+
apiPath: string;
|
|
9
|
+
profile: string;
|
|
10
|
+
static: string;
|
|
11
|
+
unwatch: boolean;
|
|
12
|
+
readOnly: boolean;
|
|
13
|
+
bodyParser: boolean;
|
|
14
|
+
noCors: boolean;
|
|
15
|
+
noGzip: boolean;
|
|
16
|
+
delay: number;
|
|
17
|
+
id: string;
|
|
18
|
+
foreignKeySuffix: string;
|
|
19
|
+
quiet: boolean;
|
|
20
|
+
queryMode: QueryMode;
|
|
21
|
+
};
|
|
22
|
+
|
|
18
23
|
declare const pluginJsonServer: (userOptions?: Partial<PluginOptions>) => Plugin;
|
|
19
24
|
|
|
20
25
|
export { pluginJsonServer as default, pluginJsonServer };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,76 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import jsonServer from "json-server";
|
|
3
|
-
import { join } from "path";
|
|
4
|
-
import
|
|
5
|
-
import load from "json-server/lib/cli/utils/load";
|
|
3
|
+
import { join as join2 } from "path";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import load from "json-server/lib/cli/utils/load.js";
|
|
6
6
|
import pause from "connect-pause";
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
// src/model.ts
|
|
9
|
+
import { join } from "path";
|
|
10
|
+
import is from "json-server/lib/cli/utils/is.js";
|
|
11
|
+
|
|
12
|
+
// src/query.ts
|
|
13
|
+
var identity = (query) => query;
|
|
14
|
+
var applyApiV1 = (query) => {
|
|
15
|
+
const output = new URLSearchParams();
|
|
16
|
+
const filter = query.get("filter");
|
|
17
|
+
if (filter) {
|
|
18
|
+
try {
|
|
19
|
+
const attrs = JSON.parse(filter);
|
|
20
|
+
Object.entries(attrs).forEach(([attr, value]) => {
|
|
21
|
+
output.set(attr, String(value));
|
|
22
|
+
});
|
|
23
|
+
} catch (e) {
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const pager = query.get("pager");
|
|
27
|
+
if (pager) {
|
|
28
|
+
try {
|
|
29
|
+
const [page, perPage] = JSON.parse(pager);
|
|
30
|
+
if (page !== void 0)
|
|
31
|
+
output.set("_page", String(page));
|
|
32
|
+
if (perPage !== void 0)
|
|
33
|
+
output.set("_limit", String(perPage));
|
|
34
|
+
} catch (e) {
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const sort = query.get("sort");
|
|
38
|
+
if (sort) {
|
|
39
|
+
try {
|
|
40
|
+
const columns = JSON.parse(sort);
|
|
41
|
+
if (columns.length) {
|
|
42
|
+
output.set("_sort", columns.map(([col]) => col).join(","));
|
|
43
|
+
output.set("_order", columns.map(([, order]) => order).join(","));
|
|
44
|
+
}
|
|
45
|
+
} catch (e) {
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
query.forEach((value, key) => {
|
|
49
|
+
if (!["filter", "pager", "sort"].includes(key)) {
|
|
50
|
+
output.set(key, value);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return output;
|
|
54
|
+
};
|
|
55
|
+
var resolveQueryMode = (mode) => {
|
|
56
|
+
if (mode === "default")
|
|
57
|
+
return identity;
|
|
58
|
+
if (mode === "apiV1")
|
|
59
|
+
return applyApiV1;
|
|
60
|
+
return mode;
|
|
61
|
+
};
|
|
62
|
+
var createQueryAdapter = (transform) => (req, _res, next) => {
|
|
63
|
+
var _a;
|
|
64
|
+
const [path, search = ""] = ((_a = req.url) != null ? _a : "").split("?");
|
|
65
|
+
const output = transform(new URLSearchParams(search));
|
|
66
|
+
const query = output.toString();
|
|
67
|
+
req.url = query ? `${path}?${query}` : path;
|
|
68
|
+
req.query = Object.fromEntries(output.entries());
|
|
69
|
+
next();
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// src/model.ts
|
|
73
|
+
var ensureConfig = (pluginOptions) => {
|
|
8
74
|
const {
|
|
9
75
|
apiPath = "/api",
|
|
10
76
|
profile = "",
|
|
@@ -21,7 +87,8 @@ var assertPluginOptions = (pluginOptions) => {
|
|
|
21
87
|
delay = 10,
|
|
22
88
|
id = "_id",
|
|
23
89
|
foreignKeySuffix = "_id",
|
|
24
|
-
quiet = false
|
|
90
|
+
quiet = false,
|
|
91
|
+
queryMode = "default"
|
|
25
92
|
} = pluginOptions;
|
|
26
93
|
return {
|
|
27
94
|
apiPath,
|
|
@@ -39,11 +106,14 @@ var assertPluginOptions = (pluginOptions) => {
|
|
|
39
106
|
delay,
|
|
40
107
|
id,
|
|
41
108
|
foreignKeySuffix,
|
|
42
|
-
quiet
|
|
109
|
+
quiet,
|
|
110
|
+
queryMode: resolveQueryMode(queryMode)
|
|
43
111
|
};
|
|
44
112
|
};
|
|
113
|
+
|
|
114
|
+
// src/index.ts
|
|
45
115
|
var pluginJsonServer = (userOptions = {}) => {
|
|
46
|
-
const opts =
|
|
116
|
+
const opts = ensureConfig(userOptions);
|
|
47
117
|
const createServer = async (config) => {
|
|
48
118
|
const { root, logger } = config;
|
|
49
119
|
try {
|
|
@@ -52,7 +122,7 @@ var pluginJsonServer = (userOptions = {}) => {
|
|
|
52
122
|
foreignKeySuffix: opts.foreignKeySuffix
|
|
53
123
|
};
|
|
54
124
|
const defaultsOpts = {
|
|
55
|
-
static:
|
|
125
|
+
static: join2(root, opts.static),
|
|
56
126
|
logger: !opts.quiet,
|
|
57
127
|
readOnly: opts.readOnly,
|
|
58
128
|
noCors: opts.noCors,
|
|
@@ -69,6 +139,7 @@ var pluginJsonServer = (userOptions = {}) => {
|
|
|
69
139
|
logger.info(" Delay: " + opts.delay);
|
|
70
140
|
app.use(pause(opts.delay));
|
|
71
141
|
}
|
|
142
|
+
app.use(createQueryAdapter(opts.queryMode));
|
|
72
143
|
app.use(router);
|
|
73
144
|
return app;
|
|
74
145
|
} catch (error) {
|
|
@@ -80,6 +151,10 @@ var pluginJsonServer = (userOptions = {}) => {
|
|
|
80
151
|
name: "vite-plugin-json-server",
|
|
81
152
|
configureServer: async (devServer) => {
|
|
82
153
|
const { config, watcher } = devServer;
|
|
154
|
+
const dir = join2(config.root, opts.profile);
|
|
155
|
+
if (!fs.existsSync(dir)) {
|
|
156
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
157
|
+
}
|
|
83
158
|
let app = await createServer(config);
|
|
84
159
|
if (!opts.unwatch) {
|
|
85
160
|
const files = [opts.source];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-json-server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A plugin that integrates json-server with Vite.js, providing a simple way to mock REST APIs during development.",
|
|
6
6
|
"keywords": [
|
|
@@ -21,33 +21,35 @@
|
|
|
21
21
|
"type": "git",
|
|
22
22
|
"url": "https://github.com/yracnet/vite-plugin-json-server"
|
|
23
23
|
},
|
|
24
|
-
"main": "dist/index.
|
|
25
|
-
"module": "dist/index.
|
|
26
|
-
"types": "dist/index.d.ts",
|
|
24
|
+
"main": "./dist/index.cjs",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
27
|
"exports": {
|
|
28
28
|
".": {
|
|
29
|
-
"require": "./dist/index.
|
|
30
|
-
"import": "./dist/index.
|
|
29
|
+
"require": "./dist/index.cjs",
|
|
30
|
+
"import": "./dist/index.js",
|
|
31
31
|
"types": "./dist/index.d.ts"
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
"homepage": "https://github.com/yracnet/vite-plugin-json-server",
|
|
35
35
|
"bugs": "https://github.com/yracnet/vite-plugin-json-server/issues",
|
|
36
36
|
"scripts": {
|
|
37
|
+
"dev": "tsup --watch",
|
|
37
38
|
"build": "tsup"
|
|
38
39
|
},
|
|
39
|
-
"dependencies": {
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"connect-pause": "^0.1.1",
|
|
42
|
+
"json-server": "^0.17.4"
|
|
43
|
+
},
|
|
40
44
|
"peerDependencies": {
|
|
41
|
-
"vite": "^4.0.0"
|
|
42
|
-
"json-server": "^0.17.3"
|
|
45
|
+
"vite": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|
|
45
|
-
"@types/connect": "^3.4.
|
|
48
|
+
"@types/connect": "^3.4.38",
|
|
46
49
|
"@types/json-server": "^0.14.4",
|
|
47
|
-
"@types/node": "^
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"vite": "^4.0.0"
|
|
50
|
+
"@types/node": "^22.10.0",
|
|
51
|
+
"tsup": "^6.7.0",
|
|
52
|
+
"typescript": "^5.7.2",
|
|
53
|
+
"vite": "^8.0.12"
|
|
52
54
|
}
|
|
53
|
-
}
|
|
55
|
+
}
|