Skip to content

Commit fa20052

Browse files
author
Denis Gursky
authored
Merge pull request #31 from RelationalAI/dg-txn-cancel
Transaction cancellation
2 parents a057dd0 + 99644d9 commit fa20052

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@relationalai/rai-sdk-javascript",
33
"description": "RelationalAI SDK for JavaScript",
4-
"version": "0.5.11",
4+
"version": "0.6.0",
55
"author": {
66
"name": "RelationalAI",
77
"url": "https://relational.ai"

src/errors.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ export function makeError(body: any, response: Response) {
5959
return new TransactionError(body, response);
6060
}
6161

62-
if (body?.message) {
63-
return new ApiError(body.message, body.status, body.details, response);
64-
}
65-
66-
return new Error(response.statusText);
62+
return new ApiError(
63+
body?.message || response.statusText || response.status,
64+
body?.status || response.status,
65+
body?.details,
66+
response,
67+
);
6768
}
6869

6970
export type SdkError = ApiError | TransactionError | Error;

src/query/execAsyncApi.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,21 @@ export class ExecAsyncApi extends TransactionAsyncApi {
6666
readonly,
6767
);
6868
const txnId = result.transaction.id;
69-
const startedAt = Date.now();
7069

7170
if ('results' in result) {
7271
return result;
7372
}
7473

74+
return await this.pollTransaction(txnId, interval, timeout);
75+
}
76+
77+
async pollTransaction(
78+
txnId: string,
79+
interval = 3 * 1000,
80+
timeout = Number.POSITIVE_INFINITY,
81+
) {
82+
const startedAt = Date.now();
83+
7584
let transaction: TransactionAsyncCompact;
7685

7786
await new Promise<void>((resolve, reject) => {

src/transaction/transactionAsyncApi.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ describe('TransactionAsyncApi', () => {
170170
expect(result).toEqual(response);
171171
});
172172

173-
it('should delete transaction', async () => {
174-
const response = { txn_id: 'id1', message: 'deleted' };
175-
const scope = nock(baseUrl).delete(`${path}/id1`).reply(200, response);
176-
const result = await api.deleteTransaction('id1');
173+
it('should cancel transaction', async () => {
174+
const response = {};
175+
const scope = nock(baseUrl).post(`${path}/id1/cancel`).reply(200, response);
176+
const result = await api.cancelTransaction('id1');
177177

178178
scope.done();
179179

src/transaction/transactionAsyncApi.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ const ENDPOINT = 'transactions';
2929

3030
type ListResponse = { transactions: TransactionAsync[] };
3131
type SingleResponse = { transaction: TransactionAsync };
32-
type DeleteResponse = {
33-
txn_id: string;
34-
message: string;
32+
type CancelResponse = {
33+
message?: string;
3534
};
3635

3736
export class TransactionAsyncApi extends Base {
@@ -89,12 +88,12 @@ export class TransactionAsyncApi extends Base {
8988
return result;
9089
}
9190

92-
async deleteTransaction(transactionId: string) {
93-
const result = await this.delete<DeleteResponse>(
94-
`${ENDPOINT}/${transactionId}`,
91+
async cancelTransaction(transactionId: string) {
92+
const result = await this.post<CancelResponse>(
93+
`${ENDPOINT}/${transactionId}/cancel`,
9594
{},
9695
);
9796

98-
return result;
97+
return result || {};
9998
}
10099
}

src/transaction/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export type TransactionResult = {
167167
export enum TransactionAsyncState {
168168
CREATED = 'CREATED',
169169
RUNNING = 'RUNNING',
170+
CANCELLING = 'CANCELLING',
170171
ABORTED = 'ABORTED',
171172
COMPLETED = 'COMPLETED',
172173
}

0 commit comments

Comments
 (0)