Skip to content

Commit

Permalink
Replace rusty-result-ts with neverthrow package (#7)
Browse files Browse the repository at this point in the history
+ Bump version to 0.3.0
Submitted by @corners2wall
  • Loading branch information
corners2wall authored Sep 7, 2023
1 parent 5133448 commit a1f3f3b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

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

18 changes: 9 additions & 9 deletions src/api-service-base.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -36,7 +36,7 @@ export class ApiServiceBase {
protected async get<T> (
path = '',
configOverrides: AxiosRequestConfig | undefined = undefined,
): Promise<Result<T | null, ApiError>> {
): Promise<Result<T, ApiError>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.get(fullPath, config);
});
Expand All @@ -53,7 +53,7 @@ export class ApiServiceBase {
path = '',
data: unknown = undefined,
configOverrides: AxiosRequestConfig | undefined = undefined,
): Promise<Result<T | null, ApiError>> {
): Promise<Result<T, ApiError>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.post(fullPath, data, config);
});
Expand All @@ -70,7 +70,7 @@ export class ApiServiceBase {
path = '',
data: unknown = undefined,
configOverrides: AxiosRequestConfig | undefined = undefined,
): Promise<Result<T | null, ApiError>> {
): Promise<Result<T, ApiError>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.put(fullPath, data, config);
});
Expand All @@ -87,7 +87,7 @@ export class ApiServiceBase {
path = '',
data: unknown = undefined,
configOverrides: AxiosRequestConfig | undefined = undefined,
): Promise<Result<T | null, ApiError>> {
): Promise<Result<T, ApiError>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.patch(fullPath, data, config);
});
Expand All @@ -102,7 +102,7 @@ export class ApiServiceBase {
protected async delete<T> (
path = '',
configOverrides: AxiosRequestConfig | undefined = undefined,
): Promise<Result<T | null, ApiError>> {
): Promise<Result<T, ApiError>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.delete(fullPath, config);
});
Expand All @@ -112,13 +112,13 @@ export class ApiServiceBase {
subPath: string,
configOverrides: AxiosRequestConfig | undefined,
request: (fullPath: string, config: AxiosRequestConfig | undefined) => Promise<{data: unknown} | null>,
): Promise<Result<T | null, ApiError>> {
): Promise<Result<T, ApiError>> {
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));
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/api-service.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof startApiServer> | null;
let server: ReturnType<typeof startApiServer>;

function startApiServer() {
const app = express();
Expand Down Expand Up @@ -39,11 +39,11 @@ class TestService extends ApiServiceBase {
super(`http://localhost:${port}/api`);
}

public async getHello(): Promise<Result<string | null, ApiError>> {
public async getHello(): Promise<Result<string, ApiError>> {
return await this.get('/hello');
}

public async getNothing(): Promise<Result<string | null, ApiError>> {
public async getNothing(): Promise<Result<string, ApiError>> {
return await this.get('/nothing');
}
}
Expand All @@ -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);
});

Expand Down

0 comments on commit a1f3f3b

Please sign in to comment.