actions / actions/toolkit

Make all request parameters (url, method, body) accessible for RequestHandler.prepareRequest

Open
#1,554 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
5.9k
Forks
1.8k
PR merge metrics
No merged PRs in 30d

Description

I need to sign my requests with SignatureV4 to access some AWS_IAM secured APIs.

To sign a request I need to access target url, method, headers and the body data (see ky hook example implementation below).

I'd like to do the same with the @actions/http-client, however currently I can't access url, method and the body data in the RequestHandler.prepareRequest(options: http.RequestOptions):void hook, see https://github.com/actions/toolkit/blob/797f48fcfac3bcc50833df1ff0d35b16d7401503/packages/http-client/src/interfaces.ts#L57C1-L57C1

So make url, method and body data accessible to RequestHandler.prepareRequest(options: http.RequestOptions):void as well.

hy hook example of SignatureV4 signer implementation

import {SignatureV4} from "@smithy/signature-v4";
import {HttpRequest} from "@smithy/protocol-http";
import {BeforeRequestHook} from "ky";

export function AwsRequestSigner(signer: SignatureV4): BeforeRequestHook {
    return async (request: Request) => {
        const requestUrl = new URL(request.url);
        const requestHeaders = Object.fromEntries(request.headers.entries());
        if (requestHeaders["Authorization"]) {
            // preserve Authorization header
            requestHeaders["X-Authorization"] = requestHeaders["Authorization"];
            delete requestHeaders["Authorization"];
        }
        const canonicalRequest = new HttpRequest({
            hostname: requestUrl.hostname,
            // port: requestUrl.port ? parseInt(requestUrl.port) : undefined,
            path: requestUrl.pathname,
            protocol: requestUrl.protocol,
            method: request.method,
            body: await request.text(),
            query: Object.fromEntries(requestUrl.searchParams.entries()),
            headers: {
                ...requestHeaders,
                "Host": requestUrl.host,
            },
        });

        const signedCanonicalRequest = await signer.sign(canonicalRequest);

        return new Request(request, {
            headers: signedCanonicalRequest.headers,
            body: signedCanonicalRequest.body,
        })
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the RequestHandler.prepareRequest definition in packages/http-client/src/interfaces.ts, then trace how requests are built and passed to that hook. Done means the hook can access the request URL, method, and body data needed for SignatureV4 signing.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.