-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Replace axios with fetch in postCustomer method
Refactor the postCustomer method in the Unguess class to use the fetch API instead of axios for making HTTP requests. This change improves the code's performance and reduces the number of dependencies.
- Loading branch information
1 parent
a3649b2
commit abc0d66
Showing
3 changed files
with
105 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
class Unguess { | ||
private baseUrl: string; | ||
private username: string; | ||
private password: string; | ||
|
||
constructor(baseUrl: string, username: string, password: string) { | ||
this.baseUrl = baseUrl; | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
/** | ||
* Private method to fetch a token for API requests | ||
*/ | ||
private async getToken(): Promise<string> { | ||
const response = await fetch(`${this.baseUrl}/authenticate`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
username: this.username, | ||
password: this.password, | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to authenticate: " + response.statusText); | ||
} | ||
|
||
const data = await response.json(); | ||
if (!data.token) { | ||
throw new Error("Authentication failed: Token not found"); | ||
} | ||
|
||
return data.token; | ||
} | ||
|
||
/** | ||
* Private method to perform authenticated POST requests | ||
*/ | ||
private async authPost( | ||
path: string, | ||
body: Record<string, any> | ||
): Promise<any> { | ||
const token = await this.getToken(); | ||
const response = await fetch(`${this.baseUrl}${path}`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to post to ${path}: ${response.statusText}`); | ||
} | ||
|
||
return response.json(); | ||
} | ||
|
||
/** | ||
* Public method to post a new customer | ||
*/ | ||
public async postCustomer({ | ||
userId, | ||
name, | ||
}: { | ||
userId: number; | ||
name: string; | ||
}): Promise<{ id: number; name: string }> { | ||
const body = { | ||
company: name, | ||
pm_id: userId, | ||
}; | ||
const result = await this.authPost("/workspaces", body); | ||
return { | ||
id: result.id, | ||
name: result.name, | ||
}; | ||
} | ||
} | ||
|
||
export default Unguess; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.