node-fetch#Response TypeScript Examples
The following examples show how to use
node-fetch#Response.
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: utils.ts From ms-teams-deploy-card with MIT License | 7 votes |
export function submitNotification(webhookBody: WebhookBody) {
const webhookUri = getInput("webhook-uri", { required: true });
const webhookBodyJson = JSON.stringify(webhookBody, undefined, 2);
return fetch(webhookUri, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: webhookBodyJson,
})
.then((response: Response) => {
setOutput("webhook-body", webhookBodyJson);
info(webhookBodyJson);
return response;
})
.catch(console.error);
}
Example #2
Source File: FetchUrlReader.ts From backstage with Apache License 2.0 | 6 votes |
async readUrl(
url: string,
options?: ReadUrlOptions,
): Promise<ReadUrlResponse> {
let response: Response;
try {
response = await fetch(url, {
headers: {
...(options?.etag && { 'If-None-Match': options.etag }),
},
// TODO(freben): The signal cast is there because pre-3.x versions of
// node-fetch have a very slightly deviating AbortSignal type signature.
// The difference does not affect us in practice however. The cast can
// be removed after we support ESM for CLI dependencies and migrate to
// version 3 of node-fetch.
// https://github.com/backstage/backstage/issues/8242
signal: options?.signal as any,
});
} catch (e) {
throw new Error(`Unable to read ${url}, ${e}`);
}
if (response.status === 304) {
throw new NotModifiedError();
}
if (response.ok) {
return ReadUrlResponseFactory.fromNodeJSReadable(response.body, {
etag: response.headers.get('ETag') ?? undefined,
});
}
const message = `could not read ${url}, ${response.status} ${response.statusText}`;
if (response.status === 404) {
throw new NotFoundError(message);
}
throw new Error(message);
}
Example #3
Source File: Request.ts From discord-statuspage-v2 with MIT License | 6 votes |
/**
* Check the list of webhooks and remove invalid ones
* @param {Database} db The database instance
* @returns {Promise<{existing: number, deleted: number}>}
*/
export async function checkWebhooks (db: Database): Promise<{ existing: number, deleted: number }> {
const webhooks: DatabaseWebhook[] = await db.getWebhooks()
const stats = { total: webhooks.length, existing: 0, deleted: 0 }
for (const { id, token } of webhooks) {
const result: void | Response = await fetch(`https://discord.com/api/webhooks/${id}/${token}`)
.catch(console.error)
if (!result || result?.status === 404 || result?.status === 401) {
db.deleteWebhook(id, token)
.catch(console.error)
stats.deleted++
} else stats.existing++
}
return stats
}
Example #4
Source File: GithubUrlReader.ts From backstage with Apache License 2.0 | 6 votes |
private async fetchResponse(
url: string | URL,
init: RequestInit,
): Promise<Response> {
const urlAsString = url.toString();
const response = await fetch(urlAsString, init);
if (!response.ok) {
const message = `Request failed for ${urlAsString}, ${response.status} ${response.statusText}`;
if (response.status === 404) {
throw new NotFoundError(message);
}
throw new Error(message);
}
return response;
}
Example #5
Source File: verifier.ts From balancer-v2-monorepo with GNU General Public License v3.0 | 6 votes |
private async verifyContract(url: string, req: EtherscanVerifyRequest): Promise<EtherscanResponse> {
const parameters = new URLSearchParams({ ...req });
const requestDetails = { method: 'post', body: parameters };
let response: Response;
try {
response = await fetch(url, requestDetails);
} catch (error) {
throw Error(`Failed to send verification request. Reason: ${error.message}`);
}
if (!response.ok) {
const responseText = await response.text();
throw Error(`Failed to send verification request.\nHTTP code: ${response.status}.\nResponse: ${responseText}`);
}
const etherscanResponse = new EtherscanResponse(await response.json());
if (!etherscanResponse.isOk()) throw Error(etherscanResponse.message);
return etherscanResponse;
}
Example #6
Source File: index.ts From nodetskeleton with MIT License | 6 votes |
private async processErrorResponse<E>(response: Response): Promise<[E | string, boolean]> {
let result = null;
try {
result = await response.text();
return [JSON.parse(result), SERIALIZED];
} catch (error) {
return [result as E | string, !SERIALIZED];
}
}
Example #7
Source File: downloadFile.ts From shroom with GNU Lesser General Public License v3.0 | 6 votes |
export async function fetchRetry(url: string) {
let response: Response | undefined;
let count = 0;
do {
try {
response = await fetch(url);
} catch (e) {
// Ignore network error
}
await new Promise((resolve) => setTimeout(resolve, count * 5000));
count++;
} while ((response == null || response.status >= 500) && count < 20);
return response;
}
Example #8
Source File: fetch-blocks.ts From eth-total-supply with MIT License | 6 votes |
/**
* Retrieves the latest synchronized block.
*/
export async function getLatestBlockNumber(): Promise<number> {
// A normal JSON-RPC request without batching
// More information: https://eth.wiki/json-rpc/API#eth_blocknumber
const result: Readonly<Response> = await fetch(jsonRPCEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: 1,
jsonrpc: '2.0',
method: 'eth_blockNumber'
})
});
const resultJSON = await result.json();
return parseInt(resultJSON.result);
}
Example #9
Source File: util.ts From livepeer-com with MIT License | 6 votes |
fetchWithTimeout = (
url: string,
options: RequestInitWithTimeout
) =>
new Promise<Response>((resolve, reject) => {
let timeout = setTimeout(() => {
timeout = null;
reject("timeout");
}, options.timeout || 10 * 1000);
return fetch(url, options).then(
(response) => {
if (timeout === null) {
// already timed out
return;
}
clearTimeout(timeout);
return resolve(response);
},
(rejectReason) => {
if (timeout === null) {
// already timed out
return;
}
clearTimeout(timeout);
return reject(rejectReason);
}
);
})
Example #10
Source File: QBittorrent.ts From cross-seed with Apache License 2.0 | 6 votes |
async login(): Promise<void> {
const { origin, pathname, username, password } = this.url;
let searchParams;
try {
searchParams = new URLSearchParams({
username: decodeURIComponent(username),
password: decodeURIComponent(password),
});
} catch (e) {
throw new CrossSeedError("qBittorrent url must be percent-encoded");
}
let response: Response;
try {
response = await fetch(
`${origin}${pathname}/auth/login?${searchParams}`
);
} catch (e) {
throw new CrossSeedError(`qBittorrent login failed: ${e.message}`);
}
if (response.status !== 200) {
throw new CrossSeedError(
`qBittorrent login failed with code ${response.status}`
);
}
const cookieArray = response.headers.raw()["set-cookie"];
if (cookieArray) {
this.cookie = cookieArray[0].split(";")[0];
} else {
throw new CrossSeedError(
`qBittorrent login failed: Invalid username or password`
);
}
}
Example #11
Source File: eos.ts From coin-wallets with Apache License 2.0 | 6 votes |
// https://docs.dfuse.io/guides/eosio/tutorials/write-chain/
function createCustomizedFetch(
client: DfuseClient,
): (input: string | Request, init: RequestInit) => Promise<Response> {
const customizedFetch = async (input: string | Request, init: RequestInit): Promise<Response> => {
if (init.headers === undefined) {
init.headers = {}; // eslint-disable-line no-param-reassign
}
// This is highly optimized and cached, so while the token is fresh, this is very fast
const apiTokenInfo = await client.getTokenInfo();
const headers = init.headers as { [name: string]: string };
headers.Authorization = `Bearer ${apiTokenInfo.token}`;
headers['X-Eos-Push-Guarantee'] = 'in-block';
return fetch(input, init);
};
return customizedFetch;
}
Example #12
Source File: http.service.ts From loopback4-microservice-catalog with MIT License | 6 votes |
private async handleRes<T>(res: Response): Promise<T> {
if (res.status === STATUS_CODE.OK) {
return (res.json() as Promise<T>).catch(
e => Promise.resolve({}) as Promise<T>,
);
} else if (res.status === STATUS_CODE.NO_CONTENT) {
return Promise.resolve({}) as Promise<T>;
} else {
throw new HttpErrors.BadRequest(await res.text());
}
}
Example #13
Source File: http-service.ts From Discord-Bot-TypeScript-Template with MIT License | 6 votes |
public async get(url: string | URL, authorization: string): Promise<Response> {
return await fetch(url.toString(), {
method: 'get',
headers: {
Authorization: authorization,
Accept: 'application/json',
},
});
}
Example #14
Source File: Request.ts From discord-statuspage-v2 with MIT License | 6 votes |
/**
* Post an update to all of the webhooks in the list
* @param {string} body The message to send
* @param {Database} db The database instance
* @returns {Promise<{total: number, responses: Map<string, Response>}>}
*/
export async function postUpdate (body: any, db: Database): Promise<{ total: number, responses: Map<string, Response>}> {
const webhooks = await db.getWebhooks()
const stats = { total: webhooks.length, responses: new Map<string, Response>() }
for (const { id, token } of webhooks) {
const result: void | Response = await fetch(`https://discord.com/api/webhooks/${id}/${token}?wait=1`, {
method: 'post',
body,
headers: {
'content-type': 'application/json'
}
})
.catch(console.error)
if (result) stats.responses.set(id, result ?? null)
}
return stats
}
Example #15
Source File: request.ts From foundry-rpc-js with ISC License | 6 votes |
export default function request(node: string, body: Body): Promise<Response> {
return fetch(node, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(body)
});
}
Example #16
Source File: web.ts From Wikitext-VSCode-Extension with MIT License | 6 votes |
private async fetchArchive(): Promise<void> {
// Fetch content and archive in parallel
const websiteResponse: Promise<Response> = fetch(this.url);
const archiveResponse: Promise<Response> = fetch(this.archiveApiUrl);
const results: [Response, Response] = await Promise.all([websiteResponse, archiveResponse]);
const websiteText: string = await results[0].text();
this.metaData = cheerio.load(websiteText);
const archiveJSON = await results[1].json();
const re: ArchiveResult = ArchiveConvert.toArchiveResult(archiveJSON);
// Check archive and get the closest
if (re.archivedSnapshots.closest) {
this.archivedUrl = re.archivedSnapshots.closest.url;
this.archivedDate = DateTime.fromFormat(re.archivedSnapshots.closest.timestamp, "yyyyMMddhhmmss").toISODate();
}
}
Example #17
Source File: http-service.ts From Discord-Bot-TypeScript-Template with MIT License | 6 votes |
public async post(url: string | URL, authorization: string, body?: object): Promise<Response> {
return await fetch(url.toString(), {
method: 'post',
headers: {
Authorization: authorization,
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: body ? JSON.stringify(body) : undefined,
});
}
Example #18
Source File: AzureUrlReader.ts From backstage with Apache License 2.0 | 6 votes |
async readUrl(
url: string,
options?: ReadUrlOptions,
): Promise<ReadUrlResponse> {
// TODO: etag is not implemented yet.
const { signal } = options ?? {};
const builtUrl = getAzureFileFetchUrl(url);
let response: Response;
try {
response = await fetch(builtUrl, {
...getAzureRequestOptions(this.integration.config),
// TODO(freben): The signal cast is there because pre-3.x versions of
// node-fetch have a very slightly deviating AbortSignal type signature.
// The difference does not affect us in practice however. The cast can
// be removed after we support ESM for CLI dependencies and migrate to
// version 3 of node-fetch.
// https://github.com/backstage/backstage/issues/8242
...(signal && { signal: signal as any }),
});
} catch (e) {
throw new Error(`Unable to read ${url}, ${e}`);
}
// for private repos when PAT is not valid, Azure API returns a http status code 203 with sign in page html
if (response.ok && response.status !== 203) {
return ReadUrlResponseFactory.fromNodeJSReadable(response.body);
}
const message = `${url} could not be read as ${builtUrl}, ${response.status} ${response.statusText}`;
if (response.status === 404) {
throw new NotFoundError(message);
}
throw new Error(message);
}
Example #19
Source File: client.ts From Protoman with MIT License | 5 votes |
async function translateResponse(
response: Response,
request: RequestDescriptor,
protoCtx: ProtoCtx,
dt: number,
): Promise<ResponseDescriptor> {
const responseHeaders = unconvertHeaders(response.headers);
const saidContentType = responseHeaders.find(([name]) => name === 'content-type')?.[1];
const { expectedProtobufMsg, expectedProtobufMsgOnError } = request;
let responseBodyType: ResponseBodyType = 'unknown';
let responseBodyValue: ResponseBodyValue = undefined;
let warning: string | undefined = undefined;
const buf = new Uint8Array(await response.arrayBuffer());
if (buf.length === 0) {
responseBodyType = 'empty';
responseBodyValue = undefined;
} else if (saidContentType === CONTENT_TYPE_JSON) {
responseBodyType = 'json';
responseBodyValue = toJson(buf);
} else if (saidContentType?.includes(CONTENT_TYPE_HTML)) {
responseBodyType = 'html';
responseBodyValue = toStr(buf);
} else if (expectedProtobufMsg) {
let msgToUse = expectedProtobufMsg;
if (!response.ok && expectedProtobufMsgOnError) {
msgToUse = expectedProtobufMsgOnError;
}
const res = await deserializeProtobuf(buf, msgToUse, protoCtx);
switch (res.tag) {
case 'invalid':
if (res.value) {
responseBodyType = 'json';
responseBodyValue = res.value;
} else {
responseBodyType = 'unknown';
responseBodyValue = undefined;
}
warning = res.error;
break;
case 'valid':
responseBodyType = 'protobuf';
responseBodyValue = res.value;
break;
}
}
return {
statusCode: response.status,
headers: responseHeaders,
body: {
type: responseBodyType,
value: responseBodyValue,
bodySize: buf.length,
},
warning,
time: dt,
};
}
Example #20
Source File: GerritUrlReader.ts From backstage with Apache License 2.0 | 5 votes |
async readTree(
url: string,
options?: ReadTreeOptions,
): Promise<ReadTreeResponse> {
const { filePath } = parseGerritGitilesUrl(this.integration.config, url);
const apiUrl = getGerritBranchApiUrl(this.integration.config, url);
let response: Response;
try {
response = await fetch(apiUrl, {
method: 'GET',
...getGerritRequestOptions(this.integration.config),
});
} catch (e) {
throw new Error(`Unable to read branch state ${url}, ${e}`);
}
if (response.status === 404) {
throw new NotFoundError(`Not found: ${url}`);
}
if (!response.ok) {
throw new Error(
`${url} could not be read as ${apiUrl}, ${response.status} ${response.statusText}`,
);
}
const branchInfo = (await parseGerritJsonResponse(response as any)) as {
revision: string;
};
if (options?.etag === branchInfo.revision) {
throw new NotModifiedError();
}
const git = Git.fromAuth({
username: this.integration.config.username,
password: this.integration.config.password,
});
const tempDir = await createTemporaryDirectory(this.workDir);
const cloneUrl = getGerritCloneRepoUrl(this.integration.config, url);
try {
// The "fromTarArchive" function will strip the top level directory so
// an additional directory level is created when we clone.
await git.clone({
url: cloneUrl,
dir: joinPath(tempDir, 'repo'),
ref: branchInfo.revision,
depth: 1,
});
const data = await new Promise<Buffer>(async resolve => {
await pipeline(
tar.create({ cwd: tempDir }, ['']),
concatStream(resolve),
);
});
const tarArchive = Readable.from(data);
return await this.deps.treeResponseFactory.fromTarArchive({
stream: tarArchive as unknown as Readable,
subpath: filePath === '/' ? undefined : filePath,
etag: branchInfo.revision,
filter: options?.filter,
});
} catch (error) {
throw new Error(`Could not clone ${cloneUrl}: ${error}`);
} finally {
await fs.rm(tempDir, { recursive: true, force: true });
}
}
Example #21
Source File: types.ts From loopback4-microservice-catalog with MIT License | 5 votes |
identityResponseTransformer: ResponseTransformer = ( response: Response, ) => response
Example #22
Source File: GithubUrlReader.ts From backstage with Apache License 2.0 | 5 votes |
async readUrl(
url: string,
options?: ReadUrlOptions,
): Promise<ReadUrlResponse> {
const credentials = await this.deps.credentialsProvider.getCredentials({
url,
});
const ghUrl = getGitHubFileFetchUrl(
url,
this.integration.config,
credentials,
);
let response: Response;
try {
response = await fetch(ghUrl, {
headers: {
...credentials?.headers,
...(options?.etag && { 'If-None-Match': options.etag }),
Accept: 'application/vnd.github.v3.raw',
},
// TODO(freben): The signal cast is there because pre-3.x versions of
// node-fetch have a very slightly deviating AbortSignal type signature.
// The difference does not affect us in practice however. The cast can
// be removed after we support ESM for CLI dependencies and migrate to
// version 3 of node-fetch.
// https://github.com/backstage/backstage/issues/8242
signal: options?.signal as any,
});
} catch (e) {
throw new Error(`Unable to read ${url}, ${e}`);
}
if (response.status === 304) {
throw new NotModifiedError();
}
if (response.ok) {
return ReadUrlResponseFactory.fromNodeJSReadable(response.body, {
etag: response.headers.get('ETag') ?? undefined,
});
}
let message = `${url} could not be read as ${ghUrl}, ${response.status} ${response.statusText}`;
if (response.status === 404) {
throw new NotFoundError(message);
}
// GitHub returns a 403 response with a couple of headers indicating rate
// limit status. See more in the GitHub docs:
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
if (
response.status === 403 &&
response.headers.get('X-RateLimit-Remaining') === '0'
) {
message += ' (rate limit exceeded)';
}
throw new Error(message);
}
Example #23
Source File: executeCognigyApiRequest.ts From Extensions with MIT License | 5 votes |
executeCognigyApiRequest = createNodeDescriptor({
type: "executeCognigyApiRequest",
defaultLabel: "Cognigy API Request",
fields: [
{
key: "connection",
label: "The api-key connection which should be used.",
type: "connection",
params: {
connectionType: "api-key" // this needs to match the connections 'type' property
}
},
{
key: "path",
label: "The API path to call. Full path required.",
type: "cognigyText",
defaultValue: "https://swapi.dev/api/people/1"
}
],
function: async ({ cognigy, config }: IExecuteCognigyApiRequestParams) => {
const { api, context } = cognigy;
const { path, connection } = config;
let response, rawResponse: Response;
try {
rawResponse = await fetch(path);
response = await rawResponse.json();
api.say("The response of your API call is stored in the Context object");
/* write response into 'input object' */
context.apiResponse = response;
/* output the connection that was used in this execution */
api.say(`The connection that was used during this call: `, connection);
} catch (err) {
// /* communicate the error in the interaction panel */
api.say(`Your response failed. Error was: ${err}`);
}
}
})
Example #24
Source File: helper.ts From action-setup-vim with MIT License | 5 votes |
export function mockFetch(): void {
mock('node-fetch', async (url: string) =>
Promise.resolve(new Response(`dummy response for ${url}`, { status: 404, statusText: 'Not found for dummy' })),
);
}
Example #25
Source File: solana.ts From serum-rest-server with Apache License 2.0 | 5 votes |
export function createRpcRequest(url: string): RpcRequest {
const server = new jayson(async (request, callback) => {
const options = {
method: "POST",
body: request,
headers: {
"Content-Type": "application/json",
},
};
try {
let too_many_requests_retries = 5;
let res: Response = {};
let waitTime = 500;
for (;;) {
res = await fetch(url, options);
if (res.status !== 429 /* Too many requests */) {
break;
}
too_many_requests_retries -= 1;
if (too_many_requests_retries === 0) {
break;
}
console.log(
`Server responded with ${res.status} ${res.statusText}. Retrying after ${waitTime}ms delay...`
);
await sleep(waitTime);
waitTime *= 2;
}
const text = await res.text();
if (res.ok) {
callback(null, text);
} else {
callback(new Error(`${res.status} ${res.statusText}: ${text}`));
}
} catch (err) {
callback(err);
}
}, {});
return (method, args) => {
return new Promise((resolve, reject) => {
server.request(method, args, (err, response) => {
if (err) {
reject(err);
return;
}
resolve(response);
});
});
};
}
Example #26
Source File: get-buffer-response.d.ts From aws-ssm-send-command with MIT License | 5 votes |
export default function getBufferResponse(response: Response): Promise<ArrayBuffer>;
Example #27
Source File: BitbucketServerUrlReader.ts From backstage with Apache License 2.0 | 5 votes |
async readUrl(
url: string,
options?: ReadUrlOptions,
): Promise<ReadUrlResponse> {
const { etag, signal } = options ?? {};
const bitbucketUrl = getBitbucketServerFileFetchUrl(
url,
this.integration.config,
);
const requestOptions = getBitbucketServerRequestOptions(
this.integration.config,
);
let response: Response;
try {
response = await fetch(bitbucketUrl.toString(), {
headers: {
...requestOptions.headers,
...(etag && { 'If-None-Match': etag }),
},
// TODO(freben): The signal cast is there because pre-3.x versions of
// node-fetch have a very slightly deviating AbortSignal type signature.
// The difference does not affect us in practice however. The cast can be
// removed after we support ESM for CLI dependencies and migrate to
// version 3 of node-fetch.
// https://github.com/backstage/backstage/issues/8242
...(signal && { signal: signal as any }),
});
} catch (e) {
throw new Error(`Unable to read ${url}, ${e}`);
}
if (response.status === 304) {
throw new NotModifiedError();
}
if (response.ok) {
return ReadUrlResponseFactory.fromNodeJSReadable(response.body, {
etag: response.headers.get('ETag') ?? undefined,
});
}
const message = `${url} could not be read as ${bitbucketUrl}, ${response.status} ${response.statusText}`;
if (response.status === 404) {
throw new NotFoundError(message);
}
throw new Error(message);
}
Example #28
Source File: tools.ts From NanoRPCProxy with GNU General Public License v3.0 | 5 votes |
function checkStatus(res: Response) {
if (res.ok) { // res.status >= 200 && res.status < 300
return res
} else {
throw new APIError(res.statusText)
}
}
Example #29
Source File: fetchDom.ts From namemc with MIT License | 5 votes |
export async function fetchDOM(path: string): Promise<cheerio.Root> {
// Fetch page
const response: Response = await fetch(`https://namemc.com/${path}`);
const body: string = await response.text();
// Get DOM
const $ = cheerio.load(body);
// Make sure its available
if ($("#status-bar").html() !== null) {
const status = $("#status-bar").children(".card-body")
.children(".row")
.children(".col-md-5")
.children(".row")
.children(".col-sm-6")
.first()
.children("div")
.last()
.text();
if ( status === "Available*") throw new Error("Username is not in use.");
}
// Get if user is being rate limited
if (/Error:\s429\s\((\w|\s)*\)Retry-After:\s((\w|\s)+)*/g.test($(".my-2").text())) throw new Error("You are being rate limited. Retry after: 5 seconds.");
// Get if cloudflare blocked the request
if ($("body").text()
.includes("Cloudflare")) {
const ray = $("body")
.text()
.split("\n")
.filter((line) => line.replace(/\s|\s/g, "") !== "")
.slice(12)[0];
const rayID = parseInt(ray.split(": ")[1], 16);
throw new Error(`Cloudflare has rejected the request. Ray ID: ${rayID}`);
}
// Return DOM
return $;
}