Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type issue inside addStorage #227

Closed
dhoko opened this issue Feb 10, 2023 · 3 comments · Fixed by #228
Closed

Type issue inside addStorage #227

dhoko opened this issue Feb 10, 2023 · 3 comments · Fixed by #228

Comments

@dhoko
Copy link

dhoko commented Feb 10, 2023

hey 👋

I spent quite a few minutes trying to understand why my upload failed.
I was sending a JSON file and got a ... type: chrome from the API instead of the custom_fomat I was expecting (what I have if I upload via the UI).

Based on the source I had no idea what the lib wanted to have

So I looked into the tests and ok, you use a string https://github.com/crowdin/crowdin-api-client-js/blob/master/tests/uploadStorage/api.test.ts#L21

nope 😁

We must send a buffer in order to have the API doing its magic to get the correct type value (as sending a type=custom_format === 500 from the API)

Based on the API doc:

string (Binary file data) File content. Text of file or any other raw binary data.

I would expect a buffer


Can you add a custom type and describe what we can use, and what to expect?

Thx 🙏

@andrii-bodnar
Copy link
Member

Hi @dhoko,

try using the fs.readFileSync following the example in #209 for screenshots upload

You will need only to change the fileName and contentType depending on your file

@dhoko
Copy link
Author

dhoko commented Feb 10, 2023

Hum I looked at the debugger before changing the string to a buffer and they both have the correct content type, only diff between the 2 requests is the type of data inside the request.

I changed my code from:

  const storage = await uploadStorageApi.addStorage(path.basename(crowdinFile), await readFile(sourceFile, 'utf-8))

to

  const storage = await uploadStorageApi.addStorage(path.basename(crowdinFile), await readFile(sourceFile))

@yevheniyJ yevheniyJ linked a pull request Feb 11, 2023 that will close this issue
@yevheniyJ
Copy link
Collaborator

Fixed in v1.21.2
Now you can pass either Buffer or string and for both it will produce correct type

const contentString = fs.readFileSync(file, 'utf8');
const contentBuffer = fs.readFileSync(file);
//both options now will produce file with correct type
await client.uploadStorageApi.addStorage(file, contentBuffer);
//or
await client.uploadStorageApi.addStorage(file, contentString);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants