Skip to content

Commit e1823b2

Browse files
feat: update to latest spec and add wrapper functions for RecoveryPhrase operations (#156)
1 parent f332cf2 commit e1823b2

7 files changed

Lines changed: 115 additions & 13 deletions

File tree

etc/client.api.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,10 @@ export class AuthorizedApiContext {
532532
// (undocumented)
533533
createDemoAccount(demoAccountSettings?: DemoAccountSettings): Promise<openApiClient.CreatedResponseBody>;
534534
// (undocumented)
535+
createRecoveryPhrase(opts: {
536+
name: string;
537+
}): Promise<openApiClient.CreateRecoveryPhraseResult>;
538+
// (undocumented)
535539
createSessionTanChallenge(req: openApiClient.CreateSessionTanChallengeRequest): Promise<openApiClient.Challenge>;
536540
// (undocumented)
537541
createTrade(req: CreateTradeRequest, viaCryptoService?: boolean): Promise<openApiClient.CreateTradeResponse>;
@@ -552,6 +556,8 @@ export class AuthorizedApiContext {
552556
// (undocumented)
553557
deletePortfolio(portfolioId: string): Promise<openApiClient.OkResponseBody>;
554558
// (undocumented)
559+
deleteRecoveryPhrase(recoveryPhraseId: string): Promise<void>;
560+
// (undocumented)
555561
deleteTradeDraft(params: openApiClient.DeleteTradeDraftRequest): Promise<void>;
556562
// (undocumented)
557563
destroy(): void;
@@ -616,6 +622,8 @@ export class AuthorizedApiContext {
616622
// (undocumented)
617623
getQuote(p: GetQuoteRequest): Promise<openApiClient.GetQuoteResponse>;
618624
// (undocumented)
625+
getRecoveryPhrases(): Promise<openApiClient.GetRecoveryPhrasesResponse>;
626+
// (undocumented)
619627
getSecurityDetailedInfo(token: string): Promise<openApiClient.GenericTable>;
620628
// (undocumented)
621629
getSecurityQuotes(opts: {
@@ -787,6 +795,8 @@ function BrokerEnvironmentToJSONRecursive(value?: BrokerEnvironment | null, igno
787795
// @public (undocumented)
788796
export class Brokerize {
789797
constructor(cfg: BrokerizeConfig);
798+
// (undocumented)
799+
checkRecoveryPhrase(recoveryPhrase: string): Promise<openApiClient.CheckRecoveryPhrase200Response>;
790800
createAuth(authCtxCfg: AuthContextConfiguration, tokenRefreshCallback?: TokenRefreshCallback): Auth;
791801
createAuthorizedContext(authCtxCfg: AuthContextConfiguration, tokenRefreshCallback?: TokenRefreshCallback, customWebSocketClient?: BrokerizeWebSocketClient): AuthorizedApiContext;
792802
createCustomWebSocketClient({ url, auth, }: {
@@ -798,6 +808,8 @@ export class Brokerize {
798808
// (undocumented)
799809
getCognitoConfig(): CognitoPoolConfig | undefined;
800810
// (undocumented)
811+
obtainTokenByRecoveryPhrase(recoveryPhrase: string): Promise<AuthContextConfiguration>;
812+
// (undocumented)
801813
refreshGuestUser(refreshToken: string): Promise<GuestAuthContextConfiguration>;
802814
}
803815

@@ -1355,6 +1367,7 @@ function ChangeOrderResponseToJSONRecursive(value?: ChangeOrderResponse | null,
13551367
// @public
13561368
interface CheckRecoveryPhrase200Response {
13571369
expiresAt: Date;
1370+
userId: string;
13581371
}
13591372

13601373
// @public (undocumented)
@@ -4491,7 +4504,10 @@ declare namespace Models {
44914504
TradeStatistics,
44924505
GetPortfolioCalendarResponse,
44934506
PortfolioCalendarDateRange,
4494-
PortfolioCalendarItem
4507+
PortfolioCalendarItem,
4508+
GetRecoveryPhrasesResponse,
4509+
CreateRecoveryPhraseResult,
4510+
CheckRecoveryPhrase200Response
44954511
}
44964512
}
44974513
export { Models }

openapi.json

Lines changed: 7 additions & 9 deletions
Large diffs are not rendered by default.

src/authorizedApiContext.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,28 @@ export class AuthorizedApiContext {
628628
);
629629
}
630630

631+
async getRecoveryPhrases() {
632+
return this._userApi.getRecoveryPhrases(await this._initRequestInit());
633+
}
634+
635+
async createRecoveryPhrase(opts: { name: string }) {
636+
return this._userApi.createRecoveryPhrase(
637+
{
638+
createRecoveryPhraseParams: { name: opts.name },
639+
},
640+
await this._initRequestInit()
641+
);
642+
}
643+
644+
async deleteRecoveryPhrase(recoveryPhraseId: string) {
645+
return this._userApi.deleteRecoveryPhrase(
646+
{
647+
recoveryPhraseId,
648+
},
649+
await this._initRequestInit()
650+
);
651+
}
652+
631653
/**
632654
* Subscribe to security quotes. Note that this currently uses polling to load the quotes from the
633655
* API. This will be replaced with a websocket-based solution in the future, but we can keep this

src/index.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,22 @@ export class Brokerize {
8585
}
8686

8787
this._cfg = cfg;
88-
this._userApi = new openApiClient.UserApi(createConfiguration(cfg));
88+
89+
const postMiddleware = async (
90+
r: openApiClient.ResponseContext
91+
): Promise<void> => {
92+
const statusCode = r.response.status;
93+
if (statusCode >= 400) {
94+
const decJson =
95+
(await r.response.json()) as openApiClient.ErrorResponse;
96+
const err = new BrokerizeError(statusCode, decJson);
97+
throw err;
98+
}
99+
};
100+
101+
this._userApi = new openApiClient.UserApi(
102+
createConfiguration(cfg)
103+
).withPostMiddleware(postMiddleware);
89104
}
90105

91106
async refreshGuestUser(
@@ -203,6 +218,46 @@ export class Brokerize {
203218
});
204219
}
205220

221+
checkRecoveryPhrase(recoveryPhrase: string) {
222+
return this._userApi.checkRecoveryPhrase(
223+
{
224+
obtainTokenByRecoveryPhraseParams: { recoveryPhrase },
225+
},
226+
{
227+
headers: {
228+
"x-brkrz-client-id": this._cfg.clientId,
229+
"Content-Type": "application/json",
230+
},
231+
}
232+
);
233+
}
234+
235+
async obtainTokenByRecoveryPhrase(
236+
recoveryPhrase: string
237+
): Promise<AuthContextConfiguration> {
238+
const tokResult = await this._userApi.obtainTokenByRecoveryPhrase(
239+
{
240+
obtainTokenByRecoveryPhraseParams: { recoveryPhrase },
241+
},
242+
{
243+
headers: {
244+
"x-brkrz-client-id": this._cfg.clientId,
245+
"Content-Type": "application/json",
246+
},
247+
}
248+
);
249+
250+
const updatedAt = Date.now();
251+
return {
252+
type: "guest",
253+
idToken: "", // deprecated
254+
tokens: {
255+
updatedAt,
256+
response: tokResult as openApiClient.CreateGuestUserResponse,
257+
},
258+
};
259+
}
260+
206261
/**
207262
* Create a customized WebSocket client. You can override the WebSocket connection URL and the Auth implementation
208263
* for a custom token retrieval behavior.

src/modelExports.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,6 @@ export { TradeStatistics } from "./swagger/models/TradeStatistics";
168168
export { GetPortfolioCalendarResponse } from "./swagger/models/GetPortfolioCalendarResponse";
169169
export { PortfolioCalendarDateRange } from "./swagger/models/PortfolioCalendarDateRange";
170170
export { PortfolioCalendarItem } from "./swagger/models/PortfolioCalendarItem";
171+
export { GetRecoveryPhrasesResponse } from "./swagger/models/GetRecoveryPhrasesResponse";
172+
export { CreateRecoveryPhraseResult } from "./swagger/models/CreateRecoveryPhraseResult";
173+
export { CheckRecoveryPhrase200Response } from "./swagger/models/CheckRecoveryPhrase200Response";

0 commit comments

Comments
 (0)