@@ -4,6 +4,21 @@ import { stringify } from 'javascript-stringify'
4
4
import type { CodeSample , Operation } from '@/rest/components/types'
5
5
import { type VersionItem } from '@/frame/components/context/MainContext'
6
6
7
+ // Helper function to determine if authentication should be omitted
8
+ function shouldOmitAuthentication ( operation : Operation , currentVersion : string ) : boolean {
9
+ // Only omit auth for operations that explicitly allow permissionless access
10
+ if ( ! operation ?. progAccess ?. allowPermissionlessAccess ) {
11
+ return false
12
+ }
13
+
14
+ // Only omit auth on dotcom versions (free-pro-team, enterprise-cloud)
15
+ // GHES and other versions still require authentication
16
+ const isDotcomVersion =
17
+ currentVersion . startsWith ( 'free-pro-team' ) || currentVersion . startsWith ( 'enterprise-cloud' )
18
+
19
+ return isDotcomVersion
20
+ }
21
+
7
22
// Helper function to escape shell values containing single quotes (contractions)
8
23
// This prevents malformed shell commands when contractions like "there's" are used
9
24
function escapeShellValue ( value : string ) : string {
@@ -46,6 +61,9 @@ export function getShellExample(
46
61
contentTypeHeader = '-H "Content-Type: multipart/form-data"'
47
62
}
48
63
64
+ // Check if we should omit authentication for this operation
65
+ const omitAuth = shouldOmitAuthentication ( operation , currentVersion )
66
+
49
67
// GHES Manage API requests differ from the dotcom API requests and make use of multipart/form-data and json content types
50
68
if ( operation . subcategory === 'manage-ghes' ) {
51
69
// GET requests don't have a requestBody set, therefore let's default them to application/json
@@ -94,7 +112,7 @@ export function getShellExample(
94
112
}
95
113
}
96
114
97
- let authHeader = '-H "Authorization: Bearer <YOUR-TOKEN>"'
115
+ let authHeader = omitAuth ? '' : '-H "Authorization: Bearer <YOUR-TOKEN>"'
98
116
let apiVersionHeader =
99
117
allVersions [ currentVersion ] . apiVersions . length > 0 &&
100
118
allVersions [ currentVersion ] . latestApiVersion
@@ -116,6 +134,15 @@ export function getShellExample(
116
134
acceptHeader = acceptHeader === `-H "Accept: application/vnd.github+json"` ? '' : acceptHeader
117
135
}
118
136
137
+ // For unauthenticated endpoints, remove the auth header completely
138
+ if (
139
+ omitAuth &&
140
+ operation . subcategory !== 'management-console' &&
141
+ operation . subcategory !== 'manage-ghes'
142
+ ) {
143
+ authHeader = ''
144
+ }
145
+
119
146
if ( operation ?. progAccess ?. basicAuth ) {
120
147
authHeader = '-u "<YOUR_CLIENT_ID>:<YOUR_CLIENT_SECRET>"'
121
148
}
@@ -306,6 +333,8 @@ export function getJSExample(
306
333
currentVersion : string ,
307
334
allVersions : Record < string , VersionItem > ,
308
335
) {
336
+ // Check if we should omit authentication for this operation
337
+ const omitAuth = shouldOmitAuthentication ( operation , currentVersion )
309
338
const parameters : { [ key : string ] : string | object } = { }
310
339
311
340
if ( codeSample . request ) {
@@ -359,9 +388,15 @@ export function getJSExample(
359
388
360
389
const comment = `// Octokit.js\n// https://github.com/octokit/core.js#readme\n`
361
390
const authOctokit = `const octokit = new Octokit(${ stringify ( { auth : 'YOUR-TOKEN' } , null , 2 ) } )\n\n`
391
+ const unauthenticatedOctokit = `const octokit = new Octokit()\n\n`
362
392
const oauthOctokit = `import { createOAuthAppAuth } from "@octokit/auth-oauth-app"\n\nconst octokit = new Octokit({\n authStrategy: createOAuthAppAuth,\n auth:{\n clientType: 'oauth-app',\n clientId: '<YOUR_CLIENT ID>',\n clientSecret: '<YOUR_CLIENT SECRET>'\n }\n})\n\n`
363
393
const isBasicAuth = operation ?. progAccess ?. basicAuth
364
- const authString = isBasicAuth ? oauthOctokit : authOctokit
394
+ let authString = isBasicAuth ? oauthOctokit : authOctokit
395
+
396
+ // Use unauthenticated Octokit for endpoints that allow permissionless access
397
+ if ( omitAuth ) {
398
+ authString = unauthenticatedOctokit
399
+ }
365
400
366
401
return `${ comment } ${ authString } await octokit.request('${ operation . verb . toUpperCase ( ) } ${
367
402
operation . requestPath
0 commit comments