@technomoron/api-server-base 1.0.24 → 1.0.25

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;
@@ -72,21 +72,28 @@ export declare class ApiError extends Error {
72
72
  constructor({ code, message, data, errors }: ApiErrorParams);
73
73
  }
74
74
  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;
75
+ apiPort: number;
76
+ apiHost: string;
77
+ uploadPath: string;
78
+ uploadMax: number;
79
+ origins: string[];
80
+ debug: boolean;
81
+ apiBasePath: string;
82
+ accessSecret: string;
83
+ refreshSecret: string;
84
+ cookieDomain: string;
85
+ accessCookie: string;
86
+ refreshCookie: string;
87
+ accessExpiry: number;
88
+ refreshExpiry: number;
89
+ authApi: boolean;
90
+ devMode: boolean;
85
91
  }
86
92
  export declare class ApiServer {
87
93
  app: Application;
88
94
  currReq: ApiRequest | null;
89
95
  readonly config: ApiServerConf;
96
+ constructor(config?: Partial<ApiServerConf>);
90
97
  jwtSign(payload: any, secret: string, expiresInSeconds: number, options?: SignOptions): JwtSignResult;
91
98
  jwtVerify<T>(token: string, secret: string, options?: VerifyOptions): JwtVerifyResult<T>;
92
99
  jwtDecode<T>(token: string, options?: jwt.DecodeOptions): JwtDecodeResult<T>;
@@ -97,6 +104,9 @@ export declare class ApiServer {
97
104
  access: string;
98
105
  refresh: string;
99
106
  userId: unknown;
107
+ domain?: string;
108
+ fingerprint?: string;
109
+ label?: string;
100
110
  }): Promise<void>;
101
111
  getToken(params: {
102
112
  accessToken?: string;
@@ -112,10 +122,12 @@ export declare class ApiServer {
112
122
  refreshToken?: string;
113
123
  accessToken?: string;
114
124
  userId?: unknown;
125
+ domain?: string;
126
+ fingerprint?: string;
127
+ label?: string;
115
128
  }): Promise<number>;
116
129
  verifyPassword(password: string, hash: string): Promise<boolean>;
117
130
  filterUser<T = any, U = any>(fullUser: T): U;
118
- constructor(config: ApiServerConf);
119
131
  guessExceptionText(error: any, defMsg?: string): string;
120
132
  protected authorize(apiReq: ApiRequest, requiredClass: ApiAuthClass): Promise<void>;
121
133
  private middlewares;
@@ -52,7 +52,38 @@ class ApiError extends Error {
52
52
  }
53
53
  }
54
54
  exports.ApiError = ApiError;
