@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.
@@ -0,0 +1,270 @@
1
+ import { Component } from '@figma/rest-api-spec';
2
+ import { ComponentSet } from '@figma/rest-api-spec';
3
+ import { FigmaRestApi } from '@animaapp/http-client-figma';
4
+ import { GetFileResponse } from '@figma/rest-api-spec';
5
+ import { Node as Node_2 } from '@figma/rest-api-spec';
6
+ import { Style } from '@figma/rest-api-spec';
7
+
8
+ export declare class Anima {
9
+ #private;
10
+ constructor({ auth, apiBaseAddress, }?: {
11
+ auth?: Auth;
12
+ apiBaseAddress?: string;
13
+ path?: string;
14
+ });
15
+ protected hasAuth(): boolean;
16
+ set auth(auth: Auth);
17
+ protected get headers(): Record<string, string>;
18
+ generateCode(params: GetCodeParams, handler?: GetCodeHandler): Promise<AnimaSDKResult>;
19
+ }
20
+
21
+ export declare type AnimaFiles = Record<string, {
22
+ content: string;
23
+ isBinary: boolean;
24
+ }>;
25
+
26
+ export declare type AnimaSDKResult = BaseResult & {
27
+ files: AnimaFiles;
28
+ };
29
+
30
+ export declare type Auth = {
31
+ token: string;
32
+ teamId: string;
33
+ } | {
34
+ token: string;
35
+ userId?: string;
36
+ };
37
+
38
+ export declare type BaseResult = {
39
+ sessionId: string;
40
+ figmaFileName: string;
41
+ figmaSelectedFrameName: string;
42
+ };
43
+
44
+ export declare class CodegenError extends Error {
45
+ status?: number;
46
+ constructor({ name, reason, status }: {
47
+ name: string;
48
+ reason: CodegenErrorReason;
49
+ status?: number;
50
+ });
51
+ }
52
+
53
+ export declare type CodegenErrorReason = "The selected node is not a frame" | "There is no node with the given id" | "Invalid Figma token" | "Figma token expired" | "No files found" | "Connection closed before the 'done' message" | "Unknown" | "Response body is null";
54
+
55
+ export declare type CodegenResult = BaseResult & {
56
+ files: Record<string, {
57
+ code: string;
58
+ type: "code";
59
+ }>;
60
+ };
61
+
62
+ export declare type CodegenSettings = {
63
+ language?: "typescript" | "javascript";
64
+ framework: "react" | "html";
65
+ styling: "plain_css" | "css_modules" | "styled_components" | "tailwind" | "sass" | "scss" | "inline_styles";
66
+ uiLibrary?: "mui" | "antd" | "radix" | "shadcn";
67
+ enableTranslation?: boolean;
68
+ };
69
+
70
+ export declare const convertCodegenFilesToAnimaFiles: (codegenFiles: Record<string, {
71
+ code: string;
72
+ type: "code";
73
+ }>) => AnimaFiles;
74
+
75
+ /**
76
+ * Creates a Server-Sent Events (SSE) `Response` that forwards all messages from the code generation stream.
77
+ *
78
+ * But, if the first message indicates an error (e.g., connection failed), the function returns a 500 response with the error message.
79
+ *
80
+ * @param {Anima} anima - The Anima instance to use for creating the data stream.
81
+ * @param {GetCodeParams} params - The parameters for the code generation request.
82
+ * @returns {Promise<Response>} - A promise that resolves to an HTTP response.
83
+ */
84
+ export declare const createCodegenResponseEventStream: (anima: Anima, params: GetCodeParams) => Promise<Response>;
85
+
86
+ /**
87
+ * Start the code generation and creates a ReadableStream to output its result.
88
+ *
89
+ * The stream is closed when the codegen ends.
90
+ *
91
+ * @param {Anima} anima - An Anima service instance to generate the code from.
92
+ * @param {GetCodeParams} params - Parameters required for the code generation process.
93
+ * @returns {ReadableStream<StreamCodgenMessage>} - A ReadableStream that emits messages related to the code generation process.
94
+ */
95
+ export declare const createCodegenStream: (anima: Anima, params: GetCodeParams) => ReadableStream<StreamCodgenMessage>;
96
+
97
+ export declare type FigmaApiErrorType = "FigmaTokenIssue" | "RateLimitExceeded" | "NotFound" | "UnknownFigmaApiException";
98
+
99
+ export declare type FigmaNode = Node_2;
100
+
101
+ export declare type FigmaPage = {
102
+ id: string;
103
+ name: string;
104
+ };
105
+
106
+ export declare class FigmaTokenIssue extends Error {
107
+ fileKey: string;
108
+ reason: string;
109
+ constructor({ fileKey, reason }: {
110
+ fileKey: string;
111
+ reason: string;
112
+ });
113
+ }
114
+
115
+ export declare const formatToFigmaLink: ({ fileKey, nodeId, }: {
116
+ fileKey: string;
117
+ nodeId: string;
118
+ }) => URL;
119
+
120
+ export declare type GetCodeHandler = ((message: SSECodgenMessage) => void) | {
121
+ onStart?: ({ sessionId }: {
122
+ sessionId: string;
123
+ }) => void;
124
+ onPreCodegen?: ({ message }: {
125
+ message: string;
126
+ }) => void;
127
+ onAssetsUploaded?: () => void;
128
+ onFigmaMetadata?: ({ figmaFileName, figmaSelectedFrameName, }: {
129
+ figmaFileName: string;
130
+ figmaSelectedFrameName: string;
131
+ }) => void;
132
+ onGeneratingCode?: ({ status, progress, files, }: {
133
+ status: "success" | "running" | "failure";
134
+ progress: number;
135
+ files: AnimaFiles;
136
+ }) => void;
137
+ onCodegenCompleted?: () => void;
138
+ };
139
+
140
+ export declare type GetCodeParams = {
141
+ fileKey: string;
142
+ figmaToken: string;
143
+ nodesId: string[];
144
+ settings: CodegenSettings;
145
+ };
146
+
147
+ export declare const getFigmaApiErrorType: (error: Error) => FigmaApiErrorType;
148
+
149
+ export declare const getFigmaFile: ({ fileKey, authToken, figmaRestApi, params, }: GetFilePagesParams) => Promise<GetFigmaFileResult>;
150
+
151
+ export declare type GetFigmaFileResult = GetFileResponse | undefined;
152
+
153
+ export declare const getFileNodes: ({ fileKey, authToken, nodeIds, figmaRestApi, params, }: GetFileNodesParams) => Promise<{
154
+ [key: string]: {
155
+ document: Node_2;
156
+ components: {
157
+ [key: string]: Component;
158
+ };
159
+ componentSets: {
160
+ [key: string]: ComponentSet;
161
+ };
162
+ schemaVersion: number;
163
+ styles: {
164
+ [key: string]: Style;
165
+ };
166
+ };
167
+ }>;
168
+
169
+ export declare type GetFileNodesParams = {
170
+ fileKey: string;
171
+ authToken?: string;
172
+ nodeIds: string[];
173
+ figmaRestApi?: FigmaRestApi;
174
+ params?: Record<string, any>;
175
+ };
176
+
177
+ export declare type GetFilePagesParams = {
178
+ fileKey: string;
179
+ authToken?: string;
180
+ figmaRestApi?: FigmaRestApi;
181
+ params?: Record<string, any>;
182
+ };
183
+
184
+ export declare type GetFilePagesResult = FigmaPage[] | undefined;
185
+
186
+ export declare type GetFileParams = {
187
+ fileKey: string;
188
+ authToken?: string;
189
+ figmaRestApi?: FigmaRestApi;
190
+ };
191
+
192
+ export declare const handleFigmaApiError: (error: any, fileKey: string) => never;
193
+
194
+ export declare const isFigmaTokenIssue: (error: Error) => boolean;
195
+
196
+ export declare const isNotFound: (error: Error) => boolean;
197
+
198
+ export declare const isRateLimitExceeded: (error: Error) => boolean;
199
+
200
+ export declare const isUnknownFigmaApiException: (error: Error) => boolean;
201
+
202
+ export declare const isValidFigmaUrl: (figmaLink: string) => [hasCorrectPrefix: boolean, fileKey: string, nodeId: string];
203
+
204
+ export declare class NotFound extends Error {
205
+ fileKey: string;
206
+ constructor({ fileKey }: {
207
+ fileKey: string;
208
+ });
209
+ }
210
+
211
+ export declare class RateLimitExceeded extends Error {
212
+ fileKey: string;
213
+ constructor({ fileKey }: {
214
+ fileKey: string;
215
+ });
216
+ }
217
+
218
+ export declare type SSECodgenMessage = {
219
+ type: "start";
220
+ sessionId: string;
221
+ } | {
222
+ type: "pre_codegen";
223
+ message: string;
224
+ } | {
225
+ type: "figma_metadata";
226
+ figmaFileName: string;
227
+ figmaSelectedFrameName: string;
228
+ } | {
229
+ type: "generating_code";
230
+ payload: any;
231
+ } | {
232
+ type: "codegen_completed";
233
+ } | {
234
+ type: "assets_uploaded";
235
+ } | {
236
+ type: "aborted";
237
+ } | {
238
+ type: "error";
239
+ payload: SSECodgenMessageErrorPayload;
240
+ } | {
241
+ type: "done";
242
+ };
243
+
244
+ export declare type SSECodgenMessageErrorPayload = {
245
+ errorName: string;
246
+ task?: string;
247
+ reason: CodegenErrorReason;
248
+ };
249
+
250
+ export declare type StreamCodgenMessage = Exclude<SSECodgenMessage, {
251
+ type: "error";
252
+ }> | {
253
+ type: "error";
254
+ payload: {
255
+ message: CodegenErrorReason;
256
+ status?: number;
257
+ };
258
+ };
259
+
260
+ export declare class UnknownFigmaApiException extends Error {
261
+ fileKey: string;
262
+ constructor({ fileKey, cause }: {
263
+ fileKey: string;
264
+ cause: unknown;
265
+ });
266
+ }
267
+
268
+ export declare const validateSettings: (obj: unknown) => CodegenSettings;
269
+
270
+ export { }