@yash101/schwab-api-client
Version:
A TypeScript client library for interacting with the Charles Schwab Brokerage APIs.
97 lines • 3.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccountNumbers = getAccountNumbers;
exports.getAccounts = getAccounts;
exports.getAccount = getAccount;
const assert_1 = __importDefault(require("assert"));
async function getAccountNumbers(request, token, apiOptions) {
const uri = new URL('/trader/v1/accountNumbers', apiOptions?.getBaseUri() || 'https://api.schwabapi.com');
try {
const response = await fetch(uri, {
headers: {
Authorization: token.getAuthHeader(),
'Content-Type': 'application/json',
}
});
const json = await response.json();
if (!response.ok) {
return json;
}
return json;
}
catch (e) {
throw new Error(`Failed to get account numbers: ${e.message}`);
}
}
/**
*
* @param request optional since all fields are optional
* @param token
* @param apiOptions
* @returns
*/
async function getAccounts(request, token, apiOptions) {
const uri = new URL('/trader/v1/accounts', apiOptions?.getBaseUri() || 'https://api.schwabapi.com');
if (request?.fields) {
// this might not be correct
uri.searchParams.append('fields', request.fields.join(','));
}
try {
const response = await fetch(uri, {
headers: {
Authorization: token.getAuthHeader(),
'Content-Type': 'application/json',
Accept: 'application/json',
}
});
const json = await response.json();
if (!response.ok) {
return json;
}
return json;
}
catch (e) {
throw new Error(`Failed to get accounts: ${e.message}`);
}
}
/**
* Get a single account by account number. Note that account number is
* the encrypted account number, not the plain text account number.
*
* Use the /accountNumbers endpoint to get the encrypted account number, which
* is called hashValue in the response.
*
* @param request contains required parameters
* @param token
* @param apiOptions
* @returns
*/
async function getAccount(request, token, apiOptions) {
(0, assert_1.default)(request.accountNumber, 'accountNumber is required');
const uri = new URL(`/trader/v1/accounts/${request.accountNumber}`, apiOptions?.getBaseUri() || 'https://api.schwabapi.com');
if (request.fields) {
// this might not be correct
uri.searchParams.append('fields', request.fields.join(','));
}
try {
const response = await fetch(uri, {
headers: {
Authorization: token.getAuthHeader(),
'Content-Type': 'application/json',
Accept: 'application/json',
}
});
const json = await response.json();
if (!response.ok) {
return json;
}
return json;
}
catch (e) {
throw new Error(`Failed to get account: ${e.message}`);
}
}
//# sourceMappingURL=accounts.api.js.map