Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move to jq #6539

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 90 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/insomnia/electron-builder.config.js
Expand Up @@ -37,6 +37,10 @@ const config = {
to: '.',
filter: 'opensource-licenses.txt',
},
{
from: './src/utils/node-jq/bin',
to: './src/utils/node-jq/bin',
},
],
extraMetadata: {
main: 'main.min.js', // Override the main path in package.json
Expand Down Expand Up @@ -68,6 +72,7 @@ const config = {
asarUnpack: [
'node_modules/@getinsomnia/node-libcurl',
],
x64ArchFiles: '*',
},
dmg: {
window: {
Expand Down
3 changes: 3 additions & 0 deletions packages/insomnia/package.json
Expand Up @@ -46,6 +46,7 @@
"@stoplight/spectral-formats": "^1.5.0",
"@stoplight/spectral-ruleset-bundler": "1.5.2",
"@stoplight/spectral-rulesets": "^1.16.0",
"@types/strip-final-newline": "3.0.0",
"apiconnect-wsdl": "1.8.31",
"aws4": "^1.12.0",
"axios": "^1.4.0",
Expand All @@ -66,7 +67,9 @@
"html-entities": "^2.4.0",
"httpsnippet": "^2.0.0",
"iconv-lite": "^0.6.3",
"is-valid-path": "0.1.1",
"isomorphic-git": "^1.10.4",
"joi": "17.10.2",
"js-yaml": "^3.14.1",
"jshint": "^2.13.6",
"jsonlint-mod-fixed": "1.7.7",
Expand Down
Binary file added packages/insomnia/src/bin/linux/jq-linux-i386
Binary file not shown.
Binary file added packages/insomnia/src/bin/macOS/jq-macos-arm64
Binary file not shown.
Binary file not shown.
33 changes: 21 additions & 12 deletions packages/insomnia/src/ui/components/codemirror/code-editor.tsx
@@ -1,4 +1,4 @@
import './base-imports';

Check failure on line 1 in packages/insomnia/src/ui/components/codemirror/code-editor.tsx

View workflow job for this annotation

GitHub Actions / Test

Run autofix to sort these imports!

import classnames from 'classnames';
import clone from 'clone';
Expand Down Expand Up @@ -26,6 +26,7 @@
import { showModal } from '../modals/index';
import { isKeyCombinationInRegistry } from '../settings/shortcuts';
import { normalizeIrregularWhitespace } from './normalizeIrregularWhitespace';
import run from '../../../utils/node-jq/src/jq';
const TAB_SIZE = 4;
const MAX_SIZE_FOR_LINTING = 1000000; // Around 1MB

Expand Down Expand Up @@ -208,26 +209,34 @@
return code;
}
};
const prettifyJSON = (code: string, filter?: string) => {
const prettifyJSON = async (code: string, filter?: string) => {
try {
let jsonString = code;
let jsonString: any = code;
if (updateFilter && filter) {
try {
const codeObj = JSON.parse(code);
const results = JSONPath({ json: codeObj, path: filter.trim() });
jsonString = JSON.stringify(results);
if (filter.startsWith('$')) {
const codeObj = JSON.parse(code);
const results = JSONPath({ json: codeObj, path: filter.trim() });
jsonString = JSON.stringify(results);
}

if (filter.startsWith('.')) {
const codeObj = JSON.parse(code);
const results = await run(`${filter.trim()}`, codeObj, { input: 'json' });
jsonString = results;
}
} catch (err) {
console.log('[jsonpath] Error: ', err);
jsonString = '[]';
console.log('JSON query Error: ', err);
jsonString = '{}';
}
}
return jsonPrettify(jsonString, indentChars, autoPrettify);
return jsonPrettify(jsonString, indentChars, autoPrettify);
} catch (error) {
// That's Ok, just leave it
return code;
}
};
const maybePrettifyAndSetValue = (code?: string, forcePrettify?: boolean, filter?: string) => {
const maybePrettifyAndSetValue = async (code?: string, forcePrettify?: boolean, filter?: string) => {
if (typeof code !== 'string') {
console.warn('Code editor was passed non-string value', code);
return;
Expand All @@ -238,7 +247,7 @@
if (mode?.includes('xml')) {
code = prettifyXML(code, filter);
} else if (mode?.includes('json')) {
code = prettifyJSON(code, filter);
code = await prettifyJSON(code, filter);
}
}
// this prevents codeMirror from needlessly setting the same thing repeatedly (which has the effect of moving the user's cursor and resetting the viewport scroll: a bad user experience)
Expand All @@ -252,7 +261,7 @@
useDocBodyKeyboardShortcuts({
beautifyRequestBody: () => {
if (mode?.includes('json') || mode?.includes('xml')) {
maybePrettifyAndSetValue(codeMirror.current?.getValue());
maybePrettifyAndSetValue(codeMirror.current?.getValue()).then();
}
},
});
Expand Down Expand Up @@ -553,7 +562,7 @@
type="text"
title="Filter response body"
defaultValue={filter || ''}
placeholder={mode?.includes('json') ? '$.store.books[*].author' : '/store/books/author'}
placeholder={mode?.includes('json') ? 'jq: .store.book[].author or JSONPath: $.store.books[*].author' : '/store/books/author'}
onKeyDown={createKeybindingsHandler({
'Enter': () => {
const filter = inputRef.current?.value;
Expand Down
21 changes: 18 additions & 3 deletions packages/insomnia/src/ui/components/modals/filter-help-modal.tsx
Expand Up @@ -23,11 +23,25 @@ const HelpExamples: FC<{ helpExamples: HelpExample[] }> = ({ helpExamples }) =>
</table>
);

const JSONPathHelp: FC = () => (
const JqHelper: FC = () => (
<ModalBody className="pad">
<p>
Use <Link href="https://github.com/jqlang/jq/wiki/For-JSONPath-users">Jq</Link> to filter the response body. Here are some examples that you might use on a book store API:
</p>
<br />
<HelpExamples
helpExamples={[
{ code: '.store.book[].title', description: 'Get titles of all books in the store' },
{ code: '.store.book[] | select(.price < 10)', description: 'Get books costing less than $10' },
{ code: '..|objects.book[-1]', description: 'Get the last book in the store' },
{ code: '.store.book[] | select(.isbn)', description: 'Get all books in the store with an isbn number' },
]}
/>
<br />
<p>
Use <Link href="http://goessner.net/articles/JsonPath/">JSONPath</Link> to filter the response body. Here are some examples that you might use on a book store API:
</p>
<br />
<HelpExamples
helpExamples={[
{ code: '$.store.books[*].title', description: 'Get titles of all books in the store' },
Expand All @@ -38,7 +52,8 @@ const JSONPathHelp: FC = () => (
]}
/>
<p className="notice info">
Note that there's <Link href="https://cburgmer.github.io/json-path-comparison/">no standard</Link> for JSONPath. Insomnia uses <Link href="https://www.npmjs.com/package/jsonpath-plus">jsonpath-plus</Link>.
Insomnia supports both <Link href="https://www.npmjs.com/package/node-jq">jq</Link> and <Link href="https://www.npmjs.com/package/jsonpath-plus">JSONPath</Link>.
Note that there's <Link href="https://cburgmer.github.io/json-path-comparison/">no standard</Link> for JSONPath.
</p>
</ModalBody>
);
Expand Down Expand Up @@ -88,7 +103,7 @@ export const FilterHelpModal = forwardRef<FilterHelpModalHandle, ModalProps>((_,
return (
<Modal ref={modalRef}>
<ModalHeader>Response Filtering Help</ModalHeader>
{isJSON ? <JSONPathHelp /> : null}
{isJSON ? <JqHelper /> : null}
{isXPath ? <XPathHelp /> : null}
</Modal>
);
Expand Down