1
- import { FigmaRuntimeContext } from " ./types" ;
1
+ import { FigmaRuntimeContext } from ' ./types' ;
2
2
3
3
export interface FileNodeId {
4
4
fileId : string ;
@@ -17,16 +17,16 @@ interface FigmaAPINodes extends FigmaAPIFile {
17
17
[ key : string ] : {
18
18
document ?: {
19
19
name ?: string ;
20
- }
21
- }
22
- }
20
+ } ;
21
+ } ;
22
+ } ;
23
23
}
24
24
25
25
// https://www.figma.com/developers/api#get-images-endpoint
26
26
interface FigmaAPIImages {
27
27
images : {
28
28
[ key : string ] : string ;
29
- }
29
+ } ;
30
30
}
31
31
32
32
/**
@@ -55,17 +55,21 @@ export function extractNodeFromURL(input: string): FileNodeId | undefined {
55
55
/**
56
56
* Fetch the informations about a Figma node.
57
57
*/
58
- export async function fetchFigmaNode ( fileId : string , nodeId : string , context : FigmaRuntimeContext ) {
58
+ export async function fetchFigmaNode ( fileId : string , nodeId : string , context : FigmaRuntimeContext ) {
59
59
try {
60
60
const [ node , image ] = await Promise . all ( [
61
- fetchFigmaAPI < FigmaAPINodes > ( `files/${ fileId } /nodes` , { ids : nodeId , depth : 1 } , context ) ,
62
- fetchFigmaImage ( fileId , nodeId , context )
61
+ fetchFigmaAPI < FigmaAPINodes > (
62
+ `files/${ fileId } /nodes` ,
63
+ { ids : nodeId , depth : 1 } ,
64
+ context
65
+ ) ,
66
+ fetchFigmaImage ( fileId , nodeId , context ) ,
63
67
] ) ;
64
68
return {
65
69
name : node . name ,
66
70
thumbnailUrl : node . thumbnailUrl ,
67
71
nodeName : node . nodes [ nodeId ] ?. document ?. name ,
68
- nodeImage : image
72
+ nodeImage : image ,
69
73
} ;
70
74
} catch ( err ) {
71
75
return undefined ;
@@ -75,9 +79,17 @@ export function extractNodeFromURL(input: string): FileNodeId | undefined {
75
79
/**
76
80
* Fetch the preview image for a node.
77
81
*/
78
- export async function fetchFigmaImage ( fileId : string , nodeId : string , context : FigmaRuntimeContext ) {
82
+ export async function fetchFigmaImage (
83
+ fileId : string ,
84
+ nodeId : string ,
85
+ context : FigmaRuntimeContext
86
+ ) {
79
87
try {
80
- const image = await fetchFigmaAPI < FigmaAPIImages > ( `images/${ fileId } ` , { ids : nodeId , format : 'svg' } , context ) ;
88
+ const image = await fetchFigmaAPI < FigmaAPIImages > (
89
+ `images/${ fileId } ` ,
90
+ { ids : nodeId , format : 'svg' } ,
91
+ context
92
+ ) ;
81
93
const imageUrl = image . images ?. [ nodeId ] ;
82
94
if ( ! imageUrl ) {
83
95
return undefined ;
@@ -86,12 +98,12 @@ export function extractNodeFromURL(input: string): FileNodeId | undefined {
86
98
const response = await fetch ( imageUrl ) ;
87
99
const svgText = await response . text ( ) ;
88
100
89
- const [ , width , height ] = svgText . match ( / v i e w B o x = " 0 0 ( \d + ) ( \d + ) " / ) || [ ] ;
101
+ const [ , width , height ] = svgText . match ( / v i e w B o x = " 0 0 ( \d + ) ( \d + ) " / ) || [ ] ;
90
102
91
103
return {
92
104
width : parseInt ( width , 10 ) ,
93
105
height : parseInt ( height , 10 ) ,
94
- url : imageUrl
106
+ url : imageUrl ,
95
107
} ;
96
108
} catch ( err ) {
97
109
return undefined ;
@@ -113,7 +125,11 @@ export async function fetchFigmaFile(fileId: string, context: FigmaRuntimeContex
113
125
/**
114
126
* Execute a Figma API request.
115
127
*/
116
- export async function fetchFigmaAPI < T > ( path : string , params : object , { environment } : FigmaRuntimeContext ) : Promise < T > {
128
+ export async function fetchFigmaAPI < T > (
129
+ path : string ,
130
+ params : object ,
131
+ { environment } : FigmaRuntimeContext
132
+ ) : Promise < T > {
117
133
const url = new URL ( `https://api.figma.com/v1/${ path } ` ) ;
118
134
119
135
Object . entries ( params ) . forEach ( ( [ key , value ] ) => {
@@ -122,7 +138,7 @@ export async function fetchFigmaAPI<T>(path: string, params: object, { environme
122
138
123
139
const response = await fetch ( url . toString ( ) , {
124
140
headers : {
125
- ' Authorization' : `Bearer ${ environment . installation . configuration . oauth_credentials . access_token } ` ,
141
+ Authorization : `Bearer ${ environment . installation . configuration . oauth_credentials . access_token } ` ,
126
142
} ,
127
143
} ) ;
128
144
0 commit comments