Skip to content

Commit 64f7573

Browse files
authored
Replace axios with fetch (#11)
* ✨ feat: Replace axios with fetch * 🐛 fix: Fix missing query params * 📚 docs: Update README.md * 🐛 fix: Add missing content type header
1 parent eadb073 commit 64f7573

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+577
-551
lines changed

README.md

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,11 @@
99
</a>
1010
</p>
1111

12-
<p align="center">
13-
12+
<div align="center">
1413
<a href="https://github.com/localazy/api-client"><img src="https://img.shields.io/badge/@localazy-ts--api-066fef?style=for-the-badge" height="22" alt="@localazy/api-client"></a>
15-
1614
<a href="https://npmjs.com/package/@localazy/api-client"><img src="https://img.shields.io/github/package-json/v/localazy/ts-api/main?style=for-the-badge&label=version&color=066fef" height="22" alt="npm"></a>
17-
18-
<a href="https://github.com/localazy/api-client/blob/main/LICENSE"><img src="https://img.shields.io/github/license/localazy/ts-api?style=for-the-badge&color=066fef" height="22" alt="license">
19-
20-
</a>
21-
22-
[//]: # '<a href="#">'
23-
[//]: # ' <img src="https://img.shields.io/badge/schema-OpenAPI-066fef?style=for-the-badge&color=066fef" height="22" alt="schema"></a>'
24-
[//]: # ' <br>'
25-
[//]: # ' <a href="#"><img src="https://img.shields.io/badge/build-passing-success?style=for-the-badge" height="22" alt="build"></a>'
26-
[//]: # ' <a href="#"><img src="https://img.shields.io/badge/coverage-100%25-success?style=for-the-badge" height="22" alt="coverage"></a>'
27-
28-
</p>
15+
<a href="https://github.com/localazy/api-client/blob/main/LICENSE"><img src="https://img.shields.io/github/license/localazy/ts-api?style=for-the-badge&color=066fef" height="22" alt="license"></a>
16+
</div>
2917

3018
# 📦 Localazy API Client
3119

docs/api-client-reference.md

Lines changed: 49 additions & 50 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 0 additions & 128 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
},
6969
"dependencies": {
7070
"@localazy/languages": "^0.1.6",
71-
"axios": "^1.6.8",
7271
"lodash-es": "^4.17.21"
7372
},
7473
"devDependencies": {
@@ -80,7 +79,6 @@
8079
"@typescript-eslint/parser": "^7.2.0",
8180
"@vitest/coverage-v8": "^1.4.0",
8281
"@vitest/ui": "^1.4.0",
83-
"axios-mock-adapter": "^1.22.0",
8482
"eslint": "^8.57.0",
8583
"eslint-config-airbnb": "^19.0.4",
8684
"eslint-config-airbnb-base": "^15.0.0",

src/api/api-client.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CreateAxiosDefaults } from 'axios';
1+
import { FetchHttpAdapter } from '@/http/fetch-http-adapter';
22
import { ApiExport } from '@/api/methods/api-export';
33
import { ApiFiles } from '@/api/methods/api-files';
44
import { ApiFormats } from '@/api/methods/api-formats';
@@ -8,7 +8,6 @@ import { ApiKeys } from '@/api/methods/api-keys';
88
import { ApiProjects } from '@/api/methods/api-projects';
99
import { ApiScreenshots } from '@/api/methods/api-screenshots';
1010
import { ApiWebhooks } from '@/api/methods/api-webhooks';
11-
import { AxiosHttpAdapter } from '@/http/axios-http-adapter';
1211
import { IHttpAdapter } from '@/http/i-http-adapter';
1312
import { ApiClientOptions } from '@/types/api-client-options';
1413

@@ -33,8 +32,8 @@ export class ApiClient {
3332

3433
public screenshots: ApiScreenshots;
3534

36-
constructor(options: ApiClientOptions, config?: CreateAxiosDefaults) {
37-
this.client = new AxiosHttpAdapter(options, config);
35+
constructor(options: ApiClientOptions) {
36+
this.client = new FetchHttpAdapter(options);
3837

3938
this.projects = new ApiProjects(this);
4039
this.import = new ApiImport(this);

src/api/methods/api-export.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Locales } from '@localazy/languages';
2-
import { AxiosRequestConfig } from 'axios';
2+
import { RequestConfig } from '@/types/request-config';
33
import { ApiBase } from '@/api/methods/api-base';
44
import { ExportJsonRequest } from '@/types/export-json-request';
55
import { I18nJson } from '@/types/i18n-json';
@@ -11,9 +11,9 @@ export class ApiExport extends ApiBase {
1111
* Export translated keys as JSON object.
1212
*
1313
* @param request Export JSON request config.
14-
* @param config Axios request config.
14+
* @param config Request config.
1515
*/
16-
public async json(request: ExportJsonRequest, config?: AxiosRequestConfig): Promise<I18nJson> {
16+
public async json(request: ExportJsonRequest, config?: RequestConfig): Promise<I18nJson> {
1717
const { project, file, langs }: ExportJsonRequest = request;
1818

1919
const result: Key[][] = await Promise.all(

src/api/methods/api-files.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AxiosRequestConfig } from 'axios';
21
import { Blob } from 'node:buffer';
2+
import { RequestConfig } from '@/types/request-config';
33
import { ApiBase } from '@/api/methods/api-base';
44
import { File } from '@/types/file';
55
import { FileGetContentsRequest } from '@/types/file-get-contents-request';
@@ -13,27 +13,27 @@ export class ApiFiles extends ApiBase {
1313
* List all {@link File files} in the project.
1414
*
1515
* @param request Files list request config.
16-
* @param config Axios request config.
16+
* @param config Request config.
1717
*
1818
* @see {@link https://localazy.com/docs/api/files#list-files-in-project Localazy API Docs}
1919
*/
20-
public async list(request: FilesListRequest, config?: AxiosRequestConfig): Promise<File[]> {
20+
public async list(request: FilesListRequest, config?: RequestConfig): Promise<File[]> {
2121
const { project }: FilesListRequest = request;
2222
const projectId: string = ApiBase.getId(project, 'project');
2323

24-
return this.api.client.get(`/projects/${projectId}/files`, config);
24+
return (await this.api.client.get(`/projects/${projectId}/files`, config)) as Promise<File[]>;
2525
}
2626

2727
/**
2828
* First {@link File file} in the project.
2929
*
3030
* @param request Files list request config.
31-
* @param config Axios request config.
31+
* @param config Request config.
3232
* @throws Error At least one file must exist, otherwise an error is thrown.
3333
*
3434
* @see {@link https://localazy.com/docs/api/files#list-files-in-project Localazy API Docs}
3535
*/
36-
public async first(request: FilesListRequest, config?: AxiosRequestConfig): Promise<File> {
36+
public async first(request: FilesListRequest, config?: RequestConfig): Promise<File> {
3737
const files: File[] = await this.list(request, config);
3838

3939
if (files.length === 0) {
@@ -47,12 +47,12 @@ export class ApiFiles extends ApiBase {
4747
* List all {@link Key keys} for the language in the {@link File file}.
4848
*
4949
* @param request File list keys request config.
50-
* @param config Axios request config.
50+
* @param config Request config.
5151
*
5252
* @see {@link https://localazy.com/docs/api/files#retrieve-a-list-of-keys-and-translations-from-file
5353
* | Localazy API Docs}
5454
*/
55-
public async listKeys(request: FileListKeysRequest, config?: AxiosRequestConfig): Promise<Key[]> {
55+
public async listKeys(request: FileListKeysRequest, config?: RequestConfig): Promise<Key[]> {
5656
const keys: Key[] = [];
5757
let pageResult: KeysPaginated = {
5858
keys: [],
@@ -72,37 +72,38 @@ export class ApiFiles extends ApiBase {
7272
* List all {@link Key keys} for the language in the {@link File file}. Result is paginated.
7373
*
7474
* @param request File list keys request config.
75-
* @param config Axios request config.
75+
* @param config Request config.
7676
*
7777
* @see {@link https://localazy.com/docs/api/files#retrieve-a-list-of-keys-and-translations-from-file
7878
* | Localazy API Docs}
7979
*/
80-
public async listKeysPage(request: FileListKeysRequest, config?: AxiosRequestConfig): Promise<KeysPaginated> {
80+
public async listKeysPage(request: FileListKeysRequest, config?: RequestConfig): Promise<KeysPaginated> {
8181
const { project, file, lang, ...params }: FileListKeysRequest = request;
8282
const projectId: string = ApiBase.getId(project, 'project');
8383
const fileId: string = ApiBase.getId(file, 'file');
8484

85-
return this.api.client.get(`/projects/${projectId}/files/${fileId}/keys/${lang}`, { ...config, params });
85+
return (await this.api.client.get(`/projects/${projectId}/files/${fileId}/keys/${lang}`, {
86+
...config,
87+
params,
88+
})) as KeysPaginated;
8689
}
8790

8891
/**
8992
* Get the contents of the {@link File file}.
9093
*
9194
* @param request File get contents request config.
92-
* @param config Axios request config.
95+
* @param config Request config.
9396
*
9497
* @see {@link https://localazy.com/docs/api/files#list-file-content Localazy API Docs}
9598
*/
96-
public async getContents(request: FileGetContentsRequest, config?: AxiosRequestConfig): Promise<Blob> {
99+
public async getContents(request: FileGetContentsRequest, config?: RequestConfig): Promise<Blob> {
97100
const { project, file, lang }: FileGetContentsRequest = request;
98101
const projectId: string = ApiBase.getId(project, 'project');
99102
const fileId: string = ApiBase.getId(file, 'file');
100103

101-
const buffer: Uint8Array = await this.api.client.get(`/projects/${projectId}/files/${fileId}/download/${lang}`, {
104+
return (await this.api.client.get(`/projects/${projectId}/files/${fileId}/download/${lang}`, {
102105
...config,
103-
responseType: 'arraybuffer',
104-
});
105-
106-
return new Blob([buffer]);
106+
responseType: 'blob',
107+
})) as Blob;
107108
}
108109
}

0 commit comments

Comments
 (0)