Skip to content

Commit 91bc168

Browse files
committed
feat: pass user location
1 parent 617815c commit 91bc168

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

packages/frames.js/src/getUserDataForFid.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { HubHttpUrlOptions, UserDataReturnType } from "./types";
66
* Returns the latest user data for a given Farcaster users Fid if available.
77
*/
88
export async function getUserDataForFid<
9-
Options extends HubHttpUrlOptions | undefined,
9+
Options extends HubHttpUrlOptions | undefined
1010
>({
1111
fid,
1212
options = {},
@@ -34,32 +34,42 @@ export async function getUserDataForFid<
3434
.catch(async () => {
3535
// body has not been
3636
throw new Error(
37-
`Failed to parse response body as JSON because server hub returned response with status "${userDataResponse.status}" and body "${await userDataResponse.clone().text()}"`
37+
`Failed to parse response body as JSON because server hub returned response with status "${
38+
userDataResponse.status
39+
}" and body "${await userDataResponse.clone().text()}"`
3840
);
3941
})) as { messages?: Record<string, any>[] };
4042

4143
if (messages && messages.length > 0) {
4244
const valuesByType = messages.reduce<
4345
Partial<Record<UserDataType, { value: string; timestamp: number }>>
4446
>((acc, messageJson) => {
45-
const message = Message.fromJSON(messageJson);
47+
try {
48+
const message = Message.fromJSON(messageJson);
4649

47-
if (message.data?.type !== MessageType.USER_DATA_ADD) {
48-
return acc;
49-
}
50+
if (message.data?.type !== MessageType.USER_DATA_ADD) {
51+
return acc;
52+
}
5053

51-
if (!message.data.userDataBody) {
52-
return acc;
53-
}
54+
if (!message.data.userDataBody) {
55+
return acc;
56+
}
5457

55-
const timestamp = message.data.timestamp;
56-
const { type, value } = message.data.userDataBody;
57-
const foundValue = acc[type];
58+
const timestamp = message.data.timestamp;
59+
const { type, value } = message.data.userDataBody;
60+
const foundValue = acc[type];
5861

59-
if (foundValue && foundValue.timestamp < timestamp) {
60-
acc[type] = { value, timestamp };
61-
} else {
62-
acc[type] = { value, timestamp };
62+
if (foundValue && foundValue.timestamp < timestamp) {
63+
acc[type] = { value, timestamp };
64+
} else {
65+
acc[type] = { value, timestamp };
66+
}
67+
} catch (error) {
68+
console.warn(
69+
`Failed to parse user data message for fid ${fid}`,
70+
messageJson,
71+
error
72+
);
6373
}
6474

6575
return acc;
@@ -70,6 +80,7 @@ export async function getUserDataForFid<
7080
displayName: valuesByType[UserDataType.DISPLAY]?.value,
7181
username: valuesByType[UserDataType.USERNAME]?.value,
7282
bio: valuesByType[UserDataType.BIO]?.value,
83+
location: valuesByType[UserDataType.LOCATION]?.value,
7384
};
7485
}
7586

packages/frames.js/src/types.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ type FrameOptionalStringKeys =
4444
| keyof OpenFramesProperties
4545
| "frames.js:version"
4646
| "frames.js:debug-info:image";
47-
type FrameOptionalActionButtonTypeKeys =
48-
`fc:frame:button:${1 | 2 | 3 | 4}:action`;
47+
type FrameOptionalActionButtonTypeKeys = `fc:frame:button:${
48+
| 1
49+
| 2
50+
| 3
51+
| 4}:action`;
4952
type FrameOptionalButtonStringKeys =
5053
| `fc:frame:button:${1 | 2 | 3 | 4}`
5154
| `fc:frame:button:${1 | 2 | 3 | 4}:target`
@@ -59,8 +62,8 @@ type MapFrameOptionalKeyToValueType<K extends FrameKeys> =
5962
K extends FrameOptionalStringKeys
6063
? string | undefined
6164
: K extends FrameOptionalActionButtonTypeKeys
62-
? ActionButtonType | undefined
63-
: string | undefined;
65+
? ActionButtonType | undefined
66+
: string | undefined;
6467

6568
type FrameRequiredProperties = {
6669
"fc:frame": FrameVersion;
@@ -153,7 +156,7 @@ export type FrameButtonsType =
153156
| [FrameButton, FrameButton, FrameButton, FrameButton];
154157

155158
export type AddressReturnType<
156-
Options extends { fallbackToCustodyAddress?: boolean } | undefined,
159+
Options extends { fallbackToCustodyAddress?: boolean } | undefined
157160
> = Options extends { fallbackToCustodyAddress: true }
158161
? `0x${string}`
159162
: `0x${string}` | null;
@@ -214,6 +217,7 @@ export type UserDataReturnType = {
214217
username?: string;
215218
bio?: string;
216219
profileImage?: string;
220+
location?: string;
217221
} | null;
218222

219223
export type FrameActionDataParsedAndHubContext = FrameActionDataParsed &

0 commit comments

Comments
 (0)