mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-24 01:28:35 +02:00
Fix up some of the new formatting stuff
This commit is contained in:
@@ -63,8 +63,12 @@ async function pluginHookExport(_ctx, request) {
|
|||||||
}
|
}
|
||||||
xs.push(NEWLINE);
|
xs.push(NEWLINE);
|
||||||
}
|
}
|
||||||
|
} else if (typeof request.body?.query === "string") {
|
||||||
|
const body = { query: request.body.query || "", variables: maybeParseJSON(request.body.variables, void 0) };
|
||||||
|
xs.push("--data-raw", `${quote(JSON.stringify(body))}`);
|
||||||
|
xs.push(NEWLINE);
|
||||||
} else if (typeof request.body?.text === "string") {
|
} else if (typeof request.body?.text === "string") {
|
||||||
xs.push("--data-raw", `$${quote(request.body.text)}`);
|
xs.push("--data-raw", `${quote(request.body.text)}`);
|
||||||
xs.push(NEWLINE);
|
xs.push(NEWLINE);
|
||||||
}
|
}
|
||||||
if (request.authenticationType === "basic" || request.authenticationType === "digest") {
|
if (request.authenticationType === "basic" || request.authenticationType === "digest") {
|
||||||
@@ -91,6 +95,13 @@ function quote(arg) {
|
|||||||
function onlyEnabled(v) {
|
function onlyEnabled(v) {
|
||||||
return v.enabled !== false && !!v.name;
|
return v.enabled !== false && !!v.name;
|
||||||
}
|
}
|
||||||
|
function maybeParseJSON(v, fallback) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(v);
|
||||||
|
} catch (err) {
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
}
|
||||||
// Annotate the CommonJS export names for ESM import in node:
|
// Annotate the CommonJS export names for ESM import in node:
|
||||||
0 && (module.exports = {
|
0 && (module.exports = {
|
||||||
plugin,
|
plugin,
|
||||||
|
|||||||
@@ -96,20 +96,12 @@ pub fn format_json(text: &str, tab: &str) -> String {
|
|||||||
new_json.push('\n');
|
new_json.push('\n');
|
||||||
new_json.push_str(tab.to_string().repeat(depth).as_str());
|
new_json.push_str(tab.to_string().repeat(depth).as_str());
|
||||||
new_json.push(current_char);
|
new_json.push(current_char);
|
||||||
// Pad with space if the next char is not a comma
|
|
||||||
if let Some(',') = chars.peek() {
|
|
||||||
new_json.push(' ');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
']' => {
|
']' => {
|
||||||
depth -= 1;
|
depth -= 1;
|
||||||
new_json.push('\n');
|
new_json.push('\n');
|
||||||
new_json.push_str(tab.to_string().repeat(depth).as_str());
|
new_json.push_str(tab.to_string().repeat(depth).as_str());
|
||||||
new_json.push(current_char);
|
new_json.push(current_char);
|
||||||
// Pad with space if the next char is not a comma
|
|
||||||
if let Some(',') = chars.peek() {
|
|
||||||
new_json.push(' ');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
':' => {
|
':' => {
|
||||||
new_json.push(current_char);
|
new_json.push(current_char);
|
||||||
@@ -227,4 +219,37 @@ mod test {
|
|||||||
.trim()
|
.trim()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_graphql_response() {
|
||||||
|
assert_eq!(
|
||||||
|
format_json(r#"{"data":{"capsules":[{"landings":null,"original_launch":null,"reuse_count":0,"status":"retired","type":"Dragon 1.0","missions":null},{"id":"5e9e2c5bf3591882af3b2665","landings":null,"original_launch":null,"reuse_count":0,"status":"retired","type":"Dragon 1.0","missions":null}]}}"#, " "),
|
||||||
|
r#"
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"capsules": [
|
||||||
|
{
|
||||||
|
"landings": null,
|
||||||
|
"original_launch": null,
|
||||||
|
"reuse_count": 0,
|
||||||
|
"status": "retired",
|
||||||
|
"type": "Dragon 1.0",
|
||||||
|
"missions": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "5e9e2c5bf3591882af3b2665",
|
||||||
|
"landings": null,
|
||||||
|
"original_launch": null,
|
||||||
|
"reuse_count": 0,
|
||||||
|
"status": "retired",
|
||||||
|
"type": "Dragon 1.0",
|
||||||
|
"missions": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#
|
||||||
|
.trim()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,12 +20,12 @@ type Props = Pick<EditorProps, 'heightMode' | 'className' | 'forceUpdateKey'> &
|
|||||||
export function GraphQLEditor({ body, onChange, baseRequest, ...extraEditorProps }: Props) {
|
export function GraphQLEditor({ body, onChange, baseRequest, ...extraEditorProps }: Props) {
|
||||||
const editorViewRef = useRef<EditorView>(null);
|
const editorViewRef = useRef<EditorView>(null);
|
||||||
const { schema, isLoading, error, refetch } = useIntrospectGraphQL(baseRequest);
|
const { schema, isLoading, error, refetch } = useIntrospectGraphQL(baseRequest);
|
||||||
const [currentBody, setCurrentBody] = useState<{ query: string; variables: string }>(() => {
|
const [currentBody, setCurrentBody] = useState<{ query: string; variables: string | undefined }>(() => {
|
||||||
// Migrate text bodies to GraphQL format
|
// Migrate text bodies to GraphQL format
|
||||||
// NOTE: This is how GraphQL used to be stored
|
// NOTE: This is how GraphQL used to be stored
|
||||||
if ('text' in body) {
|
if ('text' in body) {
|
||||||
const b = tryParseJson(body.text, {});
|
const b = tryParseJson(body.text, {});
|
||||||
const variables = JSON.stringify(b.variables ?? '', null, 2);
|
const variables = JSON.stringify(b.variables || undefined, null, 2);
|
||||||
return { query: b.query ?? '', variables };
|
return { query: b.query ?? '', variables };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,13 +33,13 @@ export function GraphQLEditor({ body, onChange, baseRequest, ...extraEditorProps
|
|||||||
});
|
});
|
||||||
|
|
||||||
const handleChangeQuery = (query: string) => {
|
const handleChangeQuery = (query: string) => {
|
||||||
const newBody = { query, variables: currentBody.variables };
|
const newBody = { query, variables: currentBody.variables || undefined };
|
||||||
setCurrentBody(newBody);
|
setCurrentBody(newBody);
|
||||||
onChange(newBody);
|
onChange(newBody);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleChangeVariables = (variables: string) => {
|
const handleChangeVariables = (variables: string) => {
|
||||||
const newBody = { query: currentBody.query, variables };
|
const newBody = { query: currentBody.query, variables: variables || undefined };
|
||||||
setCurrentBody(newBody);
|
setCurrentBody(newBody);
|
||||||
onChange(newBody);
|
onChange(newBody);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ export const syntaxHighlightStyle = HighlightStyle.define([
|
|||||||
textDecoration: 'underline',
|
textDecoration: 'underline',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tag: [t.paren, t.bracket, t.brace],
|
tag: [t.paren, t.bracket, t.squareBracket, t.brace, t.separator],
|
||||||
color: 'var(--textSubtle)',
|
color: 'var(--textSubtle)',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ export function TextViewer({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formattedBody.isFetching) {
|
if (formattedBody.data == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export function useFormatText({
|
|||||||
pretty: boolean;
|
pretty: boolean;
|
||||||
}) {
|
}) {
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: [text],
|
queryKey: [text, language, pretty],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
if (text === '' || !pretty) {
|
if (text === '' || !pretty) {
|
||||||
return text;
|
return text;
|
||||||
|
|||||||
@@ -5,7 +5,24 @@ const INDENT = ' ';
|
|||||||
|
|
||||||
export async function tryFormatJson(text: string): Promise<string> {
|
export async function tryFormatJson(text: string): Promise<string> {
|
||||||
if (text === '') return text;
|
if (text === '') return text;
|
||||||
return invokeCmd('cmd_format_json', { text });
|
|
||||||
|
try {
|
||||||
|
const result = await invokeCmd<string>('cmd_format_json', { text });
|
||||||
|
return result;
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
} catch (err) {
|
||||||
|
console.warn("Failed to format JSON", err);
|
||||||
|
// Nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.stringify(JSON.parse(text), null, 2);
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
} catch (err) {
|
||||||
|
// Nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function tryFormatXml(text: string): Promise<string> {
|
export async function tryFormatXml(text: string): Promise<string> {
|
||||||
|
|||||||
Reference in New Issue
Block a user