Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding request ID #18

Merged
merged 1 commit into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/tokens/tokens.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.

import { ApiProperty } from '@nestjs/swagger';
import { IsDefined, IsInt, IsNotEmpty, Min } from 'class-validator';
import { IsDefined, IsInt, IsNotEmpty, IsOptional, Min } from 'class-validator';
import { Event } from '../event-stream/event-stream.interfaces';

// Ethconnect interfaces
Expand Down Expand Up @@ -73,6 +73,10 @@ export class TokenPool {
@ApiProperty()
@IsNotEmpty()
clientId: string;

@ApiProperty()
@IsOptional()
requestId?: string;
}

export class TokenMint {
Expand All @@ -88,6 +92,10 @@ export class TokenMint {
@IsInt()
@Min(1)
amount: number;

@ApiProperty()
@IsOptional()
requestId?: string;
}

export class TokenBalanceQuery {
Expand Down Expand Up @@ -130,6 +138,10 @@ export class TokenTransfer {
@IsInt()
@Min(1)
amount: number;

@ApiProperty()
@IsOptional()
requestId?: string;
}

// Websocket notifications
Expand Down
12 changes: 7 additions & 5 deletions src/tokens/tokens.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ export class TokensService {
this.shortPrefix = shortPrefix;
}

private get postOptions() {
private postOptions(requestId?: string) {
const from = `${this.shortPrefix}-from`;
const sync = `${this.shortPrefix}-sync`;
const id = `${this.shortPrefix}-id`;
return {
params: {
[from]: this.identity,
[sync]: 'false',
[id]: requestId,
},
};
}
Expand All @@ -92,7 +94,7 @@ export class TokensService {
data: packTokenData(dto.namespace, dto.name, dto.clientId),
is_fungible: dto.type === TokenType.FUNGIBLE,
},
this.postOptions,
this.postOptions(dto.requestId),
)
.toPromise();
return { id: response.data.id };
Expand All @@ -110,7 +112,7 @@ export class TokensService {
amounts: [dto.amount],
data: [0],
},
this.postOptions,
this.postOptions(dto.requestId),
)
.toPromise();
return { id: response.data.id };
Expand All @@ -128,7 +130,7 @@ export class TokensService {
to,
data: [0],
},
this.postOptions,
this.postOptions(dto.requestId),
)
.toPromise();
return { id: response.data.id };
Expand Down Expand Up @@ -158,7 +160,7 @@ export class TokensService {
amount: dto.amount,
data: [0],
},
this.postOptions,
this.postOptions(dto.requestId),
)
.toPromise();
return { id: response.data.id };
Expand Down
13 changes: 10 additions & 3 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,16 @@ describe('AppController (e2e)', () => {
namespace: 'testns',
name: 'token1',
clientId: '1',
requestId: '12345',
};
const response: EthConnectAsyncResponse = {
id: '1',
id: '12345',
sent: true,
};

http.post = jest.fn(() => new FakeObservable(response));

await server.post('/pool').send(request).expect(202).expect({ id: '1' });
await server.post('/pool').send(request).expect(202).expect({ id: '12345' });

expect(http.post).toHaveBeenCalledTimes(1);
expect(http.post).toHaveBeenCalledWith(
Expand All @@ -145,7 +146,13 @@ describe('AppController (e2e)', () => {
data: '0x746573746e7300746f6b656e310031',
is_fungible: true,
},
OPTIONS,
{
...OPTIONS,
params: {
...OPTIONS.params,
'fly-id': '12345',
},
},
);
});

Expand Down