How to use the ky.get function in ky

To help you get started, we’ve selected a few ky examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github facebook / create-react-app / test / fixtures / builds-with-multiple-runtimes / src / index.js View on Github external
app.router(() => {
  ky.get('https://canihazip.com/s')
    .then(r => r.text())
    .then(console.log, console.error)
    .then(() => console.log('ok'));
  return <div>Test</div>;
});
app.start('#root');
github ralscha / blog2019 / ky / client / src / main.js View on Github external
async function timeout() {
  let response;

  console.log('ky, default, timeout 10s');
  try {
    console.log('request ', Date.now());
    response = await ky.get('http://localhost:8080/timeout');
  } catch (e) {
    console.log('response', Date.now());
    console.log(e);
  }

  console.log('ky, timeout 1s');
  try {
    console.log('request ', Date.now());
    response = await ky.get('http://localhost:8080/timeout', { timeout: 1000 });
  } catch (e) {
    console.log('response', Date.now());
    console.log(e);
  }

  console.log('ky, timeout disabled');
  console.log('request ', Date.now());
github ralscha / blog2019 / ky / client / src / main.js View on Github external
async function retry() {
  try {
    const response = await ky.get('http://localhost:8080/retry-test').text();
  } catch (e) {
    console.log(e);
  }
  try {
    const response = await ky.get('http://localhost:8080/retry-test', { retry: 1 }).text();
  } catch (e) {
    console.log(e);
  }

  let response = await ky.get('http://localhost:8080/retry').text();
  response = await ky.get('http://localhost:8080/retry').text();

  response = await ky.get('http://localhost:8080/retry', { retry: 5 }).text();

  response = await ky.get('http://localhost:8080/retry', {
    retry: {
      limit: 10,
      methods: ['get'],
      afterStatusCodes: [429]
    }
  }).text();

}
github ralscha / blog2019 / ky / client / src / main.js View on Github external
async function customDefaults() {
  console.log('default ky');
  let body = await ky.get('simple-get', { prefixUrl: 'http://localhost:8080' }).text();
  console.log(body);

  console.log('custom ky with defaults');
  const customKy = ky.create({ prefixUrl: 'http://localhost:8080' });
  body = await customKy('simple-get').text();
  console.log(body);

  const customApiKy = customKy.extend({ headers: { 'x-api-key': '1111' } });
  body = await customApiKy('simple-get').text();
  console.log(body);
}
github ralscha / blog2019 / ky / client / src / main.js View on Github external
async function retry() {
  try {
    const response = await ky.get('http://localhost:8080/retry-test').text();
  } catch (e) {
    console.log(e);
  }
  try {
    const response = await ky.get('http://localhost:8080/retry-test', { retry: 1 }).text();
  } catch (e) {
    console.log(e);
  }

  let response = await ky.get('http://localhost:8080/retry').text();
  response = await ky.get('http://localhost:8080/retry').text();

  response = await ky.get('http://localhost:8080/retry', { retry: 5 }).text();

  response = await ky.get('http://localhost:8080/retry', {
    retry: {
github ralscha / blog2019 / ky / client / src / main.js View on Github external
async function retry() {
  try {
    const response = await ky.get('http://localhost:8080/retry-test').text();
  } catch (e) {
    console.log(e);
  }
  try {
    const response = await ky.get('http://localhost:8080/retry-test', { retry: 1 }).text();
  } catch (e) {
    console.log(e);
  }

  let response = await ky.get('http://localhost:8080/retry').text();
  response = await ky.get('http://localhost:8080/retry').text();

  response = await ky.get('http://localhost:8080/retry', { retry: 5 }).text();

  response = await ky.get('http://localhost:8080/retry', {
    retry: {
      limit: 10,
      methods: ['get'],
      afterStatusCodes: [429]
    }
  }).text();

}
github ralscha / blog2019 / ky / client / src / main.js View on Github external
async function abort() {
  const controller = new AbortController();

  setTimeout(() => {
    console.log('abort', Date.now());
    controller.abort();
  }, 2500);

  try {
    console.log('request', Date.now());
    const body = await ky.get('http://localhost:8080/timeout', { signal: controller.signal }).text();
  } catch (error) {
    console.log('exception', Date.now());
    if (error.name === 'AbortError') {
      console.log('Fetch aborted');
    } else {
      console.error('Fetch error:', error);
    }
  }
}
github ralscha / blog2019 / ky / client / src / main.js View on Github external
console.log('without body method');
  response = await ky.get('http://localhost:8080/simple-get');
  body = await response.text();
  console.log('body: ', body);

  console.log('with json()');
  body = await ky.get('http://localhost:8080/simple-get').json();
  console.log('body: ', body);

  console.log('with text()');
  body = await ky.get('http://localhost:8080/simple-get').text();
  console.log('body: ', body);

  console.log('with formData()');
  body = await ky.get('http://localhost:8080/simple-get').formData();
  console.log('body: ', body.get('response'));

  console.log('with arrayBuffer()');
  body = await ky.get('http://localhost:8080/simple-get').arrayBuffer();
  console.log('body: ', body);

  console.log('with blob()');
  body = await ky.get('http://localhost:8080/simple-get').blob();
  console.log('body: ', body);

  console.log('with arrayBuffer() and accept header, overrides header');
  body = await ky.get('http://localhost:8080/simple-get', { headers: { Accept: 'application/octet-stream' } }).arrayBuffer();
  console.log('body: ', body);

  console.log('with arrayBuffer() and accept header');
  response = await ky.get('http://localhost:8080/simple-get', { headers: { Accept: 'application/octet-stream' } });
github ralscha / blog2019 / ky / client / src / main.js View on Github external
console.log('body: ', body);

  console.log('with text()');
  body = await ky.get('http://localhost:8080/simple-get').text();
  console.log('body: ', body);

  console.log('with formData()');
  body = await ky.get('http://localhost:8080/simple-get').formData();
  console.log('body: ', body.get('response'));

  console.log('with arrayBuffer()');
  body = await ky.get('http://localhost:8080/simple-get').arrayBuffer();
  console.log('body: ', body);

  console.log('with blob()');
  body = await ky.get('http://localhost:8080/simple-get').blob();
  console.log('body: ', body);

  console.log('with arrayBuffer() and accept header, overrides header');
  body = await ky.get('http://localhost:8080/simple-get', { headers: { Accept: 'application/octet-stream' } }).arrayBuffer();
  console.log('body: ', body);

  console.log('with arrayBuffer() and accept header');
  response = await ky.get('http://localhost:8080/simple-get', { headers: { Accept: 'application/octet-stream' } });
  body = await response.arrayBuffer();
  console.log('body: ', body);

  console.log('with blob() and accept header');
  response = await ky.get('http://localhost:8080/simple-get', { headers: { Accept: 'application/octet-stream' } });
  body = await response.blob();
  console.log('body: ', body);
}

ky

Tiny and elegant HTTP client based on the browser Fetch API

MIT
Latest version published 12 days ago

Package Health Score

91 / 100
Full package analysis