Example #1
0
export function get(url: string, headers: any): Promise<any> {
  headers["User-Agent"] = userAgent;
  return got
    .get(url, { headers })
    .then((res: any) => parseJSON(res.body))
    .catch(wrapError);
}
Example #2
0
test('promise mode', async (t) => {
    t.is((await got.get(s.url)).body, 'ok');

    try {
        await got.get(`${s.url}/404`);
        t.fail('Exception was not thrown');
    } catch (err) {
        t.is(err.response.body, 'not found');
    }

    try {
        await got.get('.com', {retries: 0});
        t.fail('Exception was not thrown');
    } catch (err) {
        t.truthy(err);
    }
});
Example #3
0
async function getUpdateYaml(): Promise<string> {
  const targetUrl = getUpdateCheckUrl();
  const { body } = await get(targetUrl, getGotOptions());

  if (!body) {
    throw new Error('Got unexpected response back from update check');
  }

  return body.toString('utf8');
}
Example #4
0
export async function downloadUpdate(
  fileName: string,
  logger: LoggerType
): Promise<string> {
  const baseUrl = getUpdatesBase();
  const updateFileUrl = `${baseUrl}/${fileName}`;

  const signatureFileName = getSignatureFileName(fileName);
  const signatureUrl = `${baseUrl}/${signatureFileName}`;

  let tempDir;
  try {
    tempDir = await createTempDir();
    const targetUpdatePath = join(tempDir, fileName);
    const targetSignaturePath = join(tempDir, getSignatureFileName(fileName));

    logger.info(`downloadUpdate: Downloading ${signatureUrl}`);
    const { body } = await get(signatureUrl, getGotOptions());
    await writeFile(targetSignaturePath, body);

    logger.info(`downloadUpdate: Downloading ${updateFileUrl}`);
    const downloadStream = stream(updateFileUrl, getGotOptions());
    const writeStream = createWriteStream(targetUpdatePath);

    await new Promise((resolve, reject) => {
      downloadStream.on('error', error => {
        reject(error);
      });
      downloadStream.on('end', () => {
        resolve();
      });

      writeStream.on('error', error => {
        reject(error);
      });

      downloadStream.pipe(writeStream);
    });

    return targetUpdatePath;
  } catch (error) {
    if (tempDir) {
      await deleteTempDir(tempDir);
    }
    throw error;
  }
}
Example #5
0
test('GET can have body', async (t) => {
    const {body, headers} = await got.get(s.url, {body: 'hi'});
    t.is(body, 'hi');
    t.is(headers.method, 'GET');
});