@cllbk/ghl
Version:
A public Node.js compatible SDK for working with HighLevel's (GHL's) Version 2 API.
52 lines (44 loc) • 1.28 kB
text/typescript
import {
BadRequestDTO,
UnauthorizedDTO,
UnprocessableDTO,
} from "../../types/_global";
import { withExponentialBackoff } from "../../contexts/requestUtils";
import {
GetAccessCodeBodyDTO,
GetAccessCodeSuccessfulResponseDTO,
} from "../../types/oauth";
const baseUrl = "https://services.leadconnectorhq.com/oauth/token";
type ResponseTypes =
| GetAccessCodeSuccessfulResponseDTO
| BadRequestDTO
| UnauthorizedDTO
| UnprocessableDTO;
const getAccessToken = async (
options: GetAccessCodeBodyDTO,
): Promise<ResponseTypes | null> => {
const executeRequest = async (): Promise<ResponseTypes> => {
const response = await fetch(baseUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
body: new URLSearchParams(options),
});
if (!response.ok) {
const error = new Error(`Request failed with status ${response.status}`);
(error as any).response = response;
throw error;
}
return response.json();
};
try {
const data = await withExponentialBackoff(executeRequest);
return data;
} catch (error) {
console.error("Failed after retries:", error);
return null;
}
};
export default getAccessToken;