@dbos-inc/koa-serve 2.11.6-preview.gcb74958171 → 2.11.7-preview.gc208400219
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/src/dboshttp.d.ts +13 -0
- package/dist/src/dboshttp.d.ts.map +1 -1
- package/dist/src/dboshttp.js +28 -0
- package/dist/src/dboshttp.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/dboshttp.ts +53 -0
- package/tests/argsource.test.ts +2 -2
- package/tests/endpoints.test.ts +2 -2
- package/tests/validation.test.ts +16 -24
package/package.json
CHANGED
package/src/dboshttp.ts
CHANGED
|
@@ -8,6 +8,13 @@ import {
|
|
|
8
8
|
Error as DBOSErrors,
|
|
9
9
|
MethodParameter,
|
|
10
10
|
requestArgValidation,
|
|
11
|
+
ArgRequired,
|
|
12
|
+
ArgOptional,
|
|
13
|
+
DefaultArgRequired,
|
|
14
|
+
DefaultArgValidate,
|
|
15
|
+
DefaultArgOptional,
|
|
16
|
+
ArgDate,
|
|
17
|
+
ArgVarchar,
|
|
11
18
|
} from '@dbos-inc/dbos-sdk';
|
|
12
19
|
|
|
13
20
|
export enum APITypes {
|
|
@@ -43,6 +50,25 @@ export interface DBOSHTTPArgInfo {
|
|
|
43
50
|
argSource?: ArgSources;
|
|
44
51
|
}
|
|
45
52
|
|
|
53
|
+
enum ArgRequiredOptions {
|
|
54
|
+
REQUIRED = 'REQUIRED',
|
|
55
|
+
OPTIONAL = 'OPTIONAL',
|
|
56
|
+
DEFAULT = 'DEFAULT',
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface ValidatorClassInfo {
|
|
60
|
+
defaultArgRequired?: ArgRequiredOptions;
|
|
61
|
+
defaultArgValidate?: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface ValidatorFuncInfo {
|
|
65
|
+
performArgValidation?: boolean;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface ValidatorArgInfo {
|
|
69
|
+
required?: ArgRequiredOptions;
|
|
70
|
+
}
|
|
71
|
+
|
|
46
72
|
/**
|
|
47
73
|
* HTTPRequest includes useful information from http.IncomingMessage and parsed body,
|
|
48
74
|
* URL parameters, and parsed query string.
|
|
@@ -81,6 +107,8 @@ export function isClientRequestError(e: Error) {
|
|
|
81
107
|
return DBOSErrors.isDataValidationError(e);
|
|
82
108
|
}
|
|
83
109
|
|
|
110
|
+
const VALIDATOR = 'validator';
|
|
111
|
+
|
|
84
112
|
export class DBOSHTTPBase extends DBOSLifecycleCallback {
|
|
85
113
|
static HTTP_OPERATION_TYPE: string = 'http';
|
|
86
114
|
|
|
@@ -182,4 +210,29 @@ export class DBOSHTTPBase extends DBOSLifecycleCallback {
|
|
|
182
210
|
}
|
|
183
211
|
}
|
|
184
212
|
}
|
|
213
|
+
|
|
214
|
+
static argRequired(target: object, propertyKey: string | symbol, parameterIndex: number) {
|
|
215
|
+
ArgRequired(target, propertyKey, parameterIndex);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
static argOptional(target: object, propertyKey: string | symbol, parameterIndex: number) {
|
|
219
|
+
ArgOptional(target, propertyKey, parameterIndex);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
static argDate() {
|
|
223
|
+
return ArgDate();
|
|
224
|
+
}
|
|
225
|
+
static argVarchar(n: number) {
|
|
226
|
+
return ArgVarchar(n);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
static defaultArgRequired<T extends { new (...args: unknown[]): object }>(ctor: T) {
|
|
230
|
+
return DefaultArgRequired(ctor);
|
|
231
|
+
}
|
|
232
|
+
static defaultArgOptional<T extends { new (...args: unknown[]): object }>(ctor: T) {
|
|
233
|
+
return DefaultArgOptional(ctor);
|
|
234
|
+
}
|
|
235
|
+
static defaultArgValidate<T extends { new (...args: unknown[]): object }>(ctor: T) {
|
|
236
|
+
return DefaultArgValidate(ctor);
|
|
237
|
+
}
|
|
185
238
|
}
|
package/tests/argsource.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Koa from 'koa';
|
|
2
2
|
import Router from '@koa/router';
|
|
3
3
|
|
|
4
|
-
import { DBOS
|
|
4
|
+
import { DBOS } from '@dbos-inc/dbos-sdk';
|
|
5
5
|
|
|
6
6
|
import { ArgSources, DBOSKoa } from '../src';
|
|
7
7
|
|
|
@@ -108,7 +108,7 @@ describe('httpserver-argsource-tests', () => {
|
|
|
108
108
|
parsedMethods: ['GET', 'POST'],
|
|
109
109
|
}),
|
|
110
110
|
)
|
|
111
|
-
@
|
|
111
|
+
@DBOSKoa.defaultArgRequired
|
|
112
112
|
class ArgTestEndpoints {
|
|
113
113
|
@dhttp.getApi('/getquery')
|
|
114
114
|
static async getQuery(@dhttp.argSource(ArgSources.QUERY) name: string) {
|
package/tests/endpoints.test.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import Koa from 'koa';
|
|
3
3
|
import Router from '@koa/router';
|
|
4
4
|
|
|
5
|
-
import { DBOS, DBOSResponseError, Error as DBOSErrors,
|
|
5
|
+
import { DBOS, DBOSResponseError, Error as DBOSErrors, StatusString } from '@dbos-inc/dbos-sdk';
|
|
6
6
|
|
|
7
7
|
import { DBOSKoa, DBOSKoaAuthContext, RequestIDHeader, WorkflowIDHeader } from '../src';
|
|
8
8
|
|
|
@@ -351,7 +351,7 @@ describe('httpserver-tests', () => {
|
|
|
351
351
|
parsedMethods: ['POST', 'PUT', 'PATCH', 'GET', 'DELETE'],
|
|
352
352
|
}),
|
|
353
353
|
)
|
|
354
|
-
@
|
|
354
|
+
@DBOSKoa.defaultArgRequired
|
|
355
355
|
class TestEndpoints {
|
|
356
356
|
@dhttp.getApi('/hello')
|
|
357
357
|
static async hello() {
|
package/tests/validation.test.ts
CHANGED
|
@@ -2,15 +2,7 @@
|
|
|
2
2
|
import Koa from 'koa';
|
|
3
3
|
import Router from '@koa/router';
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
ArgDate,
|
|
7
|
-
ArgOptional,
|
|
8
|
-
ArgRequired,
|
|
9
|
-
ArgVarchar,
|
|
10
|
-
DBOS,
|
|
11
|
-
DefaultArgOptional,
|
|
12
|
-
DefaultArgRequired,
|
|
13
|
-
} from '@dbos-inc/dbos-sdk';
|
|
5
|
+
import { DBOS } from '@dbos-inc/dbos-sdk';
|
|
14
6
|
|
|
15
7
|
import { DBOSKoa } from '../src';
|
|
16
8
|
|
|
@@ -346,7 +338,7 @@ describe('httpserver-datavalidation-tests', () => {
|
|
|
346
338
|
}
|
|
347
339
|
});
|
|
348
340
|
|
|
349
|
-
@
|
|
341
|
+
@DBOSKoa.defaultArgRequired
|
|
350
342
|
class TestEndpointDataVal {
|
|
351
343
|
@dhttp.getApi('/hello')
|
|
352
344
|
static async hello() {
|
|
@@ -370,7 +362,7 @@ describe('httpserver-datavalidation-tests', () => {
|
|
|
370
362
|
}
|
|
371
363
|
|
|
372
364
|
@dhttp.getApi('/varchar')
|
|
373
|
-
static async checkVarcharG(@
|
|
365
|
+
static async checkVarcharG(@DBOSKoa.argVarchar(10) v: string) {
|
|
374
366
|
if (typeof v !== 'string') {
|
|
375
367
|
return Promise.reject(new Error('THIS SHOULD NEVER HAPPEN'));
|
|
376
368
|
}
|
|
@@ -378,7 +370,7 @@ describe('httpserver-datavalidation-tests', () => {
|
|
|
378
370
|
}
|
|
379
371
|
|
|
380
372
|
@dhttp.postApi('/varchar')
|
|
381
|
-
static async checkVarcharP(@
|
|
373
|
+
static async checkVarcharP(@DBOSKoa.argVarchar(10) v: string) {
|
|
382
374
|
if (typeof v !== 'string') {
|
|
383
375
|
return Promise.reject(new Error('THIS SHOULD NEVER HAPPEN'));
|
|
384
376
|
}
|
|
@@ -418,7 +410,7 @@ describe('httpserver-datavalidation-tests', () => {
|
|
|
418
410
|
}
|
|
419
411
|
|
|
420
412
|
@dhttp.getApi('/date')
|
|
421
|
-
static async checkDateG(@
|
|
413
|
+
static async checkDateG(@DBOSKoa.argDate() v: Date) {
|
|
422
414
|
if (!(v instanceof Date)) {
|
|
423
415
|
return Promise.reject(new Error('THIS SHOULD NEVER HAPPEN'));
|
|
424
416
|
}
|
|
@@ -426,7 +418,7 @@ describe('httpserver-datavalidation-tests', () => {
|
|
|
426
418
|
}
|
|
427
419
|
|
|
428
420
|
@dhttp.postApi('/date')
|
|
429
|
-
static async checkDateP(@
|
|
421
|
+
static async checkDateP(@DBOSKoa.argDate() v: Date) {
|
|
430
422
|
if (!(v instanceof Date)) {
|
|
431
423
|
return Promise.reject(new Error('THIS SHOULD NEVER HAPPEN'));
|
|
432
424
|
}
|
|
@@ -457,15 +449,15 @@ describe('httpserver-datavalidation-tests', () => {
|
|
|
457
449
|
// JSON
|
|
458
450
|
}
|
|
459
451
|
|
|
460
|
-
@
|
|
452
|
+
@DBOSKoa.defaultArgRequired
|
|
461
453
|
class DefaultArgToRequired {
|
|
462
454
|
@dhttp.postApi('/rrequired')
|
|
463
|
-
static async checkReqValueR(@
|
|
455
|
+
static async checkReqValueR(@DBOSKoa.argRequired v: string) {
|
|
464
456
|
return Promise.resolve({ message: `Got string ${v}` });
|
|
465
457
|
}
|
|
466
458
|
|
|
467
459
|
@dhttp.postApi('/roptional')
|
|
468
|
-
static async checkOptValueR(@
|
|
460
|
+
static async checkOptValueR(@DBOSKoa.argOptional v?: string) {
|
|
469
461
|
return Promise.resolve({ message: `Got string ${v}` });
|
|
470
462
|
}
|
|
471
463
|
|
|
@@ -475,15 +467,15 @@ describe('httpserver-datavalidation-tests', () => {
|
|
|
475
467
|
}
|
|
476
468
|
}
|
|
477
469
|
|
|
478
|
-
@
|
|
470
|
+
@DBOSKoa.defaultArgOptional
|
|
479
471
|
class DefaultArgToOptional {
|
|
480
472
|
@dhttp.postApi('/orequired')
|
|
481
|
-
static async checkReqValueO(@
|
|
473
|
+
static async checkReqValueO(@DBOSKoa.argRequired v: string) {
|
|
482
474
|
return Promise.resolve({ message: `Got string ${v}` });
|
|
483
475
|
}
|
|
484
476
|
|
|
485
477
|
@dhttp.postApi('/ooptional')
|
|
486
|
-
static async checkOptValueO(@
|
|
478
|
+
static async checkOptValueO(@DBOSKoa.argOptional v?: string) {
|
|
487
479
|
return Promise.resolve({ message: `Got string ${v}` });
|
|
488
480
|
}
|
|
489
481
|
|
|
@@ -495,12 +487,12 @@ describe('httpserver-datavalidation-tests', () => {
|
|
|
495
487
|
|
|
496
488
|
class DefaultArgToDefault {
|
|
497
489
|
@dhttp.postApi('/drequired')
|
|
498
|
-
static async checkReqValueD(@
|
|
490
|
+
static async checkReqValueD(@DBOSKoa.argRequired v: string) {
|
|
499
491
|
return Promise.resolve({ message: `Got string ${v}` });
|
|
500
492
|
}
|
|
501
493
|
|
|
502
494
|
@dhttp.postApi('/doptional')
|
|
503
|
-
static async checkOptValueD(@
|
|
495
|
+
static async checkOptValueD(@DBOSKoa.argOptional v?: string) {
|
|
504
496
|
return Promise.resolve({ message: `Got string ${v}` });
|
|
505
497
|
}
|
|
506
498
|
|
|
@@ -510,12 +502,12 @@ describe('httpserver-datavalidation-tests', () => {
|
|
|
510
502
|
}
|
|
511
503
|
|
|
512
504
|
@DBOS.workflow()
|
|
513
|
-
static async opworkflow(@
|
|
505
|
+
static async opworkflow(@DBOSKoa.argOptional v?: string) {
|
|
514
506
|
return Promise.resolve({ message: v });
|
|
515
507
|
}
|
|
516
508
|
|
|
517
509
|
@dhttp.postApi('/doworkflow')
|
|
518
|
-
static async doWorkflow(@
|
|
510
|
+
static async doWorkflow(@DBOSKoa.argOptional v?: string) {
|
|
519
511
|
return await DefaultArgToDefault.opworkflow(v);
|
|
520
512
|
}
|
|
521
513
|
}
|