@aptly-as/sdk-nodejs 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # Aptly web sdk
2
+
3
+ SDK library for Node.js libraries.
4
+
5
+ ## Installation
6
+ ```
7
+ pnpm install
8
+ pnpm dev
9
+ ```
10
+
11
+ ## Code
12
+
13
+ ### AptlyResponseError
14
+ Handle errors for backend. Create simple to more complex errors with provided functions.\
15
+ Use `toJSON` to return a nice formated error to frontend.
@@ -0,0 +1,46 @@
1
+ import { AptlyErrorCode } from '@aptly-as/types';
2
+ export interface AptlyResponseErrorJson extends Omit<AptlyResponseErrorData, 'errors'> {
3
+ errors?: AptlyResponseErrorJsonSimple[];
4
+ }
5
+ export interface AptlyResponseErrorData {
6
+ id: string;
7
+ status: number;
8
+ title: string;
9
+ message: string;
10
+ code?: AptlyErrorCode;
11
+ link?: string;
12
+ detail?: string;
13
+ errors?: AptlyResponseError[];
14
+ }
15
+ export type AptlyResponseErrorJsonSimple = Pick<AptlyResponseErrorJson, 'title' | 'message' | 'code' | 'link' | 'detail'>;
16
+ export interface AptlyResponseErrorProps extends Partial<Omit<AptlyResponseErrorData, 'title' | 'status'>> {
17
+ level?: AptlyLogLevel;
18
+ error?: any;
19
+ }
20
+ export declare enum AptlyLogLevel {
21
+ Fatal = "fatal",
22
+ Error = "error",
23
+ Warning = "warn",
24
+ Info = "info",
25
+ Debug = "debug",
26
+ Trace = "trace"
27
+ }
28
+ export declare class AptlyResponseError extends Error {
29
+ readonly status: number;
30
+ private props;
31
+ name: string;
32
+ private _id;
33
+ static ofError(error: Error): AptlyResponseError;
34
+ constructor(status: number, message: string, props?: AptlyResponseErrorProps);
35
+ get level(): AptlyLogLevel;
36
+ get errors(): AptlyResponseError[];
37
+ get code(): AptlyErrorCode;
38
+ get link(): string;
39
+ get detail(): string;
40
+ get id(): string;
41
+ setId(id: string): this;
42
+ toJSON<T extends boolean>(simple?: T): T extends true ? AptlyResponseErrorJsonSimple : AptlyResponseErrorJson;
43
+ log(): this;
44
+ toString(): string;
45
+ res(_request: any, _response: any): void;
46
+ }
@@ -0,0 +1,89 @@
1
+ import { AptlyErrorCode } from '@aptly-as/types';
2
+ export var AptlyLogLevel;
3
+ (function (AptlyLogLevel) {
4
+ AptlyLogLevel["Fatal"] = "fatal";
5
+ AptlyLogLevel["Error"] = "error";
6
+ AptlyLogLevel["Warning"] = "warn";
7
+ AptlyLogLevel["Info"] = "info";
8
+ AptlyLogLevel["Debug"] = "debug";
9
+ AptlyLogLevel["Trace"] = "trace";
10
+ })(AptlyLogLevel || (AptlyLogLevel = {}));
11
+ export class AptlyResponseError extends Error {
12
+ static ofError(error) {
13
+ const responseError = Object.setPrototypeOf(error, AptlyResponseError.prototype);
14
+ responseError.status = 500;
15
+ return Object.assign(responseError, {
16
+ name: 'ResponseError',
17
+ props: {
18
+ code: AptlyErrorCode.Default,
19
+ level: AptlyLogLevel.Error,
20
+ errors: [],
21
+ detail: '',
22
+ link: '',
23
+ },
24
+ });
25
+ }
26
+ constructor(status, message, props = {}) {
27
+ super(message);
28
+ this.status = status;
29
+ this.props = props;
30
+ this.name = 'ResponseError';
31
+ this._id = '';
32
+ this.status = status;
33
+ this._id = props.id || '';
34
+ }
35
+ get level() {
36
+ return this.props.level || AptlyLogLevel.Warning;
37
+ }
38
+ get errors() {
39
+ return this.props.errors || [];
40
+ }
41
+ get code() {
42
+ return this.props.code || AptlyErrorCode.Default;
43
+ }
44
+ get link() {
45
+ return this.props.link || '';
46
+ }
47
+ get detail() {
48
+ return this.props.detail || '';
49
+ }
50
+ get id() {
51
+ return this._id;
52
+ }
53
+ setId(id) {
54
+ this._id = id;
55
+ return this;
56
+ }
57
+ toJSON(simple) {
58
+ if (simple) {
59
+ return {
60
+ code: this.code,
61
+ title: this.message,
62
+ message: this.message,
63
+ detail: this.detail,
64
+ link: this.link,
65
+ };
66
+ }
67
+ return {
68
+ id: this._id,
69
+ status: this.status,
70
+ code: this.code,
71
+ title: this.message,
72
+ message: this.message,
73
+ detail: this.detail,
74
+ link: this.link,
75
+ errors: this.errors?.map((x) => x.toJSON(true)),
76
+ };
77
+ }
78
+ log() {
79
+ console.error(this);
80
+ return this;
81
+ }
82
+ toString() {
83
+ return `${this.code}: ${this.message}`;
84
+ }
85
+ res(_request, _response) {
86
+ throw new Error('AptlyResponseError res function has not been implemented.');
87
+ }
88
+ }
89
+ //# sourceMappingURL=ResponseError.js.map
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,54 @@
1
+ import { AptlyErrorCode } from '@aptly-as/types';
2
+ import { describe, expect, test } from 'vitest';
3
+ import { createGeneralTestError } from '../test/test.utils.js';
4
+ import { AptlyResponseError, AptlyLogLevel } from './ResponseError.js';
5
+ describe('AptlyResponseError', () => {
6
+ test('should init', () => {
7
+ const error = new AptlyResponseError(500, 'message', {
8
+ errors: [],
9
+ error: new Error(),
10
+ level: AptlyLogLevel.Debug,
11
+ id: 'id',
12
+ detail: 'Some detail here',
13
+ link: 'link for helping to debug',
14
+ });
15
+ expect(error).instanceof(AptlyResponseError);
16
+ });
17
+ test('should create json for body', () => {
18
+ const responseError = AptlyResponseError.ofError(new Error('JSON'));
19
+ const json = responseError.toJSON();
20
+ expect(json).to.have.keys('id', 'code', 'detail', 'errors', 'status', 'title', 'message', 'link');
21
+ });
22
+ test('should create simple json for body', () => {
23
+ const responseError = AptlyResponseError.ofError(new Error('JSON'));
24
+ const json = responseError.toJSON(true);
25
+ expect(json).to.have.keys('code', 'detail', 'title', 'message', 'link');
26
+ });
27
+ test('should be created from normal error', () => {
28
+ const error = createGeneralTestError();
29
+ const responseError = AptlyResponseError.ofError(error).setId('id');
30
+ expect(error).instanceof(AptlyResponseError);
31
+ expect(responseError).instanceof(AptlyResponseError);
32
+ expect(responseError.id).to.equal('id');
33
+ expect(responseError.level).to.equal(AptlyLogLevel.Error);
34
+ expect(responseError.code).to.equal(AptlyErrorCode.Default);
35
+ expect(responseError.link).to.equal('');
36
+ expect(responseError.detail).to.equal('');
37
+ expect(Array.isArray(responseError.errors)).toBeTruthy();
38
+ expect(responseError.log).toBeTypeOf('function');
39
+ expect(responseError.toString).toBeTypeOf('function');
40
+ expect(responseError.toJSON).toBeTypeOf('function');
41
+ expect(responseError.stack).to.contain('\\test\\test.utils.ts');
42
+ expect(responseError.toJSON()).to.deep.equal({
43
+ id: 'id',
44
+ status: 500,
45
+ code: AptlyErrorCode.Default,
46
+ title: 'Error from another file',
47
+ message: 'Error from another file',
48
+ detail: '',
49
+ link: '',
50
+ errors: [],
51
+ });
52
+ });
53
+ });
54
+ //# sourceMappingURL=ResponseError.test.js.map
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './error/ResponseError.js';
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './error/ResponseError.js';
2
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@aptly-as/sdk-nodejs",
3
+ "version": "0.0.1",
4
+ "description": "Aptly SDK library for node.js applications",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "author": "Emil Andreas Olsen <emil@aptly.as>",
8
+ "license": "ISC",
9
+ "devDependencies": {
10
+ "@aptly-as/types": "^2.7.15",
11
+ "@vitest/ui": "^2.1.8",
12
+ "husky": "^9.1.7",
13
+ "lint-staged": "^15.2.11",
14
+ "prettier": "^3.4.2",
15
+ "typescript": "^5.7.2",
16
+ "vitest": "^2.1.8"
17
+ },
18
+ "peerDependencies": {
19
+ "@aptly-as/types": "*"
20
+ },
21
+ "lint-staged": {
22
+ "**/*": "prettier --write --ignore-unknown"
23
+ },
24
+ "scripts": {
25
+ "start": "tsc --incremental --watch",
26
+ "build": "tsc",
27
+ "test": "vitest",
28
+ "test:ui": "vitest --ui --api 9527",
29
+ "test:run": "vitest run",
30
+ "prepublish": "npm run build",
31
+ "postversion": "git push && git push --tags"
32
+ }
33
+ }