Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added axios to json fn #204

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ All notable changes to this project will be documented in this file.

---

### [0.2.0-beta] - 2024-10-22

### Feat

- feat: use toJSON method from axios instance
- fix: ensure delete logs files for 0 days

---

## Unreleased

### [0.1.5] - 2023-12-15

### Fix
Expand Down
12 changes: 12 additions & 0 deletions lib/utils/body-to-json.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CatchError } from "../types";

export const ExtractBodyToJson = (body: CatchError): object | null => {
try {
if (typeof body?.toJSON !== 'function') return null;
return body.toJSON()
} catch (error) {
return null
}
}

export default ExtractBodyToJson;
2 changes: 1 addition & 1 deletion lib/utils/delete-expired-file.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const DeleteExpiredFile = (days: number, dirname: string): void => {
const stats = statSync(filePath)
const daysToDeleteLogs = Date.now() - stats.mtime.getTime() > days * ONE_DAY_IN_MS;

if (daysToDeleteLogs) unlinkSync(filePath)
if (daysToDeleteLogs || days === 0) unlinkSync(filePath)
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/mask-sub-key.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const MaskSubObjectKey = <T = {}>(path: string, object: T, callback: Call

if ((!strOrNullA && !strOrNullB)) return object;

const valToChange: string = strOrNullA ? strOrNullA : strOrNullB;
const valToChange: string = strOrNullA ? strOrNullA : strOrNullB ?? '';

if (value?.[lastKey]) value[lastKey] = callback(valToChange);

Expand Down
3 changes: 2 additions & 1 deletion lib/utils/step-from-axios-error.util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CatchError, Method, SProps, Type } from '../types';
import ExtractBodyToJson from './body-to-json.util';
import DeleteObjectKey from './delete-object-key.util';
import ExtractBodyAsObject from './extract-body.util';
import TagsFromBody from './get-tags-from-body.util';
Expand All @@ -15,7 +16,7 @@ export const StepPropsFromAxiosError = (error: CatchError, rmKeys: string[] = []
const name = error.name;
const headId = error?.config?.headers?.['uid'] ?? error?.config?.headers?.['id'];
const uid = error?.config?.data?.['id'] ?? headId;
const body = ExtractBodyAsObject(error?.config?.data);
const body = ExtractBodyToJson(error) ?? ExtractBodyAsObject(error?.config?.data);
const reqData = DeleteObjectKey(body, rmKeys);
const stack = error.stack ?? 'none';
const message = error.message;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ts-logs",
"description": "This package provide a skd for audit and manager logs in nodejs and express",
"version": "0.1.5",
"version": "0.2.0-beta",
"main": "index.js",
"types": "index.d.ts",
"author": "Alessandro Dev",
Expand Down Expand Up @@ -44,7 +44,7 @@
"homepage": "https://github.com/4lessandrodev/ts-logs",
"license": "MIT",
"engines": {
"node": ">=14.x <19"
"node": ">=14.x <22.x"
},
"keywords": [
"Logs",
Expand Down
13 changes: 13 additions & 0 deletions tests/body-to-json.util.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ExtractBodyToJson } from "../lib/utils/body-to-json.util"

describe('', () => {
it('should return null', () => {
const result = ExtractBodyToJson({} as any);
expect(result).toBeNull();
});

it('should call toJson fn', () => {
const result = ExtractBodyToJson({ toJSON: () => ({ sample: 'data' })} as any);
expect(result).toEqual({ sample: 'data' });
});
});
Loading