Skip to content

Commit

Permalink
Add "--keep-url" argument
Browse files Browse the repository at this point in the history
  • Loading branch information
fredsted committed Jun 19, 2024
1 parent 42783f0 commit 6bd6230
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
24 changes: 14 additions & 10 deletions commands/forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ export default async (argv) => {
argv.token ?? process.env.WH_TOKEN,
argv['api-key'] ?? process.env.WH_API_KEY,
(data) => {
const target = replaceVariables(argv.target ?? process.env.WH_TARGET ?? 'https://localhost', data.variables)
const query = data.request.query !== null
? '?' + new URLSearchParams(data.request.query).toString()
: '';

// We only want the `/a/b/c` part:
// https://webhook.site/00000000-0000-0000-00000-000000000000/a/b/c
const pathMatch = data.request.url.match(/https?:\/\/[^\/]*\/[a-z0-9-]+(\/[^?#]+)/)
const path = pathMatch ? pathMatch[1] : '';
let target = replaceVariables(argv.target ?? process.env.WH_TARGET ?? 'https://localhost', data.variables)

if (!argv['keep-url']) {
const query = data.request.query !== null
? '?' + new URLSearchParams(data.request.query).toString()
: '';

// We only want the `/a/b/c` part:
// https://webhook.site/00000000-0000-0000-00000-000000000000/a/b/c
const pathMatch = data.request.url.match(/https?:\/\/[^\/]*\/[a-z0-9-]+(\/[^?#]+)/)
const path = pathMatch ? pathMatch[1] : '';
target = target + path + query;
}

let options = {
method: data.request.method,
Expand All @@ -43,7 +47,7 @@ export default async (argv) => {
options['body'] = data.request.content
}

fetch(target + path + query, options)
fetch(target, options)
.then((res) => {
log.info({
msg: 'Forwarded incoming request',
Expand Down
6 changes: 4 additions & 2 deletions commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ ${'Commands and Arguments'.bold}
traffic should be redirected from. (required)
--api-key= A valid Webhook.site API Key
--target=https://example.com?action=$request.query.action$
Specifies the forwarding target. Variables
Specifies the forwarding target URL. Variables
are replaced; see below.
--keep-url When used, skips merging path and query strings
into the target URL.
${'exec'.underline} Execute a shell command on incoming requests to a Webhook.site URL.
${'exec'.underline} Execute shell commands on traffic to a Webhook.site URL.
--token= Specifies which Webhook.site URL (token ID)
to listen for requests from. (required)
--api-key= A valid Webhook.site API Key (required)
Expand Down

0 comments on commit 6bd6230

Please sign in to comment.