@moostjs/event-http 0.2.32 → 0.2.34

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
@@ -1,7 +1,5 @@
1
1
  # @moostjs/event-http
2
2
 
3
- **!!! This is work-in-progress library, breaking changes are expected !!!**
4
-
5
3
  <p align="center">
6
4
  <img src="../../moost-logo.png" width="450px"><br>
7
5
  <a href="https://github.com/moostjs/moostjs/blob/main/LICENSE">
@@ -9,31 +7,51 @@
9
7
  </a>
10
8
  </p>
11
9
 
12
- `event-http` is a moostjs wrapper of [@wooksjs/event-http](https://github.com/wooksjs/wooksjs/tree/main/packages/event-http) with corresponding decorators for composable functions.
13
-
14
- ## Quick Start
15
-
16
- ```ts
17
- import { MoostHttp, Get } from '@moostjs/event-http'
18
- import { Moost, Param } from 'moost'
19
-
20
- class MyServer extends Moost {
21
- @Get('test/:name')
22
- test(@Param('name') name: string) {
23
- return { message: `Hello ${name}!` }
24
- }
25
- }
26
-
27
- const app = new MyServer()
28
- const http = new MoostHttp()
29
- app.adapter(http).listen(3000, () => {
30
- app.getLogger('MyApp').log('Up on port 3000')
31
- })
32
- app.init()
33
- // curl http://localhost:3000/test/World
34
- // {"message":"Hello World!"}
10
+ Welcome to `@moostjs/event-http`, a Moostjs library that serves as a wrapper for [@wooksjs/event-http](https://github.com/wooksjs/wooksjs/tree/main/packages/event-http). This package provides decorators for composing functions, thereby enhancing the simplicity and efficiency of your Moost application.
11
+
12
+ **Note:** As `@moostjs/event-http` is under active development, breaking changes can be expected.
13
+
14
+ ## Overview
15
+
16
+ The `@moostjs/event-http` module plays a crucial role in making Moost apps receptive to HTTP events. By employing it in your project, you can create handlers for HTTP events in a declarative, structured manner.
17
+
18
+ ## Getting Started
19
+
20
+ Starting a new Moost HTTP project is quite simple. All you need to do is run the following command:
21
+
22
+ ```bash
23
+ npm create moost -- --http
35
24
  ```
36
25
 
37
- ## Install
26
+ You can also provide a name for your project:
27
+
28
+ ```bash
29
+ npm create moost my-web-app -- --http
30
+ ```
31
+
32
+ This command will initiate a setup tool that will guide you through the project initialization process. It will prompt you to make certain choices such as:
33
+
34
+ - Whether you want to integrate eslint and prettier.
35
+ - Which bundler you prefer to use: esbuild or rollup.
36
+
37
+ ## [Official Documentation](https://moost.org/webapp/)
38
+
39
+ ## Contributing
40
+
41
+ We are excited to welcome contributors who are passionate about improving Moostjs. No matter your level of experience, your unique perspective and skills can make valuable contributions to our growing community.
42
+
43
+ Here are some basic steps to get you started:
44
+
45
+ 1. **Fork the Repo:** Navigate to [moostjs](https://github.com/moostjs/moostjs) and fork the repository to your own GitHub account.
46
+
47
+ 2. **Clone the Repo:** Clone the forked repository to your local machine.
48
+
49
+ 3. **Create a Branch:** Make a new branch for your feature or bug fix.
50
+
51
+ 4. **Make your Changes:** Implement your feature or fix the bug and commit the changes to your branch.
52
+
53
+ 5. **Make a Pull Request:** Navigate back to your forked repo and press the "New pull request" button.
54
+
55
+ Don't hesitate to ask for help if you need it. We believe in fostering a friendly and respectful environment for all contributors.
38
56
 
39
- `npm install moost @moostjs/event-http`
57
+ Thank you for your interest in Moostjs. We look forward to building something amazing together!
package/dist/index.cjs CHANGED
@@ -125,7 +125,7 @@ class MoostHttp {
125
125
  : typeof opts.method === 'string'
126
126
  ? opts.method
127
127
  : '';
128
- const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/');
128
+ const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/') + `${path.endsWith('//') ? '/' : ''}`; // explicit double slash "//" -> force url to end with slash
129
129
  if (!fn) {
130
130
  fn = moost.defineMoostEventHandler({
131
131
  contextType: CONTEXT_TYPE,
@@ -143,7 +143,8 @@ class MoostHttp {
143
143
  },
144
144
  });
145
145
  }
146
- const { getPath: pathBuilder } = this.httpApp.on(handler.method, targetPath, fn);
146
+ const routerBinding = this.httpApp.on(handler.method, targetPath, fn);
147
+ const { getPath: pathBuilder } = routerBinding;
147
148
  const methodMeta = moost.getMoostMate().read(opts.fakeInstance, opts.method) ||
148
149
  {};
149
150
  const id = (methodMeta.id || opts.method);
@@ -162,6 +163,10 @@ class MoostHttp {
162
163
  }
163
164
  }
164
165
  opts.logHandler(`${''}(${handler.method})${''}${targetPath}`);
166
+ const args = routerBinding.getArgs();
167
+ const params = {};
168
+ args.forEach(a => params[a] = `{${a}}`);
169
+ opts.register(handler, routerBinding.getPath(params), args);
165
170
  }
166
171
  }
167
172
  }
@@ -491,16 +496,18 @@ function Cookie(name) {
491
496
  * @paramType string | object
492
497
  */
493
498
  function Query(name) {
494
- return moost.Resolve(() => {
499
+ const isItem = !!name;
500
+ const _name = isItem ? name : 'Query';
501
+ return moost.getMoostMate().apply(moost.getMoostMate().decorate('paramSource', isItem ? 'QUERY_ITEM' : 'QUERY'), moost.getMoostMate().decorate('paramName', _name), moost.Resolve(() => {
495
502
  const { jsonSearchParams, urlSearchParams } = eventHttp.useSearchParams();
496
- if (name) {
503
+ if (isItem) {
497
504
  const p = urlSearchParams();
498
505
  const value = p.get(name);
499
- return (value === '' && p.has(name)) || value;
506
+ return value === null ? undefined : value;
500
507
  }
501
508
  const json = jsonSearchParams();
502
- return Object.keys(json).length ? json : null;
503
- }, name || 'Query');
509
+ return Object.keys(json).length ? json : undefined;
510
+ }, _name));
504
511
  }
505
512
  /**
506
513
  * Get Requested URL
@@ -565,7 +572,7 @@ function IpList() {
565
572
  * @paramType object | string | unknown
566
573
  */
567
574
  function Body() {
568
- return moost.Resolve(() => useBody().parseBody(), 'body');
575
+ return moost.getMoostMate().apply(moost.getMoostMate().decorate('paramSource', 'BODY'), moost.Resolve(() => useBody().parseBody(), 'body'));
569
576
  }
570
577
  /**
571
578
  * Get Raw Request Body Buffer
@@ -699,17 +706,6 @@ function SetStatus(...args) {
699
706
  return moost.Intercept(setStatusInterceptor(...args));
700
707
  }
701
708
 
702
- function httpValidatePipe(opts) {
703
- return moost.validatePipe(Object.assign({ errorCb: (message, details) => {
704
- throw new eventHttp.HttpError(400, {
705
- statusCode: 400,
706
- message,
707
- error: 'Validation Error',
708
- details,
709
- });
710
- } }, opts[0]));
711
- }
712
-
713
709
  Object.defineProperty(exports, 'HttpError', {
714
710
  enumerable: true,
715
711
  get: function () { return eventHttp.HttpError; }
@@ -746,4 +742,3 @@ exports.SetHeader = SetHeader;
746
742
  exports.SetStatus = SetStatus;
747
743
  exports.StatusHook = StatusHook;
748
744
  exports.Url = Url;
749
- exports.httpValidatePipe = httpValidatePipe;
package/dist/index.d.ts CHANGED
@@ -11,13 +11,11 @@ import { THook } from '@wooksjs/event-core';
11
11
  import { TInterceptorFn } from 'moost';
12
12
  import { TMoostAdapter } from 'moost';
13
13
  import { TMoostAdapterOptions } from 'moost';
14
- import { TPipeFn } from 'moost';
15
14
  import { TProstoRouterPathBuilder } from '@prostojs/router';
16
15
  import { TProvideRegistry } from '@prostojs/infact';
17
16
  import { TWooksHttpOptions } from '@wooksjs/event-http';
18
17
  import { useHttpContext } from '@wooksjs/event-http';
19
18
  import { useSetCookies } from '@wooksjs/event-http';
20
- import { validatePipe } from 'moost';
21
19
  import { WooksHttp } from '@wooksjs/event-http';
22
20
 
23
21
  export declare const All: (path?: string) => MethodDecorator;
@@ -35,7 +33,7 @@ export declare function Authorization(name: 'username' | 'password' | 'bearer' |
35
33
  * @decorator
36
34
  * @paramType object | string | unknown
37
35
  */
38
- declare function Body_2(): ParameterDecorator & PropertyDecorator;
36
+ declare function Body_2(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
39
37
  export { Body_2 as Body }
40
38
 
41
39
  /**
@@ -86,8 +84,6 @@ export { HttpError }
86
84
 
87
85
  export declare function HttpMethod(method: '*' | 'GET' | 'PUT' | 'POST' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS', path?: string): MethodDecorator;
88
86
 
89
- export declare function httpValidatePipe(opts: Parameters<typeof validatePipe>): TPipeFn<TEmpty>;
90
-
91
87
  /**
92
88
  * Get Request IP Address
93
89
  * @decorator
@@ -156,7 +152,7 @@ export declare class MoostHttp implements TMoostAdapter<THttpHandlerMeta> {
156
152
  onInit(moost: Moost): void;
157
153
  getProvideRegistry(): TProvideRegistry;
158
154
  getLogger(): TConsoleBase;
159
- bindHandler<T extends object = object>(opts: TMoostAdapterOptions<THttpHandlerMeta, T>): void | Promise<void>;
155
+ bindHandler<T extends object = object>(opts: TMoostAdapterOptions<THttpHandlerMeta, T>): void;
160
156
  }
161
157
 
162
158
  export declare const Patch: (path?: string) => MethodDecorator;
@@ -311,9 +307,6 @@ export { TCookieAttributesInput }
311
307
 
312
308
  export declare type TCookieHook = THook & Partial<THook<TCookieAttributes, 'attrs'>>;
313
309
 
314
- declare interface TEmpty {
315
- }
316
-
317
310
  export declare type THeaderHook = THook;
318
311
 
319
312
  export declare interface THttpHandlerMeta {
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { WooksHttp, createHttpApp, useRequest, HttpError, useHttpContext, useHeaders, EHttpStatusCode, WooksURLSearchParams, useStatus, useSetHeader, useSetCookie, useAuthorization, useCookies, useSearchParams, useResponse, useSetCookies } from '@wooksjs/event-http';
2
2
  export { HttpError, useHttpContext } from '@wooksjs/event-http';
3
- import { defineMoostEventHandler, getMoostMate, Resolve, Intercept, TInterceptorPriority, defineInterceptorFn, validatePipe } from 'moost';
3
+ import { defineMoostEventHandler, getMoostMate, Resolve, Intercept, TInterceptorPriority, defineInterceptorFn } from 'moost';
4
4
  import { createProvideRegistry } from '@prostojs/infact';
5
5
  import { Server } from 'http';
6
6
  import { Server as Server$1 } from 'https';
@@ -124,7 +124,7 @@ class MoostHttp {
124
124
  : typeof opts.method === 'string'
125
125
  ? opts.method
126
126
  : '';
127
- const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/');
127
+ const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/') + `${path.endsWith('//') ? '/' : ''}`; // explicit double slash "//" -> force url to end with slash
128
128
  if (!fn) {
129
129
  fn = defineMoostEventHandler({
130
130
  contextType: CONTEXT_TYPE,
@@ -142,7 +142,8 @@ class MoostHttp {
142
142
  },
143
143
  });
144
144
  }
145
- const { getPath: pathBuilder } = this.httpApp.on(handler.method, targetPath, fn);
145
+ const routerBinding = this.httpApp.on(handler.method, targetPath, fn);
146
+ const { getPath: pathBuilder } = routerBinding;
146
147
  const methodMeta = getMoostMate().read(opts.fakeInstance, opts.method) ||
147
148
  {};
148
149
  const id = (methodMeta.id || opts.method);
@@ -161,6 +162,10 @@ class MoostHttp {
161
162
  }
162
163
  }
163
164
  opts.logHandler(`${''}(${handler.method})${''}${targetPath}`);
165
+ const args = routerBinding.getArgs();
166
+ const params = {};
167
+ args.forEach(a => params[a] = `{${a}}`);
168
+ opts.register(handler, routerBinding.getPath(params), args);
164
169
  }
165
170
  }
166
171
  }
@@ -490,16 +495,18 @@ function Cookie(name) {
490
495
  * @paramType string | object
491
496
  */
492
497
  function Query(name) {
493
- return Resolve(() => {
498
+ const isItem = !!name;
499
+ const _name = isItem ? name : 'Query';
500
+ return getMoostMate().apply(getMoostMate().decorate('paramSource', isItem ? 'QUERY_ITEM' : 'QUERY'), getMoostMate().decorate('paramName', _name), Resolve(() => {
494
501
  const { jsonSearchParams, urlSearchParams } = useSearchParams();
495
- if (name) {
502
+ if (isItem) {
496
503
  const p = urlSearchParams();
497
504
  const value = p.get(name);
498
- return (value === '' && p.has(name)) || value;
505
+ return value === null ? undefined : value;
499
506
  }
500
507
  const json = jsonSearchParams();
501
- return Object.keys(json).length ? json : null;
502
- }, name || 'Query');
508
+ return Object.keys(json).length ? json : undefined;
509
+ }, _name));
503
510
  }
504
511
  /**
505
512
  * Get Requested URL
@@ -564,7 +571,7 @@ function IpList() {
564
571
  * @paramType object | string | unknown
565
572
  */
566
573
  function Body() {
567
- return Resolve(() => useBody().parseBody(), 'body');
574
+ return getMoostMate().apply(getMoostMate().decorate('paramSource', 'BODY'), Resolve(() => useBody().parseBody(), 'body'));
568
575
  }
569
576
  /**
570
577
  * Get Raw Request Body Buffer
@@ -698,15 +705,4 @@ function SetStatus(...args) {
698
705
  return Intercept(setStatusInterceptor(...args));
699
706
  }
700
707
 
701
- function httpValidatePipe(opts) {
702
- return validatePipe(Object.assign({ errorCb: (message, details) => {
703
- throw new HttpError(400, {
704
- statusCode: 400,
705
- message,
706
- error: 'Validation Error',
707
- details,
708
- });
709
- } }, opts[0]));
710
- }
711
-
712
- 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, SetStatus, StatusHook, Url, httpValidatePipe };
708
+ 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, SetStatus, StatusHook, Url };
package/package.json CHANGED
@@ -1,10 +1,18 @@
1
1
  {
2
2
  "name": "@moostjs/event-http",
3
- "version": "0.2.32",
3
+ "version": "0.2.34",
4
4
  "description": "@moostjs/event-http",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
+ "sideEffects": false,
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.cjs",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
8
16
  "files": [
9
17
  "dist"
10
18
  ],
@@ -29,10 +37,10 @@
29
37
  "homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-http#readme",
30
38
  "peerDependencies": {},
31
39
  "dependencies": {
32
- "moost": "0.2.32",
33
- "@wooksjs/event-core": "^0.3.3",
34
- "@wooksjs/event-http": "^0.3.3",
35
- "@prostojs/infact": "^0.1.11",
40
+ "moost": "0.2.34",
41
+ "@wooksjs/event-core": "^0.3.11",
42
+ "@wooksjs/event-http": "^0.3.11",
43
+ "@prostojs/infact": "^0.1.13",
36
44
  "@prostojs/router": "^0.1.0"
37
45
  }
38
46
  }