Skip to content

Commit

Permalink
Support new GraphQL body type in curl export
Browse files Browse the repository at this point in the history
  • Loading branch information
gschier committed Oct 22, 2024
1 parent 9d24aef commit d91e60f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
7 changes: 5 additions & 2 deletions plugins/exporter-curl/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ export async function pluginHookExport(_ctx: Context, request: Partial<HttpReque
}
xs.push(NEWLINE);
}
} else if (typeof request.body?.query === 'string') {
const body = { query: request.body.query, variables: request.body.variables ?? undefined };
xs.push('--data-raw', `${quote(JSON.stringify(body))}`);
xs.push(NEWLINE);
} else if (typeof request.body?.text === 'string') {
// --data-raw $'...' to do special ANSI C quoting
xs.push('--data-raw', `$${quote(request.body.text)}`);
xs.push('--data-raw', `${quote(request.body.text)}`);
xs.push(NEWLINE);
}

Expand Down
20 changes: 18 additions & 2 deletions plugins/exporter-curl/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ describe('exporter-curl', () => {
);
});

test('Exports POST with GraphQL data', async () => {
expect(
await pluginHookExport(ctx, {
url: 'https://yaak.app',
method: 'POST',
bodyType: 'graphql',
body: {
query : '{foo,bar}',
variables: {a: 'aaa', b: 'bbb'},
},
}),
).toEqual(
[`curl -X POST 'https://yaak.app'`, `--data-raw '{"query":"{foo,bar}","variables":{"a":"aaa","b":"bbb"}}'`].join(` \\\n `),
);
});

test('Exports PUT with multipart form', async () => {
expect(
await pluginHookExport(ctx, {
Expand Down Expand Up @@ -78,7 +94,7 @@ describe('exporter-curl', () => {
[
`curl -X POST 'https://yaak.app'`,
`--header 'Content-Type: application/json'`,
`--data-raw $'{"foo":"bar\\'s"}'`,
`--data-raw '{"foo":"bar\\'s"}'`,
].join(` \\\n `),
);
});
Expand All @@ -98,7 +114,7 @@ describe('exporter-curl', () => {
[
`curl -X POST 'https://yaak.app'`,
`--header 'Content-Type: application/json'`,
`--data-raw $'{"foo":"bar",\n"baz":"qux"}'`,
`--data-raw '{"foo":"bar",\n"baz":"qux"}'`,
].join(` \\\n `),
);
});
Expand Down

0 comments on commit d91e60f

Please sign in to comment.