Skip to content

Commit

Permalink
fix: sharedPluginData from Figma response could be undefined (#664)
Browse files Browse the repository at this point in the history
* fix: `sharedPluginData` from Figma response could be undefined

* chore: add changeset
  • Loading branch information
mark-tate authored Sep 23, 2024
1 parent 9124a6c commit 749b326
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .changeset/rich-queens-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@jpmorganchase/mosaic-source-figma': patch
---

fix Figma source JSON issue

If a Figma page did not defined `sharedPluginData` then the Figma source would break
31 changes: 11 additions & 20 deletions packages/source-figma/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,7 @@ const FigmaSource: Source<FigmaSourceOptions, FigmaPage> = {
create(options, sourceConfig) {
const parsedOptions = validateMosaicSchema(schema, options);

const {
endpoints,
projects,
figmaToken,
proxyEndpoint,
prefixDir,
requestHeaders: requestHeadersParam
} = parsedOptions;
const { endpoints, projects, figmaToken, prefixDir, ...rest } = parsedOptions;

const projectEndpoints: Record<string, string> = {};
const projectById: Record<string, typeof projects[number]> = {};
Expand Down Expand Up @@ -120,9 +113,7 @@ const FigmaSource: Source<FigmaSourceOptions, FigmaPage> = {
index: number,
transformerOptions: ProjectsTransformerResult[]
) => {
const {
document: { sharedPluginData }
} = response;
const { document: { sharedPluginData = {} } = {} } = response;
const sourceProvidedMetadata = transformerOptions[index].meta ?? {};
const patternPrefix = getPatternPrefix(sourceProvidedMetadata);
return Object.keys(sharedPluginData).reduce<FigmaPage[]>((figmaPagesResult, patternId) => {
Expand Down Expand Up @@ -174,13 +165,13 @@ const FigmaSource: Source<FigmaSourceOptions, FigmaPage> = {
const projects$ = createHttpSource<ProjectsResponse, ProjectsTransformerResult>(
{
endpoints: Object.values(projectEndpoints),
prefixDir,
...rest,
requestHeaders: {
'Content-Type': 'application/json',
'X-FIGMA-TOKEN': figmaToken,
...requestHeadersParam
...rest.requestHeaders
},
proxyEndpoint,
prefixDir,
transformer: projectsTransformer,
transformerOptions: { projectIds: Object.keys(projectEndpoints) }
},
Expand All @@ -193,13 +184,13 @@ const FigmaSource: Source<FigmaSourceOptions, FigmaPage> = {

return createHttpSource<ProjectFilesResponse, FigmaPage>({
endpoints: fileUrls,
prefixDir,
...rest,
requestHeaders: {
'Content-Type': 'application/json',
'X-FIGMA-TOKEN': figmaToken,
...requestHeadersParam
...rest.requestHeaders
},
proxyEndpoint,
prefixDir,
transformer: projectFilesTransformer,
transformerOptions: projectFiles
});
Expand Down Expand Up @@ -227,13 +218,13 @@ const FigmaSource: Source<FigmaSourceOptions, FigmaPage> = {
});
return createHttpSource<GenerateThumbnailResponse, FigmaPage>({
endpoints: thumbnailRequestUrls,
prefixDir,
...rest,
requestHeaders: {
'Content-Type': 'application/json',
'X-FIGMA-TOKEN': figmaToken,
...requestHeadersParam
...rest.requestHeaders
},
proxyEndpoint,
prefixDir,
transformer: generateThumbnailTransformer,
transformerOptions: { fileIds: Object.keys(thumbnailNodes), pages: figmaPages }
});
Expand Down

0 comments on commit 749b326

Please sign in to comment.