have type for both request and response

This commit is contained in:
Florian Dold 2017-05-30 19:00:51 +02:00
parent e5b88ee003
commit 8bb67c0c5f
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -23,42 +23,153 @@
import * as types from "../types"; import * as types from "../types";
/**
* Message type information.
*/
export interface MessageMap { export interface MessageMap {
"balances": { }; "balances": {
"dump-db": { }; request: { };
"import-db": { dump: object }; response: types.WalletBalance;
"get-tab-cookie": { }; };
"ping": { }; "dump-db": {
"reset": { }; request: { };
"create-reserve": { amount: types.AmountJson; exchange: string }; response: any;
"confirm-reserve": { reservePub: string }; };
"generate-nonce": { }; "import-db": {
"confirm-pay": { offer: types.OfferRecord; }; request: {
"check-pay": { offer: types.OfferRecord; }; dump: object;
"query-payment": { }; };
"exchange-info": { baseUrl: string }; response: void;
"currency-info": { name: string }; };
"hash-contract": { contract: object }; "get-tab-cookie": {
"put-history-entry": { historyEntry: types.HistoryRecord }; request: { }
"safe-offer": { offer: types.OfferRecord }; response: any;
"reserve-creation-info": { baseUrl: string }; };
"get-history": { }; "ping": {
"get-offer": { offerId: number } request: { };
"get-currencies": { }; response: void;
"update-currency": { currencyRecord: types.CurrencyRecord }; };
"get-reserves": { exchangeBaseUrl: string }; "reset": {
"get-payback-reserves": { }; request: { };
"withdraw-payback-reserve": { reservePub: string }; response: void;
"get-precoins": { exchangeBaseUrl: string }; };
"get-denoms": { exchangeBaseUrl: string }; "create-reserve": {
"payback-coin": { coinPub: string }; request: {
"payment-failed": { contractTermsHash: string }; amount: types.AmountJson;
"payment-succeeded": { contractTermsHash: string; merchantSig: string }; exchange: string
};
response: void;
};
"confirm-reserve": {
request: { reservePub: string };
response: void;
}
"generate-nonce": {
request: { }
response: string;
};
"confirm-pay": {
request: { offer: types.OfferRecord; };
response: types.ConfirmPayResult;
};
"check-pay": {
request: { offer: types.OfferRecord; };
response: types.CheckPayResult;
};
"query-payment": {
request: { };
response: void;
};
"exchange-info": {
request: { baseUrl: string };
response: types.ExchangeRecord;
};
"currency-info": {
request: { name: string };
response: types.CurrencyRecord;
};
"hash-contract": {
request: { contract: object };
response: string;
};
"put-history-entry": {
request: { historyEntry: types.HistoryRecord };
response: void;
};
"safe-offer": {
request: { offer: types.OfferRecord };
response: void;
};
"reserve-creation-info": {
request: { baseUrl: string };
response: types.ReserveCreationInfo;
}
"get-history": {
request: { };
response: types.HistoryRecord[];
};
"get-offer": {
request: { offerId: number };
response: types.OfferRecord | undefined;
};
"get-currencies": {
request: { };
response: types.CurrencyRecord[];
};
"update-currency": {
request: { currencyRecord: types.CurrencyRecord };
response: void;
};
"get-reserves": {
request: { exchangeBaseUrl: string };
response: types.ReserveRecord[];
};
"get-payback-reserves": {
request: { };
response: types.ReserveRecord[];
};
"withdraw-payback-reserve": {
request: { reservePub: string };
response: void;
}
"get-precoins": {
request: { exchangeBaseUrl: string };
response: types.PreCoinRecord[];
};
"get-denoms": {
request: { exchangeBaseUrl: string };
response: types.DenominationRecord[];
};
"payback-coin": {
request: { coinPub: string };
response: void;
};
"payment-failed": {
request: { contractTermsHash: string };
response: void;
};
"payment-succeeded": {
request: { contractTermsHash: string; merchantSig: string };
response: void;
};
} }
/**
* String literal types for messages.
*/
export type MessageType = keyof MessageMap; export type MessageType = keyof MessageMap;
export class Message<T extends MessageType> { /**
constructor (public type: T, public detail: MessageMap[T]) { * Make a request whose details match the request type.
} */
export function makeRequest<T extends MessageType>(type: T, details: MessageMap[T]["request"]) {
return { type, details };
} }
/**
* Make a response that matches the request type.
*/
export function makeResponse<T extends MessageType>(type: T, response: MessageMap[T]["response"]) {
return response;
}