Skip to content

Commit

Permalink
Replace axios with fetch (#11)
Browse files Browse the repository at this point in the history
* ✨ feat: Replace axios with fetch

* 🐛 fix: Fix missing query params

* 📚 docs: Update README.md

* 🐛 fix: Add missing content type header
  • Loading branch information
honzabubenik committed Apr 18, 2024
1 parent eadb073 commit 64f7573
Show file tree
Hide file tree
Showing 41 changed files with 577 additions and 551 deletions.
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,11 @@
</a>
</p>

<p align="center">

<div align="center">
<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>

<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>

<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>

[//]: # '<a href="#">'
[//]: # ' <img src="https://img.shields.io/badge/schema-OpenAPI-066fef?style=for-the-badge&color=066fef" height="22" alt="schema"></a>'
[//]: # ' <br>'
[//]: # ' <a href="#"><img src="https://img.shields.io/badge/build-passing-success?style=for-the-badge" height="22" alt="build"></a>'
[//]: # ' <a href="#"><img src="https://img.shields.io/badge/coverage-100%25-success?style=for-the-badge" height="22" alt="coverage"></a>'

</p>
<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>
</div>

# 📦 Localazy API Client

Expand Down
99 changes: 49 additions & 50 deletions docs/api-client-reference.md

Large diffs are not rendered by default.

128 changes: 0 additions & 128 deletions package-lock.json

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

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
},
"dependencies": {
"@localazy/languages": "^0.1.6",
"axios": "^1.6.8",
"lodash-es": "^4.17.21"
},
"devDependencies": {
Expand All @@ -80,7 +79,6 @@
"@typescript-eslint/parser": "^7.2.0",
"@vitest/coverage-v8": "^1.4.0",
"@vitest/ui": "^1.4.0",
"axios-mock-adapter": "^1.22.0",
"eslint": "^8.57.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-base": "^15.0.0",
Expand Down
7 changes: 3 additions & 4 deletions src/api/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CreateAxiosDefaults } from 'axios';
import { FetchHttpAdapter } from '@/http/fetch-http-adapter';
import { ApiExport } from '@/api/methods/api-export';
import { ApiFiles } from '@/api/methods/api-files';
import { ApiFormats } from '@/api/methods/api-formats';
Expand All @@ -8,7 +8,6 @@ import { ApiKeys } from '@/api/methods/api-keys';
import { ApiProjects } from '@/api/methods/api-projects';
import { ApiScreenshots } from '@/api/methods/api-screenshots';
import { ApiWebhooks } from '@/api/methods/api-webhooks';
import { AxiosHttpAdapter } from '@/http/axios-http-adapter';
import { IHttpAdapter } from '@/http/i-http-adapter';
import { ApiClientOptions } from '@/types/api-client-options';

Expand All @@ -33,8 +32,8 @@ export class ApiClient {

public screenshots: ApiScreenshots;

constructor(options: ApiClientOptions, config?: CreateAxiosDefaults) {
this.client = new AxiosHttpAdapter(options, config);
constructor(options: ApiClientOptions) {
this.client = new FetchHttpAdapter(options);

this.projects = new ApiProjects(this);
this.import = new ApiImport(this);
Expand Down
6 changes: 3 additions & 3 deletions src/api/methods/api-export.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Locales } from '@localazy/languages';
import { AxiosRequestConfig } from 'axios';
import { RequestConfig } from '@/types/request-config';
import { ApiBase } from '@/api/methods/api-base';
import { ExportJsonRequest } from '@/types/export-json-request';
import { I18nJson } from '@/types/i18n-json';
Expand All @@ -11,9 +11,9 @@ export class ApiExport extends ApiBase {
* Export translated keys as JSON object.
*
* @param request Export JSON request config.
* @param config Axios request config.
* @param config Request config.
*/
public async json(request: ExportJsonRequest, config?: AxiosRequestConfig): Promise<I18nJson> {
public async json(request: ExportJsonRequest, config?: RequestConfig): Promise<I18nJson> {
const { project, file, langs }: ExportJsonRequest = request;

const result: Key[][] = await Promise.all(
Expand Down
37 changes: 19 additions & 18 deletions src/api/methods/api-files.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AxiosRequestConfig } from 'axios';
import { Blob } from 'node:buffer';
import { RequestConfig } from '@/types/request-config';
import { ApiBase } from '@/api/methods/api-base';
import { File } from '@/types/file';
import { FileGetContentsRequest } from '@/types/file-get-contents-request';
Expand All @@ -13,27 +13,27 @@ export class ApiFiles extends ApiBase {
* List all {@link File files} in the project.
*
* @param request Files list request config.
* @param config Axios request config.
* @param config Request config.
*
* @see {@link https://localazy.com/docs/api/files#list-files-in-project Localazy API Docs}
*/
public async list(request: FilesListRequest, config?: AxiosRequestConfig): Promise<File[]> {
public async list(request: FilesListRequest, config?: RequestConfig): Promise<File[]> {
const { project }: FilesListRequest = request;
const projectId: string = ApiBase.getId(project, 'project');

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

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

if (files.length === 0) {
Expand All @@ -47,12 +47,12 @@ export class ApiFiles extends ApiBase {
* List all {@link Key keys} for the language in the {@link File file}.
*
* @param request File list keys request config.
* @param config Axios request config.
* @param config Request config.
*
* @see {@link https://localazy.com/docs/api/files#retrieve-a-list-of-keys-and-translations-from-file
* | Localazy API Docs}
*/
public async listKeys(request: FileListKeysRequest, config?: AxiosRequestConfig): Promise<Key[]> {
public async listKeys(request: FileListKeysRequest, config?: RequestConfig): Promise<Key[]> {
const keys: Key[] = [];
let pageResult: KeysPaginated = {
keys: [],
Expand All @@ -72,37 +72,38 @@ export class ApiFiles extends ApiBase {
* List all {@link Key keys} for the language in the {@link File file}. Result is paginated.
*
* @param request File list keys request config.
* @param config Axios request config.
* @param config Request config.
*
* @see {@link https://localazy.com/docs/api/files#retrieve-a-list-of-keys-and-translations-from-file
* | Localazy API Docs}
*/
public async listKeysPage(request: FileListKeysRequest, config?: AxiosRequestConfig): Promise<KeysPaginated> {
public async listKeysPage(request: FileListKeysRequest, config?: RequestConfig): Promise<KeysPaginated> {
const { project, file, lang, ...params }: FileListKeysRequest = request;
const projectId: string = ApiBase.getId(project, 'project');
const fileId: string = ApiBase.getId(file, 'file');

return this.api.client.get(`/projects/${projectId}/files/${fileId}/keys/${lang}`, { ...config, params });
return (await this.api.client.get(`/projects/${projectId}/files/${fileId}/keys/${lang}`, {
...config,
params,
})) as KeysPaginated;
}

/**
* Get the contents of the {@link File file}.
*
* @param request File get contents request config.
* @param config Axios request config.
* @param config Request config.
*
* @see {@link https://localazy.com/docs/api/files#list-file-content Localazy API Docs}
*/
public async getContents(request: FileGetContentsRequest, config?: AxiosRequestConfig): Promise<Blob> {
public async getContents(request: FileGetContentsRequest, config?: RequestConfig): Promise<Blob> {
const { project, file, lang }: FileGetContentsRequest = request;
const projectId: string = ApiBase.getId(project, 'project');
const fileId: string = ApiBase.getId(file, 'file');

const buffer: Uint8Array = await this.api.client.get(`/projects/${projectId}/files/${fileId}/download/${lang}`, {
return (await this.api.client.get(`/projects/${projectId}/files/${fileId}/download/${lang}`, {
...config,
responseType: 'arraybuffer',
});

return new Blob([buffer]);
responseType: 'blob',
})) as Blob;
}
}
Loading

0 comments on commit 64f7573

Please sign in to comment.