yedra 0.9.0 → 0.9.2
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/dist/routing/app.d.ts +50 -0
- package/dist/routing/app.js +140 -0
- package/dist/routing/endpoint.d.ts +35 -0
- package/dist/routing/endpoint.js +131 -0
- package/dist/routing/env.d.ts +3 -0
- package/dist/routing/env.js +16 -0
- package/dist/routing/errors.d.ts +49 -0
- package/dist/routing/errors.js +73 -0
- package/dist/routing/http.d.ts +83 -0
- package/dist/routing/http.js +142 -0
- package/dist/routing/listen.d.ts +5 -0
- package/dist/routing/listen.js +107 -0
- package/dist/routing/log.d.ts +22 -0
- package/dist/routing/log.js +34 -0
- package/dist/routing/path.d.ts +42 -0
- package/dist/routing/path.js +95 -0
- package/dist/routing/route.d.ts +50 -0
- package/dist/routing/route.js +160 -0
- package/dist/routing/test.d.ts +11 -0
- package/dist/routing/test.js +75 -0
- package/dist/validation/array.d.ts +33 -0
- package/dist/validation/array.js +85 -0
- package/dist/validation/body.d.ts +26 -0
- package/dist/validation/body.js +12 -0
- package/dist/validation/boolean.d.ts +10 -0
- package/dist/validation/boolean.js +25 -0
- package/dist/validation/date.d.ts +11 -0
- package/dist/validation/date.js +34 -0
- package/dist/validation/doc.d.ts +10 -0
- package/dist/validation/doc.js +22 -0
- package/dist/validation/either.d.ts +14 -0
- package/dist/validation/either.js +42 -0
- package/dist/validation/enum.d.ts +18 -0
- package/dist/validation/enum.js +42 -0
- package/dist/validation/error.d.ts +27 -0
- package/dist/validation/error.js +57 -0
- package/dist/validation/intersection.d.ts +15 -0
- package/dist/validation/intersection.js +56 -0
- package/dist/validation/modifiable.d.ts +32 -0
- package/dist/validation/modifiable.js +46 -0
- package/dist/validation/none.d.ts +9 -0
- package/dist/validation/none.js +19 -0
- package/dist/validation/number.d.ts +23 -0
- package/dist/validation/number.js +62 -0
- package/dist/validation/object.d.ts +36 -0
- package/dist/validation/object.js +81 -0
- package/dist/validation/raw.d.ts +12 -0
- package/dist/validation/raw.js +23 -0
- package/dist/validation/record.d.ts +16 -0
- package/dist/validation/record.js +50 -0
- package/dist/validation/schema.d.ts +22 -0
- package/dist/validation/schema.js +33 -0
- package/dist/validation/string.d.ts +21 -0
- package/dist/validation/string.js +57 -0
- package/dist/validation/undefined.d.ts +9 -0
- package/dist/validation/undefined.js +26 -0
- package/dist/validation/union.d.ts +15 -0
- package/dist/validation/union.js +40 -0
- package/dist/validation/unknown.d.ts +10 -0
- package/dist/validation/unknown.js +19 -0
- package/package.json +2 -2
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type Context } from './listen';
|
|
2
|
+
import type { Path } from './path';
|
|
3
|
+
export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
4
|
+
type HttpReq = {
|
|
5
|
+
method: string;
|
|
6
|
+
url: string;
|
|
7
|
+
query: Record<string, string | undefined>;
|
|
8
|
+
headers: Record<string, string | undefined>;
|
|
9
|
+
body: Uint8Array;
|
|
10
|
+
};
|
|
11
|
+
type HttpRes = {
|
|
12
|
+
status: number;
|
|
13
|
+
body: Uint8Array;
|
|
14
|
+
headers: Record<string, string>;
|
|
15
|
+
};
|
|
16
|
+
export type Endpoint = {
|
|
17
|
+
method: Method;
|
|
18
|
+
path: Path;
|
|
19
|
+
handle: (req: HttpReq, params: Record<string, string>) => Promise<HttpRes>;
|
|
20
|
+
documentation: () => object;
|
|
21
|
+
};
|
|
22
|
+
export declare class App {
|
|
23
|
+
private endpoints;
|
|
24
|
+
constructor(endpoints: Endpoint[]);
|
|
25
|
+
/**
|
|
26
|
+
* Handle an HTTP request.
|
|
27
|
+
* @param req - The HTTP request.
|
|
28
|
+
* @returns The HTTP response.
|
|
29
|
+
*/
|
|
30
|
+
handle(req: HttpReq): Promise<HttpRes>;
|
|
31
|
+
/**
|
|
32
|
+
* Generate OpenAPI documentation for the app.
|
|
33
|
+
*/
|
|
34
|
+
docs(options: {
|
|
35
|
+
info: {
|
|
36
|
+
title: string;
|
|
37
|
+
description: string;
|
|
38
|
+
version: string;
|
|
39
|
+
};
|
|
40
|
+
servers: {
|
|
41
|
+
description: string;
|
|
42
|
+
url: string;
|
|
43
|
+
}[];
|
|
44
|
+
}): object;
|
|
45
|
+
listen(port: number): Context;
|
|
46
|
+
private static errorResponse;
|
|
47
|
+
private matchEndpoint;
|
|
48
|
+
}
|
|
49
|
+
export declare const app: (routes: string) => Promise<App>;
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
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.app = exports.App = void 0;
|
|
13
|
+
const glob_1 = require("glob");
|
|
14
|
+
const errors_1 = require("./errors");
|
|
15
|
+
const listen_1 = require("./listen");
|
|
16
|
+
class App {
|
|
17
|
+
constructor(endpoints) {
|
|
18
|
+
this.endpoints = endpoints;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Handle an HTTP request.
|
|
22
|
+
* @param req - The HTTP request.
|
|
23
|
+
* @returns The HTTP response.
|
|
24
|
+
*/
|
|
25
|
+
handle(req) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
if (req.method !== 'GET' &&
|
|
28
|
+
req.method !== 'POST' &&
|
|
29
|
+
req.method !== 'PUT' &&
|
|
30
|
+
req.method !== 'DELETE') {
|
|
31
|
+
return App.errorResponse(405, `Method '${req.method}' not allowed.`);
|
|
32
|
+
}
|
|
33
|
+
const match = this.matchEndpoint(req.url, req.method);
|
|
34
|
+
if (!match.result) {
|
|
35
|
+
if (match.invalidMethod) {
|
|
36
|
+
return App.errorResponse(405, `Method '${req.method}' not allowed for path '${req.url}'.`);
|
|
37
|
+
}
|
|
38
|
+
return App.errorResponse(404, `Path '${req.url}' not found.`);
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
return yield match.result.endpoint.handle(req, match.result.params);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (error instanceof errors_1.HttpError) {
|
|
45
|
+
return App.errorResponse(error.status, error.message);
|
|
46
|
+
}
|
|
47
|
+
console.error(error);
|
|
48
|
+
return App.errorResponse(500, 'Internal Server Error.');
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Generate OpenAPI documentation for the app.
|
|
54
|
+
*/
|
|
55
|
+
docs(options) {
|
|
56
|
+
var _a;
|
|
57
|
+
const paths = {};
|
|
58
|
+
for (const endpoint of this.endpoints) {
|
|
59
|
+
const path = endpoint.path.toString();
|
|
60
|
+
const methods = (_a = paths[path]) !== null && _a !== void 0 ? _a : {};
|
|
61
|
+
methods[endpoint.method.toLowerCase()] = endpoint.documentation();
|
|
62
|
+
paths[path] = methods;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
openapi: '3.0.2',
|
|
66
|
+
info: options.info,
|
|
67
|
+
servers: options.servers,
|
|
68
|
+
paths,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
listen(port) {
|
|
72
|
+
return (0, listen_1.listen)(this, port);
|
|
73
|
+
}
|
|
74
|
+
static errorResponse(status, errorMessage) {
|
|
75
|
+
return {
|
|
76
|
+
status,
|
|
77
|
+
body: Buffer.from(JSON.stringify({
|
|
78
|
+
status,
|
|
79
|
+
errorMessage,
|
|
80
|
+
})),
|
|
81
|
+
headers: {
|
|
82
|
+
'content-type': 'application/json',
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
matchEndpoint(url, method) {
|
|
87
|
+
let invalidMethod = false;
|
|
88
|
+
let result = undefined;
|
|
89
|
+
for (const endpoint of this.endpoints) {
|
|
90
|
+
const params = endpoint.path.match(url);
|
|
91
|
+
if (!params) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (endpoint.method !== method) {
|
|
95
|
+
invalidMethod = true;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const score = Object.keys(params).length;
|
|
99
|
+
const previous = result === null || result === void 0 ? void 0 : result.score;
|
|
100
|
+
if (previous === undefined || score < previous) {
|
|
101
|
+
// if there was no previous match or this one is better, use it
|
|
102
|
+
result = { endpoint, params, score };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return { invalidMethod, result };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.App = App;
|
|
109
|
+
const shouldIgnore = (path) => {
|
|
110
|
+
const extensions = [
|
|
111
|
+
'.test.ts',
|
|
112
|
+
'.schema.ts',
|
|
113
|
+
'.util.ts',
|
|
114
|
+
'.d.ts',
|
|
115
|
+
'.test.js',
|
|
116
|
+
'.schema.js',
|
|
117
|
+
'.util.js',
|
|
118
|
+
];
|
|
119
|
+
for (const extension of extensions) {
|
|
120
|
+
if (path.endsWith(extension)) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
};
|
|
126
|
+
const app = (routes) => __awaiter(void 0, void 0, void 0, function* () {
|
|
127
|
+
const endpoints = [];
|
|
128
|
+
const files = yield (0, glob_1.glob)([`${routes}/**/*.ts`, `${routes}/**/*.js`], {
|
|
129
|
+
absolute: true,
|
|
130
|
+
});
|
|
131
|
+
for (const file of files) {
|
|
132
|
+
if (shouldIgnore(file)) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const endpoint = yield Promise.resolve(`${file}`).then(s => require(s));
|
|
136
|
+
endpoints.push(endpoint.default);
|
|
137
|
+
}
|
|
138
|
+
return new App(endpoints);
|
|
139
|
+
});
|
|
140
|
+
exports.app = app;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { BodyType, Typeof } from '../validation/body';
|
|
2
|
+
import type { Schema } from '../validation/schema';
|
|
3
|
+
import type { Endpoint } from './app';
|
|
4
|
+
import { Http } from './http';
|
|
5
|
+
import { Log } from './log';
|
|
6
|
+
export type EndpointRequest<Query, Headers, Req> = {
|
|
7
|
+
log: Log;
|
|
8
|
+
http: Http;
|
|
9
|
+
url: string;
|
|
10
|
+
params: Record<string, string>;
|
|
11
|
+
query: Query;
|
|
12
|
+
headers: Headers;
|
|
13
|
+
body: Req;
|
|
14
|
+
};
|
|
15
|
+
export type EndpointResponse<Res> = Promise<{
|
|
16
|
+
status?: number;
|
|
17
|
+
body: Res;
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
}> | {
|
|
20
|
+
status?: number;
|
|
21
|
+
body: Res;
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
};
|
|
24
|
+
export type EndpointOptions<Query extends Schema<unknown>, Headers extends Schema<unknown>, Req extends BodyType<unknown>, Res extends BodyType<unknown>, Ext> = {
|
|
25
|
+
summary: string;
|
|
26
|
+
description?: string;
|
|
27
|
+
category?: string;
|
|
28
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
29
|
+
query: Query;
|
|
30
|
+
headers: Headers;
|
|
31
|
+
req: Req;
|
|
32
|
+
res: Res;
|
|
33
|
+
do: (req: EndpointRequest<Typeof<Query>, Typeof<Headers>, Typeof<Req>> & Ext) => EndpointResponse<Typeof<Res>>;
|
|
34
|
+
};
|
|
35
|
+
export declare const endpoint: <Query extends Schema<unknown>, Headers extends Schema<unknown>, Req extends BodyType<unknown>, Res extends BodyType<unknown>>(path: string, options: EndpointOptions<Query, Headers, Req, Res, object>) => Endpoint;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
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.endpoint = void 0;
|
|
13
|
+
const types_1 = require("node:util/types");
|
|
14
|
+
const error_1 = require("../validation/error");
|
|
15
|
+
const errors_1 = require("./errors");
|
|
16
|
+
const http_1 = require("./http");
|
|
17
|
+
const log_1 = require("./log");
|
|
18
|
+
const path_1 = require("./path");
|
|
19
|
+
const endpoint = (path, options) => {
|
|
20
|
+
return {
|
|
21
|
+
method: options.method,
|
|
22
|
+
path: new path_1.Path(path),
|
|
23
|
+
handle(req, params) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
var _a, _b, _c, _d;
|
|
26
|
+
let body;
|
|
27
|
+
let query;
|
|
28
|
+
let headers;
|
|
29
|
+
try {
|
|
30
|
+
body = options.req.deserialize(req.body, (_a = req.headers['content-type']) !== null && _a !== void 0 ? _a : 'application/octet-stream');
|
|
31
|
+
query = options.query.parse(req.query);
|
|
32
|
+
headers = options.headers.parse(req.headers);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (error instanceof SyntaxError) {
|
|
36
|
+
throw new errors_1.BadRequestError(error.message);
|
|
37
|
+
}
|
|
38
|
+
if (error instanceof error_1.ValidationError) {
|
|
39
|
+
throw new errors_1.BadRequestError(error.format());
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
const log = new log_1.Log();
|
|
44
|
+
const http = new http_1.Http(log);
|
|
45
|
+
const response = yield options.do({
|
|
46
|
+
log,
|
|
47
|
+
http,
|
|
48
|
+
url: req.url,
|
|
49
|
+
params,
|
|
50
|
+
query,
|
|
51
|
+
headers,
|
|
52
|
+
body,
|
|
53
|
+
});
|
|
54
|
+
if ((0, types_1.isUint8Array)(response.body)) {
|
|
55
|
+
return {
|
|
56
|
+
status: (_b = response.status) !== null && _b !== void 0 ? _b : 200,
|
|
57
|
+
body: response.body,
|
|
58
|
+
headers: (_c = response.headers) !== null && _c !== void 0 ? _c : {},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
status: (_d = response.status) !== null && _d !== void 0 ? _d : 200,
|
|
63
|
+
body: Buffer.from(JSON.stringify(response.body)),
|
|
64
|
+
headers: Object.assign({ 'content-type': 'application/json' }, response.headers),
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
documentation() {
|
|
69
|
+
var _a;
|
|
70
|
+
const parameters = [
|
|
71
|
+
...paramDocs(options.query, 'query'),
|
|
72
|
+
...paramDocs(options.headers, 'header'),
|
|
73
|
+
];
|
|
74
|
+
return {
|
|
75
|
+
tags: [(_a = options.category) !== null && _a !== void 0 ? _a : path.split('/')[1]],
|
|
76
|
+
summary: options.summary,
|
|
77
|
+
description: options.description,
|
|
78
|
+
parameters,
|
|
79
|
+
requestBody: {
|
|
80
|
+
required: true,
|
|
81
|
+
content: options.req.bodyDocs(),
|
|
82
|
+
},
|
|
83
|
+
responses: {
|
|
84
|
+
'200': {
|
|
85
|
+
description: 'Success',
|
|
86
|
+
content: options.res.bodyDocs(),
|
|
87
|
+
},
|
|
88
|
+
'400': {
|
|
89
|
+
description: 'Bad Request',
|
|
90
|
+
content: {
|
|
91
|
+
'application/json': {
|
|
92
|
+
schema: {
|
|
93
|
+
type: 'object',
|
|
94
|
+
properties: {
|
|
95
|
+
status: {
|
|
96
|
+
type: 'number',
|
|
97
|
+
},
|
|
98
|
+
error: {
|
|
99
|
+
type: 'string',
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
exports.endpoint = endpoint;
|
|
112
|
+
const paramDocs = (params, position) => {
|
|
113
|
+
var _a;
|
|
114
|
+
const result = [];
|
|
115
|
+
const docs = params.documentation();
|
|
116
|
+
if (!('properties' in docs) || !Array.isArray(docs.properties)) {
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
const required = 'required' in docs && Array.isArray(docs.required) ? docs.required : [];
|
|
120
|
+
for (const name in docs.properties) {
|
|
121
|
+
const property = docs.properties[name];
|
|
122
|
+
result.push({
|
|
123
|
+
name: name,
|
|
124
|
+
in: position,
|
|
125
|
+
description: 'description' in property ? property.description : undefined,
|
|
126
|
+
required: (_a = required.includes(name)) !== null && _a !== void 0 ? _a : false,
|
|
127
|
+
schema: property,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseEnv = void 0;
|
|
4
|
+
const error_1 = require("../validation/error");
|
|
5
|
+
const parseEnv = (schema) => {
|
|
6
|
+
try {
|
|
7
|
+
return schema.parse(process.env);
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
if (error instanceof error_1.ValidationError) {
|
|
11
|
+
console.error(`error: env validation failed: ${error.format()}`);
|
|
12
|
+
}
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
exports.parseEnv = parseEnv;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base class for errors that will be handled as HTTP status codes.
|
|
3
|
+
*/
|
|
4
|
+
export declare class HttpError extends Error {
|
|
5
|
+
readonly status: number;
|
|
6
|
+
constructor(status: number, message: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Indicates a malformed request.
|
|
10
|
+
* Corresponds to HTTP status code 400 Bad Request.
|
|
11
|
+
*/
|
|
12
|
+
export declare class BadRequestError extends HttpError {
|
|
13
|
+
constructor(message: string);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Indicates missing or invalid credentials.
|
|
17
|
+
* Corresponds to HTTP status code 401 Unauthorized.
|
|
18
|
+
*/
|
|
19
|
+
export declare class UnauthorizedError extends HttpError {
|
|
20
|
+
constructor(message: string);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Indicates that some kind of payment is required.
|
|
24
|
+
* Corresponds to HTTP status code 402 Payment Required.
|
|
25
|
+
*/
|
|
26
|
+
export declare class PaymentRequiredError extends HttpError {
|
|
27
|
+
constructor(message: string);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Indicates that the user is not allowed to do something.
|
|
31
|
+
* Corresponds to HTTP status code 403 Forbidden.
|
|
32
|
+
*/
|
|
33
|
+
export declare class ForbiddenError extends HttpError {
|
|
34
|
+
constructor(message: string);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Indicates that the requested resource does not exist.
|
|
38
|
+
* Corresponds to HTTP status code 404 Not Found.
|
|
39
|
+
*/
|
|
40
|
+
export declare class NotFoundError extends HttpError {
|
|
41
|
+
constructor(message: string);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Indicates that the action conflicts with the current state.
|
|
45
|
+
* Corresponds to HTTP status code 409 Conflict.
|
|
46
|
+
*/
|
|
47
|
+
export declare class ConflictError extends HttpError {
|
|
48
|
+
constructor(message: string);
|
|
49
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
/**
|
|
5
|
+
* Base class for errors that will be handled as HTTP status codes.
|
|
6
|
+
*/
|
|
7
|
+
class HttpError extends Error {
|
|
8
|
+
constructor(status, message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.status = status;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.HttpError = HttpError;
|
|
14
|
+
/**
|
|
15
|
+
* Indicates a malformed request.
|
|
16
|
+
* Corresponds to HTTP status code 400 Bad Request.
|
|
17
|
+
*/
|
|
18
|
+
class BadRequestError extends HttpError {
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(400, message);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.BadRequestError = BadRequestError;
|
|
24
|
+
/**
|
|
25
|
+
* Indicates missing or invalid credentials.
|
|
26
|
+
* Corresponds to HTTP status code 401 Unauthorized.
|
|
27
|
+
*/
|
|
28
|
+
class UnauthorizedError extends HttpError {
|
|
29
|
+
constructor(message) {
|
|
30
|
+
super(401, message);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.UnauthorizedError = UnauthorizedError;
|
|
34
|
+
/**
|
|
35
|
+
* Indicates that some kind of payment is required.
|
|
36
|
+
* Corresponds to HTTP status code 402 Payment Required.
|
|
37
|
+
*/
|
|
38
|
+
class PaymentRequiredError extends HttpError {
|
|
39
|
+
constructor(message) {
|
|
40
|
+
super(402, message);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.PaymentRequiredError = PaymentRequiredError;
|
|
44
|
+
/**
|
|
45
|
+
* Indicates that the user is not allowed to do something.
|
|
46
|
+
* Corresponds to HTTP status code 403 Forbidden.
|
|
47
|
+
*/
|
|
48
|
+
class ForbiddenError extends HttpError {
|
|
49
|
+
constructor(message) {
|
|
50
|
+
super(403, message);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.ForbiddenError = ForbiddenError;
|
|
54
|
+
/**
|
|
55
|
+
* Indicates that the requested resource does not exist.
|
|
56
|
+
* Corresponds to HTTP status code 404 Not Found.
|
|
57
|
+
*/
|
|
58
|
+
class NotFoundError extends HttpError {
|
|
59
|
+
constructor(message) {
|
|
60
|
+
super(404, message);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.NotFoundError = NotFoundError;
|
|
64
|
+
/**
|
|
65
|
+
* Indicates that the action conflicts with the current state.
|
|
66
|
+
* Corresponds to HTTP status code 409 Conflict.
|
|
67
|
+
*/
|
|
68
|
+
class ConflictError extends HttpError {
|
|
69
|
+
constructor(message) {
|
|
70
|
+
super(409, message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.ConflictError = ConflictError;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Log, Schema, Typeof } from '../lib';
|
|
2
|
+
/**
|
|
3
|
+
* Indicates that the HTTP request failed. This can be
|
|
4
|
+
* due to a network error or a response that indicates
|
|
5
|
+
* failure. In the latter case, the response is part of
|
|
6
|
+
* this error.
|
|
7
|
+
*/
|
|
8
|
+
export declare class HttpRequestError extends Error {
|
|
9
|
+
readonly response?: HttpResponse;
|
|
10
|
+
constructor(message: string, response?: HttpResponse);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The response to an HTTP request.
|
|
14
|
+
*/
|
|
15
|
+
export declare class HttpResponse {
|
|
16
|
+
/**
|
|
17
|
+
* The HTTP status code.
|
|
18
|
+
*/
|
|
19
|
+
readonly status: number;
|
|
20
|
+
/**
|
|
21
|
+
* The response headers.
|
|
22
|
+
*/
|
|
23
|
+
readonly headers: Record<string, string>;
|
|
24
|
+
/**
|
|
25
|
+
* The raw response body.
|
|
26
|
+
*/
|
|
27
|
+
private readonly response;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new HTTP response.
|
|
30
|
+
* @param status - The status code.
|
|
31
|
+
* @param headers - The response headers.
|
|
32
|
+
* @param response - The raw response data.
|
|
33
|
+
*/
|
|
34
|
+
constructor(status: number, headers: Record<string, string>, response: Uint8Array);
|
|
35
|
+
/**
|
|
36
|
+
* @returns The raw response data.
|
|
37
|
+
*/
|
|
38
|
+
buffer(): Buffer;
|
|
39
|
+
/**
|
|
40
|
+
* @returns The response as a UTF-8 encoded string.
|
|
41
|
+
*/
|
|
42
|
+
text(): string;
|
|
43
|
+
/**
|
|
44
|
+
* Parses the response as JSON and according to the
|
|
45
|
+
* specified schema.
|
|
46
|
+
* @param schema - The schema. Default is y.unknown().
|
|
47
|
+
* @returns A JavaScript object.
|
|
48
|
+
*/
|
|
49
|
+
json<T extends Schema<unknown>>(schema?: T): Typeof<T>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A class for performing HTTP requests.
|
|
53
|
+
*/
|
|
54
|
+
export declare class Http {
|
|
55
|
+
private log;
|
|
56
|
+
constructor(log: Log);
|
|
57
|
+
/**
|
|
58
|
+
* Performs an HTTP GET request on the given URL.
|
|
59
|
+
* @param url - The URL.
|
|
60
|
+
* @param headers - The HTTP headers.
|
|
61
|
+
* @returns The response.
|
|
62
|
+
*/
|
|
63
|
+
get(url: string, headers?: Record<string, string>): Promise<HttpResponse>;
|
|
64
|
+
/**
|
|
65
|
+
* Performs an HTTP POST request on the given URL. The body is converted
|
|
66
|
+
* to JSON.
|
|
67
|
+
* @param url - The URL.
|
|
68
|
+
* @param body - The body.
|
|
69
|
+
* @param headers - The HTTP headers.
|
|
70
|
+
* @returns The response.
|
|
71
|
+
*/
|
|
72
|
+
post(url: string, body: unknown, headers?: Record<string, string>): Promise<HttpResponse>;
|
|
73
|
+
/**
|
|
74
|
+
* Performs an HTTP request on the given URL. The body is converted to JSON.
|
|
75
|
+
* @param method - The HTTP method.
|
|
76
|
+
* @param url - The URL.
|
|
77
|
+
* @param body - The body.
|
|
78
|
+
* @param headers - The HTTP headers.
|
|
79
|
+
* @returns The response.
|
|
80
|
+
*/
|
|
81
|
+
request(method: 'GET' | 'POST', url: string, body: unknown, headers?: Record<string, string>): Promise<HttpResponse>;
|
|
82
|
+
private getResponse;
|
|
83
|
+
}
|