githubauthreq
Advanced tools
Comparing version 5.19.0 to 6.0.0-next.1605169857.b79abf92b188b440f73cf03cbdfd35f106dfbe15
@@ -1,10 +0,21 @@ | ||
/// <reference types="node" /> | ||
import type { StrictUnion } from 'simplytyped' | ||
import type { StrictUnion } from 'simplytyped'; | ||
/** If the variable `GITHUB_API_URL` or `GITHUB_API` exists, use that, otherwise use the value `https://api.github.com`. */ | ||
declare type GitHubApiUrl = StrictUnion<{ | ||
/** https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables */ | ||
GITHUB_API_URL: string; | ||
} | { | ||
/** https://github.com/search?l=JavaScript&q=GITHUB_API&type=Code */ | ||
GITHUB_API: string; | ||
}>; | ||
/** | ||
* If the variable `GITHUB_ACCESS_TOKEN` exists, use that according to: | ||
* If the variable `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN` exists, use that according to: | ||
* https://developer.github.com/v3/#oauth2-token-sent-as-a-parameter | ||
*/ | ||
interface GitHubToken { | ||
GITHUB_ACCESS_TOKEN: string | ||
} | ||
declare type GitHubToken = StrictUnion<{ | ||
/** https://github.com/search?q=GITHUB_ACCESS_TOKEN&type=code */ | ||
GITHUB_ACCESS_TOKEN: string; | ||
} | { | ||
/** https://docs.github.com/en/free-pro-team@latest/actions/reference/authentication-in-a-workflow#about-the-github_token-secret */ | ||
GITHUB_TOKEN: string; | ||
}>; | ||
/** | ||
@@ -15,52 +26,80 @@ * If the variables `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` exist, use that according to: | ||
interface GitHubClient { | ||
GITHUB_CLIENT_ID: string | ||
GITHUB_CLIENT_SECRET: string | ||
GITHUB_CLIENT_ID: string; | ||
GITHUB_CLIENT_SECRET: string; | ||
} | ||
/** | ||
* Provide `GITHUB_ACCESS_TOKEN` or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` | ||
* Provide `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN`, | ||
* or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`, | ||
* optionally with `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN`. | ||
* https://developer.github.com/v3/#authentication | ||
*/ | ||
export declare type GitHubCredentials = StrictUnion<GitHubToken | GitHubClient> | ||
/** GitHub credentialed environment */ | ||
export declare type GitHubEnv = NodeJS.ProcessEnv & GitHubCredentials | ||
export declare type GitHubCredentials = StrictUnion<GitHubToken | GitHubClient> & Partial<GitHubApiUrl>; | ||
/** | ||
* Check whether or not sufficient github credentials were supplied | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns `true` if valid, otherwise throws if invalid | ||
* Check whether or not sufficient GitHub credentials were supplied. | ||
* @returns `true` if valid | ||
* @throws if invalid | ||
*/ | ||
export declare function validate(credentials?: GitHubCredentials): boolean | ||
export declare function validate(credentials: GitHubCredentials): boolean; | ||
/** | ||
* Fetch the GitHub Auth Query String. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The query string that you should append to your github API request url. Will be an empty string if no valid credentials were provided. | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
export declare function getParams(credentials?: GitHubCredentials): string | ||
export declare const githubQueryString: typeof getParams | ||
export declare const fetch: typeof getParams | ||
export default getParams | ||
export declare function getAccessToken(credentials: GitHubCredentials): string | null; | ||
/** | ||
* Fetch the GitHub Authorization Header. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The Authorization header to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* @param credentials The params to use for the authorization variables. | ||
* @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
* @returns The search params that you should append to your github API request url. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
export declare function getAuthHeader(credentials?: GitHubCredentials): string | ||
export declare const githubAuthorizationHeader: typeof getAuthHeader | ||
export declare function getSearchParams(credentials: GitHubCredentials, params?: URLSearchParams): URLSearchParams; | ||
/** | ||
* Fetch the headers to attach to the request to the GitHub API | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The headers to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
export declare function getHeaders( | ||
credentials?: GitHubCredentials | ||
): { | ||
Accept: string | ||
Authorization: string | ||
} | ||
export declare function getQueryString(credentials: GitHubCredentials): string; | ||
/** | ||
* Redact any github credentials from a string. | ||
* Get the GitHub Authorization Header. | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
*/ | ||
export declare function getAuthHeader(credentials: GitHubCredentials): string; | ||
/** | ||
* Get the headers to attach to the request to the GitHub API. | ||
* Use as the headers object within {@link fetch} calls. | ||
* Make sure to use with {@link getApiUrl} to make sure you are using the desired hostname. | ||
*/ | ||
export declare function getHeaders(credentials: GitHubCredentials): { | ||
Accept: string; | ||
Authorization: string; | ||
}; | ||
/** | ||
* Get the desired Github API URL string. | ||
* As this does not include any credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getURL} to get a credentialed URL. | ||
*/ | ||
export declare function getApiUrl(credentials: GitHubCredentials): string; | ||
/** | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
*/ | ||
export declare function getURL(credentials: GitHubCredentials, props?: { | ||
pathname?: string; | ||
searchParams?: URLSearchParams; | ||
}): URL; | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
*/ | ||
export declare function getUrl(credentials: GitHubCredentials, props?: { | ||
pathname?: string; | ||
searchParams?: URLSearchParams; | ||
}): string; | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
export declare function redactParams(value: string): string | ||
export declare const redact: typeof redactParams | ||
//# sourceMappingURL=index.d.ts.map | ||
export declare function redactSearchParams(value: string): string; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
/** | ||
* Check whether or not sufficient github credentials were supplied | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns `true` if valid, otherwise throws if invalid | ||
* Check whether or not sufficient GitHub credentials were supplied. | ||
* @returns `true` if valid | ||
* @throws if invalid | ||
*/ | ||
export function validate( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
if ( | ||
credentials.GITHUB_ACCESS_TOKEN || | ||
(credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) | ||
) { | ||
return true | ||
} else { | ||
throw new Error( | ||
'missing github credentials; provide `GITHUB_ACCESS_TOKEN` or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`' | ||
) | ||
} | ||
export function validate(credentials) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken || | ||
(credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET)) { | ||
return true; | ||
} | ||
else { | ||
throw new Error('missing github credentials; provide `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN`, or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`'); | ||
} | ||
} | ||
/** | ||
* Fetch the GitHub Auth Query String. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The query string that you should append to your github API request url. Will be an empty string if no valid credentials were provided. | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
export function getParams( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
if (credentials.GITHUB_ACCESS_TOKEN) { | ||
return `access_token=${credentials.GITHUB_ACCESS_TOKEN}` | ||
} else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `client_id=${credentials.GITHUB_CLIENT_ID}&client_secret=${credentials.GITHUB_CLIENT_SECRET}` | ||
} else { | ||
throw new Error('missing github credentials for query string') | ||
} | ||
export function getAccessToken(credentials) { | ||
return credentials.GITHUB_ACCESS_TOKEN || credentials.GITHUB_TOKEN || null; | ||
} | ||
// Aliases | ||
export const githubQueryString = getParams | ||
export const fetch = getParams | ||
export default getParams | ||
/** | ||
* Fetch the GitHub Authorization Header. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The Authorization header to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* @param credentials The params to use for the authorization variables. | ||
* @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
* @returns The search params that you should append to your github API request url. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
export function getAuthHeader( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
if (credentials.GITHUB_ACCESS_TOKEN) { | ||
return `token ${credentials.GITHUB_ACCESS_TOKEN}` | ||
} else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `Basic ${credentials.GITHUB_CLIENT_ID}:${credentials.GITHUB_CLIENT_SECRET}` | ||
} else { | ||
throw new Error('missing github credentials for authorization header') | ||
} | ||
export function getSearchParams(credentials, params = new URLSearchParams()) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken) { | ||
params.set('access_token', accessToken); | ||
} | ||
else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
params.set('client_id', credentials.GITHUB_CLIENT_ID); | ||
params.set('client_secret', credentials.GITHUB_CLIENT_SECRET); | ||
} | ||
else { | ||
// throw with detail errors | ||
validate(credentials); | ||
// if that doesn't throw, then fallback to this | ||
throw new Error('invalid github credentials'); | ||
} | ||
return params; | ||
} | ||
// Aliases | ||
export const githubAuthorizationHeader = getAuthHeader | ||
/** | ||
* Fetch the headers to attach to the request to the GitHub API | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The headers to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
export function getHeaders( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
} | ||
export function getQueryString(credentials) { | ||
return getSearchParams(credentials).toString(); | ||
} | ||
/** | ||
* Redact any github credentials from a string. | ||
* Get the GitHub Authorization Header. | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
*/ | ||
export function getAuthHeader(credentials) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken) { | ||
return `token ${accessToken}`; | ||
} | ||
else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `Basic ${credentials.GITHUB_CLIENT_ID}:${credentials.GITHUB_CLIENT_SECRET}`; | ||
} | ||
else { | ||
throw new Error('missing github credentials for authorization header'); | ||
} | ||
} | ||
/** | ||
* Get the headers to attach to the request to the GitHub API. | ||
* Use as the headers object within {@link fetch} calls. | ||
* Make sure to use with {@link getApiUrl} to make sure you are using the desired hostname. | ||
*/ | ||
export function getHeaders(credentials) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
}; | ||
} | ||
/** | ||
* Get the desired Github API URL string. | ||
* As this does not include any credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getURL} to get a credentialed URL. | ||
*/ | ||
export function getApiUrl(credentials) { | ||
return (credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com'); | ||
} | ||
/** | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
*/ | ||
export function getURL(credentials, props) { | ||
// fetch url | ||
const url = new URL(getApiUrl(credentials)); | ||
// apply params | ||
getSearchParams(credentials, url.searchParams); | ||
if (props === null || props === void 0 ? void 0 : props.searchParams) | ||
props.searchParams.forEach((value, key) => url.searchParams.set(key, value)); | ||
// apply pathname via append, as otherwise urls like `https://bevry.me/api/github` will not work | ||
if (props === null || props === void 0 ? void 0 : props.pathname) | ||
url.pathname += props.pathname; | ||
// return | ||
return url; | ||
} | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
*/ | ||
export function getUrl(credentials, props) { | ||
return getURL(credentials, props).toString(); | ||
} | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
export function redactParams(value) { | ||
return value.replace( | ||
/(&?)(access_token|client_id|client_secret)=\w+/gi, | ||
'$1$2=REDACTED' | ||
) | ||
export function redactSearchParams(value) { | ||
return value.replace(/(&?)(access_token|client_id|client_secret)=\w+/gi, '$1$2=REDACTED'); | ||
} | ||
// Alias | ||
export const redact = redactParams |
/** | ||
* Check whether or not sufficient github credentials were supplied | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns `true` if valid, otherwise throws if invalid | ||
* Check whether or not sufficient GitHub credentials were supplied. | ||
* @returns `true` if valid | ||
* @throws if invalid | ||
*/ | ||
export function validate( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
if ( | ||
credentials.GITHUB_ACCESS_TOKEN || | ||
(credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) | ||
) { | ||
return true | ||
} else { | ||
throw new Error( | ||
'missing github credentials; provide `GITHUB_ACCESS_TOKEN` or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`' | ||
) | ||
} | ||
export function validate(credentials) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken || | ||
(credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET)) { | ||
return true; | ||
} | ||
else { | ||
throw new Error('missing github credentials; provide `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN`, or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`'); | ||
} | ||
} | ||
/** | ||
* Fetch the GitHub Auth Query String. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The query string that you should append to your github API request url. Will be an empty string if no valid credentials were provided. | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
export function getParams( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
if (credentials.GITHUB_ACCESS_TOKEN) { | ||
return `access_token=${credentials.GITHUB_ACCESS_TOKEN}` | ||
} else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `client_id=${credentials.GITHUB_CLIENT_ID}&client_secret=${credentials.GITHUB_CLIENT_SECRET}` | ||
} else { | ||
throw new Error('missing github credentials for query string') | ||
} | ||
export function getAccessToken(credentials) { | ||
return credentials.GITHUB_ACCESS_TOKEN || credentials.GITHUB_TOKEN || null; | ||
} | ||
// Aliases | ||
export const githubQueryString = getParams | ||
export const fetch = getParams | ||
export default getParams | ||
/** | ||
* Fetch the GitHub Authorization Header. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The Authorization header to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* @param credentials The params to use for the authorization variables. | ||
* @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
* @returns The search params that you should append to your github API request url. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
export function getAuthHeader( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
if (credentials.GITHUB_ACCESS_TOKEN) { | ||
return `token ${credentials.GITHUB_ACCESS_TOKEN}` | ||
} else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `Basic ${credentials.GITHUB_CLIENT_ID}:${credentials.GITHUB_CLIENT_SECRET}` | ||
} else { | ||
throw new Error('missing github credentials for authorization header') | ||
} | ||
export function getSearchParams(credentials, params = new URLSearchParams()) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken) { | ||
params.set('access_token', accessToken); | ||
} | ||
else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
params.set('client_id', credentials.GITHUB_CLIENT_ID); | ||
params.set('client_secret', credentials.GITHUB_CLIENT_SECRET); | ||
} | ||
else { | ||
// throw with detail errors | ||
validate(credentials); | ||
// if that doesn't throw, then fallback to this | ||
throw new Error('invalid github credentials'); | ||
} | ||
return params; | ||
} | ||
// Aliases | ||
export const githubAuthorizationHeader = getAuthHeader | ||
/** | ||
* Fetch the headers to attach to the request to the GitHub API | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The headers to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
export function getHeaders( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
} | ||
export function getQueryString(credentials) { | ||
return getSearchParams(credentials).toString(); | ||
} | ||
/** | ||
* Redact any github credentials from a string. | ||
* Get the GitHub Authorization Header. | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
*/ | ||
export function getAuthHeader(credentials) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken) { | ||
return `token ${accessToken}`; | ||
} | ||
else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `Basic ${credentials.GITHUB_CLIENT_ID}:${credentials.GITHUB_CLIENT_SECRET}`; | ||
} | ||
else { | ||
throw new Error('missing github credentials for authorization header'); | ||
} | ||
} | ||
/** | ||
* Get the headers to attach to the request to the GitHub API. | ||
* Use as the headers object within {@link fetch} calls. | ||
* Make sure to use with {@link getApiUrl} to make sure you are using the desired hostname. | ||
*/ | ||
export function getHeaders(credentials) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
}; | ||
} | ||
/** | ||
* Get the desired Github API URL string. | ||
* As this does not include any credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getURL} to get a credentialed URL. | ||
*/ | ||
export function getApiUrl(credentials) { | ||
return (credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com'); | ||
} | ||
/** | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
*/ | ||
export function getURL(credentials, props) { | ||
// fetch url | ||
const url = new URL(getApiUrl(credentials)); | ||
// apply params | ||
getSearchParams(credentials, url.searchParams); | ||
if (props === null || props === void 0 ? void 0 : props.searchParams) | ||
props.searchParams.forEach((value, key) => url.searchParams.set(key, value)); | ||
// apply pathname via append, as otherwise urls like `https://bevry.me/api/github` will not work | ||
if (props === null || props === void 0 ? void 0 : props.pathname) | ||
url.pathname += props.pathname; | ||
// return | ||
return url; | ||
} | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
*/ | ||
export function getUrl(credentials, props) { | ||
return getURL(credentials, props).toString(); | ||
} | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
export function redactParams(value) { | ||
return value.replace( | ||
/(&?)(access_token|client_id|client_secret)=\w+/gi, | ||
'$1$2=REDACTED' | ||
) | ||
export function redactSearchParams(value) { | ||
return value.replace(/(&?)(access_token|client_id|client_secret)=\w+/gi, '$1$2=REDACTED'); | ||
} | ||
// Alias | ||
export const redact = redactParams |
@@ -1,3 +0,1 @@ | ||
{ | ||
"type": "module" | ||
} | ||
{"type": "module"} |
@@ -1,91 +0,136 @@ | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }) | ||
exports.redact = exports.redactParams = exports.getHeaders = exports.githubAuthorizationHeader = exports.getAuthHeader = exports.fetch = exports.githubQueryString = exports.getParams = exports.validate = void 0 | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.redactSearchParams = exports.getUrl = exports.getURL = exports.getApiUrl = exports.getHeaders = exports.getAuthHeader = exports.getQueryString = exports.getSearchParams = exports.getAccessToken = exports.validate = void 0; | ||
/** | ||
* Check whether or not sufficient github credentials were supplied | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns `true` if valid, otherwise throws if invalid | ||
* Check whether or not sufficient GitHub credentials were supplied. | ||
* @returns `true` if valid | ||
* @throws if invalid | ||
*/ | ||
function validate( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
if ( | ||
credentials.GITHUB_ACCESS_TOKEN || | ||
(credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) | ||
) { | ||
return true | ||
} else { | ||
throw new Error( | ||
'missing github credentials; provide `GITHUB_ACCESS_TOKEN` or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`' | ||
) | ||
} | ||
function validate(credentials) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken || | ||
(credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET)) { | ||
return true; | ||
} | ||
else { | ||
throw new Error('missing github credentials; provide `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN`, or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`'); | ||
} | ||
} | ||
exports.validate = validate | ||
exports.validate = validate; | ||
/** | ||
* Fetch the GitHub Auth Query String. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The query string that you should append to your github API request url. Will be an empty string if no valid credentials were provided. | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
function getParams( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
if (credentials.GITHUB_ACCESS_TOKEN) { | ||
return `access_token=${credentials.GITHUB_ACCESS_TOKEN}` | ||
} else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `client_id=${credentials.GITHUB_CLIENT_ID}&client_secret=${credentials.GITHUB_CLIENT_SECRET}` | ||
} else { | ||
throw new Error('missing github credentials for query string') | ||
} | ||
function getAccessToken(credentials) { | ||
return credentials.GITHUB_ACCESS_TOKEN || credentials.GITHUB_TOKEN || null; | ||
} | ||
exports.getParams = getParams | ||
// Aliases | ||
exports.githubQueryString = getParams | ||
exports.fetch = getParams | ||
exports.default = getParams | ||
exports.getAccessToken = getAccessToken; | ||
/** | ||
* Fetch the GitHub Authorization Header. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The Authorization header to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* @param credentials The params to use for the authorization variables. | ||
* @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
* @returns The search params that you should append to your github API request url. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
function getAuthHeader( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
if (credentials.GITHUB_ACCESS_TOKEN) { | ||
return `token ${credentials.GITHUB_ACCESS_TOKEN}` | ||
} else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `Basic ${credentials.GITHUB_CLIENT_ID}:${credentials.GITHUB_CLIENT_SECRET}` | ||
} else { | ||
throw new Error('missing github credentials for authorization header') | ||
} | ||
function getSearchParams(credentials, params = new URLSearchParams()) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken) { | ||
params.set('access_token', accessToken); | ||
} | ||
else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
params.set('client_id', credentials.GITHUB_CLIENT_ID); | ||
params.set('client_secret', credentials.GITHUB_CLIENT_SECRET); | ||
} | ||
else { | ||
// throw with detail errors | ||
validate(credentials); | ||
// if that doesn't throw, then fallback to this | ||
throw new Error('invalid github credentials'); | ||
} | ||
return params; | ||
} | ||
exports.getAuthHeader = getAuthHeader | ||
// Aliases | ||
exports.githubAuthorizationHeader = getAuthHeader | ||
exports.getSearchParams = getSearchParams; | ||
/** | ||
* Fetch the headers to attach to the request to the GitHub API | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The headers to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
function getHeaders( | ||
credentials = process === null || process === void 0 ? void 0 : process.env | ||
) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
} | ||
function getQueryString(credentials) { | ||
return getSearchParams(credentials).toString(); | ||
} | ||
exports.getHeaders = getHeaders | ||
exports.getQueryString = getQueryString; | ||
/** | ||
* Redact any github credentials from a string. | ||
* Get the GitHub Authorization Header. | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
*/ | ||
function getAuthHeader(credentials) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken) { | ||
return `token ${accessToken}`; | ||
} | ||
else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `Basic ${credentials.GITHUB_CLIENT_ID}:${credentials.GITHUB_CLIENT_SECRET}`; | ||
} | ||
else { | ||
throw new Error('missing github credentials for authorization header'); | ||
} | ||
} | ||
exports.getAuthHeader = getAuthHeader; | ||
/** | ||
* Get the headers to attach to the request to the GitHub API. | ||
* Use as the headers object within {@link fetch} calls. | ||
* Make sure to use with {@link getApiUrl} to make sure you are using the desired hostname. | ||
*/ | ||
function getHeaders(credentials) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
}; | ||
} | ||
exports.getHeaders = getHeaders; | ||
/** | ||
* Get the desired Github API URL string. | ||
* As this does not include any credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getURL} to get a credentialed URL. | ||
*/ | ||
function getApiUrl(credentials) { | ||
return (credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com'); | ||
} | ||
exports.getApiUrl = getApiUrl; | ||
/** | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
*/ | ||
function getURL(credentials, props) { | ||
// fetch url | ||
const url = new URL(getApiUrl(credentials)); | ||
// apply params | ||
getSearchParams(credentials, url.searchParams); | ||
if (props === null || props === void 0 ? void 0 : props.searchParams) | ||
props.searchParams.forEach((value, key) => url.searchParams.set(key, value)); | ||
// apply pathname via append, as otherwise urls like `https://bevry.me/api/github` will not work | ||
if (props === null || props === void 0 ? void 0 : props.pathname) | ||
url.pathname += props.pathname; | ||
// return | ||
return url; | ||
} | ||
exports.getURL = getURL; | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
*/ | ||
function getUrl(credentials, props) { | ||
return getURL(credentials, props).toString(); | ||
} | ||
exports.getUrl = getUrl; | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
function redactParams(value) { | ||
return value.replace( | ||
/(&?)(access_token|client_id|client_secret)=\w+/gi, | ||
'$1$2=REDACTED' | ||
) | ||
function redactSearchParams(value) { | ||
return value.replace(/(&?)(access_token|client_id|client_secret)=\w+/gi, '$1$2=REDACTED'); | ||
} | ||
exports.redactParams = redactParams | ||
// Alias | ||
exports.redact = redactParams | ||
exports.redactSearchParams = redactSearchParams; |
@@ -1,3 +0,1 @@ | ||
{ | ||
"type": "commonjs" | ||
} | ||
{"type": "commonjs"} |
@@ -1,83 +0,136 @@ | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }) | ||
exports.redact = exports.redactParams = exports.getHeaders = exports.githubAuthorizationHeader = exports.getAuthHeader = exports.fetch = exports.githubQueryString = exports.getParams = exports.validate = void 0 | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.redactSearchParams = exports.getUrl = exports.getURL = exports.getApiUrl = exports.getHeaders = exports.getAuthHeader = exports.getQueryString = exports.getSearchParams = exports.getAccessToken = exports.validate = void 0; | ||
/** | ||
* Check whether or not sufficient github credentials were supplied | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns `true` if valid, otherwise throws if invalid | ||
* Check whether or not sufficient GitHub credentials were supplied. | ||
* @returns `true` if valid | ||
* @throws if invalid | ||
*/ | ||
function validate(credentials = process?.env) { | ||
if ( | ||
credentials.GITHUB_ACCESS_TOKEN || | ||
(credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) | ||
) { | ||
return true | ||
} else { | ||
throw new Error( | ||
'missing github credentials; provide `GITHUB_ACCESS_TOKEN` or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`' | ||
) | ||
} | ||
function validate(credentials) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken || | ||
(credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET)) { | ||
return true; | ||
} | ||
else { | ||
throw new Error('missing github credentials; provide `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN`, or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`'); | ||
} | ||
} | ||
exports.validate = validate | ||
exports.validate = validate; | ||
/** | ||
* Fetch the GitHub Auth Query String. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The query string that you should append to your github API request url. Will be an empty string if no valid credentials were provided. | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
function getParams(credentials = process?.env) { | ||
if (credentials.GITHUB_ACCESS_TOKEN) { | ||
return `access_token=${credentials.GITHUB_ACCESS_TOKEN}` | ||
} else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `client_id=${credentials.GITHUB_CLIENT_ID}&client_secret=${credentials.GITHUB_CLIENT_SECRET}` | ||
} else { | ||
throw new Error('missing github credentials for query string') | ||
} | ||
function getAccessToken(credentials) { | ||
return credentials.GITHUB_ACCESS_TOKEN || credentials.GITHUB_TOKEN || null; | ||
} | ||
exports.getParams = getParams | ||
// Aliases | ||
exports.githubQueryString = getParams | ||
exports.fetch = getParams | ||
exports.default = getParams | ||
exports.getAccessToken = getAccessToken; | ||
/** | ||
* Fetch the GitHub Authorization Header. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The Authorization header to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* @param credentials The params to use for the authorization variables. | ||
* @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
* @returns The search params that you should append to your github API request url. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
function getAuthHeader(credentials = process?.env) { | ||
if (credentials.GITHUB_ACCESS_TOKEN) { | ||
return `token ${credentials.GITHUB_ACCESS_TOKEN}` | ||
} else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `Basic ${credentials.GITHUB_CLIENT_ID}:${credentials.GITHUB_CLIENT_SECRET}` | ||
} else { | ||
throw new Error('missing github credentials for authorization header') | ||
} | ||
function getSearchParams(credentials, params = new URLSearchParams()) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken) { | ||
params.set('access_token', accessToken); | ||
} | ||
else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
params.set('client_id', credentials.GITHUB_CLIENT_ID); | ||
params.set('client_secret', credentials.GITHUB_CLIENT_SECRET); | ||
} | ||
else { | ||
// throw with detail errors | ||
validate(credentials); | ||
// if that doesn't throw, then fallback to this | ||
throw new Error('invalid github credentials'); | ||
} | ||
return params; | ||
} | ||
exports.getAuthHeader = getAuthHeader | ||
// Aliases | ||
exports.githubAuthorizationHeader = getAuthHeader | ||
exports.getSearchParams = getSearchParams; | ||
/** | ||
* Fetch the headers to attach to the request to the GitHub API | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The headers to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
function getHeaders(credentials = process?.env) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
} | ||
function getQueryString(credentials) { | ||
return getSearchParams(credentials).toString(); | ||
} | ||
exports.getHeaders = getHeaders | ||
exports.getQueryString = getQueryString; | ||
/** | ||
* Redact any github credentials from a string. | ||
* Get the GitHub Authorization Header. | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
*/ | ||
function getAuthHeader(credentials) { | ||
const accessToken = getAccessToken(credentials); | ||
if (accessToken) { | ||
return `token ${accessToken}`; | ||
} | ||
else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `Basic ${credentials.GITHUB_CLIENT_ID}:${credentials.GITHUB_CLIENT_SECRET}`; | ||
} | ||
else { | ||
throw new Error('missing github credentials for authorization header'); | ||
} | ||
} | ||
exports.getAuthHeader = getAuthHeader; | ||
/** | ||
* Get the headers to attach to the request to the GitHub API. | ||
* Use as the headers object within {@link fetch} calls. | ||
* Make sure to use with {@link getApiUrl} to make sure you are using the desired hostname. | ||
*/ | ||
function getHeaders(credentials) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
}; | ||
} | ||
exports.getHeaders = getHeaders; | ||
/** | ||
* Get the desired Github API URL string. | ||
* As this does not include any credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getURL} to get a credentialed URL. | ||
*/ | ||
function getApiUrl(credentials) { | ||
return (credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com'); | ||
} | ||
exports.getApiUrl = getApiUrl; | ||
/** | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
*/ | ||
function getURL(credentials, props) { | ||
// fetch url | ||
const url = new URL(getApiUrl(credentials)); | ||
// apply params | ||
getSearchParams(credentials, url.searchParams); | ||
if (props?.searchParams) | ||
props.searchParams.forEach((value, key) => url.searchParams.set(key, value)); | ||
// apply pathname via append, as otherwise urls like `https://bevry.me/api/github` will not work | ||
if (props?.pathname) | ||
url.pathname += props.pathname; | ||
// return | ||
return url; | ||
} | ||
exports.getURL = getURL; | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
*/ | ||
function getUrl(credentials, props) { | ||
return getURL(credentials, props).toString(); | ||
} | ||
exports.getUrl = getUrl; | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
function redactParams(value) { | ||
return value.replace( | ||
/(&?)(access_token|client_id|client_secret)=\w+/gi, | ||
'$1$2=REDACTED' | ||
) | ||
function redactSearchParams(value) { | ||
return value.replace(/(&?)(access_token|client_id|client_secret)=\w+/gi, '$1$2=REDACTED'); | ||
} | ||
exports.redactParams = redactParams | ||
// Alias | ||
exports.redact = redactParams | ||
exports.redactSearchParams = redactSearchParams; |
@@ -1,3 +0,1 @@ | ||
{ | ||
"type": "commonjs" | ||
} | ||
{"type": "commonjs"} |
# History | ||
## v6.0.0 2020 November 12 | ||
- Rewrote the API to support new authorization variables, preferences, and usage | ||
## v5.19.0 2020 October 29 | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "githubauthreq", | ||
"version": "5.19.0", | ||
"description": "Authorise GitHub API requests with the appropriate environment variables", | ||
"version": "6.0.0-next.1605169857.b79abf92b188b440f73cf03cbdfd35f106dfbe15", | ||
"description": "Authorize GitHub API requests with the appropriate credentials and preferences.", | ||
"homepage": "https://github.com/bevry/githubauthreq", | ||
@@ -167,20 +167,20 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@bevry/update-contributors": "^1.17.0", | ||
"@typescript-eslint/eslint-plugin": "^4.6.0", | ||
"@typescript-eslint/parser": "^4.6.0", | ||
"@bevry/update-contributors": "^1.18.0", | ||
"@typescript-eslint/eslint-plugin": "^4.7.0", | ||
"@typescript-eslint/parser": "^4.7.0", | ||
"assert-helpers": "^8.1.0", | ||
"cross-fetch": "^3.0.6", | ||
"eslint": "^7.12.1", | ||
"eslint": "^7.13.0", | ||
"eslint-config-bevry": "^3.23.0", | ||
"eslint-config-prettier": "^6.15.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"kava": "^5.12.0", | ||
"kava": "^5.13.0", | ||
"make-deno-edition": "^1.2.0", | ||
"prettier": "^2.1.2", | ||
"projectz": "^2.16.0", | ||
"surge": "^0.21.6", | ||
"projectz": "^2.17.0", | ||
"surge": "^0.21.7", | ||
"typedoc": "^0.19.2", | ||
"typescript": "^4.0.5", | ||
"valid-directory": "^3.4.0", | ||
"valid-module": "^1.14.0" | ||
"valid-directory": "^3.5.0", | ||
"valid-module": "^1.15.0" | ||
}, | ||
@@ -187,0 +187,0 @@ "scripts": { |
@@ -31,3 +31,3 @@ <!-- TITLE/ --> | ||
Authorise GitHub API requests with the appropriate environment variables | ||
Authorize GitHub API requests with the appropriate credentials and preferences. | ||
@@ -41,60 +41,25 @@ <!-- /DESCRIPTION --> | ||
### Fetch | ||
```typescript | ||
// imports with typescript | ||
import { GitHubCredentials, getHeaders, getApiUrl, getUrl, redactSearchParams } from 'githubauthreq' | ||
import { env } from 'process | ||
const githubCredentials = env as GitHubCredentials | ||
// if using javascript, omit GitHubCredentials | ||
#### Header | ||
Using environment variables: | ||
```javascript | ||
import { getHeaders } from 'githubauthreq' | ||
fetch('https://api.github.com/user', { | ||
headers: getHeaders(), | ||
// recommended: authorization via headers | ||
fetch(getApiUrl(githubCredentials) + '/user', { | ||
headers: getHeaders(githubCredentials), | ||
}) | ||
``` | ||
```javascript | ||
import { getAuthHeader } from 'githubauthreq' | ||
fetch('https://api.github.com/user', { | ||
headers: { | ||
Authorization: getAuthHeader(), | ||
Accept: 'application/vnd.github.v3+json', | ||
}, | ||
}) | ||
// alternative: authorization via url | ||
try { | ||
// if you want to customize the search params, you can also use: { searchParams: new URLSearchParams() } | ||
fetch(getUrl(githubCredentials, {pathname: '/user'})) | ||
} | ||
catch (err) { | ||
// redact the credentials from the error | ||
console.error(redactSearchParams(err.message)) | ||
} | ||
``` | ||
#### Query String | ||
Using environment variables: | ||
```javascript | ||
import { getParams } from 'githubauthreq' | ||
fetch(`https://api.github.com/user?${getParams()}`, { | ||
headers: { | ||
Accept: 'application/vnd.github.v3+json', | ||
}, | ||
}) | ||
``` | ||
### Manual | ||
`getHeaders`, `getAuthHeader`, `getParams` accept an object containing either: | ||
- `GITHUB_ACCESS_TOKEN` | ||
- `GITHUB_CLIENT_ID` + `GITHUB_CLIENT_SECRET` | ||
By default they will use `process.env` if it exists | ||
### Redaction | ||
When using query string, you should redact the error message to prevent credentials from leaking in log files: | ||
```javascript | ||
import { getParams, redactParams } from 'githubauthreq' | ||
fetch(`https://api.github.com/user?${getParams()}`, { | ||
headers: { | ||
Accept: 'application/vnd.github.v3+json', | ||
}, | ||
}).catch((err) => console.error(redactParams(err.message))) | ||
``` | ||
<!-- INSTALL/ --> | ||
@@ -115,3 +80,3 @@ | ||
<script type="module"> | ||
import pkg from '//cdn.skypack.dev/githubauthreq@^5.19.0' | ||
import pkg from '//cdn.skypack.dev/githubauthreq@^6.0.0' | ||
</script> | ||
@@ -124,3 +89,3 @@ ``` | ||
<script type="module"> | ||
import pkg from '//unpkg.com/githubauthreq@^5.19.0' | ||
import pkg from '//unpkg.com/githubauthreq@^6.0.0' | ||
</script> | ||
@@ -133,3 +98,3 @@ ``` | ||
<script type="module"> | ||
import pkg from '//dev.jspm.io/githubauthreq@5.19.0' | ||
import pkg from '//dev.jspm.io/githubauthreq@6.0.0' | ||
</script> | ||
@@ -136,0 +101,0 @@ ``` |
import type { StrictUnion } from 'simplytyped' | ||
/** If the variable `GITHUB_API_URL` or `GITHUB_API` exists, use that, otherwise use the value `https://api.github.com`. */ | ||
type GitHubApiUrl = StrictUnion< | ||
| { | ||
/** https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables */ | ||
GITHUB_API_URL: string | ||
} | ||
| { | ||
/** https://github.com/search?l=JavaScript&q=GITHUB_API&type=Code */ | ||
GITHUB_API: string | ||
} | ||
> | ||
/** | ||
* If the variable `GITHUB_ACCESS_TOKEN` exists, use that according to: | ||
* If the variable `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN` exists, use that according to: | ||
* https://developer.github.com/v3/#oauth2-token-sent-as-a-parameter | ||
*/ | ||
interface GitHubToken { | ||
GITHUB_ACCESS_TOKEN: string | ||
} | ||
type GitHubToken = StrictUnion< | ||
| { | ||
/** https://github.com/search?q=GITHUB_ACCESS_TOKEN&type=code */ | ||
GITHUB_ACCESS_TOKEN: string | ||
} | ||
| { | ||
/** https://docs.github.com/en/free-pro-team@latest/actions/reference/authentication-in-a-workflow#about-the-github_token-secret */ | ||
GITHUB_TOKEN: string | ||
} | ||
> | ||
@@ -21,20 +40,19 @@ /** | ||
/** | ||
* Provide `GITHUB_ACCESS_TOKEN` or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` | ||
* Provide `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN`, | ||
* or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`, | ||
* optionally with `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN`. | ||
* https://developer.github.com/v3/#authentication | ||
*/ | ||
export type GitHubCredentials = StrictUnion<GitHubToken | GitHubClient> | ||
export type GitHubCredentials = StrictUnion<GitHubToken | GitHubClient> & | ||
Partial<GitHubApiUrl> | ||
/** GitHub credentialed environment */ | ||
export type GitHubEnv = NodeJS.ProcessEnv & GitHubCredentials | ||
/** | ||
* Check whether or not sufficient github credentials were supplied | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns `true` if valid, otherwise throws if invalid | ||
* Check whether or not sufficient GitHub credentials were supplied. | ||
* @returns `true` if valid | ||
* @throws if invalid | ||
*/ | ||
export function validate( | ||
credentials: GitHubCredentials = process?.env as GitHubEnv | ||
) { | ||
export function validate(credentials: GitHubCredentials) { | ||
const accessToken = getAccessToken(credentials) | ||
if ( | ||
credentials.GITHUB_ACCESS_TOKEN || | ||
accessToken || | ||
(credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) | ||
@@ -45,3 +63,3 @@ ) { | ||
throw new Error( | ||
'missing github credentials; provide `GITHUB_ACCESS_TOKEN` or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`' | ||
'missing github credentials; provide `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN`, or a combination of `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`' | ||
) | ||
@@ -52,33 +70,53 @@ } | ||
/** | ||
* Fetch the GitHub Auth Query String. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The query string that you should append to your github API request url. Will be an empty string if no valid credentials were provided. | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
export function getParams( | ||
credentials: GitHubCredentials = process?.env as GitHubEnv | ||
export function getAccessToken(credentials: GitHubCredentials): string | null { | ||
return credentials.GITHUB_ACCESS_TOKEN || credentials.GITHUB_TOKEN || null | ||
} | ||
/** | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* @param credentials The params to use for the authorization variables. | ||
* @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
* @returns The search params that you should append to your github API request url. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
export function getSearchParams( | ||
credentials: GitHubCredentials, | ||
params = new URLSearchParams() | ||
) { | ||
if (credentials.GITHUB_ACCESS_TOKEN) { | ||
return `access_token=${credentials.GITHUB_ACCESS_TOKEN}` | ||
const accessToken = getAccessToken(credentials) | ||
if (accessToken) { | ||
params.set('access_token', accessToken) | ||
} else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
return `client_id=${credentials.GITHUB_CLIENT_ID}&client_secret=${credentials.GITHUB_CLIENT_SECRET}` | ||
params.set('client_id', credentials.GITHUB_CLIENT_ID) | ||
params.set('client_secret', credentials.GITHUB_CLIENT_SECRET) | ||
} else { | ||
throw new Error('missing github credentials for query string') | ||
// throw with detail errors | ||
validate(credentials) | ||
// if that doesn't throw, then fallback to this | ||
throw new Error('invalid github credentials') | ||
} | ||
return params | ||
} | ||
// Aliases | ||
export const githubQueryString = getParams | ||
export const fetch = getParams | ||
export default getParams | ||
/** | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
*/ | ||
export function getQueryString(credentials: GitHubCredentials) { | ||
return getSearchParams(credentials).toString() | ||
} | ||
/** | ||
* Fetch the GitHub Authorization Header. | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The Authorization header to attach to the request to the GitHub request. | ||
* Get the GitHub Authorization Header. | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
*/ | ||
export function getAuthHeader( | ||
credentials: GitHubCredentials = process?.env as GitHubEnv | ||
) { | ||
if (credentials.GITHUB_ACCESS_TOKEN) { | ||
return `token ${credentials.GITHUB_ACCESS_TOKEN}` | ||
export function getAuthHeader(credentials: GitHubCredentials) { | ||
const accessToken = getAccessToken(credentials) | ||
if (accessToken) { | ||
return `token ${accessToken}` | ||
} else if (credentials.GITHUB_CLIENT_ID && credentials.GITHUB_CLIENT_SECRET) { | ||
@@ -91,13 +129,8 @@ return `Basic ${credentials.GITHUB_CLIENT_ID}:${credentials.GITHUB_CLIENT_SECRET}` | ||
// Aliases | ||
export const githubAuthorizationHeader = getAuthHeader | ||
/** | ||
* Fetch the headers to attach to the request to the GitHub API | ||
* @param credentials If no credentials were passed, then the environment variables are used if they exist. | ||
* @returns The headers to attach to the request to the GitHub request. | ||
* Get the headers to attach to the request to the GitHub API. | ||
* Use as the headers object within {@link fetch} calls. | ||
* Make sure to use with {@link getApiUrl} to make sure you are using the desired hostname. | ||
*/ | ||
export function getHeaders( | ||
credentials: GitHubCredentials = process?.env as GitHubEnv | ||
) { | ||
export function getHeaders(credentials: GitHubCredentials) { | ||
return { | ||
@@ -110,7 +143,50 @@ Accept: 'application/vnd.github.v3+json', | ||
/** | ||
* Redact any github credentials from a string. | ||
* Get the desired Github API URL string. | ||
* As this does not include any credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getURL} to get a credentialed URL. | ||
*/ | ||
export function getApiUrl(credentials: GitHubCredentials) { | ||
return ( | ||
credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com' | ||
) | ||
} | ||
/** | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
*/ | ||
export function getURL( | ||
credentials: GitHubCredentials, | ||
props?: { pathname?: string; searchParams?: URLSearchParams } | ||
): URL { | ||
// fetch url | ||
const url = new URL(getApiUrl(credentials)) | ||
// apply params | ||
getSearchParams(credentials, url.searchParams) | ||
if (props?.searchParams) | ||
props.searchParams.forEach((value, key) => url.searchParams.set(key, value)) | ||
// apply pathname via append, as otherwise urls like `https://bevry.me/api/github` will not work | ||
if (props?.pathname) url.pathname += props.pathname | ||
// return | ||
return url | ||
} | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
*/ | ||
export function getUrl( | ||
credentials: GitHubCredentials, | ||
props?: { pathname?: string; searchParams?: URLSearchParams } | ||
) { | ||
return getURL(credentials, props).toString() | ||
} | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
export function redactParams(value: string) { | ||
export function redactSearchParams(value: string) { | ||
return value.replace( | ||
@@ -121,4 +197,1 @@ /(&?)(access_token|client_id|client_secret)=\w+/gi, | ||
} | ||
// Alias | ||
export const redact = redactParams |
@@ -10,3 +10,3 @@ { | ||
"target": "ESNext", | ||
"lib": ["ESNext"], | ||
"lib": ["ESNext", "DOM"], | ||
"module": "ESNext" | ||
@@ -13,0 +13,0 @@ }, |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
62344
820
1
178
1