@sudowealth/schwab-api
Version:
TypeScript client for Charles Schwab API with OAuth support, market data, trading functionality, and complete type safety
43 lines (42 loc) • 1.51 kB
JavaScript
import { OAUTH_ENDPOINTS } from '../constants';
import { resolveBaseUrl } from '../core/config';
/**
* Get the OAuth base URL based on the current configuration using the provided context
*/
export function getOAuthBaseUrlWithContext(context) {
const config = context.config;
// Use the centralized base URL resolution function
const baseUrl = resolveBaseUrl(config);
const apiVersion = config.apiVersion;
return `${baseUrl}/${apiVersion}`;
}
/**
* Get the authorization URL based on the provided context
*/
export function getAuthorizationUrlWithContext(context) {
return `${getOAuthBaseUrlWithContext(context)}${OAUTH_ENDPOINTS.AUTHORIZE}`;
}
/**
* Get the token URL based on the provided context
*/
export function getTokenUrlWithContext(context) {
return `${getOAuthBaseUrlWithContext(context)}${OAUTH_ENDPOINTS.TOKEN}`;
}
/**
* Build the authorization URL with the provided context and options
*/
export function buildAuthorizeUrlWithContext(context, opts) {
const authBaseUrl = opts.baseUrl || getAuthorizationUrlWithContext(context);
const url = new URL(authBaseUrl);
url.searchParams.set('client_id', opts.clientId);
url.searchParams.set('redirect_uri', opts.redirectUri);
if (opts.scope) {
url.searchParams.set('scope', opts.scope);
}
if (opts.state) {
url.searchParams.set('state', opts.state);
}
// Schwab requires response_type=code
url.searchParams.set('response_type', 'code');
return url.toString();
}