Skip to content

Commit

Permalink
Add HttpClient (#31)
Browse files Browse the repository at this point in the history
* feat: remove avet-init

* feat: add httpclient
  • Loading branch information
okoala authored Dec 28, 2017
1 parent 8b5d738 commit 19646dc
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/simple/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "avet-example-egg",
"name": "avet-example-simple",
"version": "1.0.0",
"description": "this is a avet and egg example",
"description": "this is a avet example",
"private": true,
"scripts": {
"start": "NODE_ENV=production EGG_SERVER_ENV=prod node index.js",
Expand Down
6 changes: 3 additions & 3 deletions examples/simple/web/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import React from 'react';
import Header from '../component/header';

class IndexPage extends React.Component {
static async getInitialProps() {
// const res = await httpclient.get('/api/getHello');
return { hello: 'dddd' };
static async getInitialProps({ httpclient }) {
const res = await httpclient.get('/api/getHello');
return { hello: res.data };
}

render() {
Expand Down
2 changes: 1 addition & 1 deletion packages/avet-client/src/head-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class HeadManager {

this.updateTitle(tags.title ? tags.title[0] : null);

const types = ['meta', 'base', 'link', 'style', 'script'];
const types = [ 'meta', 'base', 'link', 'style', 'script' ];
types.forEach(type => {
this.updateElements(type, tags[type] || []);
});
Expand Down
9 changes: 6 additions & 3 deletions packages/avet-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import App from 'avet-shared/lib/app';
import { getURL } from 'avet-shared/lib/utils';
import PageLoader from 'avet-shared/lib/page-loader';
import { loadGetInitialProps } from 'avet-utils/lib/component';
import { getHttpClient } from 'avet-shared/lib/httpclient';

// Polyfill Promise globally
// This is needed because Webpack2's dynamic loading(common chunks) code
Expand Down Expand Up @@ -51,9 +52,10 @@ let stripAnsi = s => s;

export const emitter = new EventEmitter();

export default async (
{ ErrorDebugComponent: passedDebugComponent, stripAnsi: passedStripAnsi } = {}
) => {
export default async ({
ErrorDebugComponent: passedDebugComponent,
stripAnsi: passedStripAnsi,
} = {}) => {
// Wait for all the dynamic chunks to get loaded
for (const chunkName of chunks) {
await pageLoader.waitForChunk(chunkName);
Expand Down Expand Up @@ -151,6 +153,7 @@ async function doRender({
err,
pathname,
query,
httpclient: getHttpClient(),
asPath,
});
}
Expand Down
1 change: 0 additions & 1 deletion packages/avet-init
Submodule avet-init deleted from 68cae8
2 changes: 2 additions & 0 deletions packages/avet-server/lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const App = require('avet-shared/lib/app').default;
const ErrorDebug = require('avet-shared/lib/error-debug').default;
const { flushChunks } = require('avet-shared/lib/dynamic');
const xssFilters = require('xss-filters');
const { getHttpClient } = require('avet-shared/lib/httpclient');

const {
resolvePath,
Expand Down Expand Up @@ -137,6 +138,7 @@ async function doRender(
const _ctx = {
ctx,
asPath,
httpclient: getHttpClient(ctx),
pathname: ctx.path,
query: ctx.query,
err,
Expand Down
1 change: 1 addition & 0 deletions packages/avet-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"ansi-html": "^0.0.7",
"avet-utils": "1.0.0-16",
"axios": "^0.17.1",
"htmlescape": "^1.1.1",
"http-status": "^1.0.1",
"path-to-regexp": "^2.1.0",
Expand Down
29 changes: 29 additions & 0 deletions packages/avet-shared/src/httpclient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import axios from 'axios';

function getBaseURL(ctx) {
if (ctx) {
let port = '7001';
if (process.env.NODE_ENV === 'production') {
port = process.env.PORT;
}
return `http://127.0.0.1:${port}`;
} else {
return document.location.origin;
}
}

export function getHttpClient(ctx, options = {}) {
if (ctx) {
const { headers } = ctx.request;
Object.keys(headers).forEach(key => {
axios.defaults.headers.common[key] = headers[key];
});
}

const baseURL = getBaseURL(ctx);
axios.defaults.baseURL = baseURL;

Object.assign(axios, options);

return axios;
}
8 changes: 4 additions & 4 deletions packages/avet-shared/src/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ export default class Router {

onlyAHashChange(as) {
if (!this.asPath) return false;
const [oldUrlNoHash] = this.asPath.split('#');
const [newUrlNoHash, newHash] = as.split('#');
const [ oldUrlNoHash ] = this.asPath.split('#');
const [ newUrlNoHash, newHash ] = as.split('#');

// If the urls are change, there's more than a hash change
if (oldUrlNoHash !== newUrlNoHash) {
Expand All @@ -270,7 +270,7 @@ export default class Router {
}

scrollToHash(as) {
const [, hash] = as.split('#');
const [ , hash ] = as.split('#');
const el = document.getElementById(hash);
if (el) {
el.scrollIntoView();
Expand Down Expand Up @@ -302,7 +302,7 @@ export default class Router {

async fetchComponent(route, as) {
let cancelled = false;
const cancel = (this.componentLoadCancel = function () {
const cancel = (this.componentLoadCancel = function() {
cancelled = true;
});

Expand Down

0 comments on commit 19646dc

Please sign in to comment.