|
| 1 | +import { designToCode, Result } from "@designto/code"; |
| 2 | +import { DesignInput } from "@grida/builder-config/input"; |
| 3 | +import { parseFileAndNodeId } from "@design-sdk/figma-url"; |
| 4 | +import { fetchTargetAsReflect } from "@design-sdk/figma-remote"; |
| 5 | +import { |
| 6 | + ImageRepository, |
| 7 | + MainImageRepository, |
| 8 | +} from "@design-sdk/asset-repository"; |
| 9 | +import { RemoteImageRepositories } from "@design-sdk/figma-remote/asset-repository"; |
| 10 | +import type { FrameworkConfig } from "@grida/builder-config"; |
| 11 | +import { defaultConfigByFramework } from "@grida/builder-config-preset"; |
| 12 | +import { Language } from "@grida/builder-platform-types"; |
| 13 | +import { formatCode } from "dart-style"; |
| 14 | + |
| 15 | +type FigmaAccessTokenType = "at" | "pat"; |
| 16 | + |
| 17 | +export default async function handler(req, res) { |
| 18 | + // accept only post request |
| 19 | + if (req.method !== "POST") { |
| 20 | + res.status(405).json({ message: "method not allowed" }); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const figma_access_token: string = req.headers["x-figma-token"]; |
| 25 | + |
| 26 | + if (!figma_access_token) { |
| 27 | + res.status(401).json({ |
| 28 | + message: "No figma access token provided.", |
| 29 | + }); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const figma_access_token_type: FigmaAccessTokenType = |
| 34 | + figma_access_token.startsWith("figd") ? "pat" : "at"; |
| 35 | + |
| 36 | + const { uri, framework } = req.body; |
| 37 | + try { |
| 38 | + const src = await code({ |
| 39 | + uri, |
| 40 | + framework, |
| 41 | + auth: |
| 42 | + figma_access_token_type === "at" |
| 43 | + ? { |
| 44 | + accessToken: figma_access_token, |
| 45 | + } |
| 46 | + : { |
| 47 | + personalAccessToken: figma_access_token, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + res.status(200).json({ |
| 52 | + src, |
| 53 | + }); |
| 54 | + } catch (e) { |
| 55 | + res.status(500).json({ |
| 56 | + message: e.message, |
| 57 | + stacktrace: e.stack, |
| 58 | + }); |
| 59 | + |
| 60 | + throw e; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +async function code({ |
| 65 | + auth, |
| 66 | + uri, |
| 67 | + framework, |
| 68 | +}: { |
| 69 | + auth: |
| 70 | + | { |
| 71 | + personalAccessToken: string; |
| 72 | + } |
| 73 | + | { accessToken: string }; |
| 74 | + uri: string; |
| 75 | + framework: FrameworkConfig; |
| 76 | +}) { |
| 77 | + // |
| 78 | + |
| 79 | + const res = parseFileAndNodeId(uri as string); |
| 80 | + if (res) { |
| 81 | + const { file: filekey, node } = res; |
| 82 | + |
| 83 | + console.log(`filekey: ${filekey}`, `node: ${node}`); |
| 84 | + |
| 85 | + // |
| 86 | + const target = await fetchTargetAsReflect({ |
| 87 | + file: filekey, |
| 88 | + node, |
| 89 | + auth: auth, |
| 90 | + }); |
| 91 | + |
| 92 | + MainImageRepository.instance = new RemoteImageRepositories(target.file, { |
| 93 | + authentication: auth, |
| 94 | + }); |
| 95 | + MainImageRepository.instance.register( |
| 96 | + new ImageRepository( |
| 97 | + "fill-later-assets", |
| 98 | + "grida://assets-reservation/images/" |
| 99 | + ) |
| 100 | + ); |
| 101 | + |
| 102 | + const code = await designToCode({ |
| 103 | + input: DesignInput.fromApiResponse({ |
| 104 | + raw: target.raw, |
| 105 | + entry: target.reflect!, |
| 106 | + }), |
| 107 | + framework: { |
| 108 | + // fill missing configurations. |
| 109 | + ...defaultConfigByFramework(framework.framework), |
| 110 | + ...framework, |
| 111 | + }, |
| 112 | + asset_config: { asset_repository: MainImageRepository.instance }, |
| 113 | + }); |
| 114 | + |
| 115 | + const src = postproc_src( |
| 116 | + filesrc(code, framework.framework), |
| 117 | + framework.language |
| 118 | + ); |
| 119 | + |
| 120 | + return src; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +function filesrc( |
| 125 | + code: Result, |
| 126 | + framework: FrameworkConfig["framework"] |
| 127 | +): string { |
| 128 | + switch (framework) { |
| 129 | + case "flutter": { |
| 130 | + return code.code.raw; |
| 131 | + } |
| 132 | + default: |
| 133 | + return code.scaffold.raw; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +function postproc_src(src: string, language: Language) { |
| 138 | + if (language === Language.dart) { |
| 139 | + const { code, error } = formatCode(src); |
| 140 | + if (error) { |
| 141 | + return src; |
| 142 | + } |
| 143 | + return code; |
| 144 | + } |
| 145 | + |
| 146 | + return src; |
| 147 | +} |
0 commit comments