Skip to content

Commit

Permalink
fix: remove axios dependency and update tests to use fetch for API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Jan 20, 2025
1 parent c24850e commit 120d68e
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 113 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

**Azure Translator Code** is a powerful library for translating JSON files into multiple languages using the Azure Cognitive Translator service. This library supports translating JSON files located in multiple folders or within a single folder, depending on your needs.

**Supports:** Node.js 18.x and above

---

### Important Links
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"scripts": {
"test": "jest --verbose",
"test:coverage": "jest --coverage",
"test:cov": "jest --coverage",
"test:watch": "jest --watch",
"test:file": "jest tests/translate",
"test:silent": "jest --watchAll --silent --noStackTrace",
Expand Down Expand Up @@ -64,7 +64,6 @@
},
"packageManager": "[email protected]",
"dependencies": {
"axios": "^1",
"uuid": "^11"
}
}
23 changes: 6 additions & 17 deletions src/translate/translateText.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from "axios";
import { v4 as uuidv4 } from "uuid";

import type { TranslationType } from "../types";
Expand Down Expand Up @@ -58,21 +57,11 @@ export default async function translateText(
"X-ClientTraceId": uuidv4(),
};

const payload = [{ text }];
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify([{ text }]),
});

if (typeof window !== "undefined" && typeof window.fetch === "function") {
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(payload),
});

return await response.json();
} else {
const response = await axios.post<TranslateResponseData>(url, payload, {
headers,
});

return response.data;
}
return await response.json();
}
84 changes: 41 additions & 43 deletions tests/translate/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import axios from "axios";

