Open kimpro82 opened 10 months ago
Actually now I'm not certain why I am doing this task with TypeScript, not Python, anyway ……
Here's how you can handle token management using environment variables in TypeScript:
Install the dotenv Package: Begin by installing the dotenv
package to manage environment variables and load them from a .env
file.
npm install dotenv
Create a .env File: Create a .env
file at the root of your project and store your token securely within it.
// Contents of the .env file
GITHUB_TOKEN=YOUR_AUTH_TOKEN
Utilize Environment Variables in TypeScript Code: Next, import dotenv
in your TypeScript code to load the environment variables from the .env
file.
import * as dotenv from 'dotenv';
import axios, { AxiosRequestConfig } from 'axios';
// Load the .env file
dotenv.config();
// Assign the token variable by accessing the environment variable
const token = process.env.GITHUB_TOKEN;
if (!token) {
throw new Error('Token not found.');
}
// Configure for API requests
const config: AxiosRequestConfig = {
headers: {
Authorization: `token ${token}`
}
};
// Set up the GitHub API endpoint
const apiUrl = 'https://api.github.com/repos/kimpro82/MyPractice/languages';
// Send a GET request using Axios
axios.get(apiUrl, config)
.then(response => {
console.log(response.data); // API response data
})
.catch(error => {
console.error('API request failed:', error);
});
By calling dotenv.config()
, the .env
file gets loaded, and process.env.GITHUB_TOKEN
retrieves the GITHUB_TOKEN
environment variable. This method allows you to manage your token securely in an external file, enhancing security by preventing direct exposure within your source code.
Hmm the url https://api.github.com/repos/kimpro82/MyPractice/languages and GetGithubUserStats.ts
work again??
Terrible
https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting