@dbos-inc/koa-serve 3.0.29-preview → 3.0.35-preview

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dbos-inc/koa-serve",
3
- "version": "3.0.29-preview",
3
+ "version": "3.0.35-preview",
4
4
  "description": "DBOS HTTP Package for serving workflows with Koa",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/dboshttp.ts CHANGED
@@ -150,7 +150,7 @@ export class DBOSHTTPBase implements DBOSLifecycleCallback {
150
150
 
151
151
  /** Parameter decorator indicating which source to use (URL, BODY, etc) for arg data */
152
152
  static argSource(source: ArgSources) {
153
- return function (target: object, propertyKey: string | symbol, parameterIndex: number) {
153
+ return function (target: object, propertyKey: PropertyKey, parameterIndex: number) {
154
154
  const curParam = DBOS.associateParamWithInfo(
155
155
  DBOSHTTP,
156
156
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
@@ -190,11 +190,11 @@ export class DBOSHTTPBase implements DBOSLifecycleCallback {
190
190
  }
191
191
  }
192
192
 
193
- static argRequired(target: object, propertyKey: string | symbol, parameterIndex: number) {
193
+ static argRequired(target: object, propertyKey: PropertyKey, parameterIndex: number) {
194
194
  ArgRequired(target, propertyKey, parameterIndex);
195
195
  }
196
196
 
197
- static argOptional(target: object, propertyKey: string | symbol, parameterIndex: number) {
197
+ static argOptional(target: object, propertyKey: PropertyKey, parameterIndex: number) {
198
198
  ArgOptional(target, propertyKey, parameterIndex);
199
199
  }
200
200
 
package/src/dboskoa.ts CHANGED
@@ -72,6 +72,11 @@ function assertCurrentKoaContextStore(): DBOSKoaLocalCtx {
72
72
  }
73
73
 
74
74
  export class DBOSKoa extends DBOSHTTPBase {
75
+ constructor() {
76
+ super();
77
+ DBOS.registerLifecycleCallback(this);
78
+ }
79
+
75
80
  static get koaContext(): Koa.Context {
76
81
  return assertCurrentKoaContextStore().koaCtxt;
77
82
  }
@@ -23,7 +23,6 @@ describe('httpserver-argsource-tests', () => {
23
23
  });
24
24
 
25
25
  beforeEach(async () => {
26
- DBOS.registerLifecycleCallback(dhttp);
27
26
  const _classes = [ArgTestEndpoints];
28
27
  await DBOS.launch();
29
28
  app = new Koa();
@@ -29,7 +29,6 @@ describe('httpserver-defsec-tests', () => {
29
29
  });
30
30
 
31
31
  beforeEach(async () => {
32
- DBOS.registerLifecycleCallback(dhttp);
33
32
  const _classes = [TestEndpointDefSec, SecondClass];
34
33
  await DBOS.launch();
35
34
  await DBOS.queryUserDB(`DROP TABLE IF EXISTS ${testTableName};`);
@@ -50,7 +50,6 @@ describe('decoratorless-api-tests', () => {
50
50
 
51
51
  beforeEach(async () => {
52
52
  middlewareCounter = middlewareCounter2 = middlewareCounterG = 0;
53
- DBOS.registerLifecycleCallback(dhttp);
54
53
  await DBOS.launch();
55
54
  DBOS.logRegisteredEndpoints();
56
55
  app = new Koa();
@@ -20,7 +20,6 @@ describe('http-cors-tests', () => {
20
20
  });
21
21
 
22
22
  beforeEach(async () => {
23
- DBOS.registerLifecycleCallback(dhttp);
24
23
  await DBOS.launch();
25
24
  app = new Koa();
26
25
  appRouter = new Router();
@@ -40,7 +40,6 @@ describe('httpserver-tests', () => {
40
40
  });
41
41
 
42
42
  beforeEach(async () => {
43
- DBOS.registerLifecycleCallback(dhttp);
44
43
  const _classes = [TestEndpoints];
45
44
  await DBOS.launch();
46
45
  await DBOS.queryUserDB(`DROP TABLE IF EXISTS ${testTableName};`);
@@ -0,0 +1,79 @@
1
+ import { DBOS } from '@dbos-inc/dbos-sdk';
2
+ import Koa from 'koa';
3
+ import Router from '@koa/router';
4
+ import { DBOSKoa } from '../src';
5
+
6
+ import request from 'supertest';
7
+
8
+ const dhttp = new DBOSKoa();
9
+
10
+ function customstep() {
11
+ return function decorator<This, Args extends unknown[], Return>(
12
+ target: object,
13
+ propertyKey: PropertyKey,
14
+ descriptor: TypedPropertyDescriptor<(this: This, ...args: Args) => Promise<Return>>,
15
+ ) {
16
+ if (!descriptor.value) {
17
+ throw Error('Use of decorator when original method is undefined');
18
+ }
19
+
20
+ descriptor.value = DBOS.registerStep(descriptor.value, {
21
+ name: String(propertyKey),
22
+ ctorOrProto: target,
23
+ });
24
+
25
+ return descriptor;
26
+ };
27
+ }
28
+
29
+ describe('registerstep', () => {
30
+ let app: Koa;
31
+ let appRouter: Router;
32
+
33
+ beforeAll(async () => {});
34
+
35
+ afterAll(async () => {});
36
+
37
+ beforeEach(async () => {
38
+ DBOS.setConfig({ name: 'koa-step-test' });
39
+ await DBOS.launch();
40
+ app = new Koa();
41
+ appRouter = new Router();
42
+ dhttp.registerWithApp(app, appRouter);
43
+ });
44
+
45
+ afterEach(async () => {
46
+ await DBOS.shutdown();
47
+ });
48
+
49
+ test('use-customstep-and-koa', async () => {
50
+ const u1 = await KnexKoa.insertOneWay('joe');
51
+ expect(u1.user).toBe('joe');
52
+ const u2 = await KnexKoa.insertTheOtherWay('jack');
53
+ expect(u2.user).toBe('jack');
54
+
55
+ const response1 = await request(app.callback()).get('/api/i1?user=john');
56
+ expect(response1.statusCode).toBe(200);
57
+ const response2 = await request(app.callback()).get('/api/i2?user=jeremy');
58
+ expect(response2.statusCode).toBe(200);
59
+ const response3 = await request(app.callback()).get('/api/i1');
60
+ expect(response3.statusCode).toBe(400);
61
+ const response4 = await request(app.callback()).get('/api/i2');
62
+ expect(response4.statusCode).toBe(400);
63
+ });
64
+ });
65
+
66
+ @DBOSKoa.defaultArgValidate
67
+ class KnexKoa {
68
+ @customstep()
69
+ @dhttp.getApi('/api/i2')
70
+ static async insertTheOtherWay(user: string) {
71
+ return Promise.resolve({ user, now: Date.now() });
72
+ }
73
+
74
+ @dhttp.getApi('/api/i1')
75
+ @customstep()
76
+ static async insertOneWay(user: string) {
77
+ return Promise.resolve({ user, now: Date.now() });
78
+ }
79
+ }
@@ -57,7 +57,6 @@ describe('KnexDataSource', () => {
57
57
 
58
58
  beforeEach(async () => {
59
59
  DBOS.setConfig({ name: 'koa-knex-ds-test' });
60
- DBOS.registerLifecycleCallback(dhttp);
61
60
  await DBOS.launch();
62
61
  app = new Koa();
63
62
  appRouter = new Router();