insomnia-plugin-jsonc 0.1.0
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/README.md +16 -0
- package/main.js +36 -0
- package/package.json +38 -0
- package/test.js +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# insomnia-plugin-jsonc
|
|
2
|
+
|
|
3
|
+
Strip JSON comments from your request body.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
- Open Insomnia options (Ctrl + , | Cmd + ,)
|
|
8
|
+
- Type `insomnia-plugin-jsonc` on the Plugins tab
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
Just write valid JavaScript comments in JSON body request and send your request without minding them.
|
|
13
|
+
|
|
14
|
+
## Deps
|
|
15
|
+
|
|
16
|
+
Use [`json-easy-strip`](https://www.npmjs.com/package/json-easy-strip)'s RegExp to strip comments.
|
package/main.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Content-Type
|
|
3
|
+
*/
|
|
4
|
+
const CONTENT_TYPE_JSON = "application/json";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Strip comments from a JSON string
|
|
8
|
+
*
|
|
9
|
+
* Taken from [`json-easy-strip`](https://www.npmjs.com/package/json-easy-strip).
|
|
10
|
+
*
|
|
11
|
+
* Extracted to not parse and stringify again the JSON string
|
|
12
|
+
*
|
|
13
|
+
* @param data the JSON string with comments
|
|
14
|
+
* @returns the JSON string without comments
|
|
15
|
+
*/
|
|
16
|
+
const stripJSONComments = (data) =>
|
|
17
|
+
data.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) =>
|
|
18
|
+
g ? "" : m
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Hook to update request body and remove comments
|
|
23
|
+
* @param context insomnia request context
|
|
24
|
+
* @link https://docs.insomnia.rest/insomnia/context-object-reference
|
|
25
|
+
*/
|
|
26
|
+
const updateRequestBodyHook = (context) => {
|
|
27
|
+
const body = context.request.getBody();
|
|
28
|
+
if (body.mimeType === CONTENT_TYPE_JSON) {
|
|
29
|
+
context.request.setBody({
|
|
30
|
+
...body,
|
|
31
|
+
text: stripJSONComments(body.text),
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
module.exports.requestHooks = [updateRequestBodyHook];
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "insomnia-plugin-jsonc",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Insomnia plugin to use comments in JSON body",
|
|
5
|
+
"main": "main.js",
|
|
6
|
+
"insomnia": {
|
|
7
|
+
"name": "jsonc",
|
|
8
|
+
"displayName": "JSON Comments Plugin",
|
|
9
|
+
"description": "Plugin to use comments in JSON body.",
|
|
10
|
+
"publisher": {
|
|
11
|
+
"name": "Guillaume Ongenae",
|
|
12
|
+
"icon": "https://avatars.githubusercontent.com/u/9453090"
|
|
13
|
+
},
|
|
14
|
+
"unlisted": false
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node test.js"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+ssh://git@github.com/algoan/insomnia-plugin-jsonc.git"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"insomnia",
|
|
25
|
+
"plugin",
|
|
26
|
+
"jsonc",
|
|
27
|
+
"json-comments",
|
|
28
|
+
"comments"
|
|
29
|
+
],
|
|
30
|
+
"author": "Guillaume Ongenae <guillaume.ongenae@gmail.com>",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/g-ongenae/insomnia-plugin-jsonc/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/g-ongenae/insomnia-plugin-jsonc#readme",
|
|
36
|
+
"dependencies": {},
|
|
37
|
+
"devDependencies": {}
|
|
38
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const { requestHooks } = require('./main');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Create a fake insomnia context with a validation on the setBody
|
|
5
|
+
* @param {object} givenBody
|
|
6
|
+
* @param {object} expectedBody
|
|
7
|
+
* @returns a fake insomnia context with a validation
|
|
8
|
+
*/
|
|
9
|
+
const fakeContext = (id, givenBody, expectedBody) => {
|
|
10
|
+
return {
|
|
11
|
+
request: {
|
|
12
|
+
getBody: () => givenBody,
|
|
13
|
+
setBody: (actualBody) => {
|
|
14
|
+
const result = actualBody.text === expectedBody.text
|
|
15
|
+
? `${id} | passed`
|
|
16
|
+
: `${id} | expected: '${expectedBody.text}' => received: '${actualBody.text}'`;
|
|
17
|
+
|
|
18
|
+
console.log(result);
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* List of test cases
|
|
26
|
+
*/
|
|
27
|
+
const cases = [
|
|
28
|
+
// Should not alter non JSON body
|
|
29
|
+
[{ mimeType: 'non-json', text: 'foo' }, { mimeType: 'non-json', text: 'foo' }],
|
|
30
|
+
// Should not alter JSON body without comments
|
|
31
|
+
[{ mimeType: 'application/json', text: 'foo' }, { mimeType: 'application/json', text: 'foo' }],
|
|
32
|
+
[{ mimeType: 'application/json', text: '{"foo": "bar"}' }, { mimeType: 'application/json', text: '{"foo": "bar"}' }],
|
|
33
|
+
// Should remove comments from JSON body (space are preserved)
|
|
34
|
+
[{ mimeType: 'application/json', text: '{"foo": /* hello */ "bar"}' }, { mimeType: 'application/json', text: '{"foo": "bar"}' }],
|
|
35
|
+
[{ mimeType: 'application/json', text: '/* hello */ {"foo": "bar"}' }, { mimeType: 'application/json', text: ' {"foo": "bar"}' }],
|
|
36
|
+
[{ mimeType: 'application/json', text: '{"foo": "bar" // hello\n }' }, { mimeType: 'application/json', text: '{"foo": "bar" \n }' }],
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Call and test the hook
|
|
41
|
+
*/
|
|
42
|
+
for (const [index, test] of Object.entries(cases)) {
|
|
43
|
+
requestHooks[0](fakeContext(index, ...test));
|
|
44
|
+
}
|