-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: transform raw data into FrameMetadata (framegear) (#205)
- Loading branch information
Showing
6 changed files
with
150 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { frameResultToFrameMetadata } from './frameResultToFrameMetadata'; | ||
|
||
describe('frameResultToFrameMetadata', () => { | ||
const baseResult = { | ||
'fc:frame:button:1': 'Button 1', | ||
'fc:frame:button:1:action': 'Action 1', | ||
'fc:frame:button:1:target': 'Target 1', | ||
'fc:frame:image': 'Image URL', | ||
'fc:frame:input': 'Input Text', | ||
'fc:frame:post_url': 'Post URL', | ||
'fc:frame:state': JSON.stringify({ key: 'value' }), | ||
'fc:frame:refresh_period': '10', | ||
}; | ||
|
||
it('should correctly map frame result to frame metadata', () => { | ||
const metadata = frameResultToFrameMetadata(baseResult); | ||
|
||
expect(metadata).toEqual({ | ||
buttons: [ | ||
{ | ||
action: 'Action 1', | ||
label: 'Button 1', | ||
target: 'Target 1', | ||
}, | ||
undefined, | ||
undefined, | ||
undefined, | ||
], | ||
image: 'Image URL', | ||
input: { text: 'Input Text' }, | ||
postUrl: 'Post URL', | ||
state: { key: 'value' }, | ||
refreshPeriod: 10, | ||
}); | ||
}); | ||
|
||
it('should handle missing optional fields', () => { | ||
const result = { ...baseResult }; | ||
delete (result as any)['fc:frame:button:1']; | ||
delete (result as any)['fc:frame:image']; | ||
delete (result as any)['fc:frame:input']; | ||
delete (result as any)['fc:frame:post_url']; | ||
delete (result as any)['fc:frame:state']; | ||
delete (result as any)['fc:frame:refresh_period']; | ||
|
||
const metadata = frameResultToFrameMetadata(result); | ||
|
||
expect(metadata).toEqual({ | ||
buttons: [undefined, undefined, undefined, undefined], | ||
image: undefined, | ||
input: undefined, | ||
postUrl: undefined, | ||
state: undefined, | ||
refreshPeriod: undefined, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { FrameMetadataType } from '@coinbase/onchainkit'; | ||
|
||
export function frameResultToFrameMetadata(result: Record<string, string>): FrameMetadataType { | ||
const buttons = [1, 2, 3, 4].map((idx) => | ||
result[`fc:frame:button:${idx}`] | ||
? { | ||
action: result[`fc:frame:button:${idx}:action`], | ||
label: result[`fc:frame:button:${idx}`], | ||
target: result[`fc:frame:button:${idx}:target`], | ||
} | ||
: undefined, | ||
); | ||
const image = result['fc:frame:image']; | ||
const inputText = result['fc:frame:input']; | ||
const input = inputText ? { text: inputText } : undefined; | ||
const postUrl = result['fc:frame:post_url']; | ||
const rawState = result['fc:frame:state']; | ||
const rawRefreshPeriod = result['fc:frame:refresh_period']; | ||
const refreshPeriod = rawRefreshPeriod ? parseInt(rawRefreshPeriod, 10) : undefined; | ||
const state = rawState ? JSON.parse(result['fc:frame:state']) : undefined; | ||
|
||
return { buttons: buttons as any, image, input, postUrl, state, refreshPeriod }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters