Skip to content

Commit

Permalink
chore: new interface IRequestDataWithEvents (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjlevesque committed Mar 13, 2020
1 parent f49640b commit 69a84a5
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const validRequest: ClientTypes.IRequestData = {
type: RequestLogicTypes.CURRENCY.BTC,
value: '',
},

// tslint:disable-next-line:typedef
emit: () => true,
events: [],
expectedAmount: '10000000',
extensions: {
Expand All @@ -61,8 +58,6 @@ const validRequest: ClientTypes.IRequestData = {
meta: {
transactionManagerMeta: {},
},
// tslint:disable-next-line:typedef
on: () => null as any,
pending: null,
requestId: 'abcd',
state: RequestLogicTypes.STATE.CREATED,
Expand Down
3 changes: 0 additions & 3 deletions packages/payment-processor/test/payment/erc20-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ const validRequest: ClientTypes.IRequestData = {
value: erc20ContractAddress,
},

// tslint:disable-next-line:typedef
emit: () => true,
events: [],
expectedAmount: '100',
extensions: {
Expand All @@ -75,7 +73,6 @@ const validRequest: ClientTypes.IRequestData = {
meta: {
transactionManagerMeta: {},
},
on: () => null as any,
pending: null,
requestId: 'abcd',
state: RequestLogicTypes.STATE.CREATED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ const validRequest: ClientTypes.IRequestData = {
value: '',
},

// tslint:disable-next-line:typedef
emit: () => true,
events: [],
expectedAmount: '1',
extensions: {
Expand All @@ -67,7 +65,6 @@ const validRequest: ClientTypes.IRequestData = {
meta: {
transactionManagerMeta: {},
},
on: () => null as any,
pending: null,
requestId: 'abcd',
state: RequestLogicTypes.STATE.CREATED,
Expand Down
26 changes: 13 additions & 13 deletions packages/request-client.js/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default class Request {
*
* @returns the request data
*/
public waitForConfirmation(): Promise<Types.IRequestData> {
public waitForConfirmation(): Promise<Types.IRequestDataWithEvents> {
return new Promise((resolve): any => this.on('confirmed', resolve));
}

Expand All @@ -113,7 +113,7 @@ export default class Request {
public async accept(
signerIdentity: IdentityTypes.IIdentity,
refundInformation?: any,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (refundInformation) {
if (!this.paymentNetwork) {
Expand Down Expand Up @@ -150,7 +150,7 @@ export default class Request {
public async cancel(
signerIdentity: IdentityTypes.IIdentity,
refundInformation?: any,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (refundInformation) {
if (!this.paymentNetwork) {
Expand Down Expand Up @@ -190,7 +190,7 @@ export default class Request {
deltaAmount: RequestLogicTypes.Amount,
signerIdentity: IdentityTypes.IIdentity,
refundInformation?: any,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (refundInformation) {
if (!this.paymentNetwork) {
Expand Down Expand Up @@ -234,7 +234,7 @@ export default class Request {
deltaAmount: RequestLogicTypes.Amount,
signerIdentity: IdentityTypes.IIdentity,
paymentInformation?: any,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (paymentInformation) {
if (!this.paymentNetwork) {
Expand Down Expand Up @@ -277,7 +277,7 @@ export default class Request {
public async addPaymentInformation(
paymentInformation: any,
signerIdentity: IdentityTypes.IIdentity,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];

if (!this.paymentNetwork) {
Expand Down Expand Up @@ -319,7 +319,7 @@ export default class Request {
public async addRefundInformation(
refundInformation: any,
signerIdentity: IdentityTypes.IIdentity,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];

if (!this.paymentNetwork) {
Expand Down Expand Up @@ -363,7 +363,7 @@ export default class Request {
amount: string,
note: string,
signerIdentity: IdentityTypes.IIdentity,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];

if (!this.paymentNetwork) {
Expand Down Expand Up @@ -415,7 +415,7 @@ export default class Request {
amount: string,
note: string,
signerIdentity: IdentityTypes.IIdentity,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];

if (!this.paymentNetwork) {
Expand Down Expand Up @@ -470,7 +470,7 @@ export default class Request {
amount: string,
note: string,
signerIdentity: IdentityTypes.IIdentity,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];

if (!this.paymentNetwork) {
Expand Down Expand Up @@ -525,7 +525,7 @@ export default class Request {
amount: string,
note: string,
signerIdentity: IdentityTypes.IIdentity,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];

if (!this.paymentNetwork) {
Expand Down Expand Up @@ -573,7 +573,7 @@ export default class Request {
*
* @returns The updated request data
*/
public getData(): Types.IRequestData {
public getData(): Types.IRequestDataWithEvents {
let requestData: RequestLogicTypes.IRequest = Utils.deepCopy(this.requestData);

let pending = Utils.deepCopy(this.pendingData);
Expand Down Expand Up @@ -602,7 +602,7 @@ export default class Request {
*/
public async refresh(
requestAndMeta?: RequestLogicTypes.IReturnGetRequestFromId,
): Promise<Types.IRequestData> {
): Promise<Types.IRequestDataWithEvents> {
if (!requestAndMeta) {
requestAndMeta = await this.requestLogic.getRequestFromId(this.requestId);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/request-client.js/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ describe('index', () => {
expect(data.state).to.equal(RequestLogicTypes.STATE.PENDING);
expect(data.pending?.state).to.equal(RequestLogicTypes.STATE.CREATED);

const dataConfirmed: Types.IRequestData = await new Promise((resolve): any =>
const dataConfirmed: Types.IRequestDataWithEvents = await new Promise((resolve): any =>
request.on('confirmed', resolve),
);
expect(dataConfirmed.state).to.equal(RequestLogicTypes.STATE.CREATED);
Expand Down Expand Up @@ -471,7 +471,7 @@ describe('index', () => {
expect(data.state).to.equal(RequestLogicTypes.STATE.PENDING);
expect(data.pending?.state).to.equal(RequestLogicTypes.STATE.CREATED);

const dataConfirmed: Types.IRequestData = await new Promise((resolve): any =>
const dataConfirmed: Types.IRequestDataWithEvents = await new Promise((resolve): any =>
request.on('confirmed', resolve),
);
expect(dataConfirmed.state).to.equal(RequestLogicTypes.STATE.CREATED);
Expand Down Expand Up @@ -978,7 +978,7 @@ describe('index', () => {
expect(acceptResult.state).to.equal(RequestLogicTypes.STATE.CREATED);
expect(acceptResult.pending?.state).to.equal(RequestLogicTypes.STATE.ACCEPTED);

const dataConfirmed: Types.IRequestData = await new Promise((resolve): any =>
const dataConfirmed: Types.IRequestDataWithEvents = await new Promise((resolve): any =>
acceptResult.on('confirmed', resolve),
);
expect(dataConfirmed.state).to.equal(RequestLogicTypes.STATE.ACCEPTED);
Expand Down Expand Up @@ -1090,7 +1090,7 @@ describe('index', () => {

const acceptResult = await fetchedRequest.accept(payerIdentity);

let dataConfirmed: Types.IRequestData = await new Promise((resolve): any =>
let dataConfirmed: Types.IRequestDataWithEvents = await new Promise((resolve): any =>
acceptResult.on('confirmed', resolve),
);
expect(dataConfirmed.state).to.equal(RequestLogicTypes.STATE.ACCEPTED);
Expand Down
5 changes: 4 additions & 1 deletion packages/types/src/client-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export interface IRequestData extends Omit<RequestLogic.IRequest, 'currency'> {
contentData: any;
currencyInfo: RequestLogic.ICurrency;
pending: RequestLogic.IPendingRequest | null;
}

/** Interface request data with event emitter and subscriber */
export interface IRequestDataWithEvents extends IRequestData {
on: <K extends keyof IRequestEvents>(event: K, listener: IRequestEvents[K]) => this;
emit: <K extends keyof IRequestEvents>(
event: K,
Expand Down Expand Up @@ -46,5 +49,5 @@ export interface IRequestInfo {

/** Events types risen by a request */
export interface IRequestEvents {
confirmed: (requestData: IRequestData) => void;
confirmed: (requestData: IRequestDataWithEvents) => void;
}

0 comments on commit 69a84a5

Please sign in to comment.