55
+ function fillConfig(config) {
56
+ return {
57
+ apiPort: config.apiPort ?? 3101,
58
+ apiHost: config.apiHost ?? 'localhost',
59
+ uploadPath: config.uploadPath ?? '',
60
+ uploadMax: config.uploadMax ?? 30 * 1024 * 1024,
61
+ origins: config.origins ?? [],
62
+ debug: config.debug ?? false,
63
+ apiBasePath: config.apiBasePath ?? '/api',
64
+ accessSecret: config.accessSecret ?? '',
65
+ refreshSecret: config.refreshSecret ?? '',
66
+ cookieDomain: config.cookieDomain ?? '.somewhere-over-the-rainbow.com',
67
+ accessCookie: config.accessCookie ?? 'dat',
68
+ refreshCookie: config.refreshCookie ?? 'drt',
69
+ accessExpiry: config.accessExpiry ?? 60 * 15,
70
+ refreshExpiry: config.refreshExpiry ?? 30 * 24 * 60 * 60 * 1000,
71
+ authApi: config.authApi ?? false,
72
+ devMode: config.devMode ?? false
73
+ };
74
+ }
55
75
  class ApiServer {
76
+ constructor(config = {}) {
77
+ this.currReq = null;
78
+ this.config = fillConfig(config);
79
+ this.app = (0, express_1.default)();
80
+ if (config.uploadPath) {
81
+ const upload = (0, multer_1.default)({ dest: config.uploadPath });
82
+ this.app.use(upload.any());
83
+ }
84
+ this.middlewares();
85
+ // addSwaggerUi(this.app);
86
+ }
56
87
  jwtSign(payload, secret, expiresInSeconds, options) {
57
88
  options || (options = {});
58
89
  const opts = { ...options, expiresIn: expiresInSeconds };
@@ -147,27 +178,6 @@ class ApiServer {
147
178
  filterUser(fullUser) {
148
179
  return fullUser;
149
180
  }
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
181
  guessExceptionText(error, defMsg = 'Unkown Error') {
172
182
  return guess_exception_text(error, defMsg);
173
183
  }
@@ -219,10 +229,10 @@ class ApiServer {
219
229
  return this;
220
230
  }
221
231
  async verifyJWT(token) {
222
- if (!this.config.jwtSecret) {
232
+ if (!this.config.accessSecret) {
223
233
  return { tokenData: undefined, error: 'JWT authentication disabled; no jwtSecret set' };
224
234
  }
225
- const result = this.jwtVerify(token, this.config.jwtSecret);
235
+ const result = this.jwtVerify(token, this.config.accessSecret);
226
236
  if (!result.success) {
227
237
  return { tokenData: undefined, error: result.error };
228
238
  }
@@ -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;
@@ -72,21 +72,28 @@ export declare class ApiError extends Error {
72
72
  constructor({ code, message, data, errors }: ApiErrorParams);
73
73
  }
74
74
  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;
75
+ apiPort: number;
76
+ apiHost: string;
77
+ uploadPath: string;
78
+ uploadMax: number;
79
+ origins: string[];
80
+ debug: boolean;
81
+ apiBasePath: string;
82
+ accessSecret: string;
83
+ refreshSecret: string;
84
+ cookieDomain: string;
85
+ accessCookie: string;
86
+ refreshCookie: string;
87
+ accessExpiry: number;
88
+ refreshExpiry: number;
89
+ authApi: boolean;
90
+ devMode: boolean;
85
91
  }
86
92
  export declare class ApiServer {
87
93
  app: Application;
88
94
  currReq: ApiRequest | null;
89
95
  readonly config: ApiServerConf;
96
+ constructor(config?: Partial<ApiServerConf>);
90
97
  jwtSign(payload: any, secret: string, expiresInSeconds: number, options?: SignOptions): JwtSignResult;
91
98
  jwtVerify<T>(token: string, secret: string, options?: VerifyOptions): JwtVerifyResult<T>;
92
99
  jwtDecode<T>(token: string, options?: jwt.DecodeOptions): JwtDecodeResult<T>;
@@ -97,6 +104,9 @@ export declare class ApiServer {
97
104
  access: string;
98
105
  refresh: string;
99
106
  userId: unknown;
107
+ domain?: string;
108
+ fingerprint?: string;
109
+ label?: string;
100
110
  }): Promise<void>;
101
111
  getToken(params: {
102
112
  accessToken?: string;
@@ -112,10 +122,12 @@ export declare class ApiServer {
112
122
  refreshToken?: string;
113
123
  accessToken?: string;
114
124
  userId?: unknown;
125
+ domain?: string;
126
+ fingerprint?: string;
127
+ label?: string;
115
128
  }): Promise<number>;
116
129
  verifyPassword(password: string, hash: string): Promise<boolean>;
117
130
  filterUser<T = any, U = any>(fullUser: T): U;
118
- constructor(config: ApiServerConf);
119
131
  guessExceptionText(error: any, defMsg?: string): string;
120
132
  protected authorize(apiReq: ApiRequest, requiredClass: ApiAuthClass): Promise<void>;
121
133
  private middlewares;
@@ -44,7 +44,38 @@ export class ApiError extends Error {
44
44
  this.errors = errors !== undefined ? errors : {};
45
45
  }
46
46
  }
47
+ function fillConfig(config) {
48
+ return {
49
+ apiPort: config.apiPort ?? 3101,
50
+ apiHost: config.apiHost ?? 'localhost',
51
+ uploadPath: config.uploadPath ?? '',
52
+ uploadMax: config.uploadMax ?? 30 * 1024 * 1024,
53
+ origins: config.origins ?? [],
54
+ debug: config.debug ?? false,
55
+ apiBasePath: config.apiBasePath ?? '/api',
56
+ accessSecret: config.accessSecret ?? '',
57
+ refreshSecret: config.refreshSecret ?? '',
58
+ cookieDomain: config.cookieDomain ?? '.somewhere-over-the-rainbow.com',
59
+ accessCookie: config.accessCookie ?? 'dat',
60
+ refreshCookie: config.refreshCookie ?? 'drt',
61
+ accessExpiry: config.accessExpiry ?? 60 * 15,
62
+ refreshExpiry: config.refreshExpiry ?? 30 * 24 * 60 * 60 * 1000,
63
+ authApi: config.authApi ?? false,
64
+ devMode: config.devMode ?? false
65
+ };
66
+ }
47
67
  export class ApiServer {
68
+ constructor(config = {}) {
69
+ this.currReq = null;
70
+ this.config = fillConfig(config);
71
+ this.app = express();
72
+ if (config.uploadPath) {
73
+ const upload = multer({ dest: config.uploadPath });
74
+ this.app.use(upload.any());
75
+ }
76
+ this.middlewares();
77
+ // addSwaggerUi(this.app);
78
+ }
48
79
  jwtSign(payload, secret, expiresInSeconds, options) {
49
80
  options || (options = {});
50
81
  const opts = { ...options, expiresIn: expiresInSeconds };
@@ -139,27 +170,6 @@ export class ApiServer {
139
170
  filterUser(fullUser) {
140
171
  return fullUser;
141
172
  }
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
173
  guessExceptionText(error, defMsg = 'Unkown Error') {
164
174
  return guess_exception_text(error, defMsg);
165
175
  }
@@ -211,10 +221,10 @@ export class ApiServer {
211
221
  return this;
212
222
  }
213
223
  async verifyJWT(token) {
214
- if (!this.config.jwtSecret) {
224
+ if (!this.config.accessSecret) {
215
225
  return { tokenData: undefined, error: 'JWT authentication disabled; no jwtSecret set' };
216
226
  }
217
- const result = this.jwtVerify(token, this.config.jwtSecret);
227
+ const result = this.jwtVerify(token, this.config.accessSecret);
218
228
  if (!result.success) {
219
229
  return { tokenData: undefined, error: result.error };
220
230
  }
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.25",
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",