@animaapp/anima-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +20 -0
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +270 -0
- package/dist/index.js +3988 -0
- package/dist/index.js.map +1 -0
- package/package.json +38 -0
- package/src/anima.ts +240 -0
- package/src/codegenToAnimaFiles.ts +13 -0
- package/src/dataStream.ts +108 -0
- package/src/errors.ts +21 -0
- package/src/figma/figmaError.ts +115 -0
- package/src/figma/index.ts +2 -0
- package/src/figma/utils.ts +73 -0
- package/src/index.ts +8 -0
- package/src/settings.ts +56 -0
- package/src/types.ts +77 -0
- package/src/utils.ts +53 -0
- package/vite.config.ts +24 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { CodegenErrorReason } from "./errors";
|
|
2
|
+
import type { CodegenSettings } from "./settings";
|
|
3
|
+
|
|
4
|
+
export type AnimaFiles = Record<
|
|
5
|
+
string,
|
|
6
|
+
{
|
|
7
|
+
content: string;
|
|
8
|
+
isBinary: boolean;
|
|
9
|
+
}
|
|
10
|
+
>;
|
|
11
|
+
|
|
12
|
+
export type BaseResult = {
|
|
13
|
+
sessionId: string;
|
|
14
|
+
figmaFileName: string;
|
|
15
|
+
figmaSelectedFrameName: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type AnimaSDKResult = BaseResult & {
|
|
19
|
+
files: AnimaFiles;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type CodegenResult = BaseResult & {
|
|
23
|
+
files: Record<string, { code: string; type: "code" }>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type GetCodeParams = {
|
|
27
|
+
fileKey: string;
|
|
28
|
+
figmaToken: string;
|
|
29
|
+
nodesId: string[];
|
|
30
|
+
settings: CodegenSettings;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type GetCodeHandler =
|
|
34
|
+
| ((message: SSECodgenMessage) => void)
|
|
35
|
+
| {
|
|
36
|
+
onStart?: ({ sessionId }: { sessionId: string }) => void;
|
|
37
|
+
onPreCodegen?: ({ message }: { message: string }) => void;
|
|
38
|
+
onAssetsUploaded?: () => void;
|
|
39
|
+
onFigmaMetadata?: ({
|
|
40
|
+
figmaFileName,
|
|
41
|
+
figmaSelectedFrameName,
|
|
42
|
+
}: {
|
|
43
|
+
figmaFileName: string;
|
|
44
|
+
figmaSelectedFrameName: string;
|
|
45
|
+
}) => void;
|
|
46
|
+
onGeneratingCode?: ({
|
|
47
|
+
status,
|
|
48
|
+
progress,
|
|
49
|
+
files,
|
|
50
|
+
}: {
|
|
51
|
+
status: "success" | "running" | "failure";
|
|
52
|
+
progress: number;
|
|
53
|
+
files: AnimaFiles;
|
|
54
|
+
}) => void;
|
|
55
|
+
onCodegenCompleted?: () => void;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// TODO: `SSECodgenMessage` and `SSECodgenMessageErrorPayload` should be imported from `anima-public-api`
|
|
59
|
+
export type SSECodgenMessage =
|
|
60
|
+
| { type: "start"; sessionId: string }
|
|
61
|
+
| { type: "pre_codegen"; message: string }
|
|
62
|
+
| {
|
|
63
|
+
type: "figma_metadata";
|
|
64
|
+
figmaFileName: string;
|
|
65
|
+
figmaSelectedFrameName: string;
|
|
66
|
+
}
|
|
67
|
+
| { type: "generating_code"; payload: any }
|
|
68
|
+
| { type: "codegen_completed" }
|
|
69
|
+
| { type: "assets_uploaded" }
|
|
70
|
+
| { type: "aborted" }
|
|
71
|
+
| { type: "error"; payload: SSECodgenMessageErrorPayload }
|
|
72
|
+
| { type: "done" };
|
|
73
|
+
export type SSECodgenMessageErrorPayload = {
|
|
74
|
+
errorName: string;
|
|
75
|
+
task?: string;
|
|
76
|
+
reason: CodegenErrorReason;
|
|
77
|
+
};
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export const isValidFigmaUrl = (
|
|
2
|
+
figmaLink: string
|
|
3
|
+
): [hasCorrectPrefix: boolean, fileKey: string, nodeId: string] => {
|
|
4
|
+
if (!figmaLink) {
|
|
5
|
+
return [false, "", ""];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
const url = new URL(figmaLink);
|
|
10
|
+
const path = url.pathname;
|
|
11
|
+
|
|
12
|
+
if (url.origin !== "https://www.figma.com") {
|
|
13
|
+
return [false, "", ""];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const nodeId = (url.searchParams.get("node-id") ?? "").replace(/-/g, ":");
|
|
17
|
+
const fileKey = path.split("/")[2];
|
|
18
|
+
const hasCorrectPrefix =
|
|
19
|
+
(path.startsWith("/file/") &&
|
|
20
|
+
url.searchParams.get("type") !== "whiteboard") ||
|
|
21
|
+
path.startsWith("/design/");
|
|
22
|
+
|
|
23
|
+
return [hasCorrectPrefix && fileKey.length === 22, fileKey, nodeId];
|
|
24
|
+
} catch {
|
|
25
|
+
return [false, "", ""];
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const formatToFigmaLink = ({
|
|
30
|
+
fileKey,
|
|
31
|
+
nodeId,
|
|
32
|
+
}: {
|
|
33
|
+
fileKey: string;
|
|
34
|
+
nodeId: string;
|
|
35
|
+
}) => {
|
|
36
|
+
const url = new URL("https://www.figma.com");
|
|
37
|
+
url.pathname = `design/${fileKey}`;
|
|
38
|
+
|
|
39
|
+
if (nodeId) {
|
|
40
|
+
url.searchParams.set("node-id", nodeId.replace(":", "-"));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return url;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
class ResponseError extends Error {
|
|
47
|
+
response: Response;
|
|
48
|
+
|
|
49
|
+
constructor(message: string, res: Response) {
|
|
50
|
+
super(message);
|
|
51
|
+
this.response = res;
|
|
52
|
+
}
|
|
53
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineConfig } from "vite";
|
|
2
|
+
import dts from "vite-plugin-dts";
|
|
3
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
4
|
+
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
build: {
|
|
7
|
+
lib: {
|
|
8
|
+
entry: "./src/index.ts",
|
|
9
|
+
formats: ["cjs", "es"],
|
|
10
|
+
fileName: "index",
|
|
11
|
+
},
|
|
12
|
+
outDir: "./dist",
|
|
13
|
+
emptyOutDir: true,
|
|
14
|
+
target: "es6",
|
|
15
|
+
sourcemap: true,
|
|
16
|
+
},
|
|
17
|
+
plugins: [
|
|
18
|
+
tsconfigPaths(),
|
|
19
|
+
dts({
|
|
20
|
+
rollupTypes: true,
|
|
21
|
+
insertTypesEntry: true,
|
|
22
|
+
}),
|
|
23
|
+
],
|
|
24
|
+
});
|