@dbos-inc/koa-serve 3.6.3-preview → 3.6.7-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.
@@ -2,19 +2,14 @@
2
2
  import Koa from 'koa';
3
3
  import Router from '@koa/router';
4
4
 
5
- import { DBOS, DBOSResponseError, Error as DBOSErrors, StatusString } from '@dbos-inc/dbos-sdk';
5
+ import { DBOS, Error as DBOSErrors, StatusString } from '@dbos-inc/dbos-sdk';
6
6
 
7
- import { DBOSKoa, DBOSKoaAuthContext, RequestIDHeader, WorkflowIDHeader } from '../src';
7
+ import { DBOSKoa, DBOSKoaAuthContext, RequestIDHeader, WorkflowIDHeader, DBOSResponseError } from '../src';
8
8
 
9
9
  import request from 'supertest';
10
10
 
11
11
  const dhttp = new DBOSKoa();
12
12
 
13
- interface TestKvTable {
14
- id?: number;
15
- value?: string;
16
- }
17
-
18
13
  import { randomUUID } from 'node:crypto';
19
14
  import { IncomingMessage } from 'http';
20
15
  import { bodyParser } from '@koa/bodyparser';
@@ -29,12 +24,9 @@ describe('httpserver-tests', () => {
29
24
  let app: Koa;
30
25
  let appRouter: Router;
31
26
 
32
- const testTableName = 'dbos_test_kv';
33
-
34
27
  beforeAll(async () => {
35
28
  DBOS.setConfig({
36
29
  name: 'dbos-koa-test',
37
- userDatabaseClient: 'pg-node',
38
30
  });
39
31
  return Promise.resolve();
40
32
  });
@@ -42,8 +34,6 @@ describe('httpserver-tests', () => {
42
34
  beforeEach(async () => {
43
35
  const _classes = [TestEndpoints];
44
36
  await DBOS.launch();
45
- await DBOS.queryUserDB(`DROP TABLE IF EXISTS ${testTableName};`);
46
- await DBOS.queryUserDB(`CREATE TABLE IF NOT EXISTS ${testTableName} (id INT PRIMARY KEY, value TEXT);`);
47
37
  app = new Koa();
48
38
  appRouter = new Router();
49
39
  dhttp.registerWithApp(app, appRouter);
@@ -160,12 +150,6 @@ describe('httpserver-tests', () => {
160
150
  expect(response.statusCode).toBe(400);
161
151
  });
162
152
 
163
- test('endpoint-transaction', async () => {
164
- const response = await request(app.callback()).post('/transaction/alice');
165
- expect(response.statusCode).toBe(200);
166
- expect(response.text).toBe('hello 1');
167
- });
168
-
169
153
  test('endpoint-step', async () => {
170
154
  const response = await request(app.callback()).get('/step/alice');
171
155
  expect(response.statusCode).toBe(200);
@@ -175,31 +159,30 @@ describe('httpserver-tests', () => {
175
159
  test('endpoint-workflow', async () => {
176
160
  const response = await request(app.callback()).post('/workflow?name=alice');
177
161
  expect(response.statusCode).toBe(200);
178
- expect(response.text).toBe('hello 1');
162
+ expect(response.text).toBe('hello alice');
179
163
  });
180
164
 
181
165
  test('endpoint-error', async () => {
182
166
  const response = await request(app.callback()).post('/error').send({ name: 'alice' });
183
167
  expect(response.statusCode).toBe(500);
184
- expect(response.body.details.code).toBe('23505'); // Should be the expected error.
185
168
  });
186
169
 
187
170
  test('endpoint-handler', async () => {
188
171
  const response = await request(app.callback()).get('/handler/alice');
189
172
  expect(response.statusCode).toBe(200);
190
- expect(response.text).toBe('hello 1');
173
+ expect(response.text).toBe('hello alice');
191
174
  });
192
175
 
193
176
  test('endpoint-testStartWorkflow', async () => {
194
177
  const response = await request(app.callback()).get('/testStartWorkflow/alice');
195
178
  expect(response.statusCode).toBe(200);
196
- expect(response.text).toBe('hello 1');
179
+ expect(response.text).toBe('hello alice');
197
180
  });
198
181
 
199
182
  test('endpoint-testInvokeWorkflow', async () => {
200
183
  const response = await request(app.callback()).get('/testInvokeWorkflow/alice');
201
184
  expect(response.statusCode).toBe(200);
202
- expect(response.text).toBe('hello 1');
185
+ expect(response.text).toBe('hello alice');
203
186
  });
204
187
 
205
188
  // This feels unclean, but supertest doesn't expose the error message the people we want. See:
@@ -292,12 +275,12 @@ describe('httpserver-tests', () => {
292
275
  .post('/workflow?name=bob')
293
276
  .set({ 'dbos-idempotency-key': workflowID });
294
277
  expect(response.statusCode).toBe(200);
295
- expect(response.text).toBe('hello 1');
278
+ expect(response.text).toBe('hello bob');
296
279
 
297
280
  // Retrieve the workflow with WFID.
298
281
  const retrievedHandle = DBOS.retrieveWorkflow(workflowID);
299
282
  expect(retrievedHandle).not.toBeNull();
300
- await expect(retrievedHandle.getResult()).resolves.toBe('hello 1');
283
+ await expect(retrievedHandle.getResult()).resolves.toBe('hello bob');
301
284
  await expect(retrievedHandle.getStatus()).resolves.toMatchObject({
302
285
  status: StatusString.SUCCESS,
303
286
  });
@@ -307,12 +290,12 @@ describe('httpserver-tests', () => {
307
290
  const workflowID = randomUUID();
308
291
  const response = await request(app.callback()).get('/handler/bob').set({ 'dbos-idempotency-key': workflowID });
309
292
  expect(response.statusCode).toBe(200);
310
- expect(response.text).toBe('hello 1');
293
+ expect(response.text).toBe('hello bob');
311
294
 
312
295
  // Retrieve the workflow with WFID.
313
296
  const retrievedHandle = DBOS.retrieveWorkflow(workflowID);
314
297
  expect(retrievedHandle).not.toBeNull();
315
- await expect(retrievedHandle.getResult()).resolves.toBe('hello 1');
298
+ await expect(retrievedHandle.getResult()).resolves.toBe('hello bob');
316
299
  await expect(retrievedHandle.getStatus()).resolves.toMatchObject({
317
300
  status: StatusString.SUCCESS,
318
301
  });
@@ -424,7 +407,7 @@ describe('httpserver-tests', () => {
424
407
  }
425
408
 
426
409
  @dhttp.getApi('/dbos-error')
427
- @DBOS.transaction()
410
+ @DBOS.step()
428
411
  static async dbosErr() {
429
412
  return Promise.reject(new DBOSResponseError('customize error', 503));
430
413
  }
@@ -450,16 +433,6 @@ describe('httpserver-tests', () => {
450
433
  return await TestEndpoints.testWorkflow(name);
451
434
  }
452
435
 
453
- @dhttp.postApi('/transaction/:name')
454
- @DBOS.transaction()
455
- static async testTransaction(name: string) {
456
- const { rows } = await DBOS.pgClient.query<TestKvTable>(
457
- `INSERT INTO ${testTableName}(id, value) VALUES (1, $1) RETURNING id`,
458
- [name],
459
- );
460
- return `hello ${rows[0].id}`;
461
- }
462
-
463
436
  @dhttp.getApi('/step/:input')
464
437
  @DBOS.step()
465
438
  static async testStep(input: string) {
@@ -469,17 +442,16 @@ describe('httpserver-tests', () => {
469
442
  @dhttp.postApi('/workflow')
470
443
  @DBOS.workflow()
471
444
  static async testWorkflow(name: string) {
472
- const res = await TestEndpoints.testTransaction(name);
473
- return TestEndpoints.testStep(res);
445
+ return TestEndpoints.testStep(`hello ${name}`);
474
446
  }
475
447
 
476
448
  @dhttp.postApi('/error')
477
449
  @DBOS.workflow()
478
450
  static async testWorkflowError(name: string) {
451
+ void name;
479
452
  // This workflow should encounter duplicate primary key error.
480
- let res = await TestEndpoints.testTransaction(name);
481
- res = await TestEndpoints.testTransaction(name);
482
- return res;
453
+ throw new Error('fail');
454
+ return Promise.resolve('');
483
455
  }
484
456
 
485
457
  @dhttp.getApi('/requireduser')