@moostjs/event-http 0.2.5 → 0.2.6
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 +279 -6
- package/dist/index.d.ts +13 -0
- package/dist/index.mjs +282 -11
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var eventHttp = require('@wooksjs/event-http');
|
|
4
4
|
var moost = require('moost');
|
|
5
|
+
require('crypto');
|
|
5
6
|
|
|
6
7
|
/******************************************************************************
|
|
7
8
|
Copyright (c) Microsoft Corporation.
|
|
@@ -176,15 +177,15 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
176
177
|
});
|
|
177
178
|
}
|
|
178
179
|
|
|
179
|
-
const banner = () => `[${"@wooksjs/http-body"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
180
|
+
const banner$1 = () => `[${"@wooksjs/http-body"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
180
181
|
|
|
181
182
|
/* istanbul ignore file */
|
|
182
|
-
function logError(error) {
|
|
183
|
-
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
183
|
+
function logError$1(error) {
|
|
184
|
+
console.error('[91m' + '[1m' + banner$1() + error + '[0m');
|
|
184
185
|
}
|
|
185
186
|
|
|
186
|
-
function panic(error) {
|
|
187
|
-
logError(error);
|
|
187
|
+
function panic$1(error) {
|
|
188
|
+
logError$1(error);
|
|
188
189
|
return new Error(error);
|
|
189
190
|
}
|
|
190
191
|
|
|
@@ -199,7 +200,7 @@ function uncompressBody(encodings, body) {
|
|
|
199
200
|
let newBody = body;
|
|
200
201
|
for (const e of encodings.reverse()) {
|
|
201
202
|
if (!compressors[e]) {
|
|
202
|
-
throw panic(`Usupported compression type "${e}".`);
|
|
203
|
+
throw panic$1(`Usupported compression type "${e}".`);
|
|
203
204
|
}
|
|
204
205
|
newBody = yield compressors[e].uncompress(body);
|
|
205
206
|
}
|
|
@@ -487,6 +488,276 @@ function RawBody() {
|
|
|
487
488
|
return moost.Resolve(() => useBody().rawBody(), 'body');
|
|
488
489
|
}
|
|
489
490
|
|
|
491
|
+
function getConstructor(instance) {
|
|
492
|
+
return isConstructor(instance) ?
|
|
493
|
+
instance : instance.constructor ?
|
|
494
|
+
instance.constructor : Object.getPrototypeOf(instance).constructor;
|
|
495
|
+
}
|
|
496
|
+
function isConstructor(v) {
|
|
497
|
+
return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const banner = () => `[prostojs/mate][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
501
|
+
|
|
502
|
+
function warn(text) {
|
|
503
|
+
console.log('[33m' + banner() + text + '[0m');
|
|
504
|
+
}
|
|
505
|
+
function logError(error) {
|
|
506
|
+
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const classMetadata = {};
|
|
510
|
+
const paramMetadata = {};
|
|
511
|
+
const root = typeof global === 'object' ? global : typeof self === 'object' ? self : {};
|
|
512
|
+
function getMetaObject(target, prop) {
|
|
513
|
+
const isParam = typeof prop !== 'undefined';
|
|
514
|
+
const metadata = isParam ? paramMetadata : classMetadata;
|
|
515
|
+
const targetKey = Symbol.for(getConstructor(target));
|
|
516
|
+
let meta = metadata[targetKey] = metadata[targetKey] || {};
|
|
517
|
+
if (isParam)
|
|
518
|
+
meta = (meta[prop] = meta[prop] || {});
|
|
519
|
+
return meta;
|
|
520
|
+
}
|
|
521
|
+
const _reflect = {
|
|
522
|
+
getOwnMetadata(key, target, prop) {
|
|
523
|
+
return getMetaObject(target, prop)[key];
|
|
524
|
+
},
|
|
525
|
+
defineMetadata(key, data, target, prop) {
|
|
526
|
+
const meta = getMetaObject(target, prop);
|
|
527
|
+
meta[key] = data;
|
|
528
|
+
},
|
|
529
|
+
metadata(key, data) {
|
|
530
|
+
return ((target, propKey) => {
|
|
531
|
+
Reflect$1.defineMetadata(key, data, target, propKey);
|
|
532
|
+
});
|
|
533
|
+
},
|
|
534
|
+
};
|
|
535
|
+
if (!root.Reflect) {
|
|
536
|
+
root.Reflect = _reflect;
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
const funcs = [
|
|
540
|
+
'getOwnMetadata',
|
|
541
|
+
'defineMetadata',
|
|
542
|
+
'metadata',
|
|
543
|
+
];
|
|
544
|
+
const target = root.Reflect;
|
|
545
|
+
let isOriginalReflectMetadata = true;
|
|
546
|
+
for (const func of funcs) {
|
|
547
|
+
if (typeof target[func] !== 'function') {
|
|
548
|
+
Object.defineProperty(target, func, { configurable: true, writable: true, value: _reflect[func] });
|
|
549
|
+
isOriginalReflectMetadata = false;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (!isOriginalReflectMetadata) {
|
|
553
|
+
warn('A limited \'reflect-metadata\' implementation is used. In case of any issues include original \'reflect-metadata\' package and require it before any @prostojs/mate import.');
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
const Reflect$1 = _reflect;
|
|
557
|
+
|
|
558
|
+
function panic(error) {
|
|
559
|
+
logError(error);
|
|
560
|
+
return new Error(error);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const Reflect = (global === null || global === void 0 ? void 0 : global.Reflect) || (self === null || self === void 0 ? void 0 : self.Reflect) || Reflect$1;
|
|
564
|
+
class Mate {
|
|
565
|
+
constructor(workspace, options = {}) {
|
|
566
|
+
this.workspace = workspace;
|
|
567
|
+
this.options = options;
|
|
568
|
+
}
|
|
569
|
+
set(args, key, value, isArray) {
|
|
570
|
+
var _a;
|
|
571
|
+
const newArgs = args.level === 'CLASS' ? { target: args.target }
|
|
572
|
+
: args.level === 'PROPERTY' ? { target: args.target, propKey: args.propKey }
|
|
573
|
+
: args;
|
|
574
|
+
let meta = Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {};
|
|
575
|
+
if (newArgs.propKey && this.options.readReturnType && !meta.returnType) {
|
|
576
|
+
meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
|
|
577
|
+
}
|
|
578
|
+
if (newArgs.propKey && this.options.readType && !meta.type) {
|
|
579
|
+
meta.type = Reflect.getOwnMetadata('design:type', newArgs.target, newArgs.propKey);
|
|
580
|
+
}
|
|
581
|
+
const { index } = newArgs;
|
|
582
|
+
const cb = typeof key === 'function' ? key : undefined;
|
|
583
|
+
let data = meta;
|
|
584
|
+
if (!data.params) {
|
|
585
|
+
data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
|
|
586
|
+
}
|
|
587
|
+
if (typeof index === 'number') {
|
|
588
|
+
data.params = data.params || [];
|
|
589
|
+
data.params[index] = data.params[index] || {
|
|
590
|
+
type: undefined,
|
|
591
|
+
};
|
|
592
|
+
if (cb) {
|
|
593
|
+
data.params[index] = cb(data.params[index], args.propKey, typeof args.index === 'number' ? args.index : undefined);
|
|
594
|
+
}
|
|
595
|
+
else {
|
|
596
|
+
data = data.params[index];
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
if (typeof key !== 'function') {
|
|
600
|
+
if (isArray) {
|
|
601
|
+
const newArray = (data[key] || []);
|
|
602
|
+
if (!Array.isArray(newArray)) {
|
|
603
|
+
panic('Mate.add (isArray=true) called for non-array metadata');
|
|
604
|
+
}
|
|
605
|
+
newArray.unshift(value);
|
|
606
|
+
data[key] = newArray;
|
|
607
|
+
}
|
|
608
|
+
else {
|
|
609
|
+
data[key] = value;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
else if (cb && typeof index !== 'number') {
|
|
613
|
+
meta = cb(data, args.propKey, typeof args.index === 'number' ? args.index : undefined);
|
|
614
|
+
}
|
|
615
|
+
Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
|
|
616
|
+
}
|
|
617
|
+
read(target, propKey, index) {
|
|
618
|
+
const isConstr = isConstructor(target);
|
|
619
|
+
const constructor = isConstr ? target : getConstructor(target);
|
|
620
|
+
const proto = constructor.prototype;
|
|
621
|
+
let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
|
|
622
|
+
if (this.options.inherit) {
|
|
623
|
+
const inheritFn = typeof this.options.inherit === 'function' ? this.options.inherit : undefined;
|
|
624
|
+
let shouldInherit = this.options.inherit;
|
|
625
|
+
if (inheritFn) {
|
|
626
|
+
if (typeof propKey === 'string') {
|
|
627
|
+
const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
|
|
628
|
+
shouldInherit = inheritFn(classMeta, propKey, ownMeta);
|
|
629
|
+
}
|
|
630
|
+
else {
|
|
631
|
+
shouldInherit = inheritFn(ownMeta);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
if (shouldInherit) {
|
|
635
|
+
const parent = Object.getPrototypeOf(constructor);
|
|
636
|
+
if (typeof parent === 'function' && parent !== fnProto && parent !== constructor) {
|
|
637
|
+
const inheritedMeta = this.read(parent, propKey);
|
|
638
|
+
ownMeta = { ...inheritedMeta, ...ownMeta, params: ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params };
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return ownMeta;
|
|
643
|
+
}
|
|
644
|
+
apply(...decorators) {
|
|
645
|
+
return ((target, propKey, descriptor) => {
|
|
646
|
+
for (const d of decorators) {
|
|
647
|
+
d(target, propKey, descriptor);
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
decorate(key, value, isArray, level) {
|
|
652
|
+
return ((target, propKey, descriptor) => {
|
|
653
|
+
const args = {
|
|
654
|
+
target,
|
|
655
|
+
propKey,
|
|
656
|
+
descriptor: typeof descriptor === 'number' ? undefined : descriptor,
|
|
657
|
+
index: typeof descriptor === 'number' ? descriptor : undefined,
|
|
658
|
+
level,
|
|
659
|
+
};
|
|
660
|
+
this.set(args, key, value, isArray);
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
decorateClass(key, value, isArray) {
|
|
664
|
+
return this.decorate(key, value, isArray, 'CLASS');
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
const fnProto = Object.getPrototypeOf(Function);
|
|
668
|
+
|
|
669
|
+
const METADATA_WORKSPACE = 'moost';
|
|
670
|
+
const moostMate = new Mate(METADATA_WORKSPACE, {
|
|
671
|
+
readType: true,
|
|
672
|
+
readReturnType: true,
|
|
673
|
+
});
|
|
674
|
+
function getMoostMate() {
|
|
675
|
+
return moostMate;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
var TPipePriority;
|
|
679
|
+
(function (TPipePriority) {
|
|
680
|
+
TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
|
|
681
|
+
TPipePriority[TPipePriority["RESOLVE"] = 1] = "RESOLVE";
|
|
682
|
+
TPipePriority[TPipePriority["AFTER_RESOLVE"] = 2] = "AFTER_RESOLVE";
|
|
683
|
+
TPipePriority[TPipePriority["BEFORE_TRANSFORM"] = 3] = "BEFORE_TRANSFORM";
|
|
684
|
+
TPipePriority[TPipePriority["TRANSFORM"] = 4] = "TRANSFORM";
|
|
685
|
+
TPipePriority[TPipePriority["AFTER_TRANSFORM"] = 5] = "AFTER_TRANSFORM";
|
|
686
|
+
TPipePriority[TPipePriority["BEFORE_VALIDATE"] = 6] = "BEFORE_VALIDATE";
|
|
687
|
+
TPipePriority[TPipePriority["VALIDATE"] = 7] = "VALIDATE";
|
|
688
|
+
TPipePriority[TPipePriority["AFTER_VALIDATE"] = 8] = "AFTER_VALIDATE";
|
|
689
|
+
})(TPipePriority || (TPipePriority = {}));
|
|
690
|
+
|
|
691
|
+
const resolvePipe = (_value, meta) => {
|
|
692
|
+
if (meta === null || meta === void 0 ? void 0 : meta.resolver) {
|
|
693
|
+
return meta.resolver();
|
|
694
|
+
}
|
|
695
|
+
return undefined;
|
|
696
|
+
};
|
|
697
|
+
resolvePipe.priority = TPipePriority.RESOLVE;
|
|
698
|
+
|
|
699
|
+
[
|
|
700
|
+
{
|
|
701
|
+
handler: resolvePipe,
|
|
702
|
+
priority: TPipePriority.RESOLVE,
|
|
703
|
+
},
|
|
704
|
+
];
|
|
705
|
+
|
|
706
|
+
getMoostMate().decorate((meta) => {
|
|
707
|
+
if (!meta.injectable)
|
|
708
|
+
meta.injectable = true;
|
|
709
|
+
return meta;
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
var TInterceptorPriority;
|
|
713
|
+
(function (TInterceptorPriority) {
|
|
714
|
+
TInterceptorPriority[TInterceptorPriority["BEFORE_ALL"] = 0] = "BEFORE_ALL";
|
|
715
|
+
TInterceptorPriority[TInterceptorPriority["BEFORE_GUARD"] = 1] = "BEFORE_GUARD";
|
|
716
|
+
TInterceptorPriority[TInterceptorPriority["GUARD"] = 2] = "GUARD";
|
|
717
|
+
TInterceptorPriority[TInterceptorPriority["AFTER_GUARD"] = 3] = "AFTER_GUARD";
|
|
718
|
+
TInterceptorPriority[TInterceptorPriority["INTERCEPTOR"] = 4] = "INTERCEPTOR";
|
|
719
|
+
TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
|
|
720
|
+
TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
|
|
721
|
+
})(TInterceptorPriority || (TInterceptorPriority = {}));
|
|
722
|
+
function Intercept(handler, priority) {
|
|
723
|
+
return getMoostMate().decorate('interceptors', {
|
|
724
|
+
handler,
|
|
725
|
+
priority: priority || handler.priority || TInterceptorPriority.INTERCEPTOR,
|
|
726
|
+
}, true);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
const setHeaderInterceptor = (name, value, opts) => {
|
|
730
|
+
const fn = (before, after, onError) => {
|
|
731
|
+
const h = eventHttp.useSetHeader(name);
|
|
732
|
+
const status = eventHttp.useStatus();
|
|
733
|
+
after(() => {
|
|
734
|
+
if ((!h.value || (opts === null || opts === void 0 ? void 0 : opts.force)) && (!(opts === null || opts === void 0 ? void 0 : opts.status) || opts.status === status.value)) {
|
|
735
|
+
h.value = value;
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
};
|
|
739
|
+
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
740
|
+
return fn;
|
|
741
|
+
};
|
|
742
|
+
function SetHeader(...args) {
|
|
743
|
+
return Intercept(setHeaderInterceptor(...args));
|
|
744
|
+
}
|
|
745
|
+
const setCookieInterceptor = (name, value, attrs) => {
|
|
746
|
+
const fn = (before, after, onError) => {
|
|
747
|
+
const { setCookie, getCookie } = eventHttp.useSetCookies();
|
|
748
|
+
after(() => {
|
|
749
|
+
if (!getCookie(name)) {
|
|
750
|
+
setCookie(name, value, attrs);
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
};
|
|
754
|
+
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
755
|
+
return fn;
|
|
756
|
+
};
|
|
757
|
+
function SetCookie(...args) {
|
|
758
|
+
return Intercept(setCookieInterceptor(...args));
|
|
759
|
+
}
|
|
760
|
+
|
|
490
761
|
exports.All = All;
|
|
491
762
|
exports.Authorization = Authorization;
|
|
492
763
|
exports.Body = Body;
|
|
@@ -509,5 +780,7 @@ exports.RawBody = RawBody;
|
|
|
509
780
|
exports.Req = Req;
|
|
510
781
|
exports.ReqId = ReqId;
|
|
511
782
|
exports.Res = Res;
|
|
783
|
+
exports.SetCookie = SetCookie;
|
|
784
|
+
exports.SetHeader = SetHeader;
|
|
512
785
|
exports.StatusHook = StatusHook;
|
|
513
786
|
exports.Url = Url;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
import { IncomingMessage } from 'http';
|
|
4
4
|
import { ServerResponse } from 'http';
|
|
5
|
+
import { TInterceptorFn } from '@moostjs/moost';
|
|
5
6
|
import { TMoostAdapter } from 'moost';
|
|
6
7
|
import { TMoostAdapterOptions } from 'moost';
|
|
7
8
|
import { TProstoRouterPathBuilder } from '@prostojs/router';
|
|
8
9
|
import { TProvideRegistry } from '@prostojs/infact';
|
|
9
10
|
import { TWooksHttpOptions } from '@wooksjs/event-http';
|
|
11
|
+
import { useSetCookies } from '@wooksjs/event-http';
|
|
10
12
|
import { WooksHttp } from '@wooksjs/event-http';
|
|
11
13
|
|
|
12
14
|
export declare const All: (path?: string) => MethodDecorator;
|
|
@@ -152,6 +154,17 @@ export declare function Res(options?: {
|
|
|
152
154
|
passthrough: boolean;
|
|
153
155
|
}): ParameterDecorator;
|
|
154
156
|
|
|
157
|
+
export declare function SetCookie(...args: Parameters<typeof setCookieInterceptor>): ClassDecorator & MethodDecorator;
|
|
158
|
+
|
|
159
|
+
declare const setCookieInterceptor: (...args: Parameters<ReturnType<typeof useSetCookies>['setCookie']>) => TInterceptorFn;
|
|
160
|
+
|
|
161
|
+
export declare function SetHeader(...args: Parameters<typeof setHeaderInterceptor>): ClassDecorator & MethodDecorator;
|
|
162
|
+
|
|
163
|
+
declare const setHeaderInterceptor: (name: string, value: string, opts?: {
|
|
164
|
+
force?: boolean;
|
|
165
|
+
status?: number;
|
|
166
|
+
}) => TInterceptorFn;
|
|
167
|
+
|
|
155
168
|
/**
|
|
156
169
|
* Hook to the Response Status
|
|
157
170
|
* @decorator
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { WooksHttp, createHttpApp, useHttpContext, useRequest, useHeaders, HttpError, EHttpStatusCode, WooksURLSearchParams, useStatus, useSetHeader, useSetCookie, useAuthorization, useCookies, useSearchParams, useResponse } from '@wooksjs/event-http';
|
|
2
|
-
import { getMoostMate, Resolve } from 'moost';
|
|
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';
|
|
3
|
+
import 'crypto';
|
|
3
4
|
|
|
4
5
|
/******************************************************************************
|
|
5
6
|
Copyright (c) Microsoft Corporation.
|
|
@@ -119,7 +120,7 @@ class MoostHttp {
|
|
|
119
120
|
});
|
|
120
121
|
}
|
|
121
122
|
const pathBuilder = this.httpApp.on(handler.method, targetPath, fn);
|
|
122
|
-
const methodMeta = getMoostMate().read(opts.fakeInstance, opts.method) || {};
|
|
123
|
+
const methodMeta = getMoostMate$1().read(opts.fakeInstance, opts.method) || {};
|
|
123
124
|
const id = (methodMeta.id || opts.method);
|
|
124
125
|
if (id) {
|
|
125
126
|
const methods = this.pathBuilders[id] = this.pathBuilders[id] || {};
|
|
@@ -140,7 +141,7 @@ class MoostHttp {
|
|
|
140
141
|
}
|
|
141
142
|
|
|
142
143
|
function HttpMethod(method, path) {
|
|
143
|
-
return getMoostMate().decorate('handlers', { method, path, type: 'HTTP' }, true);
|
|
144
|
+
return getMoostMate$1().decorate('handlers', { method, path, type: 'HTTP' }, true);
|
|
144
145
|
}
|
|
145
146
|
const All = (path) => HttpMethod('*', path);
|
|
146
147
|
const Get = (path) => HttpMethod('GET', path);
|
|
@@ -174,15 +175,15 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
174
175
|
});
|
|
175
176
|
}
|
|
176
177
|
|
|
177
|
-
const banner = () => `[${"@wooksjs/http-body"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
178
|
+
const banner$1 = () => `[${"@wooksjs/http-body"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
178
179
|
|
|
179
180
|
/* istanbul ignore file */
|
|
180
|
-
function logError(error) {
|
|
181
|
-
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
181
|
+
function logError$1(error) {
|
|
182
|
+
console.error('[91m' + '[1m' + banner$1() + error + '[0m');
|
|
182
183
|
}
|
|
183
184
|
|
|
184
|
-
function panic(error) {
|
|
185
|
-
logError(error);
|
|
185
|
+
function panic$1(error) {
|
|
186
|
+
logError$1(error);
|
|
186
187
|
return new Error(error);
|
|
187
188
|
}
|
|
188
189
|
|
|
@@ -197,7 +198,7 @@ function uncompressBody(encodings, body) {
|
|
|
197
198
|
let newBody = body;
|
|
198
199
|
for (const e of encodings.reverse()) {
|
|
199
200
|
if (!compressors[e]) {
|
|
200
|
-
throw panic(`Usupported compression type "${e}".`);
|
|
201
|
+
throw panic$1(`Usupported compression type "${e}".`);
|
|
201
202
|
}
|
|
202
203
|
newBody = yield compressors[e].uncompress(body);
|
|
203
204
|
}
|
|
@@ -485,4 +486,274 @@ function RawBody() {
|
|
|
485
486
|
return Resolve(() => useBody().rawBody(), 'body');
|
|
486
487
|
}
|
|
487
488
|
|
|
488
|
-
|
|
489
|
+
function getConstructor(instance) {
|
|
490
|
+
return isConstructor(instance) ?
|
|
491
|
+
instance : instance.constructor ?
|
|
492
|
+
instance.constructor : Object.getPrototypeOf(instance).constructor;
|
|
493
|
+
}
|
|
494
|
+
function isConstructor(v) {
|
|
495
|
+
return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const banner = () => `[prostojs/mate][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
499
|
+
|
|
500
|
+
function warn(text) {
|
|
501
|
+
console.log('[33m' + banner() + text + '[0m');
|
|
502
|
+
}
|
|
503
|
+
function logError(error) {
|
|
504
|
+
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const classMetadata = {};
|
|
508
|
+
const paramMetadata = {};
|
|
509
|
+
const root = typeof global === 'object' ? global : typeof self === 'object' ? self : {};
|
|
510
|
+
function getMetaObject(target, prop) {
|
|
511
|
+
const isParam = typeof prop !== 'undefined';
|
|
512
|
+
const metadata = isParam ? paramMetadata : classMetadata;
|
|
513
|
+
const targetKey = Symbol.for(getConstructor(target));
|
|
514
|
+
let meta = metadata[targetKey] = metadata[targetKey] || {};
|
|
515
|
+
if (isParam)
|
|
516
|
+
meta = (meta[prop] = meta[prop] || {});
|
|
517
|
+
return meta;
|
|
518
|
+
}
|
|
519
|
+
const _reflect = {
|
|
520
|
+
getOwnMetadata(key, target, prop) {
|
|
521
|
+
return getMetaObject(target, prop)[key];
|
|
522
|
+
},
|
|
523
|
+
defineMetadata(key, data, target, prop) {
|
|
524
|
+
const meta = getMetaObject(target, prop);
|
|
525
|
+
meta[key] = data;
|
|
526
|
+
},
|
|
527
|
+
metadata(key, data) {
|
|
528
|
+
return ((target, propKey) => {
|
|
529
|
+
Reflect$1.defineMetadata(key, data, target, propKey);
|
|
530
|
+
});
|
|
531
|
+
},
|
|
532
|
+
};
|
|
533
|
+
if (!root.Reflect) {
|
|
534
|
+
root.Reflect = _reflect;
|
|
535
|
+
}
|
|
536
|
+
else {
|
|
537
|
+
const funcs = [
|
|
538
|
+
'getOwnMetadata',
|
|
539
|
+
'defineMetadata',
|
|
540
|
+
'metadata',
|
|
541
|
+
];
|
|
542
|
+
const target = root.Reflect;
|
|
543
|
+
let isOriginalReflectMetadata = true;
|
|
544
|
+
for (const func of funcs) {
|
|
545
|
+
if (typeof target[func] !== 'function') {
|
|
546
|
+
Object.defineProperty(target, func, { configurable: true, writable: true, value: _reflect[func] });
|
|
547
|
+
isOriginalReflectMetadata = false;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
if (!isOriginalReflectMetadata) {
|
|
551
|
+
warn('A limited \'reflect-metadata\' implementation is used. In case of any issues include original \'reflect-metadata\' package and require it before any @prostojs/mate import.');
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
const Reflect$1 = _reflect;
|
|
555
|
+
|
|
556
|
+
function panic(error) {
|
|
557
|
+
logError(error);
|
|
558
|
+
return new Error(error);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
const Reflect = (global === null || global === void 0 ? void 0 : global.Reflect) || (self === null || self === void 0 ? void 0 : self.Reflect) || Reflect$1;
|
|
562
|
+
class Mate {
|
|
563
|
+
constructor(workspace, options = {}) {
|
|
564
|
+
this.workspace = workspace;
|
|
565
|
+
this.options = options;
|
|
566
|
+
}
|
|
567
|
+
set(args, key, value, isArray) {
|
|
568
|
+
var _a;
|
|
569
|
+
const newArgs = args.level === 'CLASS' ? { target: args.target }
|
|
570
|
+
: args.level === 'PROPERTY' ? { target: args.target, propKey: args.propKey }
|
|
571
|
+
: args;
|
|
572
|
+
let meta = Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {};
|
|
573
|
+
if (newArgs.propKey && this.options.readReturnType && !meta.returnType) {
|
|
574
|
+
meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
|
|
575
|
+
}
|
|
576
|
+
if (newArgs.propKey && this.options.readType && !meta.type) {
|
|
577
|
+
meta.type = Reflect.getOwnMetadata('design:type', newArgs.target, newArgs.propKey);
|
|
578
|
+
}
|
|
579
|
+
const { index } = newArgs;
|
|
580
|
+
const cb = typeof key === 'function' ? key : undefined;
|
|
581
|
+
let data = meta;
|
|
582
|
+
if (!data.params) {
|
|
583
|
+
data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
|
|
584
|
+
}
|
|
585
|
+
if (typeof index === 'number') {
|
|
586
|
+
data.params = data.params || [];
|
|
587
|
+
data.params[index] = data.params[index] || {
|
|
588
|
+
type: undefined,
|
|
589
|
+
};
|
|
590
|
+
if (cb) {
|
|
591
|
+
data.params[index] = cb(data.params[index], args.propKey, typeof args.index === 'number' ? args.index : undefined);
|
|
592
|
+
}
|
|
593
|
+
else {
|
|
594
|
+
data = data.params[index];
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
if (typeof key !== 'function') {
|
|
598
|
+
if (isArray) {
|
|
599
|
+
const newArray = (data[key] || []);
|
|
600
|
+
if (!Array.isArray(newArray)) {
|
|
601
|
+
panic('Mate.add (isArray=true) called for non-array metadata');
|
|
602
|
+
}
|
|
603
|
+
newArray.unshift(value);
|
|
604
|
+
data[key] = newArray;
|
|
605
|
+
}
|
|
606
|
+
else {
|
|
607
|
+
data[key] = value;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
else if (cb && typeof index !== 'number') {
|
|
611
|
+
meta = cb(data, args.propKey, typeof args.index === 'number' ? args.index : undefined);
|
|
612
|
+
}
|
|
613
|
+
Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
|
|
614
|
+
}
|
|
615
|
+
read(target, propKey, index) {
|
|
616
|
+
const isConstr = isConstructor(target);
|
|
617
|
+
const constructor = isConstr ? target : getConstructor(target);
|
|
618
|
+
const proto = constructor.prototype;
|
|
619
|
+
let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
|
|
620
|
+
if (this.options.inherit) {
|
|
621
|
+
const inheritFn = typeof this.options.inherit === 'function' ? this.options.inherit : undefined;
|
|
622
|
+
let shouldInherit = this.options.inherit;
|
|
623
|
+
if (inheritFn) {
|
|
624
|
+
if (typeof propKey === 'string') {
|
|
625
|
+
const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
|
|
626
|
+
shouldInherit = inheritFn(classMeta, propKey, ownMeta);
|
|
627
|
+
}
|
|
628
|
+
else {
|
|
629
|
+
shouldInherit = inheritFn(ownMeta);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
if (shouldInherit) {
|
|
633
|
+
const parent = Object.getPrototypeOf(constructor);
|
|
634
|
+
if (typeof parent === 'function' && parent !== fnProto && parent !== constructor) {
|
|
635
|
+
const inheritedMeta = this.read(parent, propKey);
|
|
636
|
+
ownMeta = { ...inheritedMeta, ...ownMeta, params: ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params };
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
return ownMeta;
|
|
641
|
+
}
|
|
642
|
+
apply(...decorators) {
|
|
643
|
+
return ((target, propKey, descriptor) => {
|
|
644
|
+
for (const d of decorators) {
|
|
645
|
+
d(target, propKey, descriptor);
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
decorate(key, value, isArray, level) {
|
|
650
|
+
return ((target, propKey, descriptor) => {
|
|
651
|
+
const args = {
|
|
652
|
+
target,
|
|
653
|
+
propKey,
|
|
654
|
+
descriptor: typeof descriptor === 'number' ? undefined : descriptor,
|
|
655
|
+
index: typeof descriptor === 'number' ? descriptor : undefined,
|
|
656
|
+
level,
|
|
657
|
+
};
|
|
658
|
+
this.set(args, key, value, isArray);
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
decorateClass(key, value, isArray) {
|
|
662
|
+
return this.decorate(key, value, isArray, 'CLASS');
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
const fnProto = Object.getPrototypeOf(Function);
|
|
666
|
+
|
|
667
|
+
const METADATA_WORKSPACE = 'moost';
|
|
668
|
+
const moostMate = new Mate(METADATA_WORKSPACE, {
|
|
669
|
+
readType: true,
|
|
670
|
+
readReturnType: true,
|
|
671
|
+
});
|
|
672
|
+
function getMoostMate() {
|
|
673
|
+
return moostMate;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
var TPipePriority;
|
|
677
|
+
(function (TPipePriority) {
|
|
678
|
+
TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
|
|
679
|
+
TPipePriority[TPipePriority["RESOLVE"] = 1] = "RESOLVE";
|
|
680
|
+
TPipePriority[TPipePriority["AFTER_RESOLVE"] = 2] = "AFTER_RESOLVE";
|
|
681
|
+
TPipePriority[TPipePriority["BEFORE_TRANSFORM"] = 3] = "BEFORE_TRANSFORM";
|
|
682
|
+
TPipePriority[TPipePriority["TRANSFORM"] = 4] = "TRANSFORM";
|
|
683
|
+
TPipePriority[TPipePriority["AFTER_TRANSFORM"] = 5] = "AFTER_TRANSFORM";
|
|
684
|
+
TPipePriority[TPipePriority["BEFORE_VALIDATE"] = 6] = "BEFORE_VALIDATE";
|
|
685
|
+
TPipePriority[TPipePriority["VALIDATE"] = 7] = "VALIDATE";
|
|
686
|
+
TPipePriority[TPipePriority["AFTER_VALIDATE"] = 8] = "AFTER_VALIDATE";
|
|
687
|
+
})(TPipePriority || (TPipePriority = {}));
|
|
688
|
+
|
|
689
|
+
const resolvePipe = (_value, meta) => {
|
|
690
|
+
if (meta === null || meta === void 0 ? void 0 : meta.resolver) {
|
|
691
|
+
return meta.resolver();
|
|
692
|
+
}
|
|
693
|
+
return undefined;
|
|
694
|
+
};
|
|
695
|
+
resolvePipe.priority = TPipePriority.RESOLVE;
|
|
696
|
+
|
|
697
|
+
[
|
|
698
|
+
{
|
|
699
|
+
handler: resolvePipe,
|
|
700
|
+
priority: TPipePriority.RESOLVE,
|
|
701
|
+
},
|
|
702
|
+
];
|
|
703
|
+
|
|
704
|
+
getMoostMate().decorate((meta) => {
|
|
705
|
+
if (!meta.injectable)
|
|
706
|
+
meta.injectable = true;
|
|
707
|
+
return meta;
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
var TInterceptorPriority;
|
|
711
|
+
(function (TInterceptorPriority) {
|
|
712
|
+
TInterceptorPriority[TInterceptorPriority["BEFORE_ALL"] = 0] = "BEFORE_ALL";
|
|
713
|
+
TInterceptorPriority[TInterceptorPriority["BEFORE_GUARD"] = 1] = "BEFORE_GUARD";
|
|
714
|
+
TInterceptorPriority[TInterceptorPriority["GUARD"] = 2] = "GUARD";
|
|
715
|
+
TInterceptorPriority[TInterceptorPriority["AFTER_GUARD"] = 3] = "AFTER_GUARD";
|
|
716
|
+
TInterceptorPriority[TInterceptorPriority["INTERCEPTOR"] = 4] = "INTERCEPTOR";
|
|
717
|
+
TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
|
|
718
|
+
TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
|
|
719
|
+
})(TInterceptorPriority || (TInterceptorPriority = {}));
|
|
720
|
+
function Intercept(handler, priority) {
|
|
721
|
+
return getMoostMate().decorate('interceptors', {
|
|
722
|
+
handler,
|
|
723
|
+
priority: priority || handler.priority || TInterceptorPriority.INTERCEPTOR,
|
|
724
|
+
}, true);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
const setHeaderInterceptor = (name, value, opts) => {
|
|
728
|
+
const fn = (before, after, onError) => {
|
|
729
|
+
const h = useSetHeader(name);
|
|
730
|
+
const status = useStatus();
|
|
731
|
+
after(() => {
|
|
732
|
+
if ((!h.value || (opts === null || opts === void 0 ? void 0 : opts.force)) && (!(opts === null || opts === void 0 ? void 0 : opts.status) || opts.status === status.value)) {
|
|
733
|
+
h.value = value;
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
};
|
|
737
|
+
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
738
|
+
return fn;
|
|
739
|
+
};
|
|
740
|
+
function SetHeader(...args) {
|
|
741
|
+
return Intercept(setHeaderInterceptor(...args));
|
|
742
|
+
}
|
|
743
|
+
const setCookieInterceptor = (name, value, attrs) => {
|
|
744
|
+
const fn = (before, after, onError) => {
|
|
745
|
+
const { setCookie, getCookie } = useSetCookies();
|
|
746
|
+
after(() => {
|
|
747
|
+
if (!getCookie(name)) {
|
|
748
|
+
setCookie(name, value, attrs);
|
|
749
|
+
}
|
|
750
|
+
});
|
|
751
|
+
};
|
|
752
|
+
fn.priority = TInterceptorPriority.AFTER_ALL;
|
|
753
|
+
return fn;
|
|
754
|
+
};
|
|
755
|
+
function SetCookie(...args) {
|
|
756
|
+
return Intercept(setCookieInterceptor(...args));
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
export { All, Authorization, Body, Cookie, CookieHook, Delete, Get, Header, HeaderHook, HttpMethod, Ip, IpList, Method, MoostHttp, Patch, Post, Put, Query, RawBody, Req, ReqId, Res, SetCookie, SetHeader, StatusHook, Url };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/event-http",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "@moostjs/event-http",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-http#readme",
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"moost": "0.2.
|
|
31
|
+
"moost": "0.2.6",
|
|
32
32
|
"wooks": "^0.2.5"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|