@animaapp/anima-sdk-react 0.1.3 → 0.2.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@animaapp/anima-sdk-react",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "description": "Anima's JavaScript utilities library",
6
6
  "author": "Anima App, Inc.",
@@ -4,14 +4,15 @@ import type {
4
4
  GetCodeParams,
5
5
  StreamCodgenMessage,
6
6
  } from "@animaapp/anima-sdk";
7
- import { convertCodegenFilesToAnimaFiles } from "@animaapp/anima-sdk";
8
7
  import { EventSource } from "eventsource";
9
8
  import { useImmer } from "use-immer";
10
9
 
10
+ type LocalAssetsStorage =
11
+ | { strategy: "local"; path: string }
12
+ | { strategy: "local"; filePath: string; referencePath: string };
13
+
11
14
  export type UseAnimaParams = Omit<GetCodeParams, "assetsStorage"> & {
12
- assetsStorage?:
13
- | GetCodeParams["assetsStorage"]
14
- | { strategy: "local"; path: string };
15
+ assetsStorage?: GetCodeParams["assetsStorage"] | LocalAssetsStorage;
15
16
  };
16
17
 
17
18
  type Status = "idle" | "pending" | "success" | "aborted" | "error";
@@ -45,6 +46,26 @@ type StreamMessageByType<T extends StreamCodgenMessage["type"]> = Extract<
45
46
  { type: T }
46
47
  >;
47
48
 
49
+ const getAssetsLocalStrategyParams = (
50
+ localAssetsStorage: LocalAssetsStorage
51
+ ) => {
52
+ if ("path" in localAssetsStorage) {
53
+ return {
54
+ filePath: localAssetsStorage.path.replace(/^\//, ""),
55
+ referencePath:
56
+ localAssetsStorage.path === "/" ? "" : localAssetsStorage.path, // Workaround to avoid duplicated slashes in the URL. Ideally, the fix should be done in Codegen.
57
+ };
58
+ }
59
+
60
+ return {
61
+ filePath: localAssetsStorage.filePath.replace(/^\//, ""),
62
+ referencePath:
63
+ localAssetsStorage.referencePath === "/"
64
+ ? ""
65
+ : localAssetsStorage.referencePath,
66
+ };
67
+ };
68
+
48
69
  export const useAnimaCodegen = ({
49
70
  url,
50
71
  method = "POST",
@@ -67,9 +88,13 @@ export const useAnimaCodegen = ({
67
88
  const initialParams = structuredClone(params);
68
89
 
69
90
  if (params.assetsStorage?.strategy === "local") {
91
+ const { referencePath } = getAssetsLocalStrategyParams(
92
+ params.assetsStorage
93
+ );
94
+
70
95
  params.assetsStorage = {
71
96
  strategy: "external",
72
- url: params.assetsStorage.path,
97
+ url: referencePath,
73
98
  };
74
99
  }
75
100
 
@@ -134,12 +159,7 @@ export const useAnimaCodegen = ({
134
159
  event.data
135
160
  ) as StreamMessageByType<"generating_code">;
136
161
  if (message.payload.status === "success") {
137
- const codegenFiles = message.payload.files as Record<
138
- string,
139
- { code: string; type: "code" }
140
- >;
141
-
142
- result.files = convertCodegenFilesToAnimaFiles(codegenFiles);
162
+ result.files = message.payload.files;
143
163
  }
144
164
 
145
165
  updateStatus((draft) => {
@@ -220,6 +240,10 @@ export const useAnimaCodegen = ({
220
240
  initialParams.assetsStorage?.strategy === "local" &&
221
241
  result?.assets?.length
222
242
  ) {
243
+ const { filePath } = getAssetsLocalStrategyParams(
244
+ initialParams.assetsStorage
245
+ );
246
+
223
247
  const downloadAssetsPromises = result.assets.map(async (asset) => {
224
248
  const response = await fetch(asset.url);
225
249
  const buffer = await response.arrayBuffer();
@@ -237,7 +261,8 @@ export const useAnimaCodegen = ({
237
261
 
238
262
  assetsList[assetName] = base64;
239
263
 
240
- result.files[`${initialParams.assetsStorage.path}/${assetName}`] = {
264
+ const assetPath = filePath ? `${filePath}/${assetName}` : assetName;
265
+ result.files[assetPath] = {
241
266
  content: base64,
242
267
  isBinary: true,
243
268
  };
package/src/utils.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export const arrayBufferToBase64 = (buffer: ArrayBuffer) => {
2
- var binary = "";
3
- var bytes = new Uint8Array(buffer);
4
- var len = bytes.byteLength;
5
- for (var i = 0; i < len; i++) {
2
+ let binary = "";
3
+ const bytes = new Uint8Array(buffer);
4
+ const len = bytes.byteLength;
5
+ for (let i = 0; i < len; i++) {
6
6
  binary += String.fromCharCode(bytes[i]);
7
7
  }
8
8