@moostjs/event-http 0.2.13 → 0.2.15

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/index.cjs CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  var eventHttp = require('@wooksjs/event-http');
4
4
  var moost = require('moost');
5
+ var http = require('http');
6
+ var https = require('https');
5
7
  var eventCore = require('@wooksjs/event-core');
6
8
 
7
9
  /******************************************************************************
@@ -65,7 +67,7 @@ class MoostHttp {
65
67
  return this.httpApp.listen(...args);
66
68
  }
67
69
  getProvideRegistry() {
68
- return createProvideRegistry([eventHttp.WooksHttp, () => this.getHttpApp()], ['WooksHttp', () => this.getHttpApp()]);
70
+ return createProvideRegistry([eventHttp.WooksHttp, () => this.getHttpApp()], ['WooksHttp', () => this.getHttpApp()], [http.Server, () => this.getHttpApp().getServer()], [https.Server, () => this.getHttpApp().getServer()]);
69
71
  }
70
72
  bindHandler(opts) {
71
73
  let fn;
@@ -472,7 +474,7 @@ function Query(name) {
472
474
  return value === '' && p.has(name) || value;
473
475
  }
474
476
  const json = jsonSearchParams();
475
- return Object.keys(json).length ? json : undefined;
477
+ return Object.keys(json).length ? json : null;
476
478
  }, name || 'Query');
477
479
  }
478
480
  /**
@@ -481,7 +483,7 @@ function Query(name) {
481
483
  * @paramType string
482
484
  */
483
485
  function Url() {
484
- return moost.Resolve(() => eventHttp.useRequest().url, 'url');
486
+ return moost.Resolve(() => eventHttp.useRequest().url || '', 'url');
485
487
  }
486
488
  /**
487
489
  * Get Requested HTTP Method
@@ -621,11 +623,12 @@ class Mate {
621
623
  }
622
624
  set(args, key, value, isArray) {
623
625
  var _a;
626
+ let level = 'CLASS';
624
627
  const newArgs = args.level === 'CLASS' ? { target: args.target }
625
- : args.level === 'PROPERTY' ? { target: args.target, propKey: args.propKey }
628
+ : args.level === 'PROP' ? { target: args.target, propKey: args.propKey }
626
629
  : args;
627
630
  let meta = Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {};
628
- if (newArgs.propKey && this.options.readReturnType && !meta.returnType) {
631
+ if (newArgs.propKey && this.options.readReturnType && !meta.returnType && args.descriptor) {
629
632
  meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
630
633
  }
631
634
  if (newArgs.propKey && this.options.readType && !meta.type) {
@@ -638,12 +641,13 @@ class Mate {
638
641
  data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
639
642
  }
640
643
  if (typeof index === 'number') {
644
+ level = 'PARAM';
641
645
  data.params = data.params || [];
642
646
  data.params[index] = data.params[index] || {
643
647
  type: undefined,
644
648
  };
645
649
  if (cb) {
646
- data.params[index] = cb(data.params[index], args.propKey, typeof args.index === 'number' ? args.index : undefined);
650
+ data.params[index] = cb(data.params[index], level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
647
651
  }
648
652
  else {
649
653
  data = data.params[index];
@@ -660,6 +664,7 @@ class Mate {
660
664
  return meta;
661
665
  });
662
666
  }
667
+ level = typeof index === 'number' ? 'PARAM' : newArgs.propKey && newArgs.descriptor ? 'METHOD' : newArgs.propKey ? 'PROP' : 'CLASS';
663
668
  if (typeof key !== 'function') {
664
669
  if (isArray) {
665
670
  const newArray = (data[key] || []);
@@ -674,11 +679,11 @@ class Mate {
674
679
  }
675
680
  }
676
681
  else if (cb && typeof index !== 'number') {
677
- meta = cb(data, args.propKey, typeof args.index === 'number' ? args.index : undefined);
682
+ meta = cb(data, level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
678
683
  }
679
684
  Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
680
685
  }
681
- read(target, propKey, index) {
686
+ read(target, propKey) {
682
687
  const isConstr = isConstructor(target);
683
688
  const constructor = isConstr ? target : getConstructor(target);
684
689
  const proto = constructor.prototype;
@@ -689,17 +694,31 @@ class Mate {
689
694
  if (inheritFn) {
690
695
  if (typeof propKey === 'string') {
691
696
  const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
692
- shouldInherit = inheritFn(classMeta, propKey, ownMeta);
697
+ shouldInherit = inheritFn(classMeta, ownMeta, 'PROP', propKey);
693
698
  }
694
699
  else {
695
- shouldInherit = inheritFn(ownMeta);
700
+ shouldInherit = inheritFn(ownMeta, ownMeta, 'CLASS');
696
701
  }
697
702
  }
698
703
  if (shouldInherit) {
699
704
  const parent = Object.getPrototypeOf(constructor);
700
705
  if (typeof parent === 'function' && parent !== fnProto && parent !== constructor) {
701
- const inheritedMeta = this.read(parent, propKey);
702
- ownMeta = { ...inheritedMeta, ...ownMeta, params: ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params };
706
+ const inheritedMeta = this.read(parent, propKey) || {};
707
+ const ownParams = ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params;
708
+ ownMeta = { ...inheritedMeta, ...ownMeta };
709
+ if (typeof propKey === 'string' && ownParams && (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params)) {
710
+ for (let i = 0; i < ownParams.length; i++) {
711
+ if (typeof (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i]) !== 'undefined') {
712
+ const ownParam = ownParams[i];
713
+ if (ownMeta.params && inheritFn && inheritFn(ownMeta, ownParam, 'PARAM', typeof propKey === 'string' ? propKey : undefined)) {
714
+ ownMeta.params[i] = {
715
+ ...inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i],
716
+ ...ownParams[i],
717
+ };
718
+ }
719
+ }
720
+ }
721
+ }
703
722
  }
704
723
  }
705
724
  }
@@ -724,6 +743,16 @@ class Mate {
724
743
  this.set(args, key, value, isArray);
725
744
  });
726
745
  }
746
+ decorateConditional(ccb) {
747
+ return ((target, propKey, descriptor) => {
748
+ const hasIndex = typeof descriptor === 'number';
749
+ const decoratorLevel = hasIndex ? 'PARAM' : propKey && descriptor ? 'METHOD' : propKey ? 'PROP' : 'CLASS';
750
+ const d = ccb(decoratorLevel);
751
+ if (d) {
752
+ d(target, propKey, descriptor);
753
+ }
754
+ });
755
+ }
727
756
  decorateClass(key, value, isArray) {
728
757
  return this.decorate(key, value, isArray, 'CLASS');
729
758
  }
@@ -735,6 +764,15 @@ const moostMate = new Mate(METADATA_WORKSPACE, {
735
764
  readType: true,
736
765
  readReturnType: true,
737
766
  collectPropKeys: true,
767
+ inherit(classMeta, targetMeta, level) {
768
+ if (level === 'CLASS') {
769
+ return !!(classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit);
770
+ }
771
+ if (level === 'PROP') {
772
+ return !!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit) || !!((classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit) && !targetMeta);
773
+ }
774
+ return !!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit);
775
+ },
738
776
  });
739
777
  function getMoostMate() {
740
778
  return moostMate;
@@ -806,7 +844,7 @@ resolvePipe.priority = TPipePriority.RESOLVE;
806
844
  ];
807
845
 
808
846
  const setHeaderInterceptor = (name, value, opts) => {
809
- const fn = (before, after, onError) => {
847
+ const fn = (before, after) => {
810
848
  const h = eventHttp.useSetHeader(name);
811
849
  const status = eventHttp.useStatus();
812
850
  after(() => {
@@ -822,7 +860,7 @@ function SetHeader(...args) {
822
860
  return Intercept(setHeaderInterceptor(...args));
823
861
  }
824
862
  const setCookieInterceptor = (name, value, attrs) => {
825
- const fn = (before, after, onError) => {
863
+ const fn = (before, after) => {
826
864
  const { setCookie, getCookie } = eventHttp.useSetCookies();
827
865
  after(() => {
828
866
  if (!getCookie(name)) {
@@ -837,6 +875,17 @@ function SetCookie(...args) {
837
875
  return Intercept(setCookieInterceptor(...args));
838
876
  }
839
877
 
878
+ function httpValidatePipe(opts) {
879
+ return moost.validatePipe(Object.assign({ errorCb: (message, details) => {
880
+ throw new eventHttp.HttpError(400, {
881
+ statusCode: 400,
882
+ message,
883
+ error: 'Validation Error',
884
+ details,
885
+ });
886
+ } }, opts[0]));
887
+ }
888
+
840
889
  exports.All = All;
841
890
  exports.Authorization = Authorization;
842
891
  exports.Body = Body;
@@ -864,3 +913,4 @@ exports.SetCookie = SetCookie;
864
913
  exports.SetHeader = SetHeader;
865
914
  exports.StatusHook = StatusHook;
866
915
  exports.Url = Url;
916
+ exports.httpValidatePipe = httpValidatePipe;
package/dist/index.d.ts CHANGED
@@ -6,10 +6,12 @@ import { TCookieAttributesInput } from '@wooksjs/event-http';
6
6
  import { TInterceptorFn } from '@moostjs/moost';
7
7
  import { TMoostAdapter } from 'moost';
8
8
  import { TMoostAdapterOptions } from 'moost';
9
+ import { TPipeFn } from 'moost';
9
10
  import { TProstoRouterPathBuilder } from '@prostojs/router';
10
11
  import { TProvideRegistry } from '@prostojs/infact';
11
12
  import { TWooksHttpOptions } from '@wooksjs/event-http';
12
13
  import { useSetCookies } from '@wooksjs/event-http';
14
+ import { validatePipe } from 'moost';
13
15
  import { WooksHttp } from '@wooksjs/event-http';
14
16
 
15
17
  export declare const All: (path?: string) => MethodDecorator;
@@ -76,6 +78,8 @@ export declare const HeaderHook: (name: string) => ParameterDecorator & Property
76
78
 
77
79
  export declare function HttpMethod(method: '*' | 'GET' | 'PUT' | 'POST' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS', path?: string): MethodDecorator;
78
80
 
81
+ export declare function httpValidatePipe(opts: Parameters<typeof validatePipe>): TPipeFn<TEmpty>;
82
+
79
83
  /**
80
84
  * Get Request IP Address
81
85
  * @decorator
@@ -183,6 +187,9 @@ export declare const StatusHook: () => ParameterDecorator & PropertyDecorator;
183
187
 
184
188
  export { TCookieAttributesInput }
185
189
 
190
+ declare interface TEmpty {
191
+ }
192
+
186
193
  export declare interface THttpHandlerMeta {
187
194
  method: string;
188
195
  path: string;
package/dist/index.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  import { WooksHttp, createHttpApp, useHttpContext, useRequest, useHeaders, HttpError, EHttpStatusCode, WooksURLSearchParams, useStatus, useSetHeader, useSetCookie, useAuthorization, useCookies, useSearchParams, useResponse, useSetCookies } from '@wooksjs/event-http';
2
- import { getMoostMate as getMoostMate$1, Resolve } from 'moost';
2
+ import { getMoostMate as getMoostMate$1, Resolve, validatePipe } from 'moost';
3
+ import { Server as Server$1 } from 'http';
4
+ import { Server } from 'https';
3
5
  import { attachHook } from '@wooksjs/event-core';
4
6
 
5
7
  /******************************************************************************
@@ -63,7 +65,7 @@ class MoostHttp {
63
65
  return this.httpApp.listen(...args);
64
66
  }
65
67
  getProvideRegistry() {
66
- return createProvideRegistry([WooksHttp, () => this.getHttpApp()], ['WooksHttp', () => this.getHttpApp()]);
68
+ return createProvideRegistry([WooksHttp, () => this.getHttpApp()], ['WooksHttp', () => this.getHttpApp()], [Server$1, () => this.getHttpApp().getServer()], [Server, () => this.getHttpApp().getServer()]);
67
69
  }
68
70
  bindHandler(opts) {
69
71
  let fn;
@@ -470,7 +472,7 @@ function Query(name) {
470
472
  return value === '' && p.has(name) || value;
471
473
  }
472
474
  const json = jsonSearchParams();
473
- return Object.keys(json).length ? json : undefined;
475
+ return Object.keys(json).length ? json : null;
474
476
  }, name || 'Query');
475
477
  }
476
478
  /**
@@ -479,7 +481,7 @@ function Query(name) {
479
481
  * @paramType string
480
482
  */
481
483
  function Url() {
482
- return Resolve(() => useRequest().url, 'url');
484
+ return Resolve(() => useRequest().url || '', 'url');
483
485
  }
484
486
  /**
485
487
  * Get Requested HTTP Method
@@ -619,11 +621,12 @@ class Mate {
619
621
  }
620
622
  set(args, key, value, isArray) {
621
623
  var _a;
624
+ let level = 'CLASS';
622
625
  const newArgs = args.level === 'CLASS' ? { target: args.target }
623
- : args.level === 'PROPERTY' ? { target: args.target, propKey: args.propKey }
626
+ : args.level === 'PROP' ? { target: args.target, propKey: args.propKey }
624
627
  : args;
625
628
  let meta = Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {};
626
- if (newArgs.propKey && this.options.readReturnType && !meta.returnType) {
629
+ if (newArgs.propKey && this.options.readReturnType && !meta.returnType && args.descriptor) {
627
630
  meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
628
631
  }
629
632
  if (newArgs.propKey && this.options.readType && !meta.type) {
@@ -636,12 +639,13 @@ class Mate {
636
639
  data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
637
640
  }
638
641
  if (typeof index === 'number') {
642
+ level = 'PARAM';
639
643
  data.params = data.params || [];
640
644
  data.params[index] = data.params[index] || {
641
645
  type: undefined,
642
646
  };
643
647
  if (cb) {
644
- data.params[index] = cb(data.params[index], args.propKey, typeof args.index === 'number' ? args.index : undefined);
648
+ data.params[index] = cb(data.params[index], level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
645
649
  }
646
650
  else {
647
651
  data = data.params[index];
@@ -658,6 +662,7 @@ class Mate {
658
662
  return meta;
659
663
  });
660
664
  }
665
+ level = typeof index === 'number' ? 'PARAM' : newArgs.propKey && newArgs.descriptor ? 'METHOD' : newArgs.propKey ? 'PROP' : 'CLASS';
661
666
  if (typeof key !== 'function') {
662
667
  if (isArray) {
663
668
  const newArray = (data[key] || []);
@@ -672,11 +677,11 @@ class Mate {
672
677
  }
673
678
  }
674
679
  else if (cb && typeof index !== 'number') {
675
- meta = cb(data, args.propKey, typeof args.index === 'number' ? args.index : undefined);
680
+ meta = cb(data, level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
676
681
  }
677
682
  Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
678
683
  }
679
- read(target, propKey, index) {
684
+ read(target, propKey) {
680
685
  const isConstr = isConstructor(target);
681
686
  const constructor = isConstr ? target : getConstructor(target);
682
687
  const proto = constructor.prototype;
@@ -687,17 +692,31 @@ class Mate {
687
692
  if (inheritFn) {
688
693
  if (typeof propKey === 'string') {
689
694
  const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
690
- shouldInherit = inheritFn(classMeta, propKey, ownMeta);
695
+ shouldInherit = inheritFn(classMeta, ownMeta, 'PROP', propKey);
691
696
  }
692
697
  else {
693
- shouldInherit = inheritFn(ownMeta);
698
+ shouldInherit = inheritFn(ownMeta, ownMeta, 'CLASS');
694
699
  }
695
700
  }
696
701
  if (shouldInherit) {
697
702
  const parent = Object.getPrototypeOf(constructor);
698
703
  if (typeof parent === 'function' && parent !== fnProto && parent !== constructor) {
699
- const inheritedMeta = this.read(parent, propKey);
700
- ownMeta = { ...inheritedMeta, ...ownMeta, params: ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params };
704
+ const inheritedMeta = this.read(parent, propKey) || {};
705
+ const ownParams = ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params;
706
+ ownMeta = { ...inheritedMeta, ...ownMeta };
707
+ if (typeof propKey === 'string' && ownParams && (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params)) {
708
+ for (let i = 0; i < ownParams.length; i++) {
709
+ if (typeof (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i]) !== 'undefined') {
710
+ const ownParam = ownParams[i];
711
+ if (ownMeta.params && inheritFn && inheritFn(ownMeta, ownParam, 'PARAM', typeof propKey === 'string' ? propKey : undefined)) {
712
+ ownMeta.params[i] = {
713
+ ...inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i],
714
+ ...ownParams[i],
715
+ };
716
+ }
717
+ }
718
+ }
719
+ }
701
720
  }
702
721
  }
703
722
  }
@@ -722,6 +741,16 @@ class Mate {
722
741
  this.set(args, key, value, isArray);
723
742
  });
724
743
  }
744
+ decorateConditional(ccb) {
745
+ return ((target, propKey, descriptor) => {
746
+ const hasIndex = typeof descriptor === 'number';
747
+ const decoratorLevel = hasIndex ? 'PARAM' : propKey && descriptor ? 'METHOD' : propKey ? 'PROP' : 'CLASS';
748
+ const d = ccb(decoratorLevel);
749
+ if (d) {
750
+ d(target, propKey, descriptor);
751
+ }
752
+ });
753
+ }
725
754
  decorateClass(key, value, isArray) {
726
755
  return this.decorate(key, value, isArray, 'CLASS');
727
756
  }
@@ -733,6 +762,15 @@ const moostMate = new Mate(METADATA_WORKSPACE, {
733
762
  readType: true,
734
763
  readReturnType: true,
735
764
  collectPropKeys: true,
765
+ inherit(classMeta, targetMeta, level) {
766
+ if (level === 'CLASS') {
767
+ return !!(classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit);
768
+ }
769
+ if (level === 'PROP') {
770
+ return !!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit) || !!((classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit) && !targetMeta);
771
+ }
772
+ return !!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit);
773
+ },
736
774
  });
737
775
  function getMoostMate() {
738
776
  return moostMate;
@@ -804,7 +842,7 @@ resolvePipe.priority = TPipePriority.RESOLVE;
804
842
  ];
805
843
 
806
844
  const setHeaderInterceptor = (name, value, opts) => {
807
- const fn = (before, after, onError) => {
845
+ const fn = (before, after) => {
808
846
  const h = useSetHeader(name);
809
847
  const status = useStatus();
810
848
  after(() => {
@@ -820,7 +858,7 @@ function SetHeader(...args) {
820
858
  return Intercept(setHeaderInterceptor(...args));
821
859
  }
822
860
  const setCookieInterceptor = (name, value, attrs) => {
823
- const fn = (before, after, onError) => {
861
+ const fn = (before, after) => {
824
862
  const { setCookie, getCookie } = useSetCookies();
825
863
  after(() => {
826
864
  if (!getCookie(name)) {
@@ -835,4 +873,15 @@ function SetCookie(...args) {
835
873
  return Intercept(setCookieInterceptor(...args));
836
874
  }
837
875
 
838
- export { All, Authorization, Body, Cookie, CookieAttrsHook, CookieHook, Delete, Get, Header, HeaderHook, HttpMethod, Ip, IpList, Method, MoostHttp, Patch, Post, Put, Query, RawBody, Req, ReqId, Res, SetCookie, SetHeader, StatusHook, Url };
876
+ function httpValidatePipe(opts) {
877
+ return validatePipe(Object.assign({ errorCb: (message, details) => {
878
+ throw new HttpError(400, {
879
+ statusCode: 400,
880
+ message,
881
+ error: 'Validation Error',
882
+ details,
883
+ });
884
+ } }, opts[0]));
885
+ }
886
+
887
+ export { All, Authorization, Body, Cookie, CookieAttrsHook, CookieHook, Delete, Get, Header, HeaderHook, HttpMethod, Ip, IpList, Method, MoostHttp, Patch, Post, Put, Query, RawBody, Req, ReqId, Res, SetCookie, SetHeader, StatusHook, Url, httpValidatePipe };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/event-http",
3
- "version": "0.2.13",
3
+ "version": "0.2.15",
4
4
  "description": "@moostjs/event-http",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -28,11 +28,11 @@
28
28
  },
29
29
  "homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-http#readme",
30
30
  "peerDependencies": {
31
- "moost": "0.2.13",
32
- "wooks": "^0.2.7",
33
- "@wooksjs/event-core": "^0.2.7"
31
+ "moost": "0.2.15",
32
+ "wooks": "^0.2.8",
33
+ "@wooksjs/event-core": "^0.2.8"
34
34
  },
35
35
  "dependencies": {
36
- "@wooksjs/event-http": "^0.2.7"
36
+ "@wooksjs/event-http": "^0.2.8"
37
37
  }
38
38
  }