Skip to content

feat: supply operation to the httpSubscriptionLink eventSourceOptions. #6148

Closed

Description

@changesbyjames

Describe the feature you'd like to request

I have an authentication scheme that is based on signing the operation with a private key and verifying that with a public key. This is easy to do with httpLink because the function for creating headers is supplied with the operation.

const client = createTRPCClient<AppRouter>({
  links: [
   httpLink({
     url: apiUrl,
     headers: async (op) => {
        const signature = await signRequest(op.input, op.path, op.type);
        return {
          'x-signature': signature
        };
      }
    })
  ]
});

I would like to do the same to authenticate the httpSubscriptionLink with connection options:

const client = createTRPCClient<AppRouter>({
    links: [
      splitLink({
        condition: op => op.type === 'subscription',
        true: unstable_httpSubscriptionLink({
          url: apiUrl,
          eventSourceOptions: async () => {
            // no operation is supplied so no per request options
            return { headers: {} }
          }
        }),
        false: httpLink({
          url: apiUrl,
          headers: async ({op}) => {
            const signature = await signRequest(op.input, op.path, op.type);
            return {
              'x-signature': signature
            };
          }
        })
      })
    ]
  });

Describe the solution you'd like to see

I would like for the operation that starts the event source to be passed into the function, very much like httpLink.

const client = createTRPCClient<AppRouter>({
  links: [
    unstable_httpSubscriptionLink({
      url: apiUrl,
      eventSourceOptions: async ({op}) => {
        const signature = await signRequest(op.input, op.path, op.type);
        return {
           headers: {
              'x-signature': signature
            }
         };
      }
    })
  ]
});

Describe alternate solutions

  1. I have experimented with doing this as a link but unfortunately that doesn't expose the eventSourceOptions.

Additional information

I've done a quick implementation and it doesn't break any tests but this is my first contribution so take that with a grain of salt! I tried to port this to wsLink but because that doesn't have a one operation to one connection model, I don't think it makes sense.

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing this feature!

Activity

  1. added
    ✅ accepted-PRs-welcome
    on Oct 23, 2024
  2. github-actions commented on Oct 30, 2024

    @github-actions
    Contributor

    This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.

  3. locked as resolved and limited conversation to collaborators on Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✅ accepted-PRs-welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions