Skip to content

Commit

Permalink
feat: add createDefaultMetadataKeyInterceptor (#2368)
Browse files Browse the repository at this point in the history
## Why is this change needed?

Neynar requires x-api-key to access its hubs. This change makes it easy
to include that (or any other) header.

I tried to follow the docs to create a changes, but got an error. Am I
running it from the wrong directory maybe?

```
% yarn changeset
yarn run v1.22.22
error Command "changeset" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
bryan@Bryan-Neynar hub-monorepo % yarn run changeset 
yarn run v1.22.22
error Command "changeset" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
```

## Merge Checklist

_Choose all relevant options below by adding an `x` now or at any time
before submitting for review_

- [x] PR title adheres to the [conventional
commits](https://www.conventionalcommits.org/en/v1.0.0/) standard
- [x] PR has a
[changeset](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#35-adding-changesets)
- [ ] PR has been tagged with a change label(s) (i.e. documentation,
feature, bugfix, or chore)
- [ ] PR includes
[documentation](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#32-writing-docs)
if necessary.

<!-- start pr-codex -->

---

## PR-Codex overview
This PR introduces a new function to create a metadata key interceptor
for gRPC calls in the `hub-nodejs` package.

### Detailed summary
- Added the `createDefaultMetadataKeyInterceptor` function in
`packages/hub-nodejs/src/client.ts`.
- The function takes a `key` and `value`, returning an interceptor that
sets the specified metadata key with the given value if it is not
already present.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
BlinkyStitt authored Oct 16, 2024
1 parent f33cd01 commit 91b7720
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-bags-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farcaster/hub-nodejs": patch
---

Add createDefaultMetadataKeyInterceptor
18 changes: 18 additions & 0 deletions packages/hub-nodejs/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,21 @@ export const getAdminClient = (address: string, options?: Partial<grpc.ClientOpt
if (!address) throw new Error("Hub address not specified");
return new AdminServiceClient(address, grpc.credentials.createInsecure(), { ...options });
};

export function createDefaultMetadataKeyInterceptor(key: string, value: string) {
return function metadataKeyInterceptor(options: grpc.InterceptorOptions, nextCall: grpc.NextCall) {
const requester = {
start: function (
metadata: grpc.Metadata,
listener: grpc.Listener,
next: (metadata: grpc.Metadata, listener: grpc.Listener) => void,
) {
if (metadata.get(key).length === 0) {
metadata.set(key, value);
}
next(metadata, listener);
},
};
return new grpc.InterceptingCall(nextCall(options), requester);
};
}

0 comments on commit 91b7720

Please sign in to comment.