@octokit/request-error#RequestError JavaScript Examples
The following examples show how to use
@octokit/request-error#RequestError.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.js From zcl-eth-key-converter with MIT License | 5 votes |
async function requestWith2Fa(state, options, customRequest) {
const request = customRequest || state.request;
try {
if (state.totp) {
options = Object.assign({}, options, {
headers: Object.assign({}, options.headers, {
"x-github-otp": state.totp,
}),
});
}
const response = await request(options);
return response;
}
catch (error) {
if (!error.headers)
throw error;
const totpRequired = /required/.test(error.headers["x-github-otp"] || "");
const hasSmsDelivery = /sms/.test(error.headers["x-github-otp"] || "");
// handle "2FA required" error only
if (error.status !== 401 || !totpRequired) {
throw error;
}
if (error.status === 401 &&
totpRequired &&
error.request.headers["x-github-otp"]) {
if (state.totp) {
// TOTP is no longer valid, request again
delete state.totp;
}
else {
throw new RequestError("Invalid TOTP (time-based one-time password) for two-factor authentication", 401, {
headers: error.headers,
request: error.request,
});
}
}
// If user has 2Fa with SMS configured, send a bogus "PATCH /authorizations"
// request to trigger the TOTP delivery via SMS, unless the current request
// already triggered a delivery
if (hasSmsDelivery && !isSmsTriggeringRoute(options)) {
try {
await request("PATCH /authorizations", {
headers: options.headers,
});
}
catch (error) {
// we expect a 401
if (error.status !== 401)
throw error;
}
}
// we set state.totp after the request to make sure that it's valid
const totp = await state.strategyOptions.on2Fa();
try {
const response = await requestWith2Fa(state, Object.assign({}, options, {
headers: Object.assign({}, options.headers, {
"x-github-otp": totp,
}),
}), customRequest);
state.totp = totp;
return response;
}
catch (error) {
// error without a headers property is an unexpected error
// which we don’t cover with tests
/* istanbul ignore next */
if (!error.headers)
throw error;
const totpRequired = /required/.test(error.headers["x-github-otp"] || "");
// unless the error is an invalid TOTP, we can cache it
if (!totpRequired) {
state.totp = totp;
}
throw error;
}
}
}
Example #2
Source File: request-with-2fa.js From zcl-eth-key-converter with MIT License | 5 votes |
export async function requestWith2Fa(state, options, customRequest) {
const request = customRequest || state.request;
try {
if (state.totp) {
options = Object.assign({}, options, {
headers: Object.assign({}, options.headers, {
"x-github-otp": state.totp,
}),
});
}
const response = await request(options);
return response;
}
catch (error) {
if (!error.headers)
throw error;
const totpRequired = /required/.test(error.headers["x-github-otp"] || "");
const hasSmsDelivery = /sms/.test(error.headers["x-github-otp"] || "");
// handle "2FA required" error only
if (error.status !== 401 || !totpRequired) {
throw error;
}
if (error.status === 401 &&
totpRequired &&
error.request.headers["x-github-otp"]) {
if (state.totp) {
// TOTP is no longer valid, request again
delete state.totp;
}
else {
throw new RequestError("Invalid TOTP (time-based one-time password) for two-factor authentication", 401, {
headers: error.headers,
request: error.request,
});
}
}
// If user has 2Fa with SMS configured, send a bogus "PATCH /authorizations"
// request to trigger the TOTP delivery via SMS, unless the current request
// already triggered a delivery
if (hasSmsDelivery && !isSmsTriggeringRoute(options)) {
try {
await request("PATCH /authorizations", {
headers: options.headers,
});
}
catch (error) {
// we expect a 401
if (error.status !== 401)
throw error;
}
}
// we set state.totp after the request to make sure that it's valid
const totp = await state.strategyOptions.on2Fa();
try {
const response = await requestWith2Fa(state, Object.assign({}, options, {
headers: Object.assign({}, options.headers, {
"x-github-otp": totp,
}),
}), customRequest);
state.totp = totp;
return response;
}
catch (error) {
// error without a headers property is an unexpected error
// which we don’t cover with tests
/* istanbul ignore next */
if (!error.headers)
throw error;
const totpRequired = /required/.test(error.headers["x-github-otp"] || "");
// unless the error is an invalid TOTP, we can cache it
if (!totpRequired) {
state.totp = totp;
}
throw error;
}
}
}
Example #3
Source File: fetch-wrapper.js From github-actions-issue-to-work-item with GNU General Public License v3.0 | 4 votes |
export default function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect
}, requestOptions.request))
.then(response => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions
});
}
if (status >= 400) {
return response
.text()
.then(message => {
const error = new RequestError(message, status, {
headers,
request: requestOptions
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBuffer(response);
})
.then(data => {
return {
status,
url,
headers,
data
};
})
.catch(error => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions
});
});
}
Example #4
Source File: index.js From javascript-actions with MIT License | 4 votes |
function fetchWrapper(requestOptions) {
const log = requestOptions.request && requestOptions.request.log
? requestOptions.request.log
: console;
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
},
// `requestOptions.request.agent` type is incompatible
// see https://github.com/octokit/types.ts/pull/264
requestOptions.request))
.then(async (response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if ("deprecation" in headers) {
const matches = headers.link && headers.link.match(/<([^>]+)>; rel="deprecation"/);
const deprecationLink = matches && matches.pop();
log.warn(`[@octokit/request] "${requestOptions.method} ${requestOptions.url}" is deprecated. It is scheduled to be removed on ${headers.sunset}${deprecationLink ? `. See ${deprecationLink}` : ""}`);
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
response: {
url,
status,
headers,
data: undefined,
},
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
response: {
url,
status,
headers,
data: await getResponseData(response),
},
request: requestOptions,
});
}
if (status >= 400) {
const data = await getResponseData(response);
const error = new RequestError(toErrorMessage(data), status, {
response: {
url,
status,
headers,
data,
},
request: requestOptions,
});
throw error;
}
return getResponseData(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError)
throw error;
throw new RequestError(error.message, 500, {
request: requestOptions,
});
});
}
Example #5
Source File: index.js From github-action-get-latest-release with MIT License | 4 votes |
function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
}, requestOptions.request))
.then((response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions,
});
}
if (status >= 400) {
return response
.text()
.then((message) => {
const error = new RequestError(message, status, {
headers,
request: requestOptions,
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBufferResponse(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions,
});
});
}
Example #6
Source File: index.js From action-install-gh-release with Apache License 2.0 | 4 votes |
function fetchWrapper(requestOptions) {
const log = requestOptions.request && requestOptions.request.log
? requestOptions.request.log
: console;
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
},
// `requestOptions.request.agent` type is incompatible
// see https://github.com/octokit/types.ts/pull/264
requestOptions.request))
.then(async (response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if ("deprecation" in headers) {
const matches = headers.link && headers.link.match(/<([^>]+)>; rel="deprecation"/);
const deprecationLink = matches && matches.pop();
log.warn(`[@octokit/request] "${requestOptions.method} ${requestOptions.url}" is deprecated. It is scheduled to be removed on ${headers.sunset}${deprecationLink ? `. See ${deprecationLink}` : ""}`);
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
response: {
url,
status,
headers,
data: undefined,
},
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
response: {
url,
status,
headers,
data: await getResponseData(response),
},
request: requestOptions,
});
}
if (status >= 400) {
const data = await getResponseData(response);
const error = new RequestError(toErrorMessage(data), status, {
response: {
url,
status,
headers,
data,
},
request: requestOptions,
});
throw error;
}
return getResponseData(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError)
throw error;
throw new RequestError(error.message, 500, {
request: requestOptions,
});
});
}
Example #7
Source File: index.js From verify-linked-issue-action with MIT License | 4 votes |
function fetchWrapper(requestOptions) {
const log = requestOptions.request && requestOptions.request.log
? requestOptions.request.log
: console;
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
},
// `requestOptions.request.agent` type is incompatible
// see https://github.com/octokit/types.ts/pull/264
requestOptions.request))
.then(async (response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if ("deprecation" in headers) {
const matches = headers.link && headers.link.match(/<([^>]+)>; rel="deprecation"/);
const deprecationLink = matches && matches.pop();
log.warn(`[@octokit/request] "${requestOptions.method} ${requestOptions.url}" is deprecated. It is scheduled to be removed on ${headers.sunset}${deprecationLink ? `. See ${deprecationLink}` : ""}`);
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
response: {
url,
status,
headers,
data: undefined,
},
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
response: {
url,
status,
headers,
data: await getResponseData(response),
},
request: requestOptions,
});
}
if (status >= 400) {
const data = await getResponseData(response);
const error = new RequestError(toErrorMessage(data), status, {
response: {
url,
status,
headers,
data,
},
request: requestOptions,
});
throw error;
}
return getResponseData(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError)
throw error;
throw new RequestError(error.message, 500, {
request: requestOptions,
});
});
}
Example #8
Source File: index.js From action-download-artifact with MIT License | 4 votes |
function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
},
// `requestOptions.request.agent` type is incompatible
// see https://github.com/octokit/types.ts/pull/264
requestOptions.request))
.then((response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions,
});
}
if (status >= 400) {
return response
.text()
.then((message) => {
const error = new RequestError(message, status, {
headers,
request: requestOptions,
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBufferResponse(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions,
});
});
}
Example #9
Source File: fetch-wrapper.js From action-download-artifact with MIT License | 4 votes |
export default function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
},
// `requestOptions.request.agent` type is incompatible
// see https://github.com/octokit/types.ts/pull/264
requestOptions.request))
.then((response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions,
});
}
if (status >= 400) {
return response
.text()
.then((message) => {
const error = new RequestError(message, status, {
headers,
request: requestOptions,
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBuffer(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions,
});
});
}
Example #10
Source File: index.js From replace-action with MIT License | 4 votes |
function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect
}, requestOptions.request))
.then(response => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requsets
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions
});
}
if (status >= 400) {
return response
.text()
.then(message => {
const error = new RequestError(message, status, {
headers,
request: requestOptions
});
try {
Object.assign(error, JSON.parse(error.message));
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBufferResponse(response);
})
.then(data => {
return {
status,
url,
headers,
data
};
})
.catch(error => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions
});
});
}
Example #11
Source File: fetch-wrapper.js From replace-action with MIT License | 4 votes |
export default function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect
}, requestOptions.request))
.then(response => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requsets
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions
});
}
if (status >= 400) {
return response
.text()
.then(message => {
const error = new RequestError(message, status, {
headers,
request: requestOptions
});
try {
Object.assign(error, JSON.parse(error.message));
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBuffer(response);
})
.then(data => {
return {
status,
url,
headers,
data
};
})
.catch(error => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions
});
});
}
Example #12
Source File: index.js From github-actions-issue-to-work-item with GNU General Public License v3.0 | 4 votes |
function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect
}, requestOptions.request))
.then(response => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions
});
}
if (status >= 400) {
return response
.text()
.then(message => {
const error = new RequestError(message, status, {
headers,
request: requestOptions
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBufferResponse(response);
})
.then(data => {
return {
status,
url,
headers,
data
};
})
.catch(error => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions
});
});
}
Example #13
Source File: fetch-wrapper.js From change-string-case-action with ISC License | 4 votes |
export default function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
}, requestOptions.request))
.then((response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions,
});
}
if (status >= 400) {
return response
.text()
.then((message) => {
const error = new RequestError(message, status, {
headers,
request: requestOptions,
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBuffer(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions,
});
});
}
Example #14
Source File: index.js From compute-tag with BSD 3-Clause "New" or "Revised" License | 4 votes |
function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
}, requestOptions.request))
.then((response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions,
});
}
if (status >= 400) {
return response
.text()
.then((message) => {
const error = new RequestError(message, status, {
headers,
request: requestOptions,
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBufferResponse(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions,
});
});
}
Example #15
Source File: index.js From deploy-gae-action with MIT License | 4 votes |
function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
}, requestOptions.request))
.then((response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions,
});
}
if (status >= 400) {
return response
.text()
.then((message) => {
const error = new RequestError(message, status, {
headers,
request: requestOptions,
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBufferResponse(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions,
});
});
}
Example #16
Source File: index.js From ipfs-action with MIT License | 4 votes |
function fetchWrapper(requestOptions) {
const log = requestOptions.request && requestOptions.request.log
? requestOptions.request.log
: console;
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
},
// `requestOptions.request.agent` type is incompatible
// see https://github.com/octokit/types.ts/pull/264
requestOptions.request))
.then(async (response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if ("deprecation" in headers) {
const matches = headers.link && headers.link.match(/<([^>]+)>; rel="deprecation"/);
const deprecationLink = matches && matches.pop();
log.warn(`[@octokit/request] "${requestOptions.method} ${requestOptions.url}" is deprecated. It is scheduled to be removed on ${headers.sunset}${deprecationLink ? `. See ${deprecationLink}` : ""}`);
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
response: {
url,
status,
headers,
data: undefined,
},
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
response: {
url,
status,
headers,
data: await getResponseData(response),
},
request: requestOptions,
});
}
if (status >= 400) {
const data = await getResponseData(response);
const error = new RequestError(toErrorMessage(data), status, {
response: {
url,
status,
headers,
data,
},
request: requestOptions,
});
throw error;
}
return getResponseData(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError)
throw error;
throw new RequestError(error.message, 500, {
request: requestOptions,
});
});
}
Example #17
Source File: fetch-wrapper.js From ipfs-action with MIT License | 4 votes |
export default function fetchWrapper(requestOptions) {
const log = requestOptions.request && requestOptions.request.log
? requestOptions.request.log
: console;
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
},
// `requestOptions.request.agent` type is incompatible
// see https://github.com/octokit/types.ts/pull/264
requestOptions.request))
.then(async (response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if ("deprecation" in headers) {
const matches = headers.link && headers.link.match(/<([^>]+)>; rel="deprecation"/);
const deprecationLink = matches && matches.pop();
log.warn(`[@octokit/request] "${requestOptions.method} ${requestOptions.url}" is deprecated. It is scheduled to be removed on ${headers.sunset}${deprecationLink ? `. See ${deprecationLink}` : ""}`);
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
response: {
url,
status,
headers,
data: undefined,
},
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
response: {
url,
status,
headers,
data: await getResponseData(response),
},
request: requestOptions,
});
}
if (status >= 400) {
const data = await getResponseData(response);
const error = new RequestError(toErrorMessage(data), status, {
response: {
url,
status,
headers,
data,
},
request: requestOptions,
});
throw error;
}
return getResponseData(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError)
throw error;
throw new RequestError(error.message, 500, {
request: requestOptions,
});
});
}
Example #18
Source File: index.js From discord-commits with MIT License | 4 votes |
function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
}, requestOptions.request))
.then((response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions,
});
}
if (status >= 400) {
return response
.text()
.then((message) => {
const error = new RequestError(message, status, {
headers,
request: requestOptions,
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBufferResponse(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions,
});
});
}
Example #19
Source File: index.js From dotnet-bump-version with MIT License | 4 votes |
function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
}, requestOptions.request))
.then((response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions,
});
}
if (status >= 400) {
return response
.text()
.then((message) => {
const error = new RequestError(message, status, {
headers,
request: requestOptions,
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBufferResponse(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions,
});
});
}
Example #20
Source File: fetch-wrapper.js From dotnet-bump-version with MIT License | 4 votes |
export default function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
}, requestOptions.request))
.then((response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions,
});
}
if (status >= 400) {
return response
.text()
.then((message) => {
const error = new RequestError(message, status, {
headers,
request: requestOptions,
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBuffer(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions,
});
});
}
Example #21
Source File: index.js From payload-info-action with MIT License | 4 votes |
function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect
}, requestOptions.request))
.then(response => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requsets
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions
});
}
if (status >= 400) {
return response
.text()
.then(message => {
const error = new RequestError(message, status, {
headers,
request: requestOptions
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array Fotmat
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBufferResponse(response);
})
.then(data => {
return {
status,
url,
headers,
data
};
})
.catch(error => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions
});
});
}
Example #22
Source File: fetch-wrapper.js From payload-info-action with MIT License | 4 votes |
export default function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect
}, requestOptions.request))
.then(response => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requsets
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions
});
}
if (status >= 400) {
return response
.text()
.then(message => {
const error = new RequestError(message, status, {
headers,
request: requestOptions
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array Fotmat
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBuffer(response);
})
.then(data => {
return {
status,
url,
headers,
data
};
})
.catch(error => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions
});
});
}
Example #23
Source File: index.js From change-string-case-action with ISC License | 4 votes |
function fetchWrapper(requestOptions) {
if (isPlainObject(requestOptions.body) ||
Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body);
}
let headers = {};
let status;
let url;
const fetch = (requestOptions.request && requestOptions.request.fetch) || nodeFetch;
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
}, requestOptions.request))
.then((response) => {
url = response.url;
status = response.status;
for (const keyAndValue of response.headers) {
headers[keyAndValue[0]] = keyAndValue[1];
}
if (status === 204 || status === 205) {
return;
}
// GitHub API returns 200 for HEAD requests
if (requestOptions.method === "HEAD") {
if (status < 400) {
return;
}
throw new RequestError(response.statusText, status, {
headers,
request: requestOptions,
});
}
if (status === 304) {
throw new RequestError("Not modified", status, {
headers,
request: requestOptions,
});
}
if (status >= 400) {
return response
.text()
.then((message) => {
const error = new RequestError(message, status, {
headers,
request: requestOptions,
});
try {
let responseBody = JSON.parse(error.message);
Object.assign(error, responseBody);
let errors = responseBody.errors;
// Assumption `errors` would always be in Array format
error.message =
error.message + ": " + errors.map(JSON.stringify).join(", ");
}
catch (e) {
// ignore, see octokit/rest.js#684
}
throw error;
});
}
const contentType = response.headers.get("content-type");
if (/application\/json/.test(contentType)) {
return response.json();
}
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
return response.text();
}
return getBufferResponse(response);
})
.then((data) => {
return {
status,
url,
headers,
data,
};
})
.catch((error) => {
if (error instanceof RequestError) {
throw error;
}
throw new RequestError(error.message, 500, {
headers,
request: requestOptions,
});
});
}