-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Project import generated by Copybara.
GitOrigin-RevId: e71af250470d3266967f9e8621fecc006de81215
- Loading branch information
1 parent
387b1ac
commit 359c45c
Showing
21 changed files
with
409 additions
and
403 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
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 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 |
---|---|---|
@@ -1,12 +1,53 @@ | ||
import { AxiosInstance } from "axios"; | ||
import { BaseServiceParams } from ".."; | ||
import { Error as JsonError } from ".."; | ||
|
||
/** | ||
* Base class for services that communicate with the API. | ||
*/ | ||
export abstract class BaseService { | ||
/** | ||
* Create a new service. | ||
* @param params The AxiosInstance to send HTTP requests to the API. | ||
* @param params The parameters to use for the service. | ||
*/ | ||
constructor(protected client: AxiosInstance) {} | ||
constructor(private params: BaseServiceParams) {} | ||
protected async fetch( | ||
input: string, | ||
init?: RequestInit | undefined | ||
): Promise<Response> { | ||
init = init ?? {}; | ||
init.headers = init.headers ?? {}; | ||
init.headers = { | ||
Authorization: this.params.apiKey, | ||
"Content-Type": "application/json", | ||
...init.headers, | ||
}; | ||
if (!input.startsWith("http")) input = this.params.baseUrl + input; | ||
|
||
const response = await fetch(input, init); | ||
|
||
if (response.status >= 400) { | ||
let json: JsonError | undefined; | ||
const text = await response.text(); | ||
if (text) { | ||
try { | ||
json = JSON.parse(text); | ||
} catch { | ||
/* empty */ | ||
} | ||
if (json?.error) throw new Error(json.error); | ||
throw new Error(text); | ||
} | ||
throw new Error(`HTTP Error: ${response.status} ${response.statusText}`); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
protected async fetchJson<T>( | ||
input: string, | ||
init?: RequestInit | undefined | ||
): Promise<T> { | ||
const response = await this.fetch(input, init); | ||
return response.json() as Promise<T>; | ||
} | ||
} |
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 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 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 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 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "@/services/realtime/factory"; | ||
export * from "@/services/realtime/service"; | ||
export * from "./factory"; | ||
export * from "./service"; |
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
Oops, something went wrong.