@larkiny/astro-github-loader 0.10.1 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,151 @@
1
+ import { Octokit } from "octokit";
2
+ import { createAppAuth } from "@octokit/auth-app";
3
+
4
+ /**
5
+ * Configuration options for GitHub App authentication
6
+ */
7
+ export interface GitHubAppAuthConfig {
8
+ /** GitHub App ID */
9
+ appId: string | number;
10
+ /** GitHub App private key (PEM format) */
11
+ privateKey: string;
12
+ /** GitHub App installation ID */
13
+ installationId: string | number;
14
+ }
15
+
16
+ /**
17
+ * Configuration options for Personal Access Token authentication
18
+ */
19
+ export interface GitHubPATAuthConfig {
20
+ /** Personal Access Token (classic or fine-grained) */
21
+ token: string;
22
+ }
23
+
24
+ /**
25
+ * Union type for authentication configuration
26
+ */
27
+ export type GitHubAuthConfig = GitHubAppAuthConfig | GitHubPATAuthConfig;
28
+
29
+ /**
30
+ * Type guard to check if config is GitHub App authentication
31
+ */
32
+ function isGitHubAppAuth(config: GitHubAuthConfig): config is GitHubAppAuthConfig {
33
+ return 'appId' in config && 'privateKey' in config && 'installationId' in config;
34
+ }
35
+
36
+ /**
37
+ * Creates an authenticated Octokit instance with support for both Personal Access Tokens
38
+ * and GitHub App authentication.
39
+ *
40
+ * **Rate Limits:**
41
+ * - Personal Access Token: 5,000 requests/hour
42
+ * - GitHub App: 15,000 requests/hour (3x higher)
43
+ *
44
+ * **GitHub App Setup:**
45
+ * 1. Create a GitHub App: https://github.com/settings/apps/new
46
+ * 2. Grant required permissions: Contents (read-only)
47
+ * 3. Install the app to your organization/repositories
48
+ * 4. Generate and download a private key
49
+ * 5. Note your App ID and Installation ID
50
+ *
51
+ * @param config - Authentication configuration (PAT or GitHub App)
52
+ * @returns Authenticated Octokit instance
53
+ *
54
+ * @example
55
+ * // Using Personal Access Token
56
+ * const octokit = createAuthenticatedOctokit({
57
+ * token: process.env.GITHUB_TOKEN
58
+ * });
59
+ *
60
+ * @example
61
+ * // Using GitHub App (recommended for higher rate limits)
62
+ * const octokit = createAuthenticatedOctokit({
63
+ * appId: process.env.GITHUB_APP_ID,
64
+ * privateKey: process.env.GITHUB_APP_PRIVATE_KEY,
65
+ * installationId: process.env.GITHUB_APP_INSTALLATION_ID
66
+ * });
67
+ */
68
+ export function createAuthenticatedOctokit(config: GitHubAuthConfig): Octokit {
69
+ if (isGitHubAppAuth(config)) {
70
+ // GitHub App authentication (15,000 requests/hour)
71
+ return new Octokit({
72
+ authStrategy: createAppAuth,
73
+ auth: {
74
+ appId: config.appId,
75
+ privateKey: config.privateKey,
76
+ installationId: config.installationId,
77
+ },
78
+ });
79
+ } else {
80
+ // Personal Access Token authentication (5,000 requests/hour)
81
+ return new Octokit({
82
+ auth: config.token,
83
+ });
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Creates an authenticated Octokit instance from environment variables.
89
+ * Automatically detects whether to use GitHub App or PAT authentication based on
90
+ * which environment variables are present.
91
+ *
92
+ * **Priority:**
93
+ * 1. GitHub App (if GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY, and GITHUB_APP_INSTALLATION_ID are set)
94
+ * 2. Personal Access Token (if GITHUB_TOKEN is set)
95
+ *
96
+ * **Environment Variables:**
97
+ *
98
+ * For GitHub App (recommended - 15,000 req/hour):
99
+ * - `GITHUB_APP_ID` - Your GitHub App ID
100
+ * - `GITHUB_APP_PRIVATE_KEY` - Private key in PEM format (can be multiline or base64 encoded)
101
+ * - `GITHUB_APP_INSTALLATION_ID` - Installation ID for your org/repos
102
+ *
103
+ * For Personal Access Token (5,000 req/hour):
104
+ * - `GITHUB_TOKEN` - Your personal access token
105
+ *
106
+ * @returns Authenticated Octokit instance
107
+ * @throws Error if no valid authentication credentials are found
108
+ *
109
+ * @example
110
+ * // In your Astro config or content.config.ts
111
+ * const octokit = createOctokitFromEnv();
112
+ */
113
+ export function createOctokitFromEnv(): Octokit {
114
+ // Check for GitHub App credentials (preferred)
115
+ const appId = process.env.GITHUB_APP_ID;
116
+ const privateKey = process.env.GITHUB_APP_PRIVATE_KEY;
117
+ const installationId = process.env.GITHUB_APP_INSTALLATION_ID;
118
+
119
+ if (appId && privateKey && installationId) {
120
+ // Decode private key if it's base64 encoded (for easier .env storage)
121
+ let decodedPrivateKey = privateKey;
122
+ if (!privateKey.includes('BEGIN RSA PRIVATE KEY') && !privateKey.includes('BEGIN PRIVATE KEY')) {
123
+ try {
124
+ decodedPrivateKey = Buffer.from(privateKey, 'base64').toString('utf-8');
125
+ } catch {
126
+ // If decoding fails, use as-is (might already be plaintext)
127
+ }
128
+ }
129
+
130
+ console.log('✓ Using GitHub App authentication (15,000 requests/hour)');
131
+ return createAuthenticatedOctokit({
132
+ appId,
133
+ privateKey: decodedPrivateKey,
134
+ installationId,
135
+ });
136
+ }
137
+
138
+ // Fallback to Personal Access Token
139
+ const token = process.env.GITHUB_TOKEN;
140
+ if (token) {
141
+ console.log('✓ Using Personal Access Token authentication (5,000 requests/hour)');
142
+ console.log('💡 Consider switching to GitHub App for 3x higher rate limits');
143
+ return createAuthenticatedOctokit({ token });
144
+ }
145
+
146
+ throw new Error(
147
+ 'No GitHub authentication credentials found. Please set either:\n' +
148
+ ' - GITHUB_TOKEN (for PAT authentication)\n' +
149
+ ' - GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY, GITHUB_APP_INSTALLATION_ID (for GitHub App authentication)'
150
+ );
151
+ }