Skip to content

Commit

Permalink
Use native FormData when uploading to Phrase (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
jahredhope authored Jul 11, 2024
1 parent bbe6669 commit 17cc753
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-fans-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vocab/cli': patch
---

Remove unused dependency on `form-data` npm package
9 changes: 9 additions & 0 deletions .changeset/strange-candles-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@vocab/phrase': patch
---

Fix forbidden errors when pushing translations

Migrate from `form-data` npm package to the native [Node FormData class](https://nodejs.org/api/globals.html#class-formdata) to ensure compatibility with the earlier move to native Fetch.

Mixing the two was causing some consumers to receive 503 Forbidden errors when pushing translations to Phrase.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ package-lock.json
yarn-error.log
yarn.lock
.eslintcache

.env
10 changes: 9 additions & 1 deletion fixtures/phrase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@
"description": "A playground reading and writing files for Phrase tests",
"version": "1.0.0",
"author": "SEEK",
"private": true
"private": true,
"scripts": {
"push": "dotenv vocab push",
"pull": "dotenv vocab pull"
},
"dependencies": {
"@vocab/cli": "workspace:*",
"dotenv-cli": "^7.2.1"
}
}
4 changes: 4 additions & 0 deletions fixtures/phrase/vocab.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
devLanguage: 'en',
languages: [{ name: 'en' }, { name: 'fr' }],
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"copy-readme-to-packages": "tsx scripts/copy-readme-to-packages",
"start-fixture": "tsx test-helpers/src/start-fixture",
"run-server-fixture": "tsx test-helpers/src/run-server-fixture",
"compile-fixtures": "pnpm --filter @vocab-fixtures/* compile"
"compile-fixtures": "pnpm --filter @vocab-fixtures/* compile",
"fixture:phrase:push": "pnpm run --filter @vocab-fixtures/phrase push",
"fixture:phrase:pull": "pnpm run --filter @vocab-fixtures/phrase pull"
},
"dependencies": {
"@babel/core": "^7.12.0",
Expand Down
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"@vocab/phrase": "workspace:^",
"env-ci": "^7.3.0",
"fast-glob": "^3.2.4",
"form-data": "^4.0.0",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion packages/phrase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"@vocab/core": "workspace:^",
"csv-stringify": "^6.2.3",
"debug": "^4.3.1",
"form-data": "^4.0.0",
"picocolors": "^1.0.0"
},
"devDependencies": {
Expand Down
23 changes: 12 additions & 11 deletions packages/phrase/src/phrase-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-console */
import FormData from 'form-data';
import type { TranslationsByLanguage } from '@vocab/core';
import { log, trace } from './logger';
import { translationsToCsv } from './csv';
Expand Down Expand Up @@ -129,22 +128,24 @@ export async function pushTranslations(
for (const [language, csvFileString] of Object.entries(csvFileStrings)) {
const formData = new FormData();

const fileContents = Buffer.from(csvFileString);
formData.append('file', fileContents, {
contentType: 'text/csv',
filename: `${language}.translations.csv`,
});
formData.append(
'file',
new Blob([csvFileString], {
type: 'text/csv',
}),
`${language}.translations.csv`,
);

formData.append('file_format', 'csv');
formData.append('branch', branch);
formData.append('update_translations', 'true');
formData.append('update_descriptions', 'true');

formData.append(`locale_mapping[${language}]`, messageIndex);
formData.append(`locale_mapping[${language}]`, messageIndex.toString());

formData.append('format_options[key_index]', keyIndex);
formData.append('format_options[comment_index]', commentIndex);
formData.append('format_options[tag_column]', tagColumn);
formData.append('format_options[key_index]', keyIndex.toString());
formData.append('format_options[comment_index]', commentIndex.toString());
formData.append('format_options[tag_column]', tagColumn.toString());
formData.append('format_options[enable_pluralization]', 'false');

log(`Uploading translations for language ${language}`);
Expand All @@ -160,7 +161,7 @@ export async function pushTranslations(
| undefined
>(`uploads`, {
method: 'POST',
body: formData.toString(),
body: formData,
});

trace('Upload result:\n', result);
Expand Down
38 changes: 31 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 17cc753

Please sign in to comment.