Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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');
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());
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();
}
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);
}
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: {
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();
}
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);
}
}
}
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' } });
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);
}