wooks 0.1.0 → 0.2.1

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/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  <p align="center">
6
6
  <img src="../../logo.png" width="128px"><br>
7
- <a href="https://github.com/prostojs/wooks/blob/main/LICENSE">
7
+ <a href="https://github.com/wooksjs/wooksjs/blob/main/LICENSE">
8
8
  <img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" />
9
9
  </a>
10
10
  </p>
@@ -32,14 +32,16 @@ It utilizes such a technique as you can see in React Hooks or Vue Composables. I
32
32
  `npm install wooks @wooksjs/event-http`
33
33
 
34
34
  ```js
35
- import { Wooks, useRouteParams } from 'wooks'
36
- import { WooksHttp } from '@wooksjs/event-http'
35
+ import { useRouteParams } from 'wooks'
36
+ import { createHttpApp } from '@wooksjs/event-http'
37
37
 
38
- const app = new Wooks()
38
+ const app = createHttpApp()
39
39
 
40
40
  app.on('GET', 'hello/:name', () => `Hello ${ useRouteParams().get('name') }!`)
41
41
 
42
- app.subscribe(new WooksHttp(3000, () => {
43
- console.log('Wooks Server is up on port 3000')
44
- }))
42
+ // shortcuts for some methods are supported:
43
+ // app.get('hello/:name', () => `Hello ${ useRouteParams().get('name') }!`)
44
+
45
+ app.listen(3000, () => { console.log('Wooks Server is up on port 3000') })
45
46
  ```
