|
1 | | -import { InvalidFrameActionPayloadError, RequestBodyNotJSONError } from "../../core/errors"; |
| 1 | +import { |
| 2 | + InvalidFrameActionPayloadError, |
| 3 | + RequestBodyNotJSONError, |
| 4 | +} from "../../core/errors"; |
2 | 5 | import type { FramesMiddleware } from "../../core/types"; |
3 | 6 | import type { ClientProtocolId, FrameActionPayload } from "../../types"; |
4 | 7 | import type { ValidateFrameActionResponse } from "./types.message"; |
@@ -72,32 +75,42 @@ export function neynarValidate( |
72 | 75 | } |
73 | 76 |
|
74 | 77 | try { |
75 | | - const message = (await fetch( |
| 78 | + const response = await fetch( |
76 | 79 | "https://api.neynar.com/v2/farcaster/frame/validate", |
77 | 80 | { |
78 | 81 | method: "POST", |
79 | 82 | headers: { |
80 | | - accept: "application json", |
| 83 | + accept: "application/json", |
81 | 84 | api_key: options?.API_KEY || "NEYNAR_API_DOCS", |
82 | 85 | "content-type": "application/json", |
83 | 86 | }, |
84 | 87 | body: JSON.stringify({ |
85 | 88 | message_bytes_in_hex: payload.trustedData.messageBytes, |
86 | 89 | }), |
| 90 | + cache: "no-cache", |
87 | 91 | } |
88 | | - ).then(async (res) => res.json())) as ValidateFrameActionResponse; |
| 92 | + ); |
| 93 | + |
| 94 | + if (response.ok) { |
| 95 | + const message = (await response.json()) as ValidateFrameActionResponse; |
89 | 96 |
|
90 | | - return next({ |
91 | | - message, |
92 | | - clientProtocol: { |
93 | | - id: "farcaster", |
94 | | - version: "vNext", |
95 | | - }, |
96 | | - }); |
| 97 | + return next({ |
| 98 | + message, |
| 99 | + clientProtocol: { |
| 100 | + id: "farcaster", |
| 101 | + version: "vNext", |
| 102 | + }, |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + throw new Error( |
| 107 | + `Neynar API returned an error with status code ${response.status}` |
| 108 | + ); |
97 | 109 | } catch (error) { |
98 | 110 | // eslint-disable-next-line no-console -- provide feedback to the developer |
99 | | - console.info( |
100 | | - "neynarValidate middleware: could not decode farcaster message from payload, calling next." |
| 111 | + console.error( |
| 112 | + "neynarValidate middleware: could not decode farcaster message from payload, calling next.", |
| 113 | + error |
101 | 114 | ); |
102 | 115 | return next(); |
103 | 116 | } |
|
0 commit comments