@jwn-js/common 1.1.48 → 1.3.2

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/index.d.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  import * as formidable from 'formidable';
2
+ import * as stream$1 from 'stream';
3
+ import { Stream } from 'stream';
2
4
  import * as uWebSockets_js from 'uWebSockets.js';
3
5
  import { HttpResponse, HttpRequest } from 'uWebSockets.js';
4
6
  export { ApiError, ApiErrorMessage } from './ApiError';
5
7
  export { Server, Handler as ServerHandler, Options as ServerOptions, Routes as ServerRoutes, WebSocket as ServerWebsocket, codeToStatus } from './Server';
6
8
  export { Jwt } from './Jwt';
7
9
  import { Client } from 'memjs';
10
+ import { Query } from 'buildmsql';
8
11
 
9
12
  declare class Memcached {
10
13
  memjs: Client;
@@ -54,10 +57,621 @@ declare class Memcached {
54
57
  */
55
58
  declare function staticBody(res: HttpResponse, req: HttpRequest, base: string): Promise<void>;
56
59
 
60
+ interface Context$3 {
61
+ readonly manifest: Record<string, any>;
62
+ readonly memcached: Memcached;
63
+ readonly [key: string]: any;
64
+ }
65
+ /**
66
+ * Ssr handler for production
67
+ */
68
+ declare class Ssr {
69
+ res: HttpResponse;
70
+ req: HttpRequest;
71
+ context: Context$3;
72
+ entry: CallableFunction;
73
+ /**
74
+ * @constructor
75
+ * @param res response
76
+ * @param req request
77
+ * @param context params
78
+ * @param entry - entry point function
79
+ */
80
+ constructor(res: HttpResponse, req: HttpRequest, context: Context$3, entry: CallableFunction);
81
+ /**
82
+ * @param request addition request params
83
+ */
84
+ request(request?: Record<string, any>): Promise<void>;
85
+ }
86
+
87
+ /**
88
+ * Input params of error
89
+ */
90
+ interface ApiErrorMessage {
91
+ message?: string;
92
+ code?: number;
93
+ statusCode?: number;
94
+ data?: Record<string, unknown>;
95
+ headers?: Record<string, string>;
96
+ }
97
+ /**
98
+ * @class Api Error
99
+ * @description Base error class for @jwn-js
100
+ */
101
+ declare class ApiError extends Error {
102
+ statusCode: number;
103
+ code: number;
104
+ data: Record<string, unknown>;
105
+ headers: Record<string, string>;
106
+ /**
107
+ * @constructor
108
+ * @param message
109
+ * @param code
110
+ * @param statusCode
111
+ */
112
+ constructor(message?: string, code?: number, statusCode?: number);
113
+ constructor(params: ApiErrorMessage | string, code?: number, statusCode?: number);
114
+ /**
115
+ * Get error message
116
+ */
117
+ getMessage(): string;
118
+ /**
119
+ * Get internal status code
120
+ */
121
+ getStatusCode(): number;
122
+ /**
123
+ * Get internal error code
124
+ */
125
+ getCode(): number;
126
+ /**
127
+ * Get error data
128
+ */
129
+ getData(): Record<string, unknown>;
130
+ /**
131
+ * Get error headers
132
+ */
133
+ getHeaders(): Record<string, string>;
134
+ }
135
+
136
+ declare type method$2 = 'any' | 'connect' | 'del' | 'get' | 'head' | 'options' | 'patch' | 'post' | 'put' | 'trace';
137
+ declare module "uWebSockets.js" {
138
+ interface HttpRequest {
139
+ cookies: Record<string, any>;
140
+ }
141
+ }
142
+ interface Route$1 {
143
+ name: string;
144
+ method?: method$2 | Array<method$2>;
145
+ component: any;
146
+ }
147
+ interface Context$2 {
148
+ readonly routes: Array<Route$1>;
149
+ readonly config: Config$1;
150
+ readonly db: Record<string, Query>;
151
+ readonly [key: string]: any;
152
+ }
153
+ interface Response$1 {
154
+ statusCode: number;
155
+ headers: Record<string, any>;
156
+ body: any;
157
+ }
158
+ interface Config$1 {
159
+ server: Record<string, any>;
160
+ db: Record<string, any>;
161
+ [key: string]: any;
162
+ }
163
+ interface ContextWeb {
164
+ readonly config: Config$1;
165
+ readonly db: Record<string, Query>;
166
+ controller: {
167
+ id: number;
168
+ name: string;
169
+ isActive: boolean;
170
+ isSitemap: boolean;
171
+ };
172
+ subaction: {
173
+ id: number;
174
+ name: string;
175
+ isPermission: boolean;
176
+ isCheckMethod: boolean;
177
+ isLog: boolean;
178
+ isActive: boolean;
179
+ };
180
+ action: {
181
+ id: number;
182
+ name: string;
183
+ isActive: boolean;
184
+ method: method$2;
185
+ };
186
+ [key: string]: any;
187
+ }
188
+ interface ContextWebController extends ContextWeb {
189
+ method: string;
190
+ cookies: Record<string, any>;
191
+ hostname: string;
192
+ protocol: string;
193
+ url: string;
194
+ headers: Record<string, any>;
195
+ getRequest: () => Record<string, any>;
196
+ db: Record<string, Query>;
197
+ $stream?: Stream;
198
+ $body?: any;
199
+ $route?: Route$1;
200
+ }
201
+ /**
202
+ * Entry point for /web api
203
+ * handler for api request
204
+ */
205
+ declare class Web {
206
+ res: HttpResponse;
207
+ req: HttpRequest;
208
+ context: Context$2;
209
+ defaultRequest: {
210
+ controller: string;
211
+ subaction: string;
212
+ lang: string;
213
+ };
214
+ defaultResponse: {
215
+ statusCode: number;
216
+ headers: {
217
+ "content-type": string;
218
+ };
219
+ body: {};
220
+ };
221
+ route: Route$1;
222
+ contextWeb: ContextWeb;
223
+ /**
224
+ * @constructor
225
+ * @param res response
226
+ * @param req request
227
+ * @param context params
228
+ */
229
+ constructor(res: HttpResponse, req: HttpRequest, context: Context$2);
230
+ request(request?: Record<string, any>): Promise<void>;
231
+ /**
232
+ * Import controller class
233
+ * @param request params
234
+ * @private
235
+ */
236
+ private importController;
237
+ /**
238
+ * Find route in api routers
239
+ * @private
240
+ */
241
+ private findRoute;
242
+ /**
243
+ * Inject app context
244
+ * @param obj
245
+ * @private
246
+ */
247
+ private injectContext;
248
+ /**
249
+ * Initing component if init method exists
250
+ * @param obj
251
+ * @returns
252
+ * @private
253
+ */
254
+ private initComponent;
255
+ /**
256
+ * Success response
257
+ */
258
+ success(response: Response$1): void;
259
+ /**
260
+ * Error response
261
+ */
262
+ error(e: ApiError | Error): void;
263
+ }
264
+
265
+ declare type method$1 = 'any' | 'connect' | 'del' | 'get' | 'head' | 'options' | 'patch' | 'post' | 'put' | 'trace';
266
+ declare module "uWebSockets.js" {
267
+ interface HttpRequest {
268
+ cookies: Record<string, any>;
269
+ }
270
+ }
271
+ interface Route {
272
+ name: string;
273
+ method?: method$1 | Array<method$1>;
274
+ component: any;
275
+ }
276
+ interface Config {
277
+ server: Record<string, any>;
278
+ db: Record<string, any>;
279
+ [key: string]: any;
280
+ }
281
+
282
+ interface TestContext {
283
+ readonly config?: Config;
284
+ readonly db?: Record<string, Query>;
285
+ controller?: {
286
+ id: number;
287
+ name: string;
288
+ isActive: boolean;
289
+ isSitemap: boolean;
290
+ };
291
+ subaction?: {
292
+ id: number;
293
+ name: string;
294
+ isPermission: boolean;
295
+ isCheckMethod: boolean;
296
+ isLog: boolean;
297
+ isActive: boolean;
298
+ };
299
+ action?: {
300
+ id: number;
301
+ name: string;
302
+ isActive: boolean;
303
+ method: method$1;
304
+ };
305
+ method?: method$1;
306
+ cookies?: Record<string, any>;
307
+ hostname?: string;
308
+ protocol?: string;
309
+ url?: string;
310
+ headers?: Record<string, any>;
311
+ getRequest?: () => Record<string, any>;
312
+ $stream?: Stream;
313
+ $body?: any;
314
+ $route?: Route;
315
+ [key: string]: any;
316
+ }
317
+ /**
318
+ * Create class with inject context
319
+ */
320
+ declare const mountWithContext: <T>($class: new () => T, $context: TestContext, request?: Record<string, any>) => Promise<T>;
321
+
322
+ interface Context$1 extends ContextWeb {
323
+ method: string;
324
+ getRequest: () => Record<string, any>;
325
+ db: Record<string, any>;
326
+ headers: Record<string, any>;
327
+ }
328
+ /**
329
+ * Base model
330
+ */
331
+ declare class Model$1 {
332
+ protected $context: Context$1;
333
+ /**
334
+ * Create model
335
+ * @param model
336
+ * @param args
337
+ */
338
+ protected $create<T extends Model$1 | Record<string, any>, A extends Array<any>>(model: {
339
+ new (...args: [...A]): T;
340
+ }, ...args: [...A]): Promise<T>;
341
+ /**
342
+ * Inject app context
343
+ * @param context
344
+ */
345
+ $inject(context: any): void;
346
+ getContext(): Context$1;
347
+ getRequest(): Record<string, any>;
348
+ getConfig(): Config$1;
349
+ getMethod(): string;
350
+ getHeaders(): Record<string, any>;
351
+ getController(): {
352
+ id: number;
353
+ name: string;
354
+ isActive: boolean;
355
+ isSitemap: boolean;
356
+ };
357
+ getAction(): {
358
+ id: number;
359
+ name: string;
360
+ isActive: boolean;
361
+ method: method$2;
362
+ };
363
+ getSubaction(): {
364
+ id: number;
365
+ name: string;
366
+ isPermission: boolean;
367
+ isCheckMethod: boolean;
368
+ isLog: boolean;
369
+ isActive: boolean;
370
+ };
371
+ }
372
+
373
+ interface ResponseOptions {
374
+ memcache?: number | null;
375
+ headers?: Record<string, any>;
376
+ statusCode?: number;
377
+ }
378
+ /**
379
+ * Base Controller
380
+ */
381
+ declare class Controller {
382
+ $context: ContextWebController;
383
+ /**
384
+ * Set cookies
385
+ * @param name
386
+ * @param value
387
+ * @param options
388
+ */
389
+ setCookieHeader(name: string, value: any, options?: Record<string, any>): string;
390
+ /**
391
+ * Success request
392
+ * @param data response params
393
+ * @param options
394
+ * @returns
395
+ */
396
+ success(data?: {}, options?: ResponseOptions | any): {
397
+ headers: any;
398
+ body: {
399
+ isError: boolean;
400
+ data: {};
401
+ };
402
+ memcache: any;
403
+ statusCode: any;
404
+ };
405
+ /**
406
+ * Error response
407
+ * @param err
408
+ */
409
+ error(err: unknown | Error | ApiError): {
410
+ headers: {
411
+ "content-type": string;
412
+ } & Record<string, string>;
413
+ body: {
414
+ isError: boolean;
415
+ error: string;
416
+ code: number;
417
+ data: Record<string, unknown>;
418
+ stack: string | undefined;
419
+ response: {
420
+ status: any;
421
+ headers: any;
422
+ config: {
423
+ url: any;
424
+ method: any;
425
+ params: any;
426
+ headers: any;
427
+ };
428
+ data: any;
429
+ } | {
430
+ status?: undefined;
431
+ headers?: undefined;
432
+ config?: undefined;
433
+ data?: undefined;
434
+ };
435
+ };
436
+ statusCode: number;
437
+ };
438
+ /**
439
+ * Xml response
440
+ * @param data response params
441
+ * @param options addition options
442
+ */
443
+ successXml(data?: {}, options?: ResponseOptions): {
444
+ headers: Record<string, any>;
445
+ body: string;
446
+ memcache: any;
447
+ };
448
+ /**
449
+ * Error xml response
450
+ * @param err error object
451
+ */
452
+ errorXml(err: unknown | Error | ApiError): {
453
+ headers: {
454
+ "content-type": string;
455
+ } & Record<string, string>;
456
+ body: string;
457
+ statusCode: number;
458
+ };
459
+ /**
460
+ * Create model
461
+ * @param model
462
+ * @param args
463
+ */
464
+ $create<T extends Model$1 | Record<string, any>, A extends Array<any>>(model: {
465
+ new (...args: [...A]): T;
466
+ }, ...args: [...A]): Promise<T>;
467
+ /**
468
+ * Inject app context
469
+ * @param context
470
+ */
471
+ $inject(context: ContextWebController): void;
472
+ getContext(): ContextWebController;
473
+ getRequest(): Record<string, any>;
474
+ getBody(): Buffer;
475
+ getStream(): Stream;
476
+ getConfig(): Config$1;
477
+ getMethod(): string;
478
+ getCookies(): Record<string, any>;
479
+ getHostname(): string;
480
+ getUrl(): string;
481
+ getProtocol(): string;
482
+ getDb(): Record<string, Query>;
483
+ getHeaders(): Record<string, any>;
484
+ getController(): {
485
+ id: number;
486
+ name: string;
487
+ isActive: boolean;
488
+ isSitemap: boolean;
489
+ };
490
+ getAction(): {
491
+ id: number;
492
+ name: string;
493
+ isActive: boolean;
494
+ method: method$2;
495
+ };
496
+ getSubaction(): {
497
+ id: number;
498
+ name: string;
499
+ isPermission: boolean;
500
+ isCheckMethod: boolean;
501
+ isLog: boolean;
502
+ isActive: boolean;
503
+ };
504
+ getHome(): Query;
505
+ }
506
+
507
+ interface Context extends ContextWeb {
508
+ method: string;
509
+ getRequest: () => Record<string, any>;
510
+ db: Record<string, any>;
511
+ headers: Record<string, any>;
512
+ }
513
+ /**
514
+ * Base model
515
+ */
516
+ declare class Model {
517
+ protected $context: Context;
518
+ /**
519
+ * Create model
520
+ * @param model
521
+ * @param args
522
+ */
523
+ protected $create<T extends Model | Record<string, any>, A extends Array<any>>(model: {
524
+ new (...args: [...A]): T;
525
+ }, ...args: [...A]): Promise<T>;
526
+ /**
527
+ * Inject app context
528
+ * @param context
529
+ */
530
+ $inject(context: any): void;
531
+ getContext(): Context;
532
+ getRequest(): Record<string, any>;
533
+ getConfig(): Config$1;
534
+ getMethod(): string;
535
+ getHeaders(): Record<string, any>;
536
+ getController(): {
537
+ id: number;
538
+ name: string;
539
+ isActive: boolean;
540
+ isSitemap: boolean;
541
+ };
542
+ getAction(): {
543
+ id: number;
544
+ name: string;
545
+ isActive: boolean;
546
+ method: method$2;
547
+ };
548
+ getSubaction(): {
549
+ id: number;
550
+ name: string;
551
+ isPermission: boolean;
552
+ isCheckMethod: boolean;
553
+ isLog: boolean;
554
+ isActive: boolean;
555
+ };
556
+ }
557
+
558
+ /**
559
+ * Response interface
560
+ */
561
+ interface Response {
562
+ memcache?: number | null;
563
+ headers?: Record<string, any>;
564
+ statusCode?: number;
565
+ }
566
+ declare type CallbackFunctionAny = (...args: any[]) => any;
567
+ /**
568
+ * Mixin helper for decorators
569
+ * @param behaviour
570
+ * @param sharedBehaviour
571
+ * http://raganwald.com/2015/06/26/decorators-in-es7.html
572
+ * http://raganwald.com/2015/06/17/functional-mixins.html
573
+ * https://habr.com/ru/post/277021/
574
+ */
575
+ declare function mixin<T extends Record<string, any>>(behaviour: T, sharedBehaviour?: {}): (clazz: any) => any;
576
+ /**
577
+ * @request() decorator
578
+ */
579
+ declare function request(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
580
+ /**
581
+ * @context() decorator
582
+ */
583
+ declare function context(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
584
+ /**
585
+ * @config() decorator
586
+ */
587
+ declare function config(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
588
+ /**
589
+ * @method() decorator
590
+ */
591
+ declare function method(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
592
+ /**
593
+ * @cookies() decorator
594
+ */
595
+ declare function cookies(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
596
+ /**
597
+ * @hostname() decorator
598
+ */
599
+ declare function hostname(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
600
+ /**
601
+ * @url() decorator
602
+ */
603
+ declare function url(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
604
+ /**
605
+ * @protocol() decorator
606
+ */
607
+ declare function protocol(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
608
+ /**
609
+ * @db() decorator
610
+ */
611
+ declare function db(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
612
+ /**
613
+ * @headers() decorator
614
+ */
615
+ declare function headers(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
616
+ /**
617
+ * @controller() decorator
618
+ */
619
+ declare function controller(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
620
+ /**
621
+ * @action() decorator
622
+ */
623
+ declare function action(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
624
+ /**
625
+ * @subaction() decorator
626
+ */
627
+ declare function subaction(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
628
+ /**
629
+ * @body() decorator
630
+ */
631
+ declare function body(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
632
+ /**
633
+ * @stream() decorator
634
+ */
635
+ declare function stream(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
636
+ /**
637
+ * @home() decorator
638
+ */
639
+ declare function home(): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
640
+ /**
641
+ * @connection() decorator
642
+ */
643
+ declare function connection(name?: string): (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
644
+ /**
645
+ * Json response
646
+ * @param opt response options
647
+ */
648
+ declare function json(opt?: Response): (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<CallbackFunctionAny>) => void;
649
+ /**
650
+ * Xml response
651
+ * @param opt response options
652
+ */
653
+ declare function xml(opt?: Response): (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<CallbackFunctionAny>) => void;
654
+ /**
655
+ * Logger
656
+ * @param name logger name
657
+ */
658
+ declare function logerror(name: string): (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<CallbackFunctionAny>) => void;
659
+ /**
660
+ * Subaction method decorator
661
+ * @http() decorator
662
+ */
663
+ declare function http(): (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<CallbackFunctionAny>) => void;
664
+ /**
665
+ * @init decorator
666
+ */
667
+ declare function init(): (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<CallbackFunctionAny>) => void;
668
+
57
669
  declare const helpers: {
58
670
  cookieParse: (str: string) => Record<string, any>;
59
671
  cookieString: (name: string, value: any, options?: Record<string, any>) => string;
60
672
  jsonBody: (res: uWebSockets_js.HttpResponse, req: uWebSockets_js.HttpRequest) => Promise<any>;
673
+ rawBody: (res: uWebSockets_js.HttpResponse, req: uWebSockets_js.HttpRequest) => Promise<Buffer>;
674
+ streamBody: (res: uWebSockets_js.HttpResponse, req: uWebSockets_js.HttpRequest) => stream$1.Stream;
61
675
  urlencodedBody: (res: uWebSockets_js.HttpResponse, req: uWebSockets_js.HttpRequest) => Promise<any>;
62
676
  multipartBody: (res: uWebSockets_js.HttpResponse, req: uWebSockets_js.HttpRequest, options?: formidable.Options) => Promise<any>;
63
677
  readConfig: (jsonfile: string) => Promise<any>;
@@ -67,4 +681,4 @@ declare const helpers: {
67
681
  getExt: (path: string) => string;
68
682
  };
69
683
 
70
- export { Memcached, helpers };
684
+ export { Controller, Memcached, Model, Ssr, Web, action, body, config, connection, context, controller, cookies, db, headers, helpers, home, hostname, http, init, json, logerror, method, mixin, mountWithContext as mount, protocol, request, stream, subaction, url, xml };