diff --git a/package.json b/package.json index 2604ff2..4e277e8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "axios-service-base-ts", "private": false, - "version": "0.2.1", + "version": "0.3.0", "description": "A base class for API services using Axios", "homepage": "https://github.com/Xkonti/axios-service-base-ts#readme", "repository": { @@ -42,7 +42,7 @@ "dependencies": { "axios": "^1.4.0", "lodash-es": "^4.17.21", - "rusty-result-ts": "^0.1.0" + "neverthrow": "^6.0.0" }, "devDependencies": { "@types/express": "^4.17.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e8d9faa..66ceea8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,9 +11,9 @@ dependencies: lodash-es: specifier: ^4.17.21 version: 4.17.21 - rusty-result-ts: - specifier: ^0.1.0 - version: 0.1.0 + neverthrow: + specifier: ^6.0.0 + version: 6.0.0 devDependencies: '@types/express': @@ -1842,6 +1842,10 @@ packages: engines: {node: '>= 0.6'} dev: true + /neverthrow@6.0.0: + resolution: {integrity: sha512-kPZKRs4VkdloCGQXPoP84q4sT/1Z+lYM61AXyV8wWa2hnuo5KpPBF2S3crSFnMrOgUISmEBP8Vo/ngGZX60NhA==} + dev: false + /object-inspect@1.12.3: resolution: {integrity: sha512-geUvdk7c+eizMNUDkRpW1wJwgfOiOeHbxBR/hLXK1aT6zmVSO0jsQcs7fj6MGw89jC/cjGfLcNOrtMYtGqm81g==} dev: true @@ -2082,10 +2086,6 @@ packages: queue-microtask: 1.2.3 dev: true - /rusty-result-ts@0.1.0: - resolution: {integrity: sha512-dmuHmysHbx72D9ELpHaM7eKmiW/Syb/rSPUvwQVoTU4IeOVIqk26dE7Bdp+zesbShozz4STsl1N+DkqRHRvhKA==} - dev: false - /safe-buffer@5.2.1: resolution: {integrity: sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==} dev: true diff --git a/src/api-service-base.ts b/src/api-service-base.ts index 8e01097..f1feab0 100644 --- a/src/api-service-base.ts +++ b/src/api-service-base.ts @@ -1,6 +1,6 @@ import axios, { AxiosRequestConfig } from 'axios'; import { merge } from 'lodash-es'; -import { err, ok, Result } from 'rusty-result-ts'; +import { err, ok, Result } from 'neverthrow'; import { ApiError } from './api-error'; @@ -36,7 +36,7 @@ export class ApiServiceBase { protected async get ( path = '', configOverrides: AxiosRequestConfig | undefined = undefined, - ): Promise> { + ): Promise> { return await this.requestResultWrapper(path, configOverrides, (fullPath, config) => { return axios.get(fullPath, config); }); @@ -53,7 +53,7 @@ export class ApiServiceBase { path = '', data: unknown = undefined, configOverrides: AxiosRequestConfig | undefined = undefined, - ): Promise> { + ): Promise> { return await this.requestResultWrapper(path, configOverrides, (fullPath, config) => { return axios.post(fullPath, data, config); }); @@ -70,7 +70,7 @@ export class ApiServiceBase { path = '', data: unknown = undefined, configOverrides: AxiosRequestConfig | undefined = undefined, - ): Promise> { + ): Promise> { return await this.requestResultWrapper(path, configOverrides, (fullPath, config) => { return axios.put(fullPath, data, config); }); @@ -87,7 +87,7 @@ export class ApiServiceBase { path = '', data: unknown = undefined, configOverrides: AxiosRequestConfig | undefined = undefined, - ): Promise> { + ): Promise> { return await this.requestResultWrapper(path, configOverrides, (fullPath, config) => { return axios.patch(fullPath, data, config); }); @@ -102,7 +102,7 @@ export class ApiServiceBase { protected async delete ( path = '', configOverrides: AxiosRequestConfig | undefined = undefined, - ): Promise> { + ): Promise> { return await this.requestResultWrapper(path, configOverrides, (fullPath, config) => { return axios.delete(fullPath, config); }); @@ -112,13 +112,13 @@ export class ApiServiceBase { subPath: string, configOverrides: AxiosRequestConfig | undefined, request: (fullPath: string, config: AxiosRequestConfig | undefined) => Promise<{data: unknown} | null>, - ): Promise> { + ): Promise> { if (subPath.length > 0 && subPath[0] !== '/') subPath = `/${subPath}`; const config = merge(this.getConfig() || {}, configOverrides || {}); try { - const responseData: T | null = (await request(`${this.urlBase}${subPath}`, config))?.data as T ?? null; + const responseData: T = (await request(`${this.urlBase}${subPath}`, config))?.data as T; return ok(responseData); - } catch (e: unknown) { + } catch (e) { return err(new ApiError(e)); } } diff --git a/src/api-service.test.ts b/src/api-service.test.ts index a4dbdf3..8ba27c6 100644 --- a/src/api-service.test.ts +++ b/src/api-service.test.ts @@ -1,13 +1,13 @@ import { test, expect, beforeEach, afterEach, assert } from 'vitest'; import express from 'express'; import {ApiError} from './api-error.ts'; -import {Result} from 'rusty-result-ts'; +import {Result} from 'neverthrow'; import {ApiServiceBase} from './api-service-base.ts'; const port = 3167; const helloMessage = 'Hello there!'; -let server: ReturnType | null; +let server: ReturnType; function startApiServer() { const app = express(); @@ -39,11 +39,11 @@ class TestService extends ApiServiceBase { super(`http://localhost:${port}/api`); } - public async getHello(): Promise> { + public async getHello(): Promise> { return await this.get('/hello'); } - public async getNothing(): Promise> { + public async getNothing(): Promise> { return await this.get('/nothing'); } } @@ -52,7 +52,7 @@ test('should make a GET request', async () => { const service = new TestService(); const result = await service.getHello(); - assert(result.isOk()); + assert(result.isOk()); expect(result.value).toBe(helloMessage); });