Skip to content

Instantly share code, notes, and snippets.

@koakh
Forked from jsobell/app.html
Last active December 21, 2017 11:26
Show Gist options
  • Save koakh/dadbe5ddfc41be4585eb93c76da014e8 to your computer and use it in GitHub Desktop.
Save koakh/dadbe5ddfc41be4585eb93c76da014e8 to your computer and use it in GitHub Desktop.
Aurelia Fetch Client Gist
<template>
<h1>${message}</h1>
<pre>data:[${data}]</pre>
<ul>
<!--
<li repeat.for="user of users">
${user.login}
</li>
-->
</ul>
</template>
import { inject } from 'aurelia-framework';
import { HttpClient } from 'aurelia-fetch-client';
// Update #1
@inject(HttpClient)
export class App {
message = 'Hello World!';
users = null;
data = null;
httpClient;
constructor(httpClient) {
this.httpClient = httpClient;
}
activate() {
console.log('Config');
this.httpClient.configure(config => {
config
.withBaseUrl('https://koakh.com:8084/api/')
.withDefaults({
credentials: 'same-origin',
headers: {
'Accept': 'application/json',
'X-Requested-With': 'Fetch'
},
mode: 'no-cors'
})
.withInterceptor({
request(request) {
console.log(`Requesting ${request.method} ${request.url}`);
let authHeader = 'Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJKVUNoTEVnZkYzNjBXRWg0dzJ4OVFneEhqamhYUzN6Y0gyLW5helQ1clNnIn0.eyJqdGkiOiI4MTJhM2RhOC04YWNiLTQ3NWQtYWY4MC1lNzllMWI2ZWMwMjIiLCJleHAiOjE1MTM4NTQ4MDIsIm5iZiI6MCwiaWF0IjoxNTEzODU0NTAyLCJpc3MiOiJodHRwczovL2F1dGgua29ha2guY29tL2F1dGgvcmVhbG1zL0RlbW8tUmVhbG0iLCJhdWQiOiJhdXJlbGlhLW9wZW5pZC1jb25uZWN0LXRvLWtleWNsb2FrIiwic3ViIjoiZTllM2EzMDktMzkwYS00MzJjLWIxNjQtOGM3NjJiNzE3MzRjIiwidHlwIjoiQmVhcmVyIiwiYXpwIjoiYXVyZWxpYS1vcGVuaWQtY29ubmVjdC10by1rZXljbG9hayIsImF1dGhfdGltZSI6MTU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(response) {
console.log(`Received ${response.status} ${response.url}`);
return response;
}
});
}).fetch('user')
.then(response => response.json())
.then(data => {
this.data = data;
})
.catch(error => {
this.data = error;
});
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script>
require.config({
paths: {
"aurelia-fetch-client": "https://jdanyow.github.io/rjs-bundle/node_modules/aurelia-fetch-client/dist/amd/aurelia-fetch-client"
}
})
</script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment