node-fetch#HeadersInit TypeScript Examples
The following examples show how to use
node-fetch#HeadersInit.
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.ts From fetch-gh-release-asset with MIT License | 7 votes |
baseFetchAssetFile = async (
octokit: ReturnType<typeof github.getOctokit>,
{ id, outputPath, owner, repo, token }: FetchAssetFileOptions
) => {
const {
body,
headers: { accept, 'user-agent': userAgent },
method,
url,
} = octokit.request.endpoint(
'GET /repos/:owner/:repo/releases/assets/:asset_id',
{
asset_id: id,
headers: {
accept: 'application/octet-stream',
},
owner,
repo,
}
);
let headers: HeadersInit = {
accept,
authorization: `token ${token}`,
};
if (typeof userAgent !== 'undefined')
headers = { ...headers, 'user-agent': userAgent };
const response = await fetch(url, { body, headers, method });
if (!response.ok) {
const text = await response.text();
core.warning(text);
throw new Error('Invalid response');
}
const blob = await response.blob();
const arrayBuffer = await blob.arrayBuffer();
await mkdir(dirname(outputPath), { recursive: true });
void (await writeFile(outputPath, new Uint8Array(arrayBuffer)));
}
Example #2
Source File: connector.ts From majsoul-api with MIT License | 4 votes |
async function getPassport(
{userId, accessToken, userAgent, existingCookies }: {
userId: string;
accessToken: string;
userAgent: string;
existingCookies: store.Cookie[];
}
): Promise<{
passport: majsoul.Passport,
loginCookies: store.Cookie[]
}> {
const sharedSpoofHeaders = {
"User-Agent": userAgent
};
console.log(sharedSpoofHeaders);
const cookie = (existingCookies)
.map(cookie => `${cookie.key}=${cookie.value}`).join(";");
console.log(cookie, cookie.length);
const optionsHeaders: HeadersInit = {
...sharedSpoofHeaders,
}
if (cookie.length) {
(optionsHeaders as any).cookie = cookie;
}
const loginCookies = [...existingCookies];
try {
const headers = (await fetch("https://passport.mahjongsoul.com/user/login", {
method: "OPTIONS",
headers: optionsHeaders,
})).headers.raw();
console.log(headers);
const cookieTime = Date.now();
const newCookies = headers["set-cookie"]
?.map(cookie => {
const parts = cookie.split(';').map(part => part.trim().split(/=(.*)/s));
const [key, value] = parts[0];
const maxAgePart = parts.find(([key]) => key.startsWith("Max-Age"));
if (maxAgePart) {
const maxAge = parseInt(maxAgePart[1]);
if (!isNaN(maxAge)) {
return {
key,
value,
expires: cookieTime + maxAge * 1000
}
}
}
const expires = parts.find(([key]) => key.startsWith("expires"));
if (!expires) {
return {
key,
value,
expires: cookieTime + 24 * 60 * 60 * 1000
}
}
return {
key,
value,
expires: Date.parse(expires[1])
}
}) ?? [];
console.log(newCookies);
loginCookies.push(...newCookies);
} catch (e) {
console.log(e)
return {
passport: null,
loginCookies
}
}
const joinedCookies = loginCookies
.map(cookie => `${cookie.key}=${cookie.value}`).join(";");
console.log(joinedCookies);
try {
const passport = await (await fetch("https://passport.mahjongsoul.com/user/login", {
method: "POST",
headers: {
...sharedSpoofHeaders,
'Accept': 'application/json',
'Content-Type': 'application/json',
cookies: joinedCookies
},
body: JSON.stringify({
"uid": userId,
"token": accessToken,
"deviceId": `web|${userId}`
})
})).json();
return {
passport,
loginCookies,
}
} catch {
return {
loginCookies,
passport: null,
}
}
}