import translate from "../../src/translate";
import translateText from "../../src/translate/translateText";
import type { TranslationType } from "../../src/types";
Expand All @@ -22,8 +20,8 @@ describe("translate", () => {

describe("translateText", () => {
it("should return the translated text", async () => {
const spyOnAxios = jest.spyOn(axios, "post").mockResolvedValue({
data: [
const spyOnFetch = jest.spyOn(global, "fetch").mockResolvedValue({
json: async () => [
{
translations: [
{
Expand All @@ -32,7 +30,7 @@ describe("translate", () => {
],
},
],
});
} as Response);

const result = await translateText(
"Welcome",
Expand All @@ -43,7 +41,7 @@ describe("translate", () => {
location,
);

expect(spyOnAxios).toHaveBeenCalled();
expect(spyOnFetch).toHaveBeenCalled();

expect(result[0].translations[0].text).toEqual("Bem-vindo");
});
Expand All @@ -52,8 +50,8 @@ describe("translate", () => {
describe("translate", () => {
describe("translateValue", () => {
it("should throw an error if invalid JSON object is passed", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: [
jest.spyOn(global, "fetch").mockResolvedValue({
json: async () => [
{
translations: [
{
Expand All @@ -62,7 +60,7 @@ describe("translate", () => {
],
},
],
});
} as Response);

const jsonFile = {
Welcome: "Welcome",
Expand All @@ -83,8 +81,8 @@ describe("translate", () => {

describe("translateString", () => {
it("should translate a JSON object from one language to another with welcome msg", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: [
jest.spyOn(global, "fetch").mockResolvedValue({
json: async () => [
{
translations: [
{
Expand All @@ -93,7 +91,7 @@ describe("translate", () => {
],
},
],
});
} as Response);

const jsonFile = {
Welcome: "Welcome",
Expand All @@ -114,8 +112,8 @@ describe("translate", () => {
});

it("should return an array with multiple translations", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: [
jest.spyOn(global, "fetch").mockResolvedValue({
json: async () => [
{
translations: [
{
Expand All @@ -127,7 +125,7 @@ describe("translate", () => {
],
},
],
});
} as Response);

const jsonFile = {
Welcome: "Welcome",
Expand All @@ -150,10 +148,10 @@ describe("translate", () => {
describe("translateArray", () => {
it("should translate a JSON object from one language to another with arrays", async () => {
jest
.spyOn(axios, "post")
.spyOn(global, "fetch")
.mockImplementationOnce(() =>
Promise.resolve({
data: [
json: async () => [
{
translations: [
{
Expand All @@ -162,11 +160,11 @@ describe("translate", () => {
],
},
],
}),
} as Response),
)
.mockImplementationOnce(() =>
Promise.resolve({
data: [
json: async () => [
{
translations: [
{
Expand All @@ -175,7 +173,7 @@ describe("translate", () => {
],
},
],
}),
} as Response),
);

const jsonFile = {
Expand Down Expand Up @@ -206,10 +204,10 @@ describe("translate", () => {

it("should translate a JSON object with arrays and if elements are not strings", async () => {
jest
.spyOn(axios, "post")
.spyOn(global, "fetch")
.mockImplementationOnce(() =>
Promise.resolve({
data: [
json: async () => [
{
translations: [
{
Expand All @@ -218,11 +216,11 @@ describe("translate", () => {
],
},
],
}),
} as Response),
)
.mockImplementationOnce(() =>
Promise.resolve({
data: [
json: async () => [
{
translations: [
{
Expand All @@ -231,7 +229,7 @@ describe("translate", () => {
],
},
],
}),
} as Response),
);

const jsonFile = {
Expand Down Expand Up @@ -262,11 +260,11 @@ describe("translate", () => {
});
describe("translateObject", () => {
it("should translate a JSON object from one language to another with nested objects", async () => {
const spyOnAxios = jest
.spyOn(axios, "post")
const spyOnFetch = jest
.spyOn(global, "fetch")
.mockImplementationOnce(() =>
Promise.resolve({
data: [
json: async () => [
{
translations: [
{
Expand All @@ -275,11 +273,11 @@ describe("translate", () => {
],
},
],
}),
} as Response),
)
.mockImplementationOnce(() =>
Promise.resolve({
data: [
json: async () => [
{
translations: [
{
Expand All @@ -288,7 +286,7 @@ describe("translate", () => {
],
},
],
}),
} as Response),
);

const jsonFile = {
Expand All @@ -311,7 +309,7 @@ describe("translate", () => {
jsonFile as unknown as TranslationType,
);

expect(spyOnAxios).toHaveBeenCalledTimes(2);
expect(spyOnFetch).toHaveBeenCalledTimes(2);

expect(result).toEqual({
HomePage: {
Expand All @@ -327,10 +325,10 @@ describe("translate", () => {

it("should translate a JSON object from one language to another with fake booleans", async () => {
jest
.spyOn(axios, "post")
.spyOn(global, "fetch")
.mockImplementationOnce(() =>
Promise.resolve({
data: [
json: async () => [
{
translations: [
{
Expand All @@ -339,11 +337,11 @@ describe("translate", () => {
],
},
],
}),
} as Response),
)
.mockImplementationOnce(() =>
Promise.resolve({
data: [
json: async () => [
{
translations: [
{
Expand All @@ -352,7 +350,7 @@ describe("translate", () => {
],
},
],
}),
} as Response),
);

const jsonFile = {
Expand Down Expand Up @@ -386,7 +384,7 @@ describe("translate", () => {

describe("translateBool or numbers", () => {
it("should translate a JSON object from one language to another with booleans", async () => {
const spyOnAxios = jest.spyOn(axios, "post");
const spyOnFetch = jest.spyOn(global, "fetch");

const jsonFile = {
HomePage: {
Expand All @@ -406,7 +404,7 @@ describe("translate", () => {
jsonFile as unknown as TranslationType,
);

expect(spyOnAxios).not.toHaveBeenCalled();
expect(spyOnFetch).not.toHaveBeenCalled();

expect(result).toEqual({
HomePage: {
Expand All @@ -419,7 +417,7 @@ describe("translate", () => {
});

it("should translate a JSON object from one language to another with numbers", async () => {
const spyOnAxios = jest.spyOn(axios, "post");
const spyOnFetch = jest.spyOn(global, "fetch");

const jsonFile = {
HomePage: {
Expand All @@ -438,7 +436,7 @@ describe("translate", () => {
jsonFile as unknown as TranslationType,
);

expect(spyOnAxios).not.toHaveBeenCalled();
expect(spyOnFetch).not.toHaveBeenCalled();

expect(result).toEqual({
HomePage: {
Expand All @@ -452,7 +450,7 @@ describe("translate", () => {

describe("translateNull", () => {
it("should translate a JSON object from one language to another with null", async () => {
const spyOnAxios = jest.spyOn(axios, "post");
const spyOnFetch = jest.spyOn(global, "fetch");

const jsonFile = {
HomePage: {
Expand All @@ -471,7 +469,7 @@ describe("translate", () => {
jsonFile as unknown as TranslationType,
);

expect(spyOnAxios).not.toHaveBeenCalled();
expect(spyOnFetch).not.toHaveBeenCalled();

expect(result).toEqual({
HomePage: {
Expand Down
7 changes: 3 additions & 4 deletions tests/translateToMultipleFolders/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from "axios";
import { existsSync, readFileSync, rmSync } from "fs";

import { translateToMultipleFolders, type TranslationType } from "../../src";
Expand All @@ -25,8 +24,8 @@ describe("translateToMultipleFolders", () => {
});

it("should translate to multiple folders", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: [
jest.spyOn(global, "fetch").mockResolvedValue({
json: async () => [
{
translations: [
{
Expand All @@ -35,7 +34,7 @@ describe("translateToMultipleFolders", () => {
],
},
],
});
} as Response);

const jsonFile = {
Welcome: "Welcome",
Expand Down
Loading

0 comments on commit 120d68e

Please sign in to comment.