mielk-api 1.0.9 → 1.1.0
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/dist/back/index.d.ts +1 -6
- package/dist/back/index.js +7 -4
- package/dist/db/pg/connection/pool.d.ts +4 -0
- package/dist/db/pg/connection/pool.js +36 -0
- package/dist/db/pg/connection/tunnel.d.ts +2 -0
- package/dist/db/pg/connection/tunnel.js +30 -0
- package/dist/db/pg/index.d.ts +6 -0
- package/dist/db/pg/index.js +4 -0
- package/dist/{back/enums → db/pg/static}/PostgreErrorCodes.d.ts +1 -1
- package/dist/{back/enums → db/pg/static}/PostgreErrorCodes.js +1 -1
- package/dist/db/pg/types/DbConfig.d.ts +14 -0
- package/dist/{back/enums → http}/ApiStatus.d.ts +1 -1
- package/dist/{back/enums → http}/ApiStatus.js +2 -2
- package/dist/http/index.d.ts +7 -0
- package/dist/http/index.js +2 -0
- package/dist/{front/types.d.ts → http/types/ApiResponse.d.ts} +3 -3
- package/dist/http/types/ApiResponse.js +1 -0
- package/dist/http/types/ApiStatus.js +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/internal/messaging/messageTags.d.ts +8 -1
- package/dist/internal/messaging/messageTags.js +17 -9
- package/package.json +19 -10
- package/dist/front/index.d.ts +0 -4
- /package/dist/{back/types/types.js → db/mssql/index.d.ts} +0 -0
- /package/dist/{front → db/mssql}/index.js +0 -0
- /package/dist/{front/types.js → db/pg/types/DbConfig.js} +0 -0
- /package/dist/{utils → http/client}/Post.d.ts +0 -0
- /package/dist/{utils → http/client}/Post.js +0 -0
- /package/dist/{back/types/types.d.ts → http/types/ApiStatus.d.ts} +0 -0
package/dist/back/index.d.ts
CHANGED
package/dist/back/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export {
|
|
1
|
+
// import type { ApiStatus } from '../http/types/ApiStatus.js';
|
|
2
|
+
// import { PG_ERRORS } from '../db/PostgreErrorCodes.js';
|
|
3
|
+
// import { API_STATUS } from '../http/ApiStatus.js';
|
|
4
|
+
export {};
|
|
5
|
+
// export type { ApiStatus }
|
|
6
|
+
// export { PG_ERRORS }
|
|
7
|
+
// export { API_STATUS }
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Msg } from '../../../internal/messaging/messageTags.js';
|
|
11
|
+
import { Pool } from 'pg';
|
|
12
|
+
import { openSshTunnel } from './tunnel.js';
|
|
13
|
+
let pool = null;
|
|
14
|
+
let config = null;
|
|
15
|
+
export const initDb = (dbConfig) => {
|
|
16
|
+
config = Object.assign({ max: 10, min: 0, idleTimeoutMillis: 30000, connectionTimeoutMillis: 10000 }, dbConfig);
|
|
17
|
+
};
|
|
18
|
+
export function getPool() {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
if (pool)
|
|
21
|
+
return pool;
|
|
22
|
+
if (config === null)
|
|
23
|
+
throw new Error(Msg.connection.notInitialized);
|
|
24
|
+
if (!config.isProd) {
|
|
25
|
+
if (!config.ssh)
|
|
26
|
+
throw new Error(Msg.connection.sshOptionsNotSpecified);
|
|
27
|
+
const port = yield openSshTunnel(config.ssh, config.port);
|
|
28
|
+
config.port = port;
|
|
29
|
+
}
|
|
30
|
+
pool = new Pool(config);
|
|
31
|
+
pool.on('error', (err) => {
|
|
32
|
+
console.error(Msg.connection.postgreConnectionError, err);
|
|
33
|
+
});
|
|
34
|
+
return pool;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { createTunnel } from 'tunnel-ssh';
|
|
11
|
+
import { Msg } from '../../../internal/messaging/messageTags.js';
|
|
12
|
+
const tunnelOptions = {
|
|
13
|
+
autoClose: true,
|
|
14
|
+
reconnectOnError: true,
|
|
15
|
+
};
|
|
16
|
+
const serverOptions = {};
|
|
17
|
+
export function openSshTunnel(sshOptions, dstPort) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
try {
|
|
20
|
+
const forwardOptions = { dstPort };
|
|
21
|
+
const [server] = yield createTunnel(tunnelOptions, serverOptions, sshOptions, forwardOptions);
|
|
22
|
+
const port = server.address().port;
|
|
23
|
+
return port;
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
console.error(Msg.connection.sshTunnelFailed, err);
|
|
27
|
+
throw err;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SshOptions } from "tunnel-ssh";
|
|
2
|
+
export interface DbConfig {
|
|
3
|
+
isProd: boolean;
|
|
4
|
+
host: string;
|
|
5
|
+
port: number;
|
|
6
|
+
user: string;
|
|
7
|
+
password: string;
|
|
8
|
+
database: string;
|
|
9
|
+
ssh?: SshOptions;
|
|
10
|
+
max?: number;
|
|
11
|
+
min?: number;
|
|
12
|
+
idleTimeoutMillis?: number;
|
|
13
|
+
connectionTimeoutMillis?: number;
|
|
14
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Msg } from "
|
|
1
|
+
import { Msg } from "../internal/messaging/messageTags.js";
|
|
2
2
|
const createApiStatus = (success, code, defaultMessageTag) => ({ success, code, defaultMessageTag });
|
|
3
|
-
const msg = Msg.
|
|
3
|
+
const msg = Msg.apiStatus;
|
|
4
4
|
export const API_STATUS = {
|
|
5
5
|
OK: createApiStatus(true, 200, msg.ok),
|
|
6
6
|
CREATED: createApiStatus(true, 201, msg.created),
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ApiSuccess, ApiError, ApiErrorDetails } from '../http/types/ApiResponse.js';
|
|
2
|
+
import type { ApiResponse } from '../http/types/ApiResponse.js';
|
|
3
|
+
import type { ApiStatus } from './types/ApiStatus.js';
|
|
4
|
+
import { API_STATUS } from './ApiStatus.js';
|
|
5
|
+
export type { ApiStatus, ApiResponse };
|
|
6
|
+
export { ApiError, ApiSuccess, ApiErrorDetails };
|
|
7
|
+
export { API_STATUS };
|
|
@@ -7,8 +7,8 @@ export interface ApiError {
|
|
|
7
7
|
message: string;
|
|
8
8
|
params?: Record<string, string | number | Date | unknown | boolean>;
|
|
9
9
|
}
|
|
10
|
-
export
|
|
11
|
-
export type ApiErrorDetails = {
|
|
10
|
+
export interface ApiErrorDetails {
|
|
12
11
|
message: string;
|
|
13
12
|
params?: Record<string, string | number | Date | unknown | boolean>;
|
|
14
|
-
}
|
|
13
|
+
}
|
|
14
|
+
export type ApiResponse<T> = ApiSuccess<T> | ApiError;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * as
|
|
2
|
-
export * as
|
|
1
|
+
export * as pg from './db/pg/index.js';
|
|
2
|
+
export * as mssql from './db/mssql/index.js';
|
|
3
|
+
export * as http from './http/index.js';
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * as
|
|
2
|
-
export * as
|
|
1
|
+
export * as pg from './db/pg/index.js';
|
|
2
|
+
export * as mssql from './db/mssql/index.js';
|
|
3
|
+
export * as http from './http/index.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const Msg: {
|
|
2
|
-
|
|
2
|
+
apiStatus: {
|
|
3
3
|
ok: string;
|
|
4
4
|
created: string;
|
|
5
5
|
unauthorized: string;
|
|
@@ -8,4 +8,11 @@ export declare const Msg: {
|
|
|
8
8
|
conflict: string;
|
|
9
9
|
serverError: string;
|
|
10
10
|
};
|
|
11
|
+
connection: {
|
|
12
|
+
notInitialized: string;
|
|
13
|
+
sshOptionsNotSpecified: string;
|
|
14
|
+
sshTunnelFailed: string;
|
|
15
|
+
postgreConnectionError: string;
|
|
16
|
+
unauthorizedRequest: string;
|
|
17
|
+
};
|
|
11
18
|
};
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
const PARENT_FOLDER = 'api/';
|
|
2
|
-
const
|
|
2
|
+
const ___API_STATUS___ = `${PARENT_FOLDER}/ApiStatus`;
|
|
3
|
+
const ___CONNECTION___ = `${PARENT_FOLDER}/Connection`;
|
|
3
4
|
export const Msg = {
|
|
4
|
-
|
|
5
|
-
ok: `${
|
|
6
|
-
created: `${
|
|
7
|
-
unauthorized: `${
|
|
8
|
-
badRequest: `${
|
|
9
|
-
notFound: `${
|
|
10
|
-
conflict: `${
|
|
11
|
-
serverError: `${
|
|
5
|
+
apiStatus: {
|
|
6
|
+
ok: `${___API_STATUS___}:ok`,
|
|
7
|
+
created: `${___API_STATUS___}:created`,
|
|
8
|
+
unauthorized: `${___API_STATUS___}:unauthorized`,
|
|
9
|
+
badRequest: `${___API_STATUS___}:badRequest`,
|
|
10
|
+
notFound: `${___API_STATUS___}:notFound`,
|
|
11
|
+
conflict: `${___API_STATUS___}:conflict`,
|
|
12
|
+
serverError: `${___API_STATUS___}:serverError`,
|
|
13
|
+
},
|
|
14
|
+
connection: {
|
|
15
|
+
notInitialized: `${___CONNECTION___}:notInitialized`,
|
|
16
|
+
sshOptionsNotSpecified: `${___CONNECTION___}:sshOptionsNotSpecified`,
|
|
17
|
+
sshTunnelFailed: `${___CONNECTION___}:sshTunnelFailed`, // ❌ SSH tunnel failed
|
|
18
|
+
postgreConnectionError: `${___CONNECTION___}:postgreConnectionError`, // Unexpected PostgreSQL error
|
|
19
|
+
unauthorizedRequest: `${___CONNECTION___}:unauthorizedRequest`
|
|
12
20
|
}
|
|
13
21
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mielk-api",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"author": "mielk",
|
|
6
6
|
"description": "Wrapper for API operations",
|
|
@@ -13,15 +13,20 @@
|
|
|
13
13
|
"import": "./dist/index.js",
|
|
14
14
|
"default": "./dist/index.js"
|
|
15
15
|
},
|
|
16
|
-
"./
|
|
17
|
-
"types": "./dist/
|
|
18
|
-
"import": "./dist/
|
|
19
|
-
"default": "./dist/
|
|
16
|
+
"./db-mssql": {
|
|
17
|
+
"types": "./dist/db/mssql/index.d.ts",
|
|
18
|
+
"import": "./dist/db/mssql/index.js",
|
|
19
|
+
"default": "./dist/db/mssql/index.js"
|
|
20
20
|
},
|
|
21
|
-
"./
|
|
22
|
-
"types": "./dist/
|
|
23
|
-
"import": "./dist/
|
|
24
|
-
"default": "./dist/
|
|
21
|
+
"./db-pg": {
|
|
22
|
+
"types": "./dist/db/pg/index.d.ts",
|
|
23
|
+
"import": "./dist/db/pg/index.js",
|
|
24
|
+
"default": "./dist/db/pg/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./http": {
|
|
27
|
+
"types": "./dist/http/index.d.ts",
|
|
28
|
+
"import": "./dist/http/index.js",
|
|
29
|
+
"default": "./dist/http/index.js"
|
|
25
30
|
}
|
|
26
31
|
},
|
|
27
32
|
"scripts": {
|
|
@@ -44,6 +49,8 @@
|
|
|
44
49
|
"@babel/preset-env": "^7.25.8",
|
|
45
50
|
"@types/jest": "^30.0.0",
|
|
46
51
|
"@types/node": "^22.7.5",
|
|
52
|
+
"@types/pg": "^8.18.0",
|
|
53
|
+
"@types/tunnel-ssh": "^5.0.4",
|
|
47
54
|
"babel-jest": "^29.7.0",
|
|
48
55
|
"jest": "^30.3.0",
|
|
49
56
|
"jest-html-reporters": "^3.1.7",
|
|
@@ -56,6 +63,8 @@
|
|
|
56
63
|
"dist"
|
|
57
64
|
],
|
|
58
65
|
"dependencies": {
|
|
59
|
-
"mielk-fn": "^1.1.1"
|
|
66
|
+
"mielk-fn": "^1.1.1",
|
|
67
|
+
"pg": "^8.20.0",
|
|
68
|
+
"tunnel-ssh": "^5.2.0"
|
|
60
69
|
}
|
|
61
70
|
}
|
package/dist/front/index.d.ts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|