@technomoron/api-server-base 1.0.24 → 1.0.26

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.
@@ -49,7 +49,7 @@ export type ApiRoute = {
49
49
  req: ApiAuthClass;
50
50
  };
51
51
  };
52
- export declare class ApiModule<T extends ApiServer> {
52
+ export declare class ApiModule<T> {
53
53
  server: T;
54
54
  namespace: string;
55
55
  mountpath: string;
@@ -57,6 +57,7 @@ export declare class ApiModule<T extends ApiServer> {
57
57
  constructor(opts?: {
58
58
  namespace?: string;
59
59
  });
60
+ checkConfig(): boolean;
60
61
  defineRoutes(): ApiRoute[];
61
62
  }
62
63
  export interface ApiErrorParams {
@@ -72,21 +73,28 @@ export declare class ApiError extends Error {
72
73
  constructor({ code, message, data, errors }: ApiErrorParams);
73
74
  }
74
75
  export interface ApiServerConf {
75
- jwtSecret?: string;
76
- apiPort?: number;
77
- apiHost?: string;
78
- uploadPath?: string;
79
- uploadMax?: number;
80
- origins?: string[];
81
- debug?: boolean;
82
- accessExpire?: number;
83
- refreshExpire?: number;
84
- apiBasePath?: string;
76
+ apiPort: number;
77
+ apiHost: string;
78
+ uploadPath: string;
79
+ uploadMax: number;
80
+ origins: string[];
81
+ debug: boolean;
82
+ apiBasePath: string;
83
+ accessSecret: string;
84
+ refreshSecret: string;
85
+ cookieDomain: string;
86
+ accessCookie: string;
87
+ refreshCookie: string;
88
+ accessExpiry: number;
89
+ refreshExpiry: number;
90
+ authApi: boolean;
91
+ devMode: boolean;
85
92
  }
86
93
  export declare class ApiServer {
87
94
  app: Application;
88
95
  currReq: ApiRequest | null;
89
96
  readonly config: ApiServerConf;
97
+ constructor(config?: Partial<ApiServerConf>);
90
98
  jwtSign(payload: any, secret: string, expiresInSeconds: number, options?: SignOptions): JwtSignResult;
91
99
  jwtVerify<T>(token: string, secret: string, options?: VerifyOptions): JwtVerifyResult<T>;
92
100
  jwtDecode<T>(token: string, options?: jwt.DecodeOptions): JwtDecodeResult<T>;
@@ -97,6 +105,9 @@ export declare class ApiServer {
97
105
  access: string;
98
106
  refresh: string;
99
107
  userId: unknown;
108
+ domain?: string;
109
+ fingerprint?: string;
110
+ label?: string;
100
111
  }): Promise<void>;
101
112
  getToken(params: {
102
113
  accessToken?: string;
@@ -112,10 +123,12 @@ export declare class ApiServer {
112
123
  refreshToken?: string;
113
124
  accessToken?: string;
114
125
  userId?: unknown;
126
+ domain?: string;
127
+ fingerprint?: string;
128
+ label?: string;
115
129
  }): Promise<number>;
116
130
  verifyPassword(password: string, hash: string): Promise<boolean>;
117
131
  filterUser<T = any, U = any>(fullUser: T): U;
118
- constructor(config: ApiServerConf);
119
132
  guessExceptionText(error: any, defMsg?: string): string;
120
133
  protected authorize(apiReq: ApiRequest, requiredClass: ApiAuthClass): Promise<void>;
121
134
  private middlewares;
@@ -20,6 +20,9 @@ class ApiModule {
20
20
  this.mountpath = '';
21
21
  this.namespace = opts.namespace ?? this.constructor.defaultNamespace ?? '';
22
22
  }
23
+ checkConfig() {
24
+ return true;
25
+ }
23
26
  defineRoutes() {
24
27
  return [];
25
28
  }
@@ -52,7 +55,38 @@ class ApiError extends Error {
52
55
  }
53
56
  }
54
57
  exports.ApiError = ApiError;
58
+ function fillConfig(config) {
59
+ return {
60
+ apiPort: config.apiPort ?? 3101,
61
+ apiHost: config.apiHost ?? 'localhost',
62
+ uploadPath: config.uploadPath ?? '',
63
+ uploadMax: config.uploadMax ?? 30 * 1024 * 1024,
64
+ origins: config.origins ?? [],
65
+ debug: config.debug ?? false,
66
+ apiBasePath: config.apiBasePath ?? '/api',
67
+ accessSecret: config.accessSecret ?? '',
68
+ refreshSecret: config.refreshSecret ?? '',
69
+ cookieDomain: config.cookieDomain ?? '.somewhere-over-the-rainbow.com',
70
+ accessCookie: config.accessCookie ?? 'dat',
71
+ refreshCookie: config.refreshCookie ?? 'drt',
72
+ accessExpiry: config.accessExpiry ?? 60 * 15,
73
+ refreshExpiry: config.refreshExpiry ?? 30 * 24 * 60 * 60 * 1000,
74
+ authApi: config.authApi ?? false,
75
+ devMode: config.devMode ?? false
76
+ };
77
+ }
55
78
  class ApiServer {
79
+ constructor(config = {}) {
80
+ this.currReq = null;
81
+ this.config = fillConfig(config);
82
+ this.app = (0, express_1.default)();
83
+ if (config.uploadPath) {
84
+ const upload = (0, multer_1.default)({ dest: config.uploadPath });
85
+ this.app.use(upload.any());
86
+ }
87
+ this.middlewares();
88
+ // addSwaggerUi(this.app);
89
+ }
56
90
  jwtSign(payload, secret, expiresInSeconds, options) {
57
91
  options || (options = {});
58
92
  const opts = { ...options, expiresIn: expiresInSeconds };
@@ -147,27 +181,6 @@ class ApiServer {
147
181
  filterUser(fullUser) {
148
182
  return fullUser;
149
183
  }
150
- constructor(config) {
151
- this.currReq = null;
152
- config.jwtSecret || (config.jwtSecret = '');
153
- config.uploadMax || (config.uploadMax = 30 * 1024 * 1024);
154
- config.uploadPath || (config.uploadPath = '');
155
- config.origins || (config.origins = []);
156
- config.debug || (config.debug = false);
157
- config.apiHost || (config.apiHost = 'localhost');
158
- config.apiPort || (config.apiPort = 3101);
159
- config.accessExpire || (config.accessExpire = 60 * 15); // 15 minutes default
160
- config.refreshExpire || (config.refreshExpire = 30 * 24 * 60 * 60); // 30 days
161
- config.apiBasePath ?? (config.apiBasePath = '/api');
162
- this.config = config;
163
- this.app = (0, express_1.default)();
164
- if (config.uploadPath) {
165
- const upload = (0, multer_1.default)({ dest: config.uploadPath });
166
- this.app.use(upload.any());
167
- }
168
- this.middlewares();
169
- // addSwaggerUi(this.app);
170
- }
171
184
  guessExceptionText(error, defMsg = 'Unkown Error') {
172
185
  return guess_exception_text(error, defMsg);
173
186
  }
@@ -219,10 +232,10 @@ class ApiServer {
219
232
  return this;
220
233
  }
221
234
  async verifyJWT(token) {
222
- if (!this.config.jwtSecret) {
235
+ if (!this.config.accessSecret) {
223
236
  return { tokenData: undefined, error: 'JWT authentication disabled; no jwtSecret set' };
224
237
  }
225
- const result = this.jwtVerify(token, this.config.jwtSecret);
238
+ const result = this.jwtVerify(token, this.config.accessSecret);
226
239
  if (!result.success) {
227
240
  return { tokenData: undefined, error: result.error };
228
241
  }
@@ -327,6 +340,7 @@ class ApiServer {
327
340
  api(module) {
328
341
  const router = express_1.default.Router();
329
342
  module.server = this;
343
+ module.checkConfig();
330
344
  const base = this.config.apiBasePath ?? '/api';
331
345
  const ns = module.namespace;
332
346
  const mountPath = `${base}${ns}`;
@@ -49,7 +49,7 @@ export type ApiRoute = {
49
49
  req: ApiAuthClass;
50
50
  };
51
51
  };
52
- export declare class ApiModule<T extends ApiServer> {
52
+ export declare class ApiModule<T> {
53
53
  server: T;
54
54
  namespace: string;
55
55
  mountpath: string;
@@ -57,6 +57,7 @@ export declare class ApiModule<T extends ApiServer> {
57
57
  constructor(opts?: {
58
58
  namespace?: string;
59
59
  });
60
+ checkConfig(): boolean;
60
61
  defineRoutes(): ApiRoute[];
61
62
  }
62
63
  export interface ApiErrorParams {
@@ -72,21 +73,28 @@ export declare class ApiError extends Error {
72
73
  constructor({ code, message, data, errors }: ApiErrorParams);
73
74
  }
74
75
  export interface ApiServerConf {
75
- jwtSecret?: string;
76
- apiPort?: number;
77
- apiHost?: string;
78
- uploadPath?: string;
79
- uploadMax?: number;
80
- origins?: string[];
81
- debug?: boolean;
82
- accessExpire?: number;
83
- refreshExpire?: number;
84
- apiBasePath?: string;
76
+ apiPort: number;
77
+ apiHost: string;
78
+ uploadPath: string;
79
+ uploadMax: number;
80
+ origins: string[];
81
+ debug: boolean;
82
+ apiBasePath: string;
83
+ accessSecret: string;
84
+ refreshSecret: string;
85
+ cookieDomain: string;
86
+ accessCookie: string;
87
+ refreshCookie: string;
88
+ accessExpiry: number;
89
+ refreshExpiry: number;
90
+ authApi: boolean;
91
+ devMode: boolean;
85
92
  }
86
93
  export declare class ApiServer {
87
94
  app: Application;
88
95
  currReq: ApiRequest | null;
89
96
  readonly config: ApiServerConf;
97
+ constructor(config?: Partial<ApiServerConf>);
90
98
  jwtSign(payload: any, secret: string, expiresInSeconds: number, options?: SignOptions): JwtSignResult;
91
99
  jwtVerify<T>(token: string, secret: string, options?: VerifyOptions): JwtVerifyResult<T>;
92
100
  jwtDecode<T>(token: string, options?: jwt.DecodeOptions): JwtDecodeResult<T>;
@@ -97,6 +105,9 @@ export declare class ApiServer {
97
105
  access: string;
98
106
  refresh: string;
99
107
  userId: unknown;
108
+ domain?: string;
109
+ fingerprint?: string;
110
+ label?: string;
100
111
  }): Promise<void>;
101
112
  getToken(params: {
102
113
  accessToken?: string;
@@ -112,10 +123,12 @@ export declare class ApiServer {
112
123
  refreshToken?: string;
113
124
  accessToken?: string;
114
125
  userId?: unknown;
126
+ domain?: string;
127
+ fingerprint?: string;
128
+ label?: string;
115
129
  }): Promise<number>;
116
130
  verifyPassword(password: string, hash: string): Promise<boolean>;
117
131
  filterUser<T = any, U = any>(fullUser: T): U;
118
- constructor(config: ApiServerConf);
119
132
  guessExceptionText(error: any, defMsg?: string): string;
120
133
  protected authorize(apiReq: ApiRequest, requiredClass: ApiAuthClass): Promise<void>;
121
134
  private middlewares;
@@ -14,6 +14,9 @@ export class ApiModule {
14
14
  this.mountpath = '';
15
15
  this.namespace = opts.namespace ?? this.constructor.defaultNamespace ?? '';
16
16
  }
17
+ checkConfig() {
18
+ return true;
19
+ }
17
20
  defineRoutes() {
18
21
  return [];
19
22
  }
@@ -44,7 +47,38 @@ export class ApiError extends Error {
44
47
  this.errors = errors !== undefined ? errors : {};
45
48
  }
46
49
  }
50
+ function fillConfig(config) {
51
+ return {
52
+ apiPort: config.apiPort ?? 3101,
53
+ apiHost: config.apiHost ?? 'localhost',
54
+ uploadPath: config.uploadPath ?? '',
55
+ uploadMax: config.uploadMax ?? 30 * 1024 * 1024,
56
+ origins: config.origins ?? [],
57
+ debug: config.debug ?? false,
58
+ apiBasePath: config.apiBasePath ?? '/api',
59
+ accessSecret: config.accessSecret ?? '',
60
+ refreshSecret: config.refreshSecret ?? '',
61
+ cookieDomain: config.cookieDomain ?? '.somewhere-over-the-rainbow.com',
62
+ accessCookie: config.accessCookie ?? 'dat',
63
+ refreshCookie: config.refreshCookie ?? 'drt',
64
+ accessExpiry: config.accessExpiry ?? 60 * 15,
65
+ refreshExpiry: config.refreshExpiry ?? 30 * 24 * 60 * 60 * 1000,
66
+ authApi: config.authApi ?? false,
67
+ devMode: config.devMode ?? false
68
+ };
69
+ }
47
70
  export class ApiServer {
71
+ constructor(config = {}) {
72
+ this.currReq = null;
73
+ this.config = fillConfig(config);
74
+ this.app = express();
75
+ if (config.uploadPath) {
76
+ const upload = multer({ dest: config.uploadPath });
77
+ this.app.use(upload.any());
78
+ }
79
+ this.middlewares();
80
+ // addSwaggerUi(this.app);
81
+ }
48
82
  jwtSign(payload, secret, expiresInSeconds, options) {
49
83
  options || (options = {});
50
84
  const opts = { ...options, expiresIn: expiresInSeconds };
@@ -139,27 +173,6 @@ export class ApiServer {
139
173
  filterUser(fullUser) {
140
174
  return fullUser;
141
175
  }
142
- constructor(config) {
143
- this.currReq = null;
144
- config.jwtSecret || (config.jwtSecret = '');
145
- config.uploadMax || (config.uploadMax = 30 * 1024 * 1024);
146
- config.uploadPath || (config.uploadPath = '');
147
- config.origins || (config.origins = []);
148
- config.debug || (config.debug = false);
149
- config.apiHost || (config.apiHost = 'localhost');
150
- config.apiPort || (config.apiPort = 3101);
151
- config.accessExpire || (config.accessExpire = 60 * 15); // 15 minutes default
152
- config.refreshExpire || (config.refreshExpire = 30 * 24 * 60 * 60); // 30 days
153
- config.apiBasePath ?? (config.apiBasePath = '/api');
154
- this.config = config;
155
- this.app = express();
156
- if (config.uploadPath) {
157
- const upload = multer({ dest: config.uploadPath });
158
- this.app.use(upload.any());
159
- }
160
- this.middlewares();
161
- // addSwaggerUi(this.app);
162
- }
163
176
  guessExceptionText(error, defMsg = 'Unkown Error') {
164
177
  return guess_exception_text(error, defMsg);
165
178
  }
@@ -211,10 +224,10 @@ export class ApiServer {
211
224
  return this;
212
225
  }
213
226
  async verifyJWT(token) {
214
- if (!this.config.jwtSecret) {
227
+ if (!this.config.accessSecret) {
215
228
  return { tokenData: undefined, error: 'JWT authentication disabled; no jwtSecret set' };
216
229
  }
217
- const result = this.jwtVerify(token, this.config.jwtSecret);
230
+ const result = this.jwtVerify(token, this.config.accessSecret);
218
231
  if (!result.success) {
219
232
  return { tokenData: undefined, error: result.error };
220
233
  }
@@ -319,6 +332,7 @@ export class ApiServer {
319
332
  api(module) {
320
333
  const router = express.Router();
321
334
  module.server = this;
335
+ module.checkConfig();
322
336
  const base = this.config.apiBasePath ?? '/api';
323
337
  const ns = module.namespace;
324
338
  const mountPath = `${base}${ns}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@technomoron/api-server-base",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "description": "Api Server Skeleton / Base Class",
5
5
  "main": "dist/cjs/api-server-base.js",
6
6
  "module": "dist/esm/api-server-base.js",
@@ -34,20 +34,20 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "@types/cookie-parser": "^1.4.8",
37
- "@types/cors": "^2.8.17",
38
- "@types/express": "^5.0.1",
37
+ "@types/cors": "^2.8.18",
38
+ "@types/express": "^5.0.2",
39
39
  "@types/express-serve-static-core": "^5.0.6",
40
40
  "@types/jsonwebtoken": "^9.0.9",
41
41
  "@types/multer": "^1.4.12",
42
- "@typescript-eslint/eslint-plugin": "^8.30.1",
43
- "@typescript-eslint/parser": "^8.30.1",
42
+ "@typescript-eslint/eslint-plugin": "^8.33.0",
43
+ "@typescript-eslint/parser": "^8.33.0",
44
44
  "@vue/eslint-config-prettier": "10.2.0",
45
45
  "@vue/eslint-config-typescript": "14.5.0",
46
- "eslint": "^9.25.1",
46
+ "eslint": "^9.28.0",
47
47
  "eslint-plugin-import": "^2.31.0",
48
- "eslint-plugin-vue": "^10.0.0",
48
+ "eslint-plugin-vue": "^10.1.0",
49
49
  "prettier": "^3.5.3",
50
- "typescript": "^5.6.3"
50
+ "typescript": "^5.8.3"
51
51
  },
52
52
  "files": [
53
53
  "dist/",