Skip to content

Commit

Permalink
fix: deal better with errors in neynar validator middleware (#515)
Browse files Browse the repository at this point in the history
* fix: deal better with errors in neynar validator middleware

* chore: changeset

* test: skip neynar validation test
  • Loading branch information
michalkvasnicak authored Oct 25, 2024
1 parent aec8740 commit d13c6ca
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-horses-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"frames.js": patch
---

fix: deal better with errors in neynar validator middleware
39 changes: 26 additions & 13 deletions packages/frames.js/src/middleware/neynar/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { InvalidFrameActionPayloadError, RequestBodyNotJSONError } from "../../core/errors";
import {
InvalidFrameActionPayloadError,
RequestBodyNotJSONError,
} from "../../core/errors";
import type { FramesMiddleware } from "../../core/types";
import type { ClientProtocolId, FrameActionPayload } from "../../types";
import type { ValidateFrameActionResponse } from "./types.message";
Expand Down Expand Up @@ -72,32 +75,42 @@ export function neynarValidate(
}

try {
const message = (await fetch(
const response = await fetch(
"https://api.neynar.com/v2/farcaster/frame/validate",
{
method: "POST",
headers: {
accept: "application json",
accept: "application/json",
api_key: options?.API_KEY || "NEYNAR_API_DOCS",
"content-type": "application/json",
},
body: JSON.stringify({
message_bytes_in_hex: payload.trustedData.messageBytes,
}),
cache: "no-cache",
}
).then(async (res) => res.json())) as ValidateFrameActionResponse;
);

if (response.ok) {
const message = (await response.json()) as ValidateFrameActionResponse;

return next({
message,
clientProtocol: {
id: "farcaster",
version: "vNext",
},
});
return next({
message,
clientProtocol: {
id: "farcaster",
version: "vNext",
},
});
}

throw new Error(
`Neynar API returned an error with status code ${response.status}`
);
} catch (error) {
// eslint-disable-next-line no-console -- provide feedback to the developer
console.info(
"neynarValidate middleware: could not decode farcaster message from payload, calling next."
console.error(
"neynarValidate middleware: could not decode farcaster message from payload, calling next.",
error
);
return next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ describe("neynarValidate middleware", () => {
expect(next).toHaveBeenCalledWith();
});

it("parses frame message from request body and fetches external hub context and adds it to context", async () => {
// skipped for now as the default api key doesn't work without browser UA
it.skip("parses frame message from request body and fetches external hub context and adds it to context", async () => {
const context = {
request: sampleFrameActionRequest.clone(),
} as unknown as FramesContext;
Expand Down

0 comments on commit d13c6ca

Please sign in to comment.