yedra 0.9.2 → 0.9.4
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 +16 -50
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -5
- package/dist/lib.d.ts +27 -27
- package/dist/lib.js +29 -67
- package/dist/routing/app.d.ts +2 -2
- package/dist/routing/app.js +33 -50
- package/dist/routing/endpoint.d.ts +5 -5
- package/dist/routing/endpoint.js +50 -65
- package/dist/routing/env.d.ts +2 -2
- package/dist/routing/env.js +3 -7
- package/dist/routing/errors.js +7 -17
- package/dist/routing/http.d.ts +1 -1
- package/dist/routing/http.js +34 -53
- package/dist/routing/listen.d.ts +1 -1
- package/dist/routing/listen.js +20 -36
- package/dist/routing/log.js +1 -5
- package/dist/routing/path.js +1 -5
- package/dist/routing/route.d.ts +8 -7
- package/dist/routing/route.js +58 -70
- package/dist/routing/test.d.ts +2 -2
- package/dist/routing/test.js +52 -64
- package/dist/validation/array.d.ts +3 -3
- package/dist/validation/array.js +12 -16
- package/dist/validation/body.js +1 -5
- package/dist/validation/boolean.d.ts +1 -1
- package/dist/validation/boolean.js +6 -10
- package/dist/validation/date.d.ts +1 -1
- package/dist/validation/date.js +8 -12
- package/dist/validation/doc.d.ts +1 -1
- package/dist/validation/doc.js +7 -7
- package/dist/validation/either.d.ts +1 -1
- package/dist/validation/either.js +7 -11
- package/dist/validation/enum.d.ts +1 -1
- package/dist/validation/enum.js +6 -10
- package/dist/validation/error.js +4 -9
- package/dist/validation/intersection.d.ts +2 -2
- package/dist/validation/intersection.js +8 -13
- package/dist/validation/modifiable.d.ts +2 -2
- package/dist/validation/modifiable.js +6 -10
- package/dist/validation/none.d.ts +1 -1
- package/dist/validation/none.js +3 -8
- package/dist/validation/number.d.ts +1 -1
- package/dist/validation/number.js +12 -16
- package/dist/validation/object.d.ts +3 -3
- package/dist/validation/object.js +14 -19
- package/dist/validation/raw.d.ts +1 -1
- package/dist/validation/raw.js +4 -8
- package/dist/validation/record.d.ts +3 -3
- package/dist/validation/record.js +8 -12
- package/dist/validation/schema.d.ts +1 -1
- package/dist/validation/schema.js +5 -9
- package/dist/validation/string.d.ts +1 -1
- package/dist/validation/string.js +11 -15
- package/dist/validation/undefined.d.ts +1 -1
- package/dist/validation/undefined.js +6 -11
- package/dist/validation/union.d.ts +2 -2
- package/dist/validation/union.js +6 -10
- package/dist/validation/unknown.d.ts +1 -1
- package/dist/validation/unknown.js +3 -7
- package/package.json +3 -2
package/dist/routing/errors.js
CHANGED
|
@@ -1,73 +1,63 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ConflictError = exports.NotFoundError = exports.ForbiddenError = exports.PaymentRequiredError = exports.UnauthorizedError = exports.BadRequestError = exports.HttpError = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Base class for errors that will be handled as HTTP status codes.
|
|
6
3
|
*/
|
|
7
|
-
class HttpError extends Error {
|
|
4
|
+
export class HttpError extends Error {
|
|
8
5
|
constructor(status, message) {
|
|
9
6
|
super(message);
|
|
10
7
|
this.status = status;
|
|
11
8
|
}
|
|
12
9
|
}
|
|
13
|
-
exports.HttpError = HttpError;
|
|
14
10
|
/**
|
|
15
11
|
* Indicates a malformed request.
|
|
16
12
|
* Corresponds to HTTP status code 400 Bad Request.
|
|
17
13
|
*/
|
|
18
|
-
class BadRequestError extends HttpError {
|
|
14
|
+
export class BadRequestError extends HttpError {
|
|
19
15
|
constructor(message) {
|
|
20
16
|
super(400, message);
|
|
21
17
|
}
|
|
22
18
|
}
|
|
23
|
-
exports.BadRequestError = BadRequestError;
|
|
24
19
|
/**
|
|
25
20
|
* Indicates missing or invalid credentials.
|
|
26
21
|
* Corresponds to HTTP status code 401 Unauthorized.
|
|
27
22
|
*/
|
|
28
|
-
class UnauthorizedError extends HttpError {
|
|
23
|
+
export class UnauthorizedError extends HttpError {
|
|
29
24
|
constructor(message) {
|
|
30
25
|
super(401, message);
|
|
31
26
|
}
|
|
32
27
|
}
|
|
33
|
-
exports.UnauthorizedError = UnauthorizedError;
|
|
34
28
|
/**
|
|
35
29
|
* Indicates that some kind of payment is required.
|
|
36
30
|
* Corresponds to HTTP status code 402 Payment Required.
|
|
37
31
|
*/
|
|
38
|
-
class PaymentRequiredError extends HttpError {
|
|
32
|
+
export class PaymentRequiredError extends HttpError {
|
|
39
33
|
constructor(message) {
|
|
40
34
|
super(402, message);
|
|
41
35
|
}
|
|
42
36
|
}
|
|
43
|
-
exports.PaymentRequiredError = PaymentRequiredError;
|
|
44
37
|
/**
|
|
45
38
|
* Indicates that the user is not allowed to do something.
|
|
46
39
|
* Corresponds to HTTP status code 403 Forbidden.
|
|
47
40
|
*/
|
|
48
|
-
class ForbiddenError extends HttpError {
|
|
41
|
+
export class ForbiddenError extends HttpError {
|
|
49
42
|
constructor(message) {
|
|
50
43
|
super(403, message);
|
|
51
44
|
}
|
|
52
45
|
}
|
|
53
|
-
exports.ForbiddenError = ForbiddenError;
|
|
54
46
|
/**
|
|
55
47
|
* Indicates that the requested resource does not exist.
|
|
56
48
|
* Corresponds to HTTP status code 404 Not Found.
|
|
57
49
|
*/
|
|
58
|
-
class NotFoundError extends HttpError {
|
|
50
|
+
export class NotFoundError extends HttpError {
|
|
59
51
|
constructor(message) {
|
|
60
52
|
super(404, message);
|
|
61
53
|
}
|
|
62
54
|
}
|
|
63
|
-
exports.NotFoundError = NotFoundError;
|
|
64
55
|
/**
|
|
65
56
|
* Indicates that the action conflicts with the current state.
|
|
66
57
|
* Corresponds to HTTP status code 409 Conflict.
|
|
67
58
|
*/
|
|
68
|
-
class ConflictError extends HttpError {
|
|
59
|
+
export class ConflictError extends HttpError {
|
|
69
60
|
constructor(message) {
|
|
70
61
|
super(409, message);
|
|
71
62
|
}
|
|
72
63
|
}
|
|
73
|
-
exports.ConflictError = ConflictError;
|
package/dist/routing/http.d.ts
CHANGED
package/dist/routing/http.js
CHANGED
|
@@ -1,33 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Http = exports.HttpResponse = exports.HttpRequestError = void 0;
|
|
13
|
-
const types_1 = require("node:util/types");
|
|
1
|
+
import { isUint8Array } from 'node:util/types';
|
|
14
2
|
/**
|
|
15
3
|
* Indicates that the HTTP request failed. This can be
|
|
16
4
|
* due to a network error or a response that indicates
|
|
17
5
|
* failure. In the latter case, the response is part of
|
|
18
6
|
* this error.
|
|
19
7
|
*/
|
|
20
|
-
class HttpRequestError extends Error {
|
|
8
|
+
export class HttpRequestError extends Error {
|
|
21
9
|
constructor(message, response) {
|
|
22
10
|
super(message);
|
|
23
11
|
this.response = response;
|
|
24
12
|
}
|
|
25
13
|
}
|
|
26
|
-
exports.HttpRequestError = HttpRequestError;
|
|
27
14
|
/**
|
|
28
15
|
* The response to an HTTP request.
|
|
29
16
|
*/
|
|
30
|
-
class HttpResponse {
|
|
17
|
+
export class HttpResponse {
|
|
31
18
|
/**
|
|
32
19
|
* Creates a new HTTP response.
|
|
33
20
|
* @param status - The status code.
|
|
@@ -65,11 +52,10 @@ class HttpResponse {
|
|
|
65
52
|
return parsed;
|
|
66
53
|
}
|
|
67
54
|
}
|
|
68
|
-
exports.HttpResponse = HttpResponse;
|
|
69
55
|
/**
|
|
70
56
|
* A class for performing HTTP requests.
|
|
71
57
|
*/
|
|
72
|
-
class Http {
|
|
58
|
+
export class Http {
|
|
73
59
|
constructor(log) {
|
|
74
60
|
this.log = log;
|
|
75
61
|
}
|
|
@@ -101,42 +87,37 @@ class Http {
|
|
|
101
87
|
* @param headers - The HTTP headers.
|
|
102
88
|
* @returns The response.
|
|
103
89
|
*/
|
|
104
|
-
request(method, url, body, headers) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return response;
|
|
116
|
-
});
|
|
90
|
+
async request(method, url, body, headers) {
|
|
91
|
+
const isBuffer = isUint8Array(body);
|
|
92
|
+
const requestBody = isBuffer ? body : JSON.stringify(body);
|
|
93
|
+
const actualHeaders = isBuffer
|
|
94
|
+
? headers
|
|
95
|
+
: { 'content-type': 'application/json', ...headers };
|
|
96
|
+
const response = await this.getResponse(method, url, requestBody, actualHeaders);
|
|
97
|
+
if (response.status < 200 || response.status >= 300) {
|
|
98
|
+
throw new HttpRequestError(`HTTP request returned status code ${response.status}`, response);
|
|
99
|
+
}
|
|
100
|
+
return response;
|
|
117
101
|
}
|
|
118
|
-
getResponse(method, url, body, headers) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
throw new HttpRequestError(error.message);
|
|
136
|
-
}
|
|
137
|
-
throw error;
|
|
102
|
+
async getResponse(method, url, body, headers) {
|
|
103
|
+
try {
|
|
104
|
+
const response = await fetch(url, {
|
|
105
|
+
method,
|
|
106
|
+
headers,
|
|
107
|
+
body,
|
|
108
|
+
});
|
|
109
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
110
|
+
const responseHeaders = {};
|
|
111
|
+
response.headers.forEach((value, key) => {
|
|
112
|
+
responseHeaders[key] = value;
|
|
113
|
+
});
|
|
114
|
+
return new HttpResponse(response.status, responseHeaders, buffer);
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
if (error instanceof Error) {
|
|
118
|
+
throw new HttpRequestError(error.message);
|
|
138
119
|
}
|
|
139
|
-
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
140
122
|
}
|
|
141
123
|
}
|
|
142
|
-
exports.Http = Http;
|
package/dist/routing/listen.d.ts
CHANGED
package/dist/routing/listen.js
CHANGED
|
@@ -1,16 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.listen = void 0;
|
|
13
|
-
const node_http_1 = require("node:http");
|
|
1
|
+
import { createServer } from 'node:http';
|
|
14
2
|
const parseQuery = (url) => {
|
|
15
3
|
const result = {};
|
|
16
4
|
url.searchParams.forEach((value, key) => {
|
|
@@ -38,20 +26,18 @@ class ConcreteContext {
|
|
|
38
26
|
this.activeConns = 0;
|
|
39
27
|
this.server = server;
|
|
40
28
|
}
|
|
41
|
-
stop() {
|
|
42
|
-
return
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
});
|
|
29
|
+
async stop() {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
this.server.close((error) => {
|
|
32
|
+
if (error !== undefined) {
|
|
33
|
+
reject(error);
|
|
34
|
+
}
|
|
35
|
+
else if (this.activeConns === 0) {
|
|
36
|
+
resolve();
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.resolve = resolve;
|
|
40
|
+
}
|
|
55
41
|
});
|
|
56
42
|
});
|
|
57
43
|
}
|
|
@@ -68,26 +54,25 @@ class ConcreteContext {
|
|
|
68
54
|
}
|
|
69
55
|
}
|
|
70
56
|
}
|
|
71
|
-
const listen = (app, port) => {
|
|
57
|
+
export const listen = (app, port) => {
|
|
72
58
|
const addConn = () => {
|
|
73
59
|
context.addConn();
|
|
74
60
|
};
|
|
75
61
|
const removeConn = () => {
|
|
76
62
|
context.removeConn();
|
|
77
63
|
};
|
|
78
|
-
const server =
|
|
64
|
+
const server = createServer((req, res) => {
|
|
79
65
|
addConn();
|
|
80
66
|
const chunks = [];
|
|
81
67
|
req.on('data', (chunk) => {
|
|
82
68
|
chunks.push(chunk);
|
|
83
69
|
});
|
|
84
|
-
req.on('end', () =>
|
|
85
|
-
var _a, _b;
|
|
70
|
+
req.on('end', async () => {
|
|
86
71
|
const body = Buffer.concat(chunks);
|
|
87
|
-
const url = new URL(
|
|
72
|
+
const url = new URL(req.url ?? '', `http://${req.headers.host}`);
|
|
88
73
|
console.info(`[${new Date().toISOString()}] ${req.method} ${url.pathname} (${context.connectionCount()} connections)`);
|
|
89
|
-
const response =
|
|
90
|
-
method:
|
|
74
|
+
const response = await app.handle({
|
|
75
|
+
method: req.method ?? 'GET',
|
|
91
76
|
url: url.pathname,
|
|
92
77
|
query: parseQuery(url),
|
|
93
78
|
headers: parseHeaders(req.headers),
|
|
@@ -96,7 +81,7 @@ const listen = (app, port) => {
|
|
|
96
81
|
res.writeHead(response.status, response.headers);
|
|
97
82
|
res.end(response.body);
|
|
98
83
|
removeConn();
|
|
99
|
-
})
|
|
84
|
+
});
|
|
100
85
|
});
|
|
101
86
|
const context = new ConcreteContext(server);
|
|
102
87
|
server.listen(port, () => {
|
|
@@ -104,4 +89,3 @@ const listen = (app, port) => {
|
|
|
104
89
|
});
|
|
105
90
|
return context;
|
|
106
91
|
};
|
|
107
|
-
exports.listen = listen;
|
package/dist/routing/log.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Log = void 0;
|
|
4
|
-
class Log {
|
|
1
|
+
export class Log {
|
|
5
2
|
/**
|
|
6
3
|
* Output a debug message.
|
|
7
4
|
* @param args - The message.
|
|
@@ -31,4 +28,3 @@ class Log {
|
|
|
31
28
|
console.error(...args);
|
|
32
29
|
}
|
|
33
30
|
}
|
|
34
|
-
exports.Log = Log;
|
package/dist/routing/path.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Path = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Represents an HTTP API path. Provides methods for concatenating paths and
|
|
6
3
|
* extracting path parameters from strings.
|
|
7
4
|
*/
|
|
8
|
-
class Path {
|
|
5
|
+
export class Path {
|
|
9
6
|
/**
|
|
10
7
|
* Creates a new path. The path must start with `/`. Every part of the path
|
|
11
8
|
* must match /^(:?[a-z0-9]+\??)|\*$/. It can either be a specific word, e.g.
|
|
@@ -92,4 +89,3 @@ class Path {
|
|
|
92
89
|
return result;
|
|
93
90
|
}
|
|
94
91
|
}
|
|
95
|
-
exports.Path = Path;
|
package/dist/routing/route.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import { type
|
|
4
|
-
import {
|
|
5
|
-
import type {
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
1
|
+
import type { BodyType, Typeof } from '../validation/body.js';
|
|
2
|
+
import { type NoneBody } from '../validation/none.js';
|
|
3
|
+
import { type ObjectSchema } from '../validation/object.js';
|
|
4
|
+
import type { Schema } from '../validation/schema.js';
|
|
5
|
+
import type { Endpoint } from './app.js';
|
|
6
|
+
import { Path } from './path.js';
|
|
7
|
+
import { Log } from './log.js';
|
|
8
|
+
import { Http } from './http.js';
|
|
8
9
|
type ReqObject<Params, Query, Headers, Body> = {
|
|
9
10
|
log: Log;
|
|
10
11
|
http: Http;
|
package/dist/routing/route.js
CHANGED
|
@@ -1,28 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.route = void 0;
|
|
13
|
-
const types_1 = require("node:util/types");
|
|
14
|
-
const lib_1 = require("../lib");
|
|
15
|
-
const none_1 = require("../validation/none");
|
|
16
|
-
const object_1 = require("../validation/object");
|
|
17
|
-
const errors_1 = require("./errors");
|
|
18
|
-
const path_1 = require("./path");
|
|
1
|
+
import { isUint8Array } from 'node:util/types';
|
|
2
|
+
import { none } from '../validation/none.js';
|
|
3
|
+
import { object } from '../validation/object.js';
|
|
4
|
+
import { BadRequestError } from './errors.js';
|
|
5
|
+
import { Path } from './path.js';
|
|
6
|
+
import { Log } from './log.js';
|
|
7
|
+
import { Http } from './http.js';
|
|
8
|
+
import { ValidationError } from '../validation/error.js';
|
|
19
9
|
class Route {
|
|
20
10
|
constructor(path, params) {
|
|
21
11
|
this.path = path;
|
|
22
12
|
this.params = params;
|
|
23
13
|
}
|
|
24
14
|
get(options) {
|
|
25
|
-
return this.create(
|
|
15
|
+
return this.create({ req: none(), ...options }, 'GET');
|
|
26
16
|
}
|
|
27
17
|
post(options) {
|
|
28
18
|
return this.create(options, 'POST');
|
|
@@ -31,72 +21,71 @@ class Route {
|
|
|
31
21
|
return this.create(options, 'PUT');
|
|
32
22
|
}
|
|
33
23
|
delete(options) {
|
|
34
|
-
return this.create(
|
|
24
|
+
return this.create({ req: none(), ...options }, 'DELETE');
|
|
35
25
|
}
|
|
36
26
|
create(options, method) {
|
|
37
|
-
const querySchema =
|
|
38
|
-
const headersSchema =
|
|
27
|
+
const querySchema = object(options.query);
|
|
28
|
+
const headersSchema = object(options.headers);
|
|
39
29
|
const paramSchema = this.params;
|
|
40
30
|
return {
|
|
41
31
|
method,
|
|
42
32
|
path: this.path,
|
|
43
|
-
handle(req, params) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
for (let i = 0; i < paramSchema.length; ++i) {
|
|
55
|
-
parsedParams[i] = paramSchema[i].parse(params[i]);
|
|
56
|
-
}
|
|
33
|
+
async handle(req, params) {
|
|
34
|
+
let body;
|
|
35
|
+
let query;
|
|
36
|
+
let headers;
|
|
37
|
+
const parsedParams = [];
|
|
38
|
+
try {
|
|
39
|
+
body = options.req.deserialize(req.body, req.headers['content-type'] ?? 'application/octet-stream');
|
|
40
|
+
query = querySchema.parse(req.query);
|
|
41
|
+
headers = headersSchema.parse(req.headers);
|
|
42
|
+
for (let i = 0; i < paramSchema.length; ++i) {
|
|
43
|
+
parsedParams[i] = paramSchema[i].parse(params[i]);
|
|
57
44
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (error instanceof lib_1.ValidationError) {
|
|
63
|
-
throw new errors_1.BadRequestError(error.format());
|
|
64
|
-
}
|
|
65
|
-
throw error;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error instanceof SyntaxError) {
|
|
48
|
+
throw new BadRequestError(error.message);
|
|
66
49
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const response = yield options.do({
|
|
70
|
-
log,
|
|
71
|
-
http,
|
|
72
|
-
url: req.url,
|
|
73
|
-
params: parsedParams,
|
|
74
|
-
query,
|
|
75
|
-
headers,
|
|
76
|
-
body,
|
|
77
|
-
});
|
|
78
|
-
if ((0, types_1.isUint8Array)(response.body)) {
|
|
79
|
-
return {
|
|
80
|
-
status: (_b = response.status) !== null && _b !== void 0 ? _b : 200,
|
|
81
|
-
body: response.body,
|
|
82
|
-
headers: (_c = response.headers) !== null && _c !== void 0 ? _c : {},
|
|
83
|
-
};
|
|
50
|
+
if (error instanceof ValidationError) {
|
|
51
|
+
throw new BadRequestError(error.format());
|
|
84
52
|
}
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
const log = new Log();
|
|
56
|
+
const http = new Http(log);
|
|
57
|
+
const response = await options.do({
|
|
58
|
+
log,
|
|
59
|
+
http,
|
|
60
|
+
url: req.url,
|
|
61
|
+
params: parsedParams,
|
|
62
|
+
query,
|
|
63
|
+
headers,
|
|
64
|
+
body,
|
|
65
|
+
});
|
|
66
|
+
if (isUint8Array(response.body)) {
|
|
85
67
|
return {
|
|
86
|
-
status:
|
|
87
|
-
body:
|
|
88
|
-
headers:
|
|
68
|
+
status: response.status ?? 200,
|
|
69
|
+
body: response.body,
|
|
70
|
+
headers: response.headers ?? {},
|
|
89
71
|
};
|
|
90
|
-
}
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
status: response.status ?? 200,
|
|
75
|
+
body: Buffer.from(JSON.stringify(response.body)),
|
|
76
|
+
headers: {
|
|
77
|
+
'content-type': 'application/json',
|
|
78
|
+
...response.headers,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
91
81
|
},
|
|
92
82
|
documentation() {
|
|
93
|
-
var _a;
|
|
94
83
|
const parameters = [
|
|
95
84
|
...paramDocs(options.query, 'query'),
|
|
96
85
|
...paramDocs(options.headers, 'header'),
|
|
97
86
|
];
|
|
98
87
|
return {
|
|
99
|
-
tags: [
|
|
88
|
+
tags: [options.category ?? this.path.toString().split('/')[1]],
|
|
100
89
|
summary: options.summary,
|
|
101
90
|
description: options.description,
|
|
102
91
|
parameters,
|
|
@@ -133,7 +122,7 @@ class Route {
|
|
|
133
122
|
};
|
|
134
123
|
}
|
|
135
124
|
}
|
|
136
|
-
const route = (path, ...params) => {
|
|
125
|
+
export const route = (path, ...params) => {
|
|
137
126
|
let resultPath = '';
|
|
138
127
|
const sections = path.length - 1;
|
|
139
128
|
for (let i = 0; i < sections; ++i) {
|
|
@@ -141,9 +130,8 @@ const route = (path, ...params) => {
|
|
|
141
130
|
resultPath += `:${i}`;
|
|
142
131
|
}
|
|
143
132
|
resultPath += path[sections];
|
|
144
|
-
return new Route(new
|
|
133
|
+
return new Route(new Path(resultPath), params);
|
|
145
134
|
};
|
|
146
|
-
exports.route = route;
|
|
147
135
|
const paramDocs = (params, position) => {
|
|
148
136
|
const result = [];
|
|
149
137
|
for (const name in params) {
|
package/dist/routing/test.d.ts
CHANGED