mielk-api 1.0.8 → 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.
@@ -1,6 +1 @@
1
- import type { ApiStatus } from './types/types.js';
2
- import { PG_ERRORS } from './enums/PostgreErrorCodes.js';
3
- import { API_STATUS } from './enums/ApiStatus.js';
4
- export type { ApiStatus };
5
- export { PG_ERRORS };
6
- export { API_STATUS };
1
+ export {};
@@ -1,4 +1,7 @@
1
- import { PG_ERRORS } from './enums/PostgreErrorCodes.js';
2
- import { API_STATUS } from './enums/ApiStatus.js';
3
- export { PG_ERRORS };
4
- export { API_STATUS };
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,4 @@
1
+ import { Pool } from 'pg';
2
+ import { DbConfig } from '../types/DbConfig.js';
3
+ export declare const initDb: (dbConfig: DbConfig) => void;
4
+ export declare function getPool(): Promise<Pool>;
@@ -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,2 @@
1
+ import { SshOptions } from 'tunnel-ssh';
2
+ export declare function openSshTunnel(sshOptions: SshOptions, dstPort: number): Promise<number>;
@@ -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,6 @@
1
+ import { POSTGRE_ERRORS } from './static/PostgreErrorCodes.js';
2
+ import { initDb, getPool } from './connection/pool.js';
3
+ import type { DbConfig } from './types/DbConfig.js';
4
+ export { POSTGRE_ERRORS };
5
+ export { initDb, getPool };
6
+ export type { DbConfig };
@@ -0,0 +1,4 @@
1
+ import { POSTGRE_ERRORS } from './static/PostgreErrorCodes.js';
2
+ import { initDb, getPool } from './connection/pool.js';
3
+ export { POSTGRE_ERRORS };
4
+ export { initDb, getPool };
@@ -1,4 +1,4 @@
1
- export declare const PG_ERRORS: {
1
+ export declare const POSTGRE_ERRORS: {
2
2
  UNIQUE_VIOLATION: string;
3
3
  FOREIGN_KEY_VIOLATION: string;
4
4
  };
@@ -1,4 +1,4 @@
1
- export const PG_ERRORS = {
1
+ export const POSTGRE_ERRORS = {
2
2
  UNIQUE_VIOLATION: '23505',
3
3
  FOREIGN_KEY_VIOLATION: '23503',
4
4
  };
@@ -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,4 +1,4 @@
1
- import { ApiStatus } from "../types/types.js";
1
+ import { ApiStatus } from "./types/ApiStatus.js";
2
2
  export declare const API_STATUS: {
3
3
  OK: ApiStatus;
4
4
  CREATED: ApiStatus;
@@ -1,6 +1,6 @@
1
- import { Msg } from "../../internal/messaging/messageTags.js";
1
+ import { Msg } from "../internal/messaging/messageTags.js";
2
2
  const createApiStatus = (success, code, defaultMessageTag) => ({ success, code, defaultMessageTag });
3
- const msg = Msg.ApiStatus;
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 };
@@ -0,0 +1,2 @@
1
+ import { API_STATUS } from './ApiStatus.js';
2
+ export { API_STATUS };
@@ -0,0 +1,14 @@
1
+ export interface ApiSuccess<T> {
2
+ success: true;
3
+ data: T;
4
+ }
5
+ export interface ApiError {
6
+ success: false;
7
+ message: string;
8
+ params?: Record<string, string | number | Date | unknown | boolean>;
9
+ }
10
+ export interface ApiErrorDetails {
11
+ message: string;
12
+ params?: Record<string, string | number | Date | unknown | boolean>;
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 back from './back/index.js';
2
- export * as front from './front/index.js';
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 back from './back/index.js';
2
- export * as front from './front/index.js';
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
- ApiStatus: {
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 API_STATUS_FOLDER = `${PARENT_FOLDER}/ApiStatus`;
2
+ const ___API_STATUS___ = `${PARENT_FOLDER}/ApiStatus`;
3
+ const ___CONNECTION___ = `${PARENT_FOLDER}/Connection`;
3
4
  export const Msg = {
4
- ApiStatus: {
5
- ok: `${API_STATUS_FOLDER}:ok`,
6
- created: `${API_STATUS_FOLDER}:created`,
7
- unauthorized: `${API_STATUS_FOLDER}:unauthorized`,
8
- badRequest: `${API_STATUS_FOLDER}:badRequest`,
9
- notFound: `${API_STATUS_FOLDER}:notFound`,
10
- conflict: `${API_STATUS_FOLDER}:conflict`,
11
- serverError: `${API_STATUS_FOLDER}: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.8",
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
- "./back": {
17
- "types": "./dist/back/index.d.ts",
18
- "import": "./dist/back/index.js",
19
- "default": "./dist/back/index.js"
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
- "./front": {
22
- "types": "./dist/front/index.d.ts",
23
- "import": "./dist/front/index.js",
24
- "default": "./dist/front/index.js"
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
  }
@@ -1,4 +0,0 @@
1
- import { ApiSuccess, ApiError } from './types.js';
2
- import type { ApiResponse, ApiErrorDetails } from './types.js';
3
- export { ApiError, ApiSuccess };
4
- export type { ApiResponse, ApiErrorDetails };
@@ -1,13 +0,0 @@
1
- export interface ApiSuccess<T> {
2
- success: true;
3
- data: T;
4
- }
5
- export interface ApiError {
6
- success: false;
7
- details: ApiErrorDetails;
8
- }
9
- export type ApiResponse<T> = ApiSuccess<T> | ApiError;
10
- export type ApiErrorDetails = {
11
- message: string;
12
- params?: (string | number | Date | unknown | boolean)[];
13
- };
File without changes
File without changes
File without changes