Skip to content
This repository was archived by the owner on Feb 18, 2021. It is now read-only.

Commit 778d45d

Browse files
committed
Restructured client architecture. Added security client
1 parent 8270bbb commit 778d45d

File tree

15 files changed

+228
-103
lines changed

15 files changed

+228
-103
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ module.exports = {
1212
rules: {
1313
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
1414
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
15+
"@typescript-eslint/no-explicit-any": "off",
1516
},
1617
};

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 0.2.0
2+
3+
- Added security client
4+
- Login functionality
5+
- Confirm account functionality
6+
- Restructured client architecture. All clients now fall under a portfolio client.
7+
18
# 0.1.2
29

310
- Fixed bug that incorrectly configured default api URL with extra trailing slash

src/apis/contact/client.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
1-
import { ClientConfig, Configurable } from "../../common/config";
2-
import { ContactMessageService } from "./messages";
1+
import axios, { AxiosInstance, AxiosResponse } from "axios";
2+
import { BaseClient, ClientConfig } from "../../common/client";
3+
import { axiosRequestConfig, PortfolioRequest } from "../../common/request";
4+
import { PortfolioResponse } from "../../common/response";
5+
import { ContactMessage, Reason, Sender } from "./models";
36

4-
export class ContactClient implements Configurable {
5-
readonly config: ClientConfig;
6-
readonly messages: ContactMessageService;
7+
export type CreateContactMessageRequest = PortfolioRequest<
8+
CreateContactMessageForm
9+
>;
710

8-
constructor(config: ClientConfig) {
9-
this.config = config;
10-
this.messages = new ContactMessageService(config);
11+
export interface CreateContactMessageForm {
12+
message: string;
13+
reason: Reason;
14+
sender: Sender;
15+
}
16+
17+
export type ContactMessageResponse = PortfolioResponse<ContactMessage>;
18+
19+
export class ContactClient extends BaseClient {
20+
constructor(config: ClientConfig, axiosInstance: AxiosInstance = axios) {
21+
super(config, axiosInstance);
22+
}
23+
24+
/**
25+
* Creates a new contact message
26+
*
27+
* @param request Sent to portfolio API
28+
*/
29+
createMessage(
30+
request: CreateContactMessageRequest
31+
): Promise<AxiosResponse<ContactMessageResponse>> {
32+
const url = `${this.config.host}/mail`;
33+
const config = axiosRequestConfig(request);
34+
35+
return this.axiosInstance.post<ContactMessageResponse>(
36+
url,
37+
request.body,
38+
config
39+
);
1140
}
1241
}

src/apis/contact/index.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/apis/contact/messages.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/apis/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ClientConfig } from "../common/client";
2+
import { ContactClient } from "./contact/client";
3+
import { SecurityClient } from "./security/client";
4+
5+
const defaultConfig: ClientConfig = {
6+
host: "https://b623pa888e.execute-api.us-east-2.amazonaws.com/api",
7+
};
8+
9+
export class PortfolioClient {
10+
readonly config: ClientConfig;
11+
readonly contact: ContactClient;
12+
readonly security: SecurityClient;
13+
14+
constructor(config: ClientConfig) {
15+
this.config = config;
16+
this.contact = new ContactClient(config);
17+
this.security = new SecurityClient(config);
18+
}
19+
}
20+
21+
/**
22+
* Factory method used for configuring and building a new contact client.
23+
*/
24+
export const portfolio = (
25+
config: ClientConfig = defaultConfig
26+
): PortfolioClient => {
27+
return new PortfolioClient(config);
28+
};
29+
30+
export * from "./contact/client";
31+
export * from "./contact/models";
32+
export * from "./security/client";
33+
export * from "./security/models";

src/apis/security/client.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import axios, { AxiosInstance, AxiosResponse } from "axios";
2+
import { BaseClient, ClientConfig, PortfolioResponse } from "../../common";
3+
import { axiosRequestConfig, PortfolioRequest } from "../../common/request";
4+
import { TokenBody } from "./models";
5+
6+
export type LoginRequest = PortfolioRequest<LoginForm>;
7+
8+
export interface LoginForm {
9+
username: string;
10+
password: string;
11+
}
12+
13+
export type LoginResponse = PortfolioResponse<TokenBody>;
14+
15+
export type UpdatePasswordRequest = PortfolioRequest<UpdatePasswordForm>;
16+
17+
export interface UpdatePasswordForm {
18+
username: string;
19+
oldPassword: string;
20+
newPassword: string;
21+
}
22+
23+
export class SecurityClient extends BaseClient {
24+
constructor(config: ClientConfig, axiosInstance: AxiosInstance = axios) {
25+
super(config, axiosInstance);
26+
}
27+
28+
login(request: LoginRequest): Promise<AxiosResponse<LoginResponse>> {
29+
const url = `${this.config.host}/login`;
30+
const config = axiosRequestConfig(request);
31+
32+
return this.axiosInstance.post<LoginResponse>(url, request.body, config);
33+
}
34+
35+
confirmAccount(
36+
request: UpdatePasswordRequest
37+
): Promise<AxiosResponse<LoginResponse>> {
38+
const url = `${this.config.host}/confirm-account`;
39+
const config = axiosRequestConfig(request);
40+
41+
return this.axiosInstance.post<LoginResponse>(url, request.body, config);
42+
}
43+
}

src/apis/security/models.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface TokenBody {
2+
idToken: string;
3+
accessToken: string;
4+
refreshToken: string;
5+
tokenType: string;
6+
}

src/common/client.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import axios, { AxiosInstance } from "axios";
2+
3+
export interface ClientConfig {
4+
host: string;
5+
jwt?: string;
6+
}
7+
8+
export abstract class BaseClient {
9+
readonly config: ClientConfig;
10+
protected readonly axiosInstance: AxiosInstance;
11+
12+
constructor(config: ClientConfig, axiosInstance: AxiosInstance = axios) {
13+
this.config = config;
14+
this.axiosInstance = axiosInstance;
15+
}
16+
}

src/common/config.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)