@tsed/hooks 8.0.0-rc.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/lib/browser/hooks.umd.min.js +2 -0
- package/lib/browser/hooks.umd.min.js.map +1 -0
- package/lib/esm/Hooks.js +131 -0
- package/lib/esm/Hooks.js.map +1 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/types/Hooks.d.ts +99 -0
- package/lib/types/index.d.ts +1 -0
- package/package.json +56 -0
- package/readme.md +64 -0
- package/vitest.config.mts +21 -0
- package/webpack.config.cjs +4 -0
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["@tsed/hooks"]=t():e["@tsed/hooks"]=t()}(self,(()=>(()=>{"use strict";var e={d:(t,s)=>{for(var n in s)e.o(s,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:s[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};function s(e,t){return!e||!t.ref||e&&t.ref===e}e.r(t),e.d(t,{$alter:()=>a,$asyncAlter:()=>h,$asyncEmit:()=>f,$emit:()=>c,$off:()=>l,$on:()=>o,$once:()=>i,Hooks:()=>n,hooks:()=>r});class n{#e=new Map;has(e){return!!this.#e.get(e)?.length}on(e,t,s){let n=t;s||(s=n,n=void 0);const r=this.#e.get(e)||[];return r.push({cb:s,ref:n}),this.#e.set(e,r),this}once(e,t,s){s||(s=t);const n=(...t)=>{s(...t),this.off(e,n)};return this.on(e,t,n),this}off(e,t){const s=(e,t)=>{t.length?this.#e.set(e,t):this.#e.delete(e)};if("string"==typeof e&&t){let n=this.#e.get(e);n&&s(e,n.filter((e=>e.cb!==t)))}else{const t=e;this.#e.forEach(((e,n)=>{s(n,e.filter((e=>e.ref!==t)))}))}return this}emit(e,t,n){Array.isArray(t)&&(n=t,t=void 0),n||=[];const r=this.#e.get(e);if(r?.length)for(const e of r)s(t,e)&&e.cb.apply(null,n)}async asyncEmit(e,t,n){Array.isArray(t)&&(n=t,t=void 0);const r=this.#e.get(e);if(r?.length){const e=r.filter((e=>s(t,e))).map((e=>e.cb.apply(null,n)));await Promise.all(e)}}alter(e,t,s=[],n=null){const r=this.#e.get(e);if(r?.length)for(const{cb:e}of r)t=e.call(n,t,...s);return t}async asyncAlter(e,t,s=[],n=null){const r=this.#e.get(e);if(r?.length)for(const e of r)t=await e.cb.call(n,t,...s);return t}destroy(){this.#e.clear()}}const r=new n,o=r.on.bind(r),i=r.once.bind(r),l=r.off.bind(r),c=r.emit.bind(r),f=r.asyncEmit.bind(r),a=r.alter.bind(r),h=r.asyncAlter.bind(r);return t})()));
|
|
2
|
+
//# sourceMappingURL=hooks.umd.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.umd.min.js","mappings":"CAAA,SAA2CA,EAAMC,GAC1B,iBAAZC,SAA0C,iBAAXC,OACxCA,OAAOD,QAAUD,IACQ,mBAAXG,QAAyBA,OAAOC,IAC9CD,OAAO,GAAIH,GACe,iBAAZC,QACdA,QAAQ,eAAiBD,IAEzBD,EAAK,eAAiBC,GACvB,CATD,CASGK,MAAM,I,mBCRT,IAAIC,EAAsB,CCA1BA,EAAwB,CAACL,EAASM,KACjC,IAAI,IAAIC,KAAOD,EACXD,EAAoBG,EAAEF,EAAYC,KAASF,EAAoBG,EAAER,EAASO,IAC5EE,OAAOC,eAAeV,EAASO,EAAK,CAAEI,YAAY,EAAMC,IAAKN,EAAWC,IAE1E,ECNDF,EAAwB,CAACQ,EAAKC,IAAUL,OAAOM,UAAUC,eAAeC,KAAKJ,EAAKC,GCClFT,EAAyBL,IACH,oBAAXkB,QAA0BA,OA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","webpack://@tsed/hooks/webpack/bootstrap","webpack://@tsed/hooks/webpack/runtime/define property getters","webpack://@tsed/hooks/webpack/runtime/hasOwnProperty shorthand","webpack://@tsed/hooks/webpack/runtime/make namespace object","webpack://@tsed/hooks/./src/Hooks.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"@tsed/hooks\"] = factory();\n\telse\n\t\troot[\"@tsed/hooks\"] = factory();\n})(self, () => {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","function match(ref, item) {\n return !ref || !item.ref || (ref && item.ref === ref);\n}\nexport class Hooks {\n #listeners = new Map();\n /**\n * Check if an event has listeners\n * @param event\n */\n has(event) {\n return !!this.#listeners.get(event)?.length;\n }\n on(event, cbORef, cb) {\n let ref = cbORef;\n if (!cb) {\n cb = ref;\n ref = undefined;\n }\n const items = this.#listeners.get(event) || [];\n items.push({\n cb,\n ref\n });\n this.#listeners.set(event, items);\n return this;\n }\n once(event, ref, cb) {\n if (!cb) {\n cb = ref;\n }\n const onceCb = (...args) => {\n cb(...args);\n this.off(event, onceCb);\n };\n this.on(event, ref, onceCb);\n return this;\n }\n off(event, cb) {\n const set = (event, items) => {\n if (items.length) {\n this.#listeners.set(event, items);\n }\n else {\n this.#listeners.delete(event);\n }\n };\n if (typeof event === \"string\" && cb) {\n let items = this.#listeners.get(event);\n if (items) {\n set(event, items.filter((item) => item.cb !== cb));\n }\n }\n else {\n const ref = event;\n this.#listeners.forEach((items, event) => {\n set(event, items.filter((item) => item.ref !== ref));\n });\n }\n return this;\n }\n emit(event, ref, args) {\n if (Array.isArray(ref)) {\n args = ref;\n ref = undefined;\n }\n args ||= [];\n const items = this.#listeners.get(event);\n if (items?.length) {\n for (const item of items) {\n if (match(ref, item)) {\n item.cb.apply(null, args);\n }\n }\n }\n }\n async asyncEmit(event, ref, args) {\n if (Array.isArray(ref)) {\n args = ref;\n ref = undefined;\n }\n const items = this.#listeners.get(event);\n if (items?.length) {\n const promises = items.filter((item) => match(ref, item)).map((item) => item.cb.apply(null, args));\n await Promise.all(promises);\n }\n }\n /**\n * Trigger an event, listener alter given value and return it.\n * @param event\n * @param value\n * @param args\n * @param callThis\n */\n alter(event, value, args = [], callThis = null) {\n const items = this.#listeners.get(event);\n if (items?.length) {\n for (const { cb } of items) {\n value = cb.call(callThis, value, ...args);\n }\n }\n return value;\n }\n /**\n * Trigger an event, async listener alter given value and return it.\n * @param event\n * @param value\n * @param args\n * @param callThis\n */\n async asyncAlter(event, value, args = [], callThis = null) {\n const items = this.#listeners.get(event);\n if (items?.length) {\n for (const item of items) {\n value = await item.cb.call(callThis, value, ...args);\n }\n }\n return value;\n }\n destroy() {\n this.#listeners.clear();\n }\n}\nexport const hooks = new Hooks();\nexport const $on = hooks.on.bind(hooks);\nexport const $once = hooks.once.bind(hooks);\nexport const $off = hooks.off.bind(hooks);\nexport const $emit = hooks.emit.bind(hooks);\nexport const $asyncEmit = hooks.asyncEmit.bind(hooks);\nexport const $alter = hooks.alter.bind(hooks);\nexport const $asyncAlter = hooks.asyncAlter.bind(hooks);\n"],"names":["root","factory","exports","module","define","amd","self","__webpack_require__","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","match","ref","item","Hooks","Map","has","event","this","length","on","cbORef","cb","undefined","items","push","set","once","onceCb","args","off","delete","filter","forEach","emit","Array","isArray","apply","async","promises","map","Promise","all","alter","callThis","destroy","clear","hooks","$on","bind","$once","$off","$emit","$asyncEmit","asyncEmit","$alter","$asyncAlter","asyncAlter"],"sourceRoot":""}
|
package/lib/esm/Hooks.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
function match(ref, item) {
|
|
2
|
+
return !ref || !item.ref || (ref && item.ref === ref);
|
|
3
|
+
}
|
|
4
|
+
export class Hooks {
|
|
5
|
+
#listeners = new Map();
|
|
6
|
+
/**
|
|
7
|
+
* Check if an event has listeners
|
|
8
|
+
* @param event
|
|
9
|
+
*/
|
|
10
|
+
has(event) {
|
|
11
|
+
return !!this.#listeners.get(event)?.length;
|
|
12
|
+
}
|
|
13
|
+
on(event, cbORef, cb) {
|
|
14
|
+
let ref = cbORef;
|
|
15
|
+
if (!cb) {
|
|
16
|
+
cb = ref;
|
|
17
|
+
ref = undefined;
|
|
18
|
+
}
|
|
19
|
+
const items = this.#listeners.get(event) || [];
|
|
20
|
+
items.push({
|
|
21
|
+
cb,
|
|
22
|
+
ref
|
|
23
|
+
});
|
|
24
|
+
this.#listeners.set(event, items);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
once(event, ref, cb) {
|
|
28
|
+
if (!cb) {
|
|
29
|
+
cb = ref;
|
|
30
|
+
}
|
|
31
|
+
const onceCb = (...args) => {
|
|
32
|
+
cb(...args);
|
|
33
|
+
this.off(event, onceCb);
|
|
34
|
+
};
|
|
35
|
+
this.on(event, ref, onceCb);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
off(event, cb) {
|
|
39
|
+
const set = (event, items) => {
|
|
40
|
+
if (items.length) {
|
|
41
|
+
this.#listeners.set(event, items);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.#listeners.delete(event);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
if (typeof event === "string" && cb) {
|
|
48
|
+
let items = this.#listeners.get(event);
|
|
49
|
+
if (items) {
|
|
50
|
+
set(event, items.filter((item) => item.cb !== cb));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
const ref = event;
|
|
55
|
+
this.#listeners.forEach((items, event) => {
|
|
56
|
+
set(event, items.filter((item) => item.ref !== ref));
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
emit(event, ref, args) {
|
|
62
|
+
if (Array.isArray(ref)) {
|
|
63
|
+
args = ref;
|
|
64
|
+
ref = undefined;
|
|
65
|
+
}
|
|
66
|
+
args ||= [];
|
|
67
|
+
const items = this.#listeners.get(event);
|
|
68
|
+
if (items?.length) {
|
|
69
|
+
for (const item of items) {
|
|
70
|
+
if (match(ref, item)) {
|
|
71
|
+
item.cb.apply(null, args);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async asyncEmit(event, ref, args) {
|
|
77
|
+
if (Array.isArray(ref)) {
|
|
78
|
+
args = ref;
|
|
79
|
+
ref = undefined;
|
|
80
|
+
}
|
|
81
|
+
const items = this.#listeners.get(event);
|
|
82
|
+
if (items?.length) {
|
|
83
|
+
const promises = items.filter((item) => match(ref, item)).map((item) => item.cb.apply(null, args));
|
|
84
|
+
await Promise.all(promises);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Trigger an event, listener alter given value and return it.
|
|
89
|
+
* @param event
|
|
90
|
+
* @param value
|
|
91
|
+
* @param args
|
|
92
|
+
* @param callThis
|
|
93
|
+
*/
|
|
94
|
+
alter(event, value, args = [], callThis = null) {
|
|
95
|
+
const items = this.#listeners.get(event);
|
|
96
|
+
if (items?.length) {
|
|
97
|
+
for (const { cb } of items) {
|
|
98
|
+
value = cb.call(callThis, value, ...args);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Trigger an event, async listener alter given value and return it.
|
|
105
|
+
* @param event
|
|
106
|
+
* @param value
|
|
107
|
+
* @param args
|
|
108
|
+
* @param callThis
|
|
109
|
+
*/
|
|
110
|
+
async asyncAlter(event, value, args = [], callThis = null) {
|
|
111
|
+
const items = this.#listeners.get(event);
|
|
112
|
+
if (items?.length) {
|
|
113
|
+
for (const item of items) {
|
|
114
|
+
value = await item.cb.call(callThis, value, ...args);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return value;
|
|
118
|
+
}
|
|
119
|
+
destroy() {
|
|
120
|
+
this.#listeners.clear();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
export const hooks = new Hooks();
|
|
124
|
+
export const $on = hooks.on.bind(hooks);
|
|
125
|
+
export const $once = hooks.once.bind(hooks);
|
|
126
|
+
export const $off = hooks.off.bind(hooks);
|
|
127
|
+
export const $emit = hooks.emit.bind(hooks);
|
|
128
|
+
export const $asyncEmit = hooks.asyncEmit.bind(hooks);
|
|
129
|
+
export const $alter = hooks.alter.bind(hooks);
|
|
130
|
+
export const $asyncAlter = hooks.asyncAlter.bind(hooks);
|
|
131
|
+
//# sourceMappingURL=Hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Hooks.js","sourceRoot":"","sources":["../../src/Hooks.ts"],"names":[],"mappings":"AAKA,SAAS,KAAK,CAAC,GAAoC,EAAE,IAAc;IACjE,OAAO,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC;AACxD,CAAC;AAED,MAAM,OAAO,KAAK;IAChB,UAAU,GAA4B,IAAI,GAAG,EAAE,CAAC;IAEhD;;;OAGG;IACH,GAAG,CAAC,KAAa;QACf,OAAO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AAY,CAAC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export type HookRef = string | symbol | any | Function;
|
|
2
|
+
export type HookListener = Function;
|
|
3
|
+
export declare class Hooks {
|
|
4
|
+
#private;
|
|
5
|
+
/**
|
|
6
|
+
* Check if an event has listeners
|
|
7
|
+
* @param event
|
|
8
|
+
*/
|
|
9
|
+
has(event: string): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Listen a hook event
|
|
12
|
+
* @param event The event name
|
|
13
|
+
* @param ref The reference of the listener
|
|
14
|
+
* @param cb The callback
|
|
15
|
+
*/
|
|
16
|
+
on(event: string, ref: HookRef, cb: HookListener): this;
|
|
17
|
+
/**
|
|
18
|
+
* Listen a hook event
|
|
19
|
+
* @param event The event name
|
|
20
|
+
* @param cb The callback
|
|
21
|
+
*/
|
|
22
|
+
on(event: string, cb: HookListener): this;
|
|
23
|
+
/**
|
|
24
|
+
* Listen a hook event once
|
|
25
|
+
*
|
|
26
|
+
* @param event The event name
|
|
27
|
+
* @param ref The reference of the listener
|
|
28
|
+
* @param cb The callback
|
|
29
|
+
*/
|
|
30
|
+
once(event: string, ref: HookRef, cb: HookListener): this;
|
|
31
|
+
/**
|
|
32
|
+
* Listen a hook event once
|
|
33
|
+
* @param event
|
|
34
|
+
* @param cb
|
|
35
|
+
*/
|
|
36
|
+
once(event: string, cb: HookListener): this;
|
|
37
|
+
/**
|
|
38
|
+
* Remove a listener attached to an event
|
|
39
|
+
* @param ref
|
|
40
|
+
*/
|
|
41
|
+
off(ref: HookRef): this;
|
|
42
|
+
/**
|
|
43
|
+
* Remove a listener attached to an event
|
|
44
|
+
* @param event
|
|
45
|
+
* @param cb
|
|
46
|
+
*/
|
|
47
|
+
off(event: string, cb: HookListener): this;
|
|
48
|
+
/**
|
|
49
|
+
* Trigger an event without arguments.
|
|
50
|
+
* @param event The event name
|
|
51
|
+
*/
|
|
52
|
+
emit(event: string): void;
|
|
53
|
+
/**
|
|
54
|
+
* Trigger an event and call listener.
|
|
55
|
+
* @param event
|
|
56
|
+
* @param args
|
|
57
|
+
* @param callThis
|
|
58
|
+
*/
|
|
59
|
+
emit(event: string, args: unknown[]): void;
|
|
60
|
+
/**
|
|
61
|
+
* Trigger an event with arguments and call only listener attached to the given ref.
|
|
62
|
+
* @param event The event name
|
|
63
|
+
* @param ref The reference of the listener
|
|
64
|
+
* @param args The arguments
|
|
65
|
+
*/
|
|
66
|
+
emit(event: string, ref: HookRef, args?: unknown[]): void;
|
|
67
|
+
/**
|
|
68
|
+
* Trigger an event and call async listener.
|
|
69
|
+
* @param event The event name
|
|
70
|
+
*/
|
|
71
|
+
asyncEmit(event: string): Promise<void>;
|
|
72
|
+
asyncEmit(event: string, args: unknown[]): Promise<void>;
|
|
73
|
+
asyncEmit(event: string, ref: HookRef, args?: unknown[]): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Trigger an event, listener alter given value and return it.
|
|
76
|
+
* @param event
|
|
77
|
+
* @param value
|
|
78
|
+
* @param args
|
|
79
|
+
* @param callThis
|
|
80
|
+
*/
|
|
81
|
+
alter<Arg = unknown, AlteredArg = Arg>(event: string, value: Arg, args?: unknown[], callThis?: unknown): AlteredArg;
|
|
82
|
+
/**
|
|
83
|
+
* Trigger an event, async listener alter given value and return it.
|
|
84
|
+
* @param event
|
|
85
|
+
* @param value
|
|
86
|
+
* @param args
|
|
87
|
+
* @param callThis
|
|
88
|
+
*/
|
|
89
|
+
asyncAlter<Arg = unknown, AlteredArg = Arg>(event: string, value: Arg, args?: unknown[], callThis?: unknown): Promise<AlteredArg>;
|
|
90
|
+
destroy(): void;
|
|
91
|
+
}
|
|
92
|
+
export declare const hooks: Hooks;
|
|
93
|
+
export declare const $on: typeof hooks.on;
|
|
94
|
+
export declare const $once: typeof hooks.once;
|
|
95
|
+
export declare const $off: typeof hooks.off;
|
|
96
|
+
export declare const $emit: typeof hooks.emit;
|
|
97
|
+
export declare const $asyncEmit: typeof hooks.asyncEmit;
|
|
98
|
+
export declare const $alter: typeof hooks.alter;
|
|
99
|
+
export declare const $asyncAlter: typeof hooks.asyncAlter;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Hooks.js";
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsed/hooks",
|
|
3
|
+
"description": "Hooks module for Ts.ED Framework",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"version": "8.0.0-rc.6",
|
|
6
|
+
"source": "./src/index.ts",
|
|
7
|
+
"main": "./lib/esm/index.js",
|
|
8
|
+
"module": "./lib/esm/index.js",
|
|
9
|
+
"typings": "./lib/types/index.d.ts",
|
|
10
|
+
"browser": "./lib/browser/core.umd.min.js",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./lib/types/index.d.ts",
|
|
14
|
+
"browser": "./lib/browser/hooks.umd.min.js",
|
|
15
|
+
"import": "./lib/esm/index.js",
|
|
16
|
+
"default": "./lib/esm/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./**/*.js": {
|
|
19
|
+
"types": "./lib/types/**/*.d.ts",
|
|
20
|
+
"import": "./lib/esm/**/*.js",
|
|
21
|
+
"default": "./lib/esm/**/*.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "yarn build:ts && yarn run build:browser",
|
|
26
|
+
"build:browser": "webpack",
|
|
27
|
+
"build:ts": "tsc --build tsconfig.json",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:ci": "vitest run --coverage.thresholds.autoUpdate=true"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"reflect-metadata": "^0.2.2",
|
|
33
|
+
"tslib": "2.7.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@tsed/monorepo-utils": "2.3.9",
|
|
37
|
+
"@tsed/typescript": "8.0.0-rc.6",
|
|
38
|
+
"@tsed/vitest": "8.0.0-rc.6",
|
|
39
|
+
"eslint": "9.12.0",
|
|
40
|
+
"typescript": "5.4.5",
|
|
41
|
+
"vite": "^5.4.8",
|
|
42
|
+
"vitest": "2.1.2",
|
|
43
|
+
"webpack": "^5.75.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {},
|
|
46
|
+
"repository": "https://github.com/tsedio/tsed",
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/tsedio/tsed/issues"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/hooks",
|
|
51
|
+
"author": "Romain Lenzotti",
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"tag": "rc"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
<p style="text-align: center" align="center">
|
|
2
|
+
<a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<h1>@tsed/core</h1>
|
|
7
|
+
|
|
8
|
+
[](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22)
|
|
9
|
+
[](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md)
|
|
10
|
+
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
11
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
12
|
+
[](https://github.com/prettier/prettier)
|
|
13
|
+
[](https://github.com/sponsors/romakita)
|
|
14
|
+
[](https://opencollective.com/tsed)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
<div align="center">
|
|
19
|
+
<a href="https://tsed.io/">Website</a>
|
|
20
|
+
<span> • </span>
|
|
21
|
+
<a href="https://tsed.io/getting-started/">Getting started</a>
|
|
22
|
+
<span> • </span>
|
|
23
|
+
<a href="https://api.tsed.io/rest/slack/tsedio/tsed">Slack</a>
|
|
24
|
+
<span> • </span>
|
|
25
|
+
<a href="https://twitter.com/TsED_io">Twitter</a>
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
<hr />
|
|
29
|
+
|
|
30
|
+
A package of Ts.ED framework. See website: https://tsed.io/
|
|
31
|
+
|
|
32
|
+
# Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install --save @tsed/core
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Contributors
|
|
39
|
+
|
|
40
|
+
Please read [contributing guidelines here](https://tsed.io/contributing.html).
|
|
41
|
+
|
|
42
|
+
<a href="https://github.com/tsedio/tsed/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
43
|
+
|
|
44
|
+
## Backers
|
|
45
|
+
|
|
46
|
+
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
47
|
+
|
|
48
|
+
<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
|
|
49
|
+
|
|
50
|
+
## Sponsors
|
|
51
|
+
|
|
52
|
+
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
|
|
56
|
+
The MIT License (MIT)
|
|
57
|
+
|
|
58
|
+
Copyright (c) 2016 - 2022 Romain Lenzotti
|
|
59
|
+
|
|
60
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
61
|
+
|
|
62
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
63
|
+
|
|
64
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import {presets} from "@tsed/vitest/presets";
|
|
3
|
+
import {defineConfig} from "vitest/config";
|
|
4
|
+
|
|
5
|
+
export default defineConfig(
|
|
6
|
+
{
|
|
7
|
+
...presets,
|
|
8
|
+
test: {
|
|
9
|
+
...presets.test,
|
|
10
|
+
coverage: {
|
|
11
|
+
...presets.test.coverage,
|
|
12
|
+
thresholds: {
|
|
13
|
+
statements: 100,
|
|
14
|
+
branches: 100,
|
|
15
|
+
functions: 100,
|
|
16
|
+
lines: 100
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
);
|