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

SDKify #7

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
npm-debug.log

.idea
74 changes: 43 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,28 @@ for a more natural declaration.
## Quickstart

```js
const WyreClient = require('@wyre/api').WyreClient
const WyreClient = require('@wyre/api')

let wyre = new WyreClient({
format: "json_numberstring",
apiKey: "AK-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA",
secretKey: "SK-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA"
// baseUrl: "https://api.testwyre.com" // todo uncomment this line to use the testwyre environment
});

wyre.get("/v2/account")
.then(account => {
console.log("I am Wyre account ", account.id);
const client = new WyreClient({
auth: {
apiKey: 'AK-api-key',
secretKey: 'SK-secret-key'
},
err => {
version: '3', // Keep this unless doing manual api calls
// uri: 'https://api.testwyre.com', // Uncomment this line to use the testwyre environment
format: 'json_numberstring',
headers: {},
qs: {}
})

// Get account from auth credentials
client.fetchAccount()
.then((account) => {
console.log("I am Wyre account ", account.id);
})
.catch((err) => {
console.log("Problems, cap'n: ", err);
});
})
```

You're all set to begin coding!
Expand All @@ -59,7 +65,7 @@ You're all set to begin coding!

Attempt a $10 USD->BTC conversion:
```js
wyre.post("/transfers", {
account.createTransfer({
sourceAmount: "10",
sourceCurrency: "USD",
destCurrency: "BTC",
Expand All @@ -71,7 +77,7 @@ Upload a document:
```js
var fs = require('fs');
let my_id = fs.readFileSync('./my_id.jpg');
wyre.post('/v3/accounts/' + account.id + '/individualGovernmentId',
client.api.post('accounts/' + account.id + '/individualGovernmentId',
my_id,
{ headers: { 'Content-Type': 'image/jpeg' }})
.then(successCallback, errorCallback);
Expand All @@ -89,11 +95,15 @@ Constructor parameters:

| parameter | description
| ----------|--------------
| apiKey | your environment-specific Wyre API key
| secretKey | your environment-specific Wyre API secret
| baseUrl | specifies the Wyre environment you'd like to use. please use either:<br>`https://api.sendwyre.com` for production<br>`https://api.testwyre.com` for testwyre
| auth.apiKey | your environment-specific Wyre API key
| auth.secretKey | your environment-specific Wyre API secret
| auth.masqueradeTarget | sub-account id to masquerade as
| uri | specifies the Wyre environment you'd like to use. please use either:<br>`https://api.sendwyre.com` for production<br>`https://api.testwyre.com` for testwyre
| version | specifies the Wyre API version to use. Defaults to 3.
| format | the data format you're requesting.<br>`json` for straight JSON <br>`json_numberstring` for JSON with all decimals as strings (see [above](#regarding-decimal-numbers)]
| options | options that are passed to the underlying [Request](https://github.com/request/request) for _every_ request
| headers | headers that are passed to the underlying [Request](https://github.com/request/request) for _every_ request
| qs | query string parameters that are passed to the underlying [Request](https://github.com/request/request) for _every_ request
| timeout | timeout limit for API request in milliseconds

Note that the ability to override options used by the [Request](https://github.com/request/request) client is
available both generally as well as per-request.
Expand All @@ -106,10 +116,10 @@ or per-request (as with all options).
Each of these methods performs a single Wyre API request and returns a promise for the resulting API response.

```js
wyre.get(path, parameters, options)
wyre.post(path, body, options)
wyre.put(path, body, options)
wyre.delete(path, body, options)
client.api.get(path, parameters, options)
client.api.post(path, body, options)
client.api.put(path, body, options)
client.api.delete(path, body, options)
```

### Masquerading API
Expand All @@ -118,14 +128,16 @@ This is an alternative to supplying the `masqueradeAs` parameter as a query para

```js
// init the wyre client as usual
let wyre = new WyreClient({ /* your master api access setup here */ });

// create another sub-client authenticated as a particular user
let user1_wyre = wyre.masqueraded('AC-ABCDE12345');

// now use that client as normal!
user1_wyre.get('/v3/accounts/AC-ABCDE12345').then(successCallback, failureCallback);

const client = new WyreClient({ /* your master api access setup here */ });

// Get sub-account with masquerade = true
client.fetchAccount('AC-sub-account-id', true)
.then((account) => {
console.log("I am Wyre sub-account ", account.id);
})
.catch((err) => {
console.log("Problems, cap'n: ", err);
})
```

### Errors
Expand Down
36 changes: 36 additions & 0 deletions dist/Account.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Model from './Model';
import Transfer from './Transfer';
import PaymentMethod from './PaymentMethod';
import Api from './utils/Api';
import type { IAccount, IAccountResponse, ICreateAccountParams, IProfileField } from './Account/IAccount';
import type { ICreateTransferParams } from './Transfer/ITransfer';
import type { ILimits } from './wyre/ILimits';
export default class Account extends Model<Account, IAccount> implements IAccount {
id: string;
status: 'OPEN' | 'PENDING' | 'APPROVED';
type: 'INDIVIDUAL' | 'BUSINESS';
country: string;
createdAt: number;
depositAddresses: {
ETH: string;
BTC: string;
};
totalBalances: {
BTC: number;
ETH: number;
};
availableBalances: {
BTC: number;
ETH: number;
};
profileFields: Array<IProfileField>;
paymentMethods: Array<PaymentMethod>;
static create(api: Api, params: ICreateAccountParams): Promise<Account>;
static fetch(api: Api, id: string): Promise<Account>;
protected static postFetch(data: IAccountResponse, api: Api): Promise<Account>;
save(): Promise<void>;
createTransfer(params: ICreateTransferParams): Promise<Transfer>;
fetchTransfers(): Promise<Array<Transfer>>;
fetchLimits(): Promise<ILimits>;
}
//# sourceMappingURL=Account.d.ts.map
1 change: 1 addition & 0 deletions dist/Account.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

161 changes: 161 additions & 0 deletions dist/Account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var Model_1 = require("./Model");
var Transfer_1 = require("./Transfer");
var PaymentMethod_1 = require("./PaymentMethod");
var Account = (function (_super) {
__extends(Account, _super);
function Account() {
return _super !== null && _super.apply(this, arguments) || this;
}
Account.create = function (api, params) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
api.requireAuthed();
return [4, api.post('accounts', params)];
case 1:
data = _a.sent();
if (params.subaccount)
api = api.masqueradeAs(data.id);
return [2, this.postFetch(data, api)];
}
});
});
};
Account.fetch = function (api, id) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
api.requireAuthed();
return [4, api.get("accounts/" + id)];
case 1:
data = _a.sent();
return [2, this.postFetch(data, api)];
}
});
});
};
Account.postFetch = function (data, api) {
return __awaiter(this, void 0, void 0, function () {
var paymentMethods;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, PaymentMethod_1.default.fetchAll(api)];
case 1:
paymentMethods = _a.sent();
return [2, new Account(__assign(__assign({}, data), { paymentMethods: paymentMethods }), api)];
}
});
});
};
Account.prototype.save = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.data.isChanged)
return [2];
return [4, this.api.post("accounts/" + this.id, this.data.updatedValues)];
case 1:
_a.sent();
return [2];
}
});
});
};
Account.prototype.createTransfer = function (params) {
return __awaiter(this, void 0, void 0, function () {
var transfer;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, Transfer_1.default.create(this.api, params)];
case 1:
transfer = _a.sent();
return [2, transfer];
}
});
});
};
Account.prototype.fetchTransfers = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, Transfer_1.default.fetchAll(this.api)];
});
});
};
Account.prototype.fetchLimits = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.api.requireAuthed();
return [2, this.api.get('limits')];
});
});
};
return Account;
}(Model_1.default));
exports.default = Account;
53 changes: 53 additions & 0 deletions dist/Account/IAccount.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { IPaymentMethod } from '../PaymentMethod/IPaymentMethod';
export interface IAccount extends IAccountResponse {
paymentMethods: Array<IPaymentMethod>;
}
export interface IAccountResponse {
id: string;
status: 'OPEN' | 'PENDING' | 'APPROVED';
type: 'INDIVIDUAL' | 'BUSINESS';
country: string;
createdAt: number;
depositAddresses: {
ETH: string;
BTC: string;
};
totalBalances: {
BTC: number;
ETH: number;
};
availableBalances: {
BTC: number;
ETH: number;
};
profileFields: Array<IProfileField>;
}
export interface IProfileField {
fieldId: IProfileFieldId;
fieldType: IProfileFieldType;
value: string | object | IProfileFieldValueAddress | Array<IProfileFieldValueDocument> | null;
note: string | null;
status: IProfileFieldStatus;
}
declare type IProfileFieldId = 'individualCellphoneNumber' | 'individualEmail' | 'individualLegalName' | 'individualDateOfBirth' | 'individualSsn' | 'individualResidenceAddress' | 'individualGovernmentId' | 'individualSourceOfFunds';
declare type IProfileFieldType = 'CELLPHONE' | 'EMAIL' | 'STRING' | 'DATE' | 'ADDRESS' | 'DOCUMENT' | 'PAYMENT_METHOD';
declare type IProfileFieldValueAddress = {
street1: string;
street2?: string;
city: string;
state: string;
postalCode: string;
country: string | 'US';
};
declare type IProfileFieldValueDocument = {};
declare type IProfileFieldStatus = 'OPEN' | 'PENDING' | 'APPROVED' | 'NULL';
export interface ICreateAccountParams {
type: string;
country: string;
profileFields: Array<IProfileField>;
referrerAccountId?: string;
subaccount?: boolean;
disableEmail?: boolean;
}
export {};
//# sourceMappingURL=IAccount.d.ts.map
1 change: 1 addition & 0 deletions dist/Account/IAccount.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions dist/Account/IAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Loading