47
+ See full documentation [here](https://github.com/wooksjs/wooksjs/tree/main/packages/event-http)
package/dist/index.cjs CHANGED
@@ -3,51 +3,8 @@
3
3
  var router = require('@prostojs/router');
4
4
  var eventCore = require('@wooksjs/event-core');
5
5
 
6
- /******************************************************************************
7
- Copyright (c) Microsoft Corporation.
8
-
9
- Permission to use, copy, modify, and/or distribute this software for any
10
- purpose with or without fee is hereby granted.
11
-
12
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
- PERFORMANCE OF THIS SOFTWARE.
19
- ***************************************************************************** */
20
-
21
- function __awaiter(thisArg, _arguments, P, generator) {
22
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
- return new (P || (P = Promise))(function (resolve, reject) {
24
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
- step((generator = generator.apply(thisArg, _arguments || [])).next());
28
- });
29
- }
30
-
31
- const banner = () => `[${"wooks"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
32
-
33
- /* istanbul ignore file */
34
- function warn(text) {
35
- console.warn('' + banner() + text + '');
36
- }
37
- function logError(error) {
38
- console.error('' + '' + banner() + error + '');
39
- }
40
- function traceError(expl, e) {
41
- logError(expl);
42
- logError(e.message);
43
- if (e.stack) {
44
- warn(e.stack);
45
- }
46
- }
47
-
48
6
  class Wooks {
49
7
  constructor() {
50
- this.adapters = [];
51
8
  this.router = new router.ProstoRouter({
52
9
  silent: true,
53
10
  });
@@ -55,33 +12,40 @@ class Wooks {
55
12
  getRouter() {
56
13
  return this.router;
57
14
  }
58
- shortcuts(shortcuts) {
59
- for (const [name, method] of Object.entries(shortcuts)) {
60
- if (typeof this[name] !== 'undefined') {
61
- traceError('System error: ', new Error(`Could not assign shortcut "${name}" because it was already assigned.`));
62
- }
63
- else {
64
- Object.defineProperty(this, name, {
65
- value: (path, handler) => {
66
- return this.on(method, path, handler);
67
- },
68
- });
69
- }
15
+ lookup(method, path) {
16
+ var _a, _b;
17
+ const found = this.getRouter().lookup(method, path || '');
18
+ eventCore.useEventContext().store('routeParams').value = ((_a = found === null || found === void 0 ? void 0 : found.ctx) === null || _a === void 0 ? void 0 : _a.params) || {};
19
+ return ((_b = found === null || found === void 0 ? void 0 : found.route) === null || _b === void 0 ? void 0 : _b.handlers) || null;
20
+ }
21
+ on(method, path, handler) {
22
+ return this.router.on(method, path, handler);
23
+ }
24
+ }
25
+ let gWooks;
26
+ function getGlobalWooks() {
27
+ if (!gWooks) {
28
+ gWooks = new Wooks();
29
+ }
30
+ return gWooks;
31
+ }
32
+ class WooksAdapterBase {
33
+ constructor(wooks) {
34
+ if (wooks && wooks instanceof WooksAdapterBase) {
35
+ this.wooks = wooks.getWooks();
36
+ }
37
+ else if (wooks && wooks instanceof Wooks) {
38
+ this.wooks = wooks;
39
+ }
40
+ else {
41
+ this.wooks = getGlobalWooks();
70
42
  }
71
- return this;
72
43
  }
73
- subscribe(adapter) {
74
- return __awaiter(this, void 0, void 0, function* () {
75
- yield adapter.subscribe(({ method, url }) => {
76
- var _a, _b;
77
- const found = this.getRouter().lookup(method, url || '');
78
- eventCore.useEventContext().store('routeParams').value = ((_a = found === null || found === void 0 ? void 0 : found.ctx) === null || _a === void 0 ? void 0 : _a.params) || {};
79
- return ((_b = found === null || found === void 0 ? void 0 : found.route) === null || _b === void 0 ? void 0 : _b.handlers) || null;
80
- });
81
- });
44
+ getWooks() {
45
+ return this.wooks;
82
46
  }
83
47
  on(method, path, handler) {
84
- return this.router.on(method, path, handler);
48
+ return this.wooks.on(method, path, handler);
85
49
  }
86
50
  }
87
51
 
@@ -90,3 +54,5 @@ Object.defineProperty(exports, 'useRouteParams', {
90
54
  get: function () { return eventCore.useRouteParams; }
91
55
  });
92
56
  exports.Wooks = Wooks;
57
+ exports.WooksAdapterBase = WooksAdapterBase;
58
+ exports.getGlobalWooks = getGlobalWooks;
package/dist/index.d.ts CHANGED
@@ -1,23 +1,13 @@
1
1
  import { ProstoRouter } from '@prostojs/router';
2
- import { TProstoParamsType } from '@prostojs/router';
3
2
  import { TProstoRouterPathBuilder } from '@prostojs/router';
4
3
 
5
- export declare type TWooksHandler<ResType = unknown> = () => Promise<ResType> | ResType;
6
-
7
- export declare type TWooksLookupArgs = {
8
- method?: string;
9
- url?: string;
10
- };
4
+ export declare function getGlobalWooks(): Wooks;
11
5
 
12
- export declare type TWooksLookupHandlers = TWooksHandler[];
6
+ export declare type TWooksHandler<ResType = unknown> = () => Promise<ResType> | ResType;
13
7
 
14
8
  export declare interface TWooksOptions {
15
9
  }
16
10
 
17
- export declare interface TWooksSubscribeAdapter {
18
- subscribe(lookup: (route: TWooksLookupArgs) => TWooksLookupHandlers | null): Promise<void> | void;
19
- }
20
-
21
11
  export declare function useRouteParams<T extends object = Record<string, string | string[]>>(): {
22
12
  params: T;
23
13
  get: <K extends keyof T>(name: K) => T[K];
@@ -25,14 +15,17 @@ export declare function useRouteParams<T extends object = Record<string, string
25
15
 
26
16
  export declare class Wooks {
27
17
  protected router: ProstoRouter<TWooksHandler>;
28
- protected adapters: TWooksSubscribeAdapter[];
29
18
  constructor();
30
19
  getRouter(): ProstoRouter<TWooksHandler<unknown>>;
31
- shortcuts<K extends PropertyKey, T extends {
32
- [name in K]: string;
33
- }>(shortcuts: T): this & { [name in keyof T]: (path: string, handler: TWooksHandler) => TProstoRouterPathBuilder; };
34
- subscribe<T extends TWooksSubscribeAdapter>(adapter: T): Promise<void>;
35
- on<ResType = unknown, ParamsType = TProstoParamsType>(method: string, path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
20
+ lookup(method: string, path: string): TWooksHandler<unknown>[] | null;
21
+ on<ResType = unknown, ParamsType = Record<string, string | string[]>>(method: string, path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
22
+ }
23
+
24
+ export declare class WooksAdapterBase {
25
+ protected wooks: Wooks;
26
+ constructor(wooks?: Wooks | WooksAdapterBase);
27
+ getWooks(): Wooks;
28
+ on<ResType = unknown, ParamsType = Record<string, string | string[]>>(method: string, path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
36
29
  }
37
30
 
38
31
  export { }
package/dist/index.mjs CHANGED
@@ -2,51 +2,8 @@ import { ProstoRouter } from '@prostojs/router';
2
2
  import { useEventContext } from '@wooksjs/event-core';
3
3
  export { useRouteParams } from '@wooksjs/event-core';
4
4
 
5
- /******************************************************************************
6
- Copyright (c) Microsoft Corporation.
7
-
8
- Permission to use, copy, modify, and/or distribute this software for any
9
- purpose with or without fee is hereby granted.
10
-
11
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
- PERFORMANCE OF THIS SOFTWARE.
18
- ***************************************************************************** */
19
-
20
- function __awaiter(thisArg, _arguments, P, generator) {
21
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
22
- return new (P || (P = Promise))(function (resolve, reject) {
23
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
24
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
25
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26
- step((generator = generator.apply(thisArg, _arguments || [])).next());
27
- });
28
- }
29
-
30
- const banner = () => `[${"wooks"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
31
-
32
- /* istanbul ignore file */
33
- function warn(text) {
34
- console.warn('' + banner() + text + '');
35
- }
36
- function logError(error) {
37
- console.error('' + '' + banner() + error + '');
38
- }
39
- function traceError(expl, e) {
40
- logError(expl);
41
- logError(e.message);
42
- if (e.stack) {
43
- warn(e.stack);
44
- }
45
- }
46
-
47
5
  class Wooks {
48
6
  constructor() {
49
- this.adapters = [];
50
7
  this.router = new ProstoRouter({
51
8
  silent: true,
52
9
  });
@@ -54,34 +11,41 @@ class Wooks {
54
11
  getRouter() {
55
12
  return this.router;
56
13
  }
57
- shortcuts(shortcuts) {
58
- for (const [name, method] of Object.entries(shortcuts)) {
59
- if (typeof this[name] !== 'undefined') {
60
- traceError('System error: ', new Error(`Could not assign shortcut "${name}" because it was already assigned.`));
61
- }
62
- else {
63
- Object.defineProperty(this, name, {
64
- value: (path, handler) => {
65
- return this.on(method, path, handler);
66
- },
67
- });
68
- }
14
+ lookup(method, path) {
15
+ var _a, _b;
16
+ const found = this.getRouter().lookup(method, path || '');
17
+ useEventContext().store('routeParams').value = ((_a = found === null || found === void 0 ? void 0 : found.ctx) === null || _a === void 0 ? void 0 : _a.params) || {};
18
+ return ((_b = found === null || found === void 0 ? void 0 : found.route) === null || _b === void 0 ? void 0 : _b.handlers) || null;
19
+ }
20
+ on(method, path, handler) {
21
+ return this.router.on(method, path, handler);
22
+ }
23
+ }
24
+ let gWooks;
25
+ function getGlobalWooks() {
26
+ if (!gWooks) {
27
+ gWooks = new Wooks();
28
+ }
29
+ return gWooks;
30
+ }
31
+ class WooksAdapterBase {
32
+ constructor(wooks) {
33
+ if (wooks && wooks instanceof WooksAdapterBase) {
34
+ this.wooks = wooks.getWooks();
35
+ }
36
+ else if (wooks && wooks instanceof Wooks) {
37
+ this.wooks = wooks;
38
+ }
39
+ else {
40
+ this.wooks = getGlobalWooks();
69
41
  }
70
- return this;
71
42
  }
72
- subscribe(adapter) {
73
- return __awaiter(this, void 0, void 0, function* () {
74
- yield adapter.subscribe(({ method, url }) => {
75
- var _a, _b;
76
- const found = this.getRouter().lookup(method, url || '');
77
- useEventContext().store('routeParams').value = ((_a = found === null || found === void 0 ? void 0 : found.ctx) === null || _a === void 0 ? void 0 : _a.params) || {};
78
- return ((_b = found === null || found === void 0 ? void 0 : found.route) === null || _b === void 0 ? void 0 : _b.handlers) || null;
79
- });
80
- });
43
+ getWooks() {
44
+ return this.wooks;
81
45
  }
82
46
  on(method, path, handler) {
83
- return this.router.on(method, path, handler);
47
+ return this.wooks.on(method, path, handler);
84
48
  }
85
49
  }
86
50
 
87
- export { Wooks };
51
+ export { Wooks, WooksAdapterBase, getGlobalWooks };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wooks",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "wooks",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -32,7 +32,7 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@prostojs/router": "^0.0.16",
35
- "@wooksjs/event-core": "0.1.0"
35
+ "@wooksjs/event-core": "0.2.1"
36
36
  },
37
37
  "homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/wooks#readme"
38
38
  }