githubauthreq
Advanced tools
Comparing version 6.4.0 to 7.0.0-next.1628061611.089f66e8fa0057182ce45419b9f6bf11f4930eeb
@@ -44,3 +44,3 @@ import type { StrictUnion } from 'simplytyped'; | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
@@ -50,3 +50,3 @@ export declare function getAccessToken(credentials: GitHubCredentials): string | null; | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
* @param credentials The params to use for the authorization variables. | ||
@@ -59,4 +59,16 @@ * @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
/** | ||
* Remove any GitHub Credentials from a URL Search Params instance. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export declare function removeSearchParams(params?: URLSearchParams): URLSearchParams; | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
export declare function redactSearchParams(value: string): string; | ||
/** | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link getURL} directly, instead of going through this method. | ||
*/ | ||
@@ -67,3 +79,4 @@ export declare function getQueryString(credentials: GitHubCredentials): string; | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
* You probably want to use {@link getHeaders} or {@link fetch} directly, instead of going through this method. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
@@ -74,5 +87,5 @@ export declare function getAuthHeader(credentials: GitHubCredentials): string; | ||
* 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. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export declare function getHeaders(credentials: GitHubCredentials): { | ||
export declare function getHeaders(credentials: GitHubCredentials, headers?: Record<string, string>): { | ||
Accept: string; | ||
@@ -82,29 +95,36 @@ 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. | ||
* Remove any GitHub Credentials from a Headers instance. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export declare function getApiUrl(credentials: GitHubCredentials): string; | ||
export declare function removeHeaders(headers: Record<string, string>): Record<string, string>; | ||
/** | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
* Get the desired Github API URL, using {@link removeSearchParams}.to ensure there are no credentials. | ||
* As this URL does not include credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getCredentialedURL} to get a credentialed URL. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export declare function getURL(credentials: GitHubCredentials, props?: { | ||
pathname?: string; | ||
searchParams?: URLSearchParams; | ||
searchParams?: URLSearchParams | Record<string, string>; | ||
}): URL; | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getURL} to get the URL, then uses {@link getSearchParams} to add the credentials. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export declare function getUrl(credentials: GitHubCredentials, props?: { | ||
export declare function getCredentialedURL(credentials: GitHubCredentials, props?: { | ||
pathname?: string; | ||
searchParams?: URLSearchParams; | ||
}): string; | ||
searchParams?: URLSearchParams | Record<string, string>; | ||
}): URL; | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted. | ||
* Fetches a GitHub API response via secure headers authorization. | ||
* Uses {@link getURL} to get the URL, then uses {@link getHeaders} to add the credentials. | ||
* This is probably the method you want to use. | ||
*/ | ||
export declare function redactSearchParams(value: string): string; | ||
export declare function fetch(credentials: GitHubCredentials, props?: { | ||
pathname?: string; | ||
searchParams?: URLSearchParams | Record<string, string>; | ||
headers?: Record<string, string>; | ||
}): Promise<any>; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -0,1 +1,2 @@ | ||
import _fetch from 'node-fetch'; | ||
/** | ||
@@ -18,3 +19,3 @@ * Check whether or not sufficient GitHub credentials were supplied. | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
@@ -26,3 +27,3 @@ export function getAccessToken(credentials) { | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
* @param credentials The params to use for the authorization variables. | ||
@@ -51,4 +52,23 @@ * @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
/** | ||
* Remove any GitHub Credentials from a URL Search Params instance. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function removeSearchParams(params = new URLSearchParams()) { | ||
params.delete('access_token'); | ||
params.delete('client_id'); | ||
params.delete('client_secret'); | ||
return params; | ||
} | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
export function redactSearchParams(value) { | ||
return value.replace(/(&?)(access_token|client_id|client_secret)=\w+/gi, '$1$2=REDACTED'); | ||
} | ||
/** | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link getURL} directly, instead of going through this method. | ||
*/ | ||
@@ -61,3 +81,4 @@ export function getQueryString(credentials) { | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
* You probably want to use {@link getHeaders} or {@link fetch} directly, instead of going through this method. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
@@ -79,34 +100,55 @@ export function getAuthHeader(credentials) { | ||
* 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. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getHeaders(credentials) { | ||
export function getHeaders(credentials, headers = {}) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
...headers, | ||
}; | ||
} | ||
/** | ||
* 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. | ||
* Remove any GitHub Credentials from a Headers instance. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getApiUrl(credentials) { | ||
return (credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com'); | ||
export function removeHeaders(headers) { | ||
// @ts-ignore | ||
delete headers.Authorization; | ||
return headers; | ||
} | ||
/** | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
* Get the desired Github API URL, using {@link removeSearchParams}.to ensure there are no credentials. | ||
* As this URL does not include credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getCredentialedURL} to get a credentialed URL. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getURL(credentials, props) { | ||
// prepare | ||
const hostname = credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com'; | ||
// 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; | ||
const url = new URL(hostname); | ||
// add user params | ||
if (props?.searchParams) { | ||
if (props.searchParams instanceof URLSearchParams) { | ||
props.searchParams.forEach((value, key) => url.searchParams.set(key, value)); | ||
} | ||
else { | ||
Object.entries(props.searchParams).forEach(([key, value]) => url.searchParams.set(key, value)); | ||
} | ||
} | ||
// ensure that there are no credentials in the URL | ||
removeSearchParams(url.searchParams); | ||
// add user pathname | ||
// the convoluted way of doing this is to make sure that with or without / is valid | ||
// as the GITHUB_API hostname may be something like `https://bevry.me/api/github` | ||
if (props?.pathname) { | ||
url.pathname = [ | ||
url.pathname.replace(/^[/]+|[/]+$/, ''), | ||
props.pathname.replace(/^[/]+|[/]+$/, ''), | ||
] | ||
.filter((i) => i) | ||
.join('/'); | ||
} | ||
// return | ||
@@ -116,14 +158,25 @@ return url; | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getURL} to get the URL, then uses {@link getSearchParams} to add the credentials. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getUrl(credentials, props) { | ||
return getURL(credentials, props).toString(); | ||
export function getCredentialedURL(credentials, props) { | ||
// fetch url | ||
const url = getURL(credentials, props); | ||
// add auth params | ||
getSearchParams(credentials, url.searchParams); | ||
// return | ||
return url; | ||
} | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted. | ||
* Fetches a GitHub API response via secure headers authorization. | ||
* Uses {@link getURL} to get the URL, then uses {@link getHeaders} to add the credentials. | ||
* This is probably the method you want to use. | ||
*/ | ||
export function redactSearchParams(value) { | ||
return value.replace(/(&?)(access_token|client_id|client_secret)=\w+/gi, '$1$2=REDACTED'); | ||
export function fetch(credentials, props) { | ||
const url = getURL(credentials, props); | ||
const opts = { | ||
headers: getHeaders(credentials, props && props.headers), | ||
}; | ||
return _fetch(url, opts); | ||
} |
@@ -0,1 +1,2 @@ | ||
import _fetch from 'node-fetch'; | ||
/** | ||
@@ -18,3 +19,3 @@ * Check whether or not sufficient GitHub credentials were supplied. | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
@@ -26,3 +27,3 @@ export function getAccessToken(credentials) { | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
* @param credentials The params to use for the authorization variables. | ||
@@ -51,4 +52,23 @@ * @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
/** | ||
* Remove any GitHub Credentials from a URL Search Params instance. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function removeSearchParams(params = new URLSearchParams()) { | ||
params.delete('access_token'); | ||
params.delete('client_id'); | ||
params.delete('client_secret'); | ||
return params; | ||
} | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
export function redactSearchParams(value) { | ||
return value.replace(/(&?)(access_token|client_id|client_secret)=\w+/gi, '$1$2=REDACTED'); | ||
} | ||
/** | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link getURL} directly, instead of going through this method. | ||
*/ | ||
@@ -61,3 +81,4 @@ export function getQueryString(credentials) { | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
* You probably want to use {@link getHeaders} or {@link fetch} directly, instead of going through this method. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
@@ -79,34 +100,55 @@ export function getAuthHeader(credentials) { | ||
* 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. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getHeaders(credentials) { | ||
export function getHeaders(credentials, headers = {}) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
...headers, | ||
}; | ||
} | ||
/** | ||
* 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. | ||
* Remove any GitHub Credentials from a Headers instance. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getApiUrl(credentials) { | ||
return (credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com'); | ||
export function removeHeaders(headers) { | ||
// @ts-ignore | ||
delete headers.Authorization; | ||
return headers; | ||
} | ||
/** | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
* Get the desired Github API URL, using {@link removeSearchParams}.to ensure there are no credentials. | ||
* As this URL does not include credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getCredentialedURL} to get a credentialed URL. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getURL(credentials, props) { | ||
// prepare | ||
const hostname = credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com'; | ||
// 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; | ||
const url = new URL(hostname); | ||
// add user params | ||
if (props === null || props === void 0 ? void 0 : props.searchParams) { | ||
if (props.searchParams instanceof URLSearchParams) { | ||
props.searchParams.forEach((value, key) => url.searchParams.set(key, value)); | ||
} | ||
else { | ||
Object.entries(props.searchParams).forEach(([key, value]) => url.searchParams.set(key, value)); | ||
} | ||
} | ||
// ensure that there are no credentials in the URL | ||
removeSearchParams(url.searchParams); | ||
// add user pathname | ||
// the convoluted way of doing this is to make sure that with or without / is valid | ||
// as the GITHUB_API hostname may be something like `https://bevry.me/api/github` | ||
if (props === null || props === void 0 ? void 0 : props.pathname) { | ||
url.pathname = [ | ||
url.pathname.replace(/^[/]+|[/]+$/, ''), | ||
props.pathname.replace(/^[/]+|[/]+$/, ''), | ||
] | ||
.filter((i) => i) | ||
.join('/'); | ||
} | ||
// return | ||
@@ -116,14 +158,25 @@ return url; | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getURL} to get the URL, then uses {@link getSearchParams} to add the credentials. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getUrl(credentials, props) { | ||
return getURL(credentials, props).toString(); | ||
export function getCredentialedURL(credentials, props) { | ||
// fetch url | ||
const url = getURL(credentials, props); | ||
// add auth params | ||
getSearchParams(credentials, url.searchParams); | ||
// return | ||
return url; | ||
} | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted. | ||
* Fetches a GitHub API response via secure headers authorization. | ||
* Uses {@link getURL} to get the URL, then uses {@link getHeaders} to add the credentials. | ||
* This is probably the method you want to use. | ||
*/ | ||
export function redactSearchParams(value) { | ||
return value.replace(/(&?)(access_token|client_id|client_secret)=\w+/gi, '$1$2=REDACTED'); | ||
export function fetch(credentials, props) { | ||
const url = getURL(credentials, props); | ||
const opts = { | ||
headers: getHeaders(credentials, props && props.headers), | ||
}; | ||
return _fetch(url, opts); | ||
} |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
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; | ||
exports.fetch = exports.getCredentialedURL = exports.getURL = exports.removeHeaders = exports.getHeaders = exports.getAuthHeader = exports.getQueryString = exports.redactSearchParams = exports.removeSearchParams = exports.getSearchParams = exports.getAccessToken = exports.validate = void 0; | ||
const node_fetch_1 = __importDefault(require("node-fetch")); | ||
/** | ||
@@ -22,3 +26,3 @@ * Check whether or not sufficient GitHub credentials were supplied. | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
@@ -31,3 +35,3 @@ function getAccessToken(credentials) { | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
* @param credentials The params to use for the authorization variables. | ||
@@ -57,4 +61,25 @@ * @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
/** | ||
* Remove any GitHub Credentials from a URL Search Params instance. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
function removeSearchParams(params = new URLSearchParams()) { | ||
params.delete('access_token'); | ||
params.delete('client_id'); | ||
params.delete('client_secret'); | ||
return params; | ||
} | ||
exports.removeSearchParams = removeSearchParams; | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
function redactSearchParams(value) { | ||
return value.replace(/(&?)(access_token|client_id|client_secret)=\w+/gi, '$1$2=REDACTED'); | ||
} | ||
exports.redactSearchParams = redactSearchParams; | ||
/** | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link getURL} directly, instead of going through this method. | ||
*/ | ||
@@ -68,3 +93,4 @@ function getQueryString(credentials) { | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
* You probably want to use {@link getHeaders} or {@link fetch} directly, instead of going through this method. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
@@ -87,8 +113,9 @@ function getAuthHeader(credentials) { | ||
* 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. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
function getHeaders(credentials) { | ||
function getHeaders(credentials, headers = {}) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
...headers, | ||
}; | ||
@@ -98,26 +125,46 @@ } | ||
/** | ||
* 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. | ||
* Remove any GitHub Credentials from a Headers instance. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
function getApiUrl(credentials) { | ||
return (credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com'); | ||
function removeHeaders(headers) { | ||
// @ts-ignore | ||
delete headers.Authorization; | ||
return headers; | ||
} | ||
exports.getApiUrl = getApiUrl; | ||
exports.removeHeaders = removeHeaders; | ||
/** | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
* Get the desired Github API URL, using {@link removeSearchParams}.to ensure there are no credentials. | ||
* As this URL does not include credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getCredentialedURL} to get a credentialed URL. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
function getURL(credentials, props) { | ||
// prepare | ||
const hostname = credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com'; | ||
// 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; | ||
const url = new URL(hostname); | ||
// add user params | ||
if (props === null || props === void 0 ? void 0 : props.searchParams) { | ||
if (props.searchParams instanceof URLSearchParams) { | ||
props.searchParams.forEach((value, key) => url.searchParams.set(key, value)); | ||
} | ||
else { | ||
Object.entries(props.searchParams).forEach(([key, value]) => url.searchParams.set(key, value)); | ||
} | ||
} | ||
// ensure that there are no credentials in the URL | ||
removeSearchParams(url.searchParams); | ||
// add user pathname | ||
// the convoluted way of doing this is to make sure that with or without / is valid | ||
// as the GITHUB_API hostname may be something like `https://bevry.me/api/github` | ||
if (props === null || props === void 0 ? void 0 : props.pathname) { | ||
url.pathname = [ | ||
url.pathname.replace(/^[/]+|[/]+$/, ''), | ||
props.pathname.replace(/^[/]+|[/]+$/, ''), | ||
] | ||
.filter((i) => i) | ||
.join('/'); | ||
} | ||
// return | ||
@@ -128,16 +175,27 @@ return url; | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getURL} to get the URL, then uses {@link getSearchParams} to add the credentials. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
function getUrl(credentials, props) { | ||
return getURL(credentials, props).toString(); | ||
function getCredentialedURL(credentials, props) { | ||
// fetch url | ||
const url = getURL(credentials, props); | ||
// add auth params | ||
getSearchParams(credentials, url.searchParams); | ||
// return | ||
return url; | ||
} | ||
exports.getUrl = getUrl; | ||
exports.getCredentialedURL = getCredentialedURL; | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted. | ||
* Fetches a GitHub API response via secure headers authorization. | ||
* Uses {@link getURL} to get the URL, then uses {@link getHeaders} to add the credentials. | ||
* This is probably the method you want to use. | ||
*/ | ||
function redactSearchParams(value) { | ||
return value.replace(/(&?)(access_token|client_id|client_secret)=\w+/gi, '$1$2=REDACTED'); | ||
function fetch(credentials, props) { | ||
const url = getURL(credentials, props); | ||
const opts = { | ||
headers: getHeaders(credentials, props && props.headers), | ||
}; | ||
return node_fetch_1.default(url, opts); | ||
} | ||
exports.redactSearchParams = redactSearchParams; | ||
exports.fetch = fetch; |
# History | ||
## v7.0.0 2021 August 4 | ||
- Rewrote the API, now all you have to do is use its exported `fetch` method in most cases | ||
## v6.4.0 2021 August 4 | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "githubauthreq", | ||
"version": "6.4.0", | ||
"version": "7.0.0-next.1628061611.089f66e8fa0057182ce45419b9f6bf11f4930eeb", | ||
"description": "Authorize GitHub API requests with the appropriate credentials and preferences.", | ||
@@ -145,2 +145,3 @@ "homepage": "https://github.com/bevry/githubauthreq", | ||
"dependencies": { | ||
"node-fetch": "^2.6.1", | ||
"simplytyped": "^3.3.0" | ||
@@ -212,2 +213,2 @@ }, | ||
} | ||
} | ||
} |
@@ -40,25 +40,33 @@ <!-- TITLE/ --> | ||
Using TypeScript: | ||
```typescript | ||
// imports with typescript | ||
import { GitHubCredentials, getHeaders, getApiUrl, getUrl, redactSearchParams } from 'githubauthreq' | ||
import { env } from 'process | ||
// imports using typescript | ||
import { fetch, GitHubCredentials } from 'githubauthreq' | ||
import { env } from 'process' | ||
const githubCredentials = env as GitHubCredentials | ||
// if using javascript, omit GitHubCredentials | ||
// recommended: authorization via headers | ||
fetch(getApiUrl(githubCredentials) + '/user', { | ||
headers: getHeaders(githubCredentials), | ||
// fetches the GitHub API URL securely via headers authorization, so no redaction is necessary | ||
fetch(githubCredentials, { | ||
pathname: `user`, | ||
// searchParams, headers | ||
}) | ||
``` | ||
// 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)) | ||
} | ||
Using JavaScript: | ||
```javascript | ||
// imports with javascript | ||
import { fetch } from 'githubauthreq' | ||
import { env as githubCredentials } from 'process' | ||
// fetches the GitHub API URL securely via headers authorization, so no redaction is necessary | ||
fetch(githubCredentials, { | ||
pathname: `user`, | ||
// searchParams, headers | ||
}) | ||
``` | ||
If you wish for a more manual approach, refer to the [Complete API Documentation.](http://master.githubauthreq.bevry.surge.sh/docs/). | ||
<!-- INSTALL/ --> | ||
@@ -79,3 +87,3 @@ | ||
<script type="module"> | ||
import * as pkg from '//cdn.skypack.dev/githubauthreq@^6.4.0' | ||
import * as pkg from '//cdn.skypack.dev/githubauthreq@^7.0.0' | ||
</script> | ||
@@ -88,3 +96,3 @@ ``` | ||
<script type="module"> | ||
import * as pkg from '//unpkg.com/githubauthreq@^6.4.0' | ||
import * as pkg from '//unpkg.com/githubauthreq@^7.0.0' | ||
</script> | ||
@@ -97,3 +105,3 @@ ``` | ||
<script type="module"> | ||
import * as pkg from '//dev.jspm.io/githubauthreq@6.4.0' | ||
import * as pkg from '//dev.jspm.io/githubauthreq@7.0.0' | ||
</script> | ||
@@ -100,0 +108,0 @@ ``` |
@@ -0,2 +1,4 @@ | ||
// external | ||
import type { StrictUnion } from 'simplytyped' | ||
import _fetch from 'node-fetch' | ||
@@ -69,3 +71,3 @@ /** If the variable `GITHUB_API_URL` or `GITHUB_API` exists, use that, otherwise use the value `https://api.github.com`. */ | ||
* Get the desired GitHub Access Token from the credentials. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
@@ -78,3 +80,3 @@ export function getAccessToken(credentials: GitHubCredentials): string | null { | ||
* Get the GitHub Authorization Search Params. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
* @param credentials The params to use for the authorization variables. | ||
@@ -98,2 +100,3 @@ * @param params If you wish to set the params on an existing URLSearchParams instance, then provide it here. | ||
validate(credentials) | ||
// if that doesn't throw, then fallback to this | ||
@@ -106,4 +109,28 @@ throw new Error('invalid github credentials') | ||
/** | ||
* Remove any GitHub Credentials from a URL Search Params instance. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function removeSearchParams(params = new URLSearchParams()) { | ||
params.delete('access_token') | ||
params.delete('client_id') | ||
params.delete('client_secret') | ||
return params | ||
} | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
export function redactSearchParams(value: string) { | ||
return value.replace( | ||
/(&?)(access_token|client_id|client_secret)=\w+/gi, | ||
'$1$2=REDACTED' | ||
) | ||
} | ||
/** | ||
* Get the GitHub Authorization as a Query String. | ||
* You probably want {@link getURL} instead. | ||
* You probably want to use {@link getURL} directly, instead of going through this method. | ||
*/ | ||
@@ -117,3 +144,4 @@ export function getQueryString(credentials: GitHubCredentials) { | ||
* Use as the `Authorization` header within {@link fetch} calls. | ||
* You probably want {@link getHeaders} instead. | ||
* You probably want to use {@link getHeaders} or {@link fetch} directly, instead of going through this method. | ||
* @throws If no valid GitHub Authorization was provided. | ||
*/ | ||
@@ -134,8 +162,12 @@ export function getAuthHeader(credentials: GitHubCredentials) { | ||
* 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. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getHeaders(credentials: GitHubCredentials) { | ||
export function getHeaders( | ||
credentials: GitHubCredentials, | ||
headers: Record<string, string> = {} | ||
) { | ||
return { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: getAuthHeader(credentials), | ||
...headers, | ||
} | ||
@@ -145,12 +177,63 @@ } | ||
/** | ||
* 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. | ||
* Remove any GitHub Credentials from a Headers instance. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getApiUrl(credentials: GitHubCredentials) { | ||
return ( | ||
export function removeHeaders(headers: Record<string, string>) { | ||
// @ts-ignore | ||
delete headers.Authorization | ||
return headers | ||
} | ||
/** | ||
* Get the desired Github API URL, using {@link removeSearchParams}.to ensure there are no credentials. | ||
* As this URL does not include credentials, use with {@link getAuthHeader} to authorize correctly. | ||
* Otherwise use {@link getCredentialedURL} to get a credentialed URL. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getURL( | ||
credentials: GitHubCredentials, | ||
props?: { | ||
pathname?: string | ||
searchParams?: URLSearchParams | Record<string, string> | ||
} | ||
) { | ||
// prepare | ||
const hostname = | ||
credentials.GITHUB_API_URL || | ||
credentials.GITHUB_API || | ||
'https://api.github.com' | ||
) | ||
// fetch url | ||
const url = new URL(hostname) | ||
// add user params | ||
if (props?.searchParams) { | ||
if (props.searchParams instanceof URLSearchParams) { | ||
props.searchParams.forEach((value, key) => | ||
url.searchParams.set(key, value) | ||
) | ||
} else { | ||
Object.entries(props.searchParams).forEach(([key, value]) => | ||
url.searchParams.set(key, value) | ||
) | ||
} | ||
} | ||
// ensure that there are no credentials in the URL | ||
removeSearchParams(url.searchParams) | ||
// add user pathname | ||
// the convoluted way of doing this is to make sure that with or without / is valid | ||
// as the GITHUB_API hostname may be something like `https://bevry.me/api/github` | ||
if (props?.pathname) { | ||
url.pathname = [ | ||
url.pathname.replace(/^[/]+|[/]+$/, ''), | ||
props.pathname.replace(/^[/]+|[/]+$/, ''), | ||
] | ||
.filter((i) => i) | ||
.join('/') | ||
} | ||
// return | ||
return url | ||
} | ||
@@ -160,16 +243,18 @@ | ||
* Get the credentialed GitHub API URL instance. | ||
* Uses {@link getApiUrl} to fill the hostname, and uses {@link getSearchParams} to fill the credentials. | ||
* Uses {@link getURL} to get the URL, then uses {@link getSearchParams} to add the credentials. | ||
* You probably want to use {@link fetch} directly, instead of going through this method. | ||
*/ | ||
export function getURL( | ||
export function getCredentialedURL( | ||
credentials: GitHubCredentials, | ||
props?: { pathname?: string; searchParams?: URLSearchParams } | ||
props?: { | ||
pathname?: string | ||
searchParams?: URLSearchParams | Record<string, string> | ||
} | ||
): URL { | ||
// fetch url | ||
const url = new URL(getApiUrl(credentials)) | ||
// apply params | ||
const url = getURL(credentials, props) | ||
// add auth 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 | ||
@@ -180,21 +265,19 @@ return url | ||
/** | ||
* Get the credentialed GitHub API URL string from {@link getURL}. | ||
* Fetches a GitHub API response via secure headers authorization. | ||
* Uses {@link getURL} to get the URL, then uses {@link getHeaders} to add the credentials. | ||
* This is probably the method you want to use. | ||
*/ | ||
export function getUrl( | ||
export function fetch( | ||
credentials: GitHubCredentials, | ||
props?: { pathname?: string; searchParams?: URLSearchParams } | ||
props?: { | ||
pathname?: string | ||
searchParams?: URLSearchParams | Record<string, string> | ||
headers?: Record<string, string> | ||
} | ||
) { | ||
return getURL(credentials, props).toString() | ||
const url = getURL(credentials, props) | ||
const opts = { | ||
headers: getHeaders(credentials, props && props.headers), | ||
} | ||
return _fetch(url, opts) | ||
} | ||
/** | ||
* Redact any GitHub Credentials from a URL string. | ||
* @param value The string to redact credentials from. | ||
* @returns The string with the credentials redacted. | ||
*/ | ||
export function redactSearchParams(value: string) { | ||
return value.replace( | ||
/(&?)(access_token|client_id|client_secret)=\w+/gi, | ||
'$1$2=REDACTED' | ||
) | ||
} |
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
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
67426
933
185
2
1
1
+ Addednode-fetch@^2.6.1
+ Addednode-fetch@2.7.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)