@e22m4u/js-trie-router 0.4.0 → 0.4.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/.mocharc.cjs +1 -0
- package/README.md +30 -27
- package/dist/cjs/index.cjs +149 -146
- package/mocha-setup.js +4 -0
- package/package.json +1 -1
- package/src/debuggable-service.spec.js +1 -1
- package/src/hooks/hook-invoker.spec.js +1 -1
- package/src/hooks/hook-registry.spec.js +1 -1
- package/src/parsers/body-parser.d.ts +2 -2
- package/src/parsers/body-parser.js +6 -6
- package/src/parsers/body-parser.spec.js +2 -2
- package/src/parsers/cookies-parser.d.ts +2 -2
- package/src/parsers/cookies-parser.js +5 -5
- package/src/parsers/cookies-parser.spec.js +1 -1
- package/src/parsers/query-parser.d.ts +2 -2
- package/src/parsers/query-parser.js +5 -5
- package/src/parsers/query-parser.spec.js +1 -1
- package/src/parsers/request-parser.d.ts +2 -2
- package/src/parsers/request-parser.js +8 -8
- package/src/parsers/request-parser.spec.js +1 -1
- package/src/request-context.d.ts +3 -3
- package/src/request-context.js +9 -9
- package/src/request-context.spec.js +4 -4
- package/src/route-registry.d.ts +2 -2
- package/src/route-registry.js +7 -7
- package/src/route-registry.spec.js +1 -1
- package/src/route.js +1 -1
- package/src/route.spec.js +1 -1
- package/src/router-options.spec.js +1 -1
- package/src/senders/data-sender.d.ts +2 -2
- package/src/senders/data-sender.js +11 -11
- package/src/senders/data-sender.spec.js +1 -1
- package/src/senders/error-sender.d.ts +11 -6
- package/src/senders/error-sender.js +19 -19
- package/src/senders/error-sender.spec.js +6 -3
- package/src/trie-router.js +19 -18
- package/src/trie-router.spec.js +6 -6
- package/src/utils/clone-deep.js +1 -1
- package/src/utils/clone-deep.spec.js +1 -1
- package/src/utils/create-cookies-string.spec.js +1 -1
- package/src/utils/create-debugger.spec.js +1 -1
- package/src/utils/create-error.spec.js +1 -1
- package/src/utils/create-request-mock.js +11 -11
- package/src/utils/create-request-mock.spec.js +1 -1
- package/src/utils/create-response-mock.d.ts +2 -1
- package/src/utils/create-response-mock.js +30 -30
- package/src/utils/create-response-mock.spec.js +1 -1
- package/src/utils/fetch-request-body.d.ts +2 -2
- package/src/utils/fetch-request-body.js +16 -13
- package/src/utils/fetch-request-body.spec.js +1 -1
- package/src/utils/get-request-pathname.d.ts +2 -2
- package/src/utils/get-request-pathname.js +8 -8
- package/src/utils/get-request-pathname.spec.js +1 -1
- package/src/utils/is-promise.spec.js +1 -1
- package/src/utils/is-readable-stream.spec.js +1 -1
- package/src/utils/is-response-sent.d.ts +2 -2
- package/src/utils/is-response-sent.js +8 -8
- package/src/utils/is-response-sent.spec.js +1 -1
- package/src/utils/is-writable-stream.spec.js +1 -1
- package/src/utils/parse-content-type.spec.js +1 -1
- package/src/utils/parse-cookies.spec.js +1 -1
- package/src/utils/to-camel-case.spec.js +1 -1
- package/src/chai.js +0 -7
package/.mocharc.cjs
CHANGED
package/README.md
CHANGED
|
@@ -78,18 +78,18 @@ server.listen(3000, 'localhost'); // прослушивание за
|
|
|
78
78
|
`RequestContext` с набором свойств, содержащих разобранные
|
|
79
79
|
данные входящего запроса.
|
|
80
80
|
|
|
81
|
-
- `
|
|
82
|
-
- `
|
|
83
|
-
- `
|
|
84
|
-
- `params: ParsedParams` объект ключ-значение с параметрами
|
|
85
|
-
- `query: ParsedQuery` объект ключ-значение с параметрами строки
|
|
86
|
-
- `headers: ParsedHeaders` объект ключ-значение с заголовками
|
|
87
|
-
- `cookies: ParsedCookies` объект ключ-значение разобранного заголовка `Cookie
|
|
88
|
-
- `method: string` метод запроса в верхнем регистре, например `GET`, `POST` и
|
|
89
|
-
- `path: string` путь включающий строку запроса, например `/myPath?foo=bar
|
|
90
|
-
- `pathname: string` путь запроса, например `/myPath
|
|
91
|
-
- `body: unknown` тело
|
|
92
|
-
- `meta: object` мета-данные из определения
|
|
81
|
+
- `container: ServiceContainer` экземпляр [сервис-контейнера](https://npmjs.com/package/@e22m4u/js-service);
|
|
82
|
+
- `request: IncomingMessage` нативный поток входящего запроса;
|
|
83
|
+
- `response: ServerResponse` нативный поток ответа сервера;
|
|
84
|
+
- `params: ParsedParams` объект ключ-значение с параметрами пути;
|
|
85
|
+
- `query: ParsedQuery` объект ключ-значение с параметрами строки запроса;
|
|
86
|
+
- `headers: ParsedHeaders` объект ключ-значение с заголовками запроса;
|
|
87
|
+
- `cookies: ParsedCookies` объект ключ-значение разобранного заголовка `Cookie`;
|
|
88
|
+
- `method: string` метод запроса в верхнем регистре, например `GET`, `POST` и т.д.;
|
|
89
|
+
- `path: string` путь включающий строку запроса, например `/myPath?foo=bar`;
|
|
90
|
+
- `pathname: string` путь запроса, например `/myPath`;
|
|
91
|
+
- `body: unknown` тело запроса;
|
|
92
|
+
- `meta: object` мета-данные из определения маршрута;
|
|
93
93
|
|
|
94
94
|
Пример доступа к контексту из обработчика маршрута.
|
|
95
95
|
|
|
@@ -101,17 +101,17 @@ router.defineRoute({
|
|
|
101
101
|
handler(ctx) {
|
|
102
102
|
// GET /users/10?include=city
|
|
103
103
|
// Cookie: foo=bar; baz=qux;
|
|
104
|
-
console.log(ctx.
|
|
105
|
-
console.log(ctx.
|
|
106
|
-
console.log(ctx.
|
|
107
|
-
console.log(ctx.
|
|
108
|
-
console.log(ctx.
|
|
109
|
-
console.log(ctx.
|
|
110
|
-
console.log(ctx.
|
|
111
|
-
console.log(ctx.
|
|
112
|
-
console.log(ctx.
|
|
113
|
-
console.log(ctx.
|
|
114
|
-
console.log(ctx.
|
|
104
|
+
console.log(ctx.container); // ServiceContainer
|
|
105
|
+
console.log(ctx.request); // IncomingMessage
|
|
106
|
+
console.log(ctx.response); // ServerResponse
|
|
107
|
+
console.log(ctx.params); // {id: 10}
|
|
108
|
+
console.log(ctx.query); // {include: 'city'}
|
|
109
|
+
console.log(ctx.headers); // {cookie: 'foo=bar; baz=qux;'}
|
|
110
|
+
console.log(ctx.cookies); // {foo: 'bar', baz: 'qux'}
|
|
111
|
+
console.log(ctx.method); // "GET"
|
|
112
|
+
console.log(ctx.path); // "/users/10?include=city"
|
|
113
|
+
console.log(ctx.pathname); // "/users/10"
|
|
114
|
+
console.log(ctx.meta); // {prop: 'value'}
|
|
115
115
|
// ...
|
|
116
116
|
},
|
|
117
117
|
});
|
|
@@ -150,10 +150,13 @@ router.defineRoute({ // регистрация маршрута
|
|
|
150
150
|
```js
|
|
151
151
|
router.defineRoute({
|
|
152
152
|
// ...
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
153
|
+
// для доступа к свойству `response` (ServerResponse)
|
|
154
|
+
// используется деструктуризация контекста запроса,
|
|
155
|
+
// что аналогично записи handler(ctx) { ctx.response ...
|
|
156
|
+
handler({response}) {
|
|
157
|
+
response.statusCode = 404;
|
|
158
|
+
response.setHeader('content-type', 'text/plain; charset=utf-8');
|
|
159
|
+
response.end('404 Not Found', 'utf-8');
|
|
157
160
|
},
|
|
158
161
|
});
|
|
159
162
|
```
|