Skip to content

Commit

Permalink
chore: replace node-fetch by fetch included with node
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulGOUX27 committed Jul 3, 2024
1 parent c2d3ed0 commit 5480c46
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 74 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [20.x, 22.x]
os: [ubuntu-latest]

steps:
Expand Down Expand Up @@ -46,12 +46,13 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12.x
node-version: 20.x
- run: yarn
- run: yarn build
- name: Semantic Release
uses: cycjimmy/semantic-release-action@v2
with:
semantic_version: 19
branches: |
[
'+([0-9])?(.{+([0-9]),x}).x',
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ node_modules/

# Source
.vscode
.idea
dist
**/*.css
!*.config.js
Expand Down
12 changes: 4 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@treatwell/wti",
"version": "1.3.1",
"version": "1.4.0",
"description": "A WebTranslateIt command-line tool in Node.js",
"keywords": [
"wti",
Expand Down Expand Up @@ -59,12 +59,10 @@
"@oclif/plugin-not-found": "^1.2.4",
"@oclif/plugin-warn-if-update-available": "^1.7.0",
"cli-ux": "^5.5.0",
"form-data": "^3.0.0",
"git-root-dir": "^1.0.2",
"kleur": "^4.1.1",
"listr": "^0.14.3",
"make-error": "^1.3.6",
"node-fetch": "3.0.0-beta.9"
"make-error": "^1.3.6"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
Expand All @@ -85,7 +83,6 @@
"husky": "^4.3.0",
"import-sort-cli": "^6.0.0",
"import-sort-parser-typescript": "^6.0.0",
"import-sort-style-leboncoin": "^1.0.3",
"jest": "^26.4.2",
"jest-junit": "^11.1.0",
"lint-staged": "^10.4.0",
Expand All @@ -97,7 +94,7 @@
"typescript": "^4.0.2"
},
"engines": {
"node": ">= 8.0.0"
"node": ">= 18.0.0"
},
"preferGlobal": true,
"publishConfig": {
Expand All @@ -106,8 +103,7 @@
},
"importSort": {
".ts": {
"parser": "typescript",
"style": "leboncoin"
"parser": "typescript"
}
},
"oclif": {
Expand Down
22 changes: 6 additions & 16 deletions src/helpers/wti.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fetch from 'node-fetch';

import { getConfig } from '.';

const WTI_API_URL = 'https://webtranslateit.com/api/projects';
Expand All @@ -17,11 +15,9 @@ export type WtiErrorResponse = {
export const wtiGet = async (path: string) => {
const config = await getConfig();

const response = await fetch(
return await fetch(
`${WTI_API_URL}/${config.project.apiKey}${path}`
);

return response;
};

/**
Expand All @@ -30,18 +26,16 @@ export const wtiGet = async (path: string) => {
* @param path path of the API call
* @param data data to be sent in the request body
*/
export const wtiPost = async (path: string, data: fetch.BodyInit) => {
export const wtiPost = async (path: string, data: BodyInit) => {
const config = await getConfig();

const response = await fetch(
return fetch(
`${WTI_API_URL}/${config.project.apiKey}${path}`,
{
method: 'POST',
body: data,
}
);

return response;
};

/**
Expand All @@ -50,18 +44,16 @@ export const wtiPost = async (path: string, data: fetch.BodyInit) => {
* @param path path of the API call
* @param data data to be sent in the request body
*/
export const wtiPut = async (path: string, data: fetch.BodyInit) => {
export const wtiPut = async (path: string, data: BodyInit) => {
const config = await getConfig();

const response = await fetch(
return fetch(
`${WTI_API_URL}/${config.project.apiKey}${path}`,
{
method: 'PUT',
body: data,
}
);

return response;
};

/**
Expand All @@ -72,12 +64,10 @@ export const wtiPut = async (path: string, data: fetch.BodyInit) => {
export const wtiDelete = async (path: string) => {
const config = await getConfig();

const response = await fetch(
return fetch(
`${WTI_API_URL}/${config.project.apiKey}${path}`,
{
method: 'DELETE',
}
);

return response;
};
4 changes: 1 addition & 3 deletions src/models/Locale.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import FormData from 'form-data';

import { wtiPost, WtiErrorResponse, wtiDelete } from '../helpers';
import { WtiErrorResponse, wtiDelete, wtiPost } from '../helpers';

export class Locale {
private _locale: string;
Expand Down
33 changes: 21 additions & 12 deletions src/models/MasterFile.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import fs from 'fs';
import FormData from 'form-data';

import { MasterFileAlreadyExistsError } from '../errors';
import {
wtiPost,
wtiPut,
WtiErrorResponse,
wtiDelete,
wtiGet,
WtiErrorResponse,
wtiPost,
wtiPut,
} from '../helpers';

import { MasterFileAlreadyExistsError } from '../errors';
import { WTIProjectFile } from './types';
import fs from 'fs';

type Translations = {
[key: string]: Translations | string;
Expand Down Expand Up @@ -52,13 +51,17 @@ export class MasterFile {

public static async create(name: string) {
try {
const file = fs.createReadStream(process.cwd() + `/${name}`);
const file = fs.readFileSync(process.cwd() + `/${name}`);

const formData = new FormData();
formData.append('file', file);
formData.append('file', new Blob([file]), name);
formData.append('name', name);

const response = await wtiPost('/files', formData);
if (!response.ok) {
throw new Error(`Failed to push new master file: ${await response.text()}`);
}

const fileId = await response.text();

if (fileId) {
Expand All @@ -79,6 +82,9 @@ export class MasterFile {
const response = await wtiGet(
`/files/${this._masterFileId}/locales/${file.locale_code}`
);
if (!response.ok) {
throw new Error(`Failed to get master file: ${await response.text()}`);
}

const result = (await response.json()) as Translations;

Expand All @@ -102,10 +108,10 @@ export class MasterFile {
}
) {
try {
const file = fs.createReadStream(process.cwd() + `/${name}`);
const file = fs.readFileSync(process.cwd() + `/${name}`);

const formData = new FormData();
formData.append('file', file);
formData.append('file', new Blob([file]), name);
formData.append('name', name);
if (options.merge) {
formData.append('merge', 'true');
Expand All @@ -117,7 +123,10 @@ export class MasterFile {
formData.append('label', options.label);
}

await wtiPut(`/files/${this._masterFileId}/locales/${locale}`, formData);
const response = await wtiPut(`/files/${this._masterFileId}/locales/${locale}`, formData);
if (!response.ok) {
throw new Error(`Failed to get master file: ${await response.text()}`);
}
} catch (err) {
console.error(String(err));
process.exit(1);
Expand Down
34 changes: 1 addition & 33 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,7 @@ colors@^1.1.2:
resolved "https://registry.yarnpkg.com/colors/-/colors-1.4.0.tgz#c50491479d4c1bdaed2c9ced32cf7c7dc2360f78"
integrity sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==

combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
combined-stream@^1.0.6, combined-stream@~1.0.6:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
Expand Down Expand Up @@ -2184,11 +2184,6 @@ dashdash@^1.12.0:
dependencies:
assert-plus "^1.0.0"

data-uri-to-buffer@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz#594b8973938c5bc2c33046535785341abc4f3636"
integrity sha512-WboRycPNsVw3B3TL559F7kuBUM4d8CgMEvk6xEJlOp7OBPjt6G7z8WMWlD2rOFZLk6OYfFIUGsCOWzcQH9K2og==

data-urls@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/data-urls/-/data-urls-2.0.0.tgz#156485a72963a970f5d5821aaf642bef2bf2db9b"
Expand Down Expand Up @@ -2857,11 +2852,6 @@ fb-watchman@^2.0.0:
dependencies:
bser "2.1.1"

fetch-blob@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-2.1.1.tgz#a54ab0d5ed7ccdb0691db77b6674308b23fb2237"
integrity sha512-Uf+gxPCe1hTOFXwkxYyckn8iUSk6CFXGy5VENZKifovUTZC9eUODWSBhOBS7zICGrAetKzdwLMr85KhIcePMAQ==

figures@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-1.7.0.tgz#cbe1e3affcf1cd44b80cadfed28dc793a9701d2e"
Expand Down Expand Up @@ -3002,15 +2992,6 @@ forever-agent@~0.6.1:
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=

form-data@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682"
integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
Expand Down Expand Up @@ -3573,11 +3554,6 @@ import-sort-style-eslint@^6.0.0:
eslint "^5.0.0"
lodash "^4.17.10"

import-sort-style-leboncoin@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/import-sort-style-leboncoin/-/import-sort-style-leboncoin-1.0.3.tgz#b04b23f60598324e5dd3a3f35907567c4ef80b44"
integrity sha512-n7IbYGctk3OMJrnfHDDzKinwN/xtBUjgIGGe+TD+Rcc7loBZR08ZRJNjM8JPkNwL46NTMTY+9aaiqZn2eyA1Zw==

import-sort-style@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/import-sort-style/-/import-sort-style-6.0.0.tgz#088523f056e5064c34a6426f4733674d81b42e6a"
Expand Down Expand Up @@ -5006,14 +4982,6 @@ nice-try@^1.0.4:
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==

[email protected]:
version "3.0.0-beta.9"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.0.0-beta.9.tgz#0a7554cfb824380dd6812864389923c783c80d9b"
integrity sha512-RdbZCEynH2tH46+tj0ua9caUHVWrd/RHnRfvly2EVdqGmI3ndS1Vn/xjm5KuGejDt2RNDQsVRLPNd2QPwcewVg==
dependencies:
data-uri-to-buffer "^3.0.1"
fetch-blob "^2.1.1"

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down

0 comments on commit 5480c46

Please sign in to comment.