@nmshd/typescript-rest 3.0.5
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/.ci/runChecks.sh +7 -0
- package/.eslintrc.js +226 -0
- package/.github/dependabot.yml +27 -0
- package/.github/workflows/publish.yml +33 -0
- package/.github/workflows/test.yml +31 -0
- package/.prettierrc +27 -0
- package/.vscode/settings.json +31 -0
- package/LICENSE +22 -0
- package/README.md +128 -0
- package/dist/decorators/methods.d.ts +165 -0
- package/dist/decorators/methods.js +263 -0
- package/dist/decorators/methods.js.map +1 -0
- package/dist/decorators/parameters.d.ts +295 -0
- package/dist/decorators/parameters.js +423 -0
- package/dist/decorators/parameters.js.map +1 -0
- package/dist/decorators/services.d.ts +199 -0
- package/dist/decorators/services.js +367 -0
- package/dist/decorators/services.js.map +1 -0
- package/dist/server/config.d.ts +4 -0
- package/dist/server/config.js +43 -0
- package/dist/server/config.js.map +1 -0
- package/dist/server/model/errors.d.ts +111 -0
- package/dist/server/model/errors.js +178 -0
- package/dist/server/model/errors.js.map +1 -0
- package/dist/server/model/metadata.d.ts +91 -0
- package/dist/server/model/metadata.js +80 -0
- package/dist/server/model/metadata.js.map +1 -0
- package/dist/server/model/return-types.d.ts +86 -0
- package/dist/server/model/return-types.js +110 -0
- package/dist/server/model/return-types.js.map +1 -0
- package/dist/server/model/server-types.d.ts +118 -0
- package/dist/server/model/server-types.js +47 -0
- package/dist/server/model/server-types.js.map +1 -0
- package/dist/server/parameter-processor.d.ts +13 -0
- package/dist/server/parameter-processor.js +84 -0
- package/dist/server/parameter-processor.js.map +1 -0
- package/dist/server/server-container.d.ts +55 -0
- package/dist/server/server-container.js +432 -0
- package/dist/server/server-container.js.map +1 -0
- package/dist/server/server.d.ts +136 -0
- package/dist/server/server.js +278 -0
- package/dist/server/server.js.map +1 -0
- package/dist/server/service-invoker.d.ts +28 -0
- package/dist/server/service-invoker.js +270 -0
- package/dist/server/service-invoker.js.map +1 -0
- package/dist/typescript-rest.d.ts +9 -0
- package/dist/typescript-rest.js +31 -0
- package/dist/typescript-rest.js.map +1 -0
- package/package.json +113 -0
- package/src/decorators/methods.ts +279 -0
- package/src/decorators/parameters.ts +442 -0
- package/src/decorators/services.ts +390 -0
- package/src/server/config.ts +40 -0
- package/src/server/model/errors.ts +178 -0
- package/src/server/model/metadata.ts +118 -0
- package/src/server/model/return-types.ts +109 -0
- package/src/server/model/server-types.ts +130 -0
- package/src/server/parameter-processor.ts +105 -0
- package/src/server/server-container.ts +504 -0
- package/src/server/server.ts +340 -0
- package/src/server/service-invoker.ts +266 -0
- package/src/typescript-rest.ts +16 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Path = Path;
|
|
4
|
+
exports.Security = Security;
|
|
5
|
+
exports.PreProcessor = PreProcessor;
|
|
6
|
+
exports.PostProcessor = PostProcessor;
|
|
7
|
+
exports.AcceptLanguage = AcceptLanguage;
|
|
8
|
+
exports.Accept = Accept;
|
|
9
|
+
exports.BodyOptions = BodyOptions;
|
|
10
|
+
exports.BodyType = BodyType;
|
|
11
|
+
exports.IgnoreNextMiddlewares = IgnoreNextMiddlewares;
|
|
12
|
+
exports.Abstract = Abstract;
|
|
13
|
+
const _ = require("lodash");
|
|
14
|
+
require("reflect-metadata");
|
|
15
|
+
const server_container_1 = require("../server/server-container");
|
|
16
|
+
/**
|
|
17
|
+
* A decorator to tell the [[Server]] that a class or a method
|
|
18
|
+
* should be bound to a given path.
|
|
19
|
+
*
|
|
20
|
+
* For example:
|
|
21
|
+
*
|
|
22
|
+
* ```
|
|
23
|
+
* @ Path('people')
|
|
24
|
+
* class PeopleService {
|
|
25
|
+
* @ PUT
|
|
26
|
+
* @ Path(':id')
|
|
27
|
+
* savePerson(person:Person) {
|
|
28
|
+
* // ...
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* @ GET
|
|
32
|
+
* @ Path(':id')
|
|
33
|
+
* getPerson():Person {
|
|
34
|
+
* // ...
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* Will create services that listen for requests like:
|
|
40
|
+
*
|
|
41
|
+
* ```
|
|
42
|
+
* PUT http://mydomain/people/123 or
|
|
43
|
+
* GET http://mydomain/people/123
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
function Path(path) {
|
|
47
|
+
return new ServiceDecorator('Path').withProperty('path', path).createDecorator();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A decorator to tell the [[Server]] that a class or a method
|
|
51
|
+
* should include a determined role
|
|
52
|
+
* or all authorized users (token) using passport
|
|
53
|
+
*
|
|
54
|
+
* For example:
|
|
55
|
+
*
|
|
56
|
+
* ```
|
|
57
|
+
* @ Path('people')
|
|
58
|
+
* @ Security()
|
|
59
|
+
* class PeopleService {
|
|
60
|
+
* @ PUT
|
|
61
|
+
* @ Path(':id', true)
|
|
62
|
+
* @ Security(['ROLE_ADMIN'])
|
|
63
|
+
* savePerson(person:Person) {
|
|
64
|
+
* // ...
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* @ GET
|
|
68
|
+
* @ Path(':id', true)
|
|
69
|
+
* getPerson():Person {
|
|
70
|
+
* // ...
|
|
71
|
+
* }
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* Will create services that listen for requests like:
|
|
76
|
+
*
|
|
77
|
+
* ```
|
|
78
|
+
* PUT http://mydomain/people/123 (Only for ADMIN roles) or
|
|
79
|
+
* GET http://mydomain/people/123 (For all authorized users)
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
function Security(roles, name) {
|
|
83
|
+
roles = _.castArray(roles || '*');
|
|
84
|
+
return new SecurityServiceDecorator('Security')
|
|
85
|
+
.withObjectProperty('authenticator', name || 'default', roles)
|
|
86
|
+
.createDecorator();
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A decorator to tell the [[Server]] that a class or a method
|
|
90
|
+
* should include a pre-processor in its request pipelines.
|
|
91
|
+
*
|
|
92
|
+
* For example:
|
|
93
|
+
* ```
|
|
94
|
+
* function validator(req: express.Request): express.Request {
|
|
95
|
+
* if (!req.body.userId) {
|
|
96
|
+
* throw new Errors.BadRequestError("userId not present");
|
|
97
|
+
* }
|
|
98
|
+
* }
|
|
99
|
+
* ```
|
|
100
|
+
* And:
|
|
101
|
+
*
|
|
102
|
+
* ```
|
|
103
|
+
* @ Path('people')
|
|
104
|
+
* class PeopleService {
|
|
105
|
+
* @ PUT
|
|
106
|
+
* @ Path(':id')
|
|
107
|
+
* @ PreProcessor(validator)
|
|
108
|
+
* savePerson(person:Person) {
|
|
109
|
+
* // ...
|
|
110
|
+
* }
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
function PreProcessor(preprocessor) {
|
|
115
|
+
return new ProcessorServiceDecorator('PreProcessor')
|
|
116
|
+
.withArrayProperty('preProcessors', preprocessor, true)
|
|
117
|
+
.createDecorator();
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* A decorator to tell the [[Server]] that a class or a method
|
|
121
|
+
* should include a post-processor in its request pipelines.
|
|
122
|
+
*
|
|
123
|
+
* For example:
|
|
124
|
+
* ```
|
|
125
|
+
* function processor(req: express.Request): express.Request {
|
|
126
|
+
* if (!req.body.userId) {
|
|
127
|
+
* throw new Errors.BadRequestError("userId not present");
|
|
128
|
+
* }
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
* And:
|
|
132
|
+
*
|
|
133
|
+
* ```
|
|
134
|
+
* @ Path('people')
|
|
135
|
+
* class PeopleService {
|
|
136
|
+
* @ PUT
|
|
137
|
+
* @ Path(':id')
|
|
138
|
+
* @ PostProcessor(validator)
|
|
139
|
+
* savePerson(person:Person) {
|
|
140
|
+
* // ...
|
|
141
|
+
* }
|
|
142
|
+
* }
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
function PostProcessor(postprocessor) {
|
|
146
|
+
return new ProcessorServiceDecorator('PostProcessor')
|
|
147
|
+
.withArrayProperty('postProcessors', postprocessor, true)
|
|
148
|
+
.createDecorator();
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* A decorator to tell the [[Server]] that a class or a method
|
|
152
|
+
* should only accept requests from clients that accepts one of
|
|
153
|
+
* the supported languages.
|
|
154
|
+
*
|
|
155
|
+
* For example:
|
|
156
|
+
*
|
|
157
|
+
* ```
|
|
158
|
+
* @ Path('accept')
|
|
159
|
+
* @ AcceptLanguage('en', 'pt-BR')
|
|
160
|
+
* class TestAcceptService {
|
|
161
|
+
* // ...
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* Will reject requests that only accepts languages that are not
|
|
166
|
+
* English or Brazilian portuguese
|
|
167
|
+
*
|
|
168
|
+
* If the language requested is not supported, a status code 406 returned
|
|
169
|
+
*/
|
|
170
|
+
function AcceptLanguage(...languages) {
|
|
171
|
+
languages = _.compact(languages);
|
|
172
|
+
return new AcceptServiceDecorator('AcceptLanguage')
|
|
173
|
+
.withArrayProperty('languages', languages, true)
|
|
174
|
+
.createDecorator();
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* A decorator to tell the [[Server]] that a class or a method
|
|
178
|
+
* should only accept requests from clients that accepts one of
|
|
179
|
+
* the supported mime types.
|
|
180
|
+
*
|
|
181
|
+
* For example:
|
|
182
|
+
*
|
|
183
|
+
* ```
|
|
184
|
+
* @ Path('accept')
|
|
185
|
+
* @ Accept('application/json')
|
|
186
|
+
* class TestAcceptService {
|
|
187
|
+
* // ...
|
|
188
|
+
* }
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* Will reject requests that only accepts mime types that are not
|
|
192
|
+
* 'application/json'
|
|
193
|
+
*
|
|
194
|
+
* If the mime type requested is not supported, a status code 406 returned
|
|
195
|
+
*/
|
|
196
|
+
function Accept(...accepts) {
|
|
197
|
+
accepts = _.compact(accepts);
|
|
198
|
+
return new AcceptServiceDecorator('Accept').withArrayProperty('accepts', accepts, true).createDecorator();
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* A decorator to inform options to pe passed to bodyParser.
|
|
202
|
+
* You can inform any property accepted by
|
|
203
|
+
* [[bodyParser]](https://www.npmjs.com/package/body-parser)
|
|
204
|
+
*/
|
|
205
|
+
function BodyOptions(options) {
|
|
206
|
+
return new ServiceDecorator('BodyOptions').withProperty('bodyParserOptions', options).createDecorator();
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* A decorator to inform the type of parser to be used to parse the body.
|
|
210
|
+
* The default type is json.
|
|
211
|
+
*/
|
|
212
|
+
function BodyType(type) {
|
|
213
|
+
return new ServiceDecorator('BodyType').withProperty('bodyParserType', type).createDecorator();
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* A decorator to inform that server should ignore other middlewares.
|
|
217
|
+
* It makes server does not call next function after service invocation.
|
|
218
|
+
*/
|
|
219
|
+
function IgnoreNextMiddlewares(...args) {
|
|
220
|
+
return new ServiceDecorator('IgnoreNextMiddlewares')
|
|
221
|
+
.withProperty('ignoreNextMiddlewares', true)
|
|
222
|
+
.decorateTypeOrMethod(args);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Mark the annotated service class as an abstract service. Abstract services has none of its
|
|
226
|
+
* methods exposed as rest enpoints, even if the class is in the services list to be exposed.
|
|
227
|
+
*
|
|
228
|
+
* For example:
|
|
229
|
+
*
|
|
230
|
+
* ```
|
|
231
|
+
* @ Abstract
|
|
232
|
+
* abstract class PeopleService {
|
|
233
|
+
* @ GET
|
|
234
|
+
* getPeople(@ Param('name') name: string) {
|
|
235
|
+
* // ...
|
|
236
|
+
* }
|
|
237
|
+
* }
|
|
238
|
+
* ```
|
|
239
|
+
*
|
|
240
|
+
* No endpoint will be registered for PeopleService. It is useful if you only plain that subclasses of
|
|
241
|
+
* PeopleService exposes the getPeople method.
|
|
242
|
+
*/
|
|
243
|
+
function Abstract(...args) {
|
|
244
|
+
args = _.without(args, undefined);
|
|
245
|
+
if (args.length === 1) {
|
|
246
|
+
const classData = server_container_1.ServerContainer.get().registerServiceClass(args[0]);
|
|
247
|
+
classData.isAbstract = true;
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
throw new Error('Invalid @Abstract Decorator declaration.');
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
class ServiceDecorator {
|
|
254
|
+
constructor(decorator) {
|
|
255
|
+
this.properties = [];
|
|
256
|
+
this.decorator = decorator;
|
|
257
|
+
}
|
|
258
|
+
withProperty(property, value, required = false) {
|
|
259
|
+
this.properties.push({
|
|
260
|
+
checkRequired: () => required && !value,
|
|
261
|
+
process: (target) => {
|
|
262
|
+
target[property] = value;
|
|
263
|
+
},
|
|
264
|
+
property: property,
|
|
265
|
+
required: required,
|
|
266
|
+
value: value
|
|
267
|
+
});
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
createDecorator() {
|
|
271
|
+
return (...args) => {
|
|
272
|
+
this.checkRequiredValue();
|
|
273
|
+
this.decorateTypeOrMethod(args);
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
decorateTypeOrMethod(args) {
|
|
277
|
+
args = _.without(args, undefined);
|
|
278
|
+
if (args.length === 1) {
|
|
279
|
+
this.decorateType(args[0]);
|
|
280
|
+
}
|
|
281
|
+
else if (args.length === 3 && typeof args[2] === 'object') {
|
|
282
|
+
this.decorateMethod(args[0], args[1]);
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
throw new Error(`Invalid @${this.decorator} Decorator declaration.`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
checkRequiredValue() {
|
|
289
|
+
this.properties.forEach((property) => {
|
|
290
|
+
if (property.checkRequired()) {
|
|
291
|
+
throw new Error(`Invalid @${this.decorator} Decorator declaration.`);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
decorateType(target) {
|
|
296
|
+
const classData = server_container_1.ServerContainer.get().registerServiceClass(target);
|
|
297
|
+
if (classData) {
|
|
298
|
+
this.updateClassMetadata(classData);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
decorateMethod(target, propertyKey) {
|
|
302
|
+
const serviceMethod = server_container_1.ServerContainer.get().registerServiceMethod(target.constructor, propertyKey);
|
|
303
|
+
if (serviceMethod) {
|
|
304
|
+
// does not intercept constructor
|
|
305
|
+
this.updateMethodMetadada(serviceMethod);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
updateClassMetadata(classData) {
|
|
309
|
+
this.properties.forEach((property) => {
|
|
310
|
+
property.process(classData);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
updateMethodMetadada(serviceMethod) {
|
|
314
|
+
this.properties.forEach((property) => {
|
|
315
|
+
property.process(serviceMethod);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
class SecurityServiceDecorator extends ServiceDecorator {
|
|
320
|
+
withObjectProperty(property, subtext, value, required = false) {
|
|
321
|
+
this.properties.push({
|
|
322
|
+
checkRequired: () => required && !value,
|
|
323
|
+
process: (target) => {
|
|
324
|
+
if (!target[property]) {
|
|
325
|
+
target[property] = {};
|
|
326
|
+
}
|
|
327
|
+
target[property][subtext] = value;
|
|
328
|
+
},
|
|
329
|
+
property: property,
|
|
330
|
+
required: required,
|
|
331
|
+
value: value
|
|
332
|
+
});
|
|
333
|
+
return this;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
class ProcessorServiceDecorator extends ServiceDecorator {
|
|
337
|
+
withArrayProperty(property, value, required = false) {
|
|
338
|
+
this.properties.push({
|
|
339
|
+
checkRequired: () => required && !value,
|
|
340
|
+
process: (target) => {
|
|
341
|
+
if (!target[property]) {
|
|
342
|
+
target[property] = [];
|
|
343
|
+
}
|
|
344
|
+
target[property].unshift(value);
|
|
345
|
+
},
|
|
346
|
+
property: property,
|
|
347
|
+
required: required,
|
|
348
|
+
value: value
|
|
349
|
+
});
|
|
350
|
+
return this;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
class AcceptServiceDecorator extends ServiceDecorator {
|
|
354
|
+
withArrayProperty(property, value, required = false) {
|
|
355
|
+
this.properties.push({
|
|
356
|
+
checkRequired: () => required && (!value || !value.length),
|
|
357
|
+
process: (target) => {
|
|
358
|
+
target[property] = _.union(target[property], value);
|
|
359
|
+
},
|
|
360
|
+
property: property,
|
|
361
|
+
required: required,
|
|
362
|
+
value: value
|
|
363
|
+
});
|
|
364
|
+
return this;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
//# sourceMappingURL=services.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"services.js","sourceRoot":"","sources":["../../src/decorators/services.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAsCb,oBAEC;AAmCD,4BAKC;AA4BD,oCAIC;AA4BD,sCAIC;AAsBD,wCAKC;AAsBD,wBAGC;AAOD,kCAEC;AAMD,4BAEC;AAMD,sDAIC;AAqBD,4BAQC;AA1PD,4BAA4B;AAC5B,4BAA0B;AAG1B,iEAA6D;AAE7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,SAAgB,IAAI,CAAC,IAAY;IAC7B,OAAO,IAAI,gBAAgB,CAAC,MAAM,CAAC,CAAC,YAAY,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,eAAe,EAAE,CAAC;AACrF,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,SAAgB,QAAQ,CAAC,KAA8B,EAAE,IAAa;IAClE,KAAK,GAAG,CAAC,CAAC,SAAS,CAAC,KAAK,IAAI,GAAG,CAAC,CAAC;IAClC,OAAO,IAAI,wBAAwB,CAAC,UAAU,CAAC;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
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServerConfig = void 0;
|
|
4
|
+
const debug = require("debug");
|
|
5
|
+
const fs = require("fs-extra");
|
|
6
|
+
const path = require("path");
|
|
7
|
+
const server_1 = require("./server");
|
|
8
|
+
const serverDebugger = debug('typescript-rest:server:config:build');
|
|
9
|
+
class ServerConfig {
|
|
10
|
+
static configure() {
|
|
11
|
+
try {
|
|
12
|
+
const CONFIG_FILE = this.searchConfigFile();
|
|
13
|
+
if (CONFIG_FILE && fs.existsSync(CONFIG_FILE)) {
|
|
14
|
+
const config = fs.readJSONSync(CONFIG_FILE);
|
|
15
|
+
serverDebugger('rest.config file found: %j', config);
|
|
16
|
+
if (config.serviceFactory) {
|
|
17
|
+
if (config.serviceFactory.indexOf('.') === 0) {
|
|
18
|
+
config.serviceFactory = path.join(process.cwd(), config.serviceFactory);
|
|
19
|
+
}
|
|
20
|
+
server_1.Server.registerServiceFactory(config.serviceFactory);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
// eslint-disable-next-line no-console
|
|
26
|
+
console.error(e);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
static searchConfigFile() {
|
|
30
|
+
serverDebugger('Searching for rest.config file');
|
|
31
|
+
let configFile = path.join(__dirname, 'rest.config');
|
|
32
|
+
while (!fs.existsSync(configFile)) {
|
|
33
|
+
const fileOnParent = path.normalize(path.join(path.dirname(configFile), '..', 'rest.config'));
|
|
34
|
+
if (configFile === fileOnParent) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
configFile = fileOnParent;
|
|
38
|
+
}
|
|
39
|
+
return configFile;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.ServerConfig = ServerConfig;
|
|
43
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","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I,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC;YAC9F,IAAI,UAAU,KAAK,YAAY,EAAE,CAAC;gBAC9B,OAAO,IAAI,CAAC;YAChB,CAAC;YACD,UAAU,GAAG,YAAY,CAAC;QAC9B,CAAC;QACD,OAAO,UAAU,CAAC;IACtB,CAAC;CACJ;AAhCD,oCAgCC"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Base class for all HTTP errors
|
|
3
|
+
*/
|
|
4
|
+
export declare abstract class HttpError extends Error {
|
|
5
|
+
message: string;
|
|
6
|
+
statusCode: number;
|
|
7
|
+
constructor(name: string, message: string);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Represents a BAD REQUEST error. The request could not be understood by the
|
|
11
|
+
* server due to malformed syntax. The client SHOULD NOT repeat the request
|
|
12
|
+
* without modifications.
|
|
13
|
+
*/
|
|
14
|
+
export declare class BadRequestError extends HttpError {
|
|
15
|
+
constructor(message?: string);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Represents an UNAUTHORIZED error. The request requires user authentication. The response
|
|
19
|
+
* MUST include a WWW-Authenticate header field containing a challenge applicable to the
|
|
20
|
+
* requested resource.
|
|
21
|
+
*/
|
|
22
|
+
export declare class UnauthorizedError extends HttpError {
|
|
23
|
+
constructor(message?: string);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Represents a FORBIDDEN error. The server understood the request, but is refusing to
|
|
27
|
+
* fulfill it. Authorization will not help and the request SHOULD NOT be repeated.
|
|
28
|
+
*/
|
|
29
|
+
export declare class ForbiddenError extends HttpError {
|
|
30
|
+
constructor(message?: string);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Represents a NOT FOUND error. The server has not found anything matching
|
|
34
|
+
* the Request-URI. No indication is given of whether the condition is temporary
|
|
35
|
+
* or permanent. The 410 (GoneError) status code SHOULD be used if the server knows,
|
|
36
|
+
* through some internally configurable mechanism, that an old resource is permanently
|
|
37
|
+
* unavailable and has no forwarding address.
|
|
38
|
+
*
|
|
39
|
+
* This error is commonly used when
|
|
40
|
+
* the server does not wish to reveal exactly why the request has been refused,
|
|
41
|
+
* or when no other response is applicable.
|
|
42
|
+
*/
|
|
43
|
+
export declare class NotFoundError extends HttpError {
|
|
44
|
+
constructor(message?: string);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Represents a METHOD NOT ALLOWED error. The method specified in the Request-Line is not allowed for
|
|
48
|
+
* the resource identified by the Request-URI. The response MUST include an Allow header
|
|
49
|
+
* containing a list of valid methods for the requested resource.
|
|
50
|
+
*/
|
|
51
|
+
export declare class MethodNotAllowedError extends HttpError {
|
|
52
|
+
constructor(message?: string);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Represents a NOT ACCEPTABLE error. The resource identified by the request is only capable of
|
|
56
|
+
* generating response entities which have content characteristics not acceptable according
|
|
57
|
+
* to the accept headers sent in the request.
|
|
58
|
+
*/
|
|
59
|
+
export declare class NotAcceptableError extends HttpError {
|
|
60
|
+
constructor(message?: string);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Represents a CONFLICT error. The request could not be completed due to a
|
|
64
|
+
* conflict with the current state of the resource.
|
|
65
|
+
*/
|
|
66
|
+
export declare class ConflictError extends HttpError {
|
|
67
|
+
constructor(message?: string);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Represents a GONE error. The requested resource is no longer available at the server
|
|
71
|
+
* and no forwarding address is known. This condition is expected to be considered
|
|
72
|
+
* permanent. Clients with link editing capabilities SHOULD delete references to
|
|
73
|
+
* the Request-URI after user approval. If the server does not know, or has
|
|
74
|
+
* no facility to determine, whether or not the condition is permanent, the
|
|
75
|
+
* error 404 (NotFoundError) SHOULD be used instead. This response is
|
|
76
|
+
* cacheable unless indicated otherwise.
|
|
77
|
+
*/
|
|
78
|
+
export declare class GoneError extends HttpError {
|
|
79
|
+
constructor(message?: string);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Represents an UNSUPPORTED MEDIA TYPE error. The server is refusing to service the request
|
|
83
|
+
* because the entity of the request is in a format not supported by the requested resource
|
|
84
|
+
* for the requested method.
|
|
85
|
+
*/
|
|
86
|
+
export declare class UnsupportedMediaTypeError extends HttpError {
|
|
87
|
+
constructor(message?: string);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Represents a UNPROCESSABLE ENTITY error. The server understands the content type of the request entity
|
|
91
|
+
* (hence a 415 Unsupported Media Type status code is inappropriate), and the syntax of the request entity is correct
|
|
92
|
+
* (thus a 400 Bad Request status code is inappropriate) but was unable to process the contained instructions.
|
|
93
|
+
*/
|
|
94
|
+
export declare class UnprocessableEntityError extends HttpError {
|
|
95
|
+
constructor(message?: string);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Represents an INTERNAL SERVER error. The server encountered an unexpected condition
|
|
99
|
+
* which prevented it from fulfilling the request.
|
|
100
|
+
*/
|
|
101
|
+
export declare class InternalServerError extends HttpError {
|
|
102
|
+
constructor(message?: string);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Represents a NOT IMPLEMENTED error. The server does not support the functionality required
|
|
106
|
+
* to fulfill the request. This is the appropriate response when the server does not recognize
|
|
107
|
+
* the request method and is not capable of supporting it for any resource.
|
|
108
|
+
*/
|
|
109
|
+
export declare class NotImplementedError extends HttpError {
|
|
110
|
+
constructor(message?: string);
|
|
111
|
+
}
|