Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update to new egg-bin #63

Merged
merged 11 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hackernews-async-ts/.autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module.exports = {
],
dep: [
'egg',
'egg-scripts',
'egg-view-nunjucks',
'moment',
'source-map-support',
],
devdep: [
'autod',
Expand Down
84 changes: 16 additions & 68 deletions hackernews-async-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -1,71 +1,19 @@
logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
logs/
.DS_Store
.vscode
*.swp
*.lock
*.js

app/**/*.js
test/**/*.js
config/**/*.js
*.map
run
logs

# Created by https://www.gitignore.io/api/node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


# End of https://www.gitignore.io/api/node
app/**/*.map
test/**/*.map
config/**/*.map
12 changes: 1 addition & 11 deletions hackernews-async-ts/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
{
"files.exclude": {
"USE_GITIGNORE": true,
"**/*.js": {
"when": "$(basename).ts"
},
"**/*.map": true,
"run": true,
"logs": true,
"out": true,
"node_modules": true
}
"typescript.tsdk": "node_modules/typescript/lib"
}
6 changes: 0 additions & 6 deletions hackernews-async-ts/app/controller/index.d.ts

This file was deleted.

11 changes: 6 additions & 5 deletions hackernews-async-ts/app/router.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Application } from 'egg';

export default (app: Application) => {
const controller = app.controller;
app.redirect('/', '/news');
app.router.get('/news', controller.news.list);
app.router.get('/news/item/:id', controller.news.detail);
app.router.get('/news/user/:id', controller.news.user);
const { controller, router } = app;

// router.redirect('/', '/news');
router.get('/news', controller.news.list);
router.get('/news/item/:id', controller.news.detail);
router.get('/news/user/:id', controller.news.user);
};
13 changes: 3 additions & 10 deletions hackernews-async-ts/app/service/HackerNews.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Service } from 'egg';
import { Service } from 'egg';

export interface NewsItem {
id: number;
Expand All @@ -16,13 +16,6 @@ export interface NewsItem {
* HackerNews Api Service
*/
export class HackerNews extends Service {
constructor(ctx: Context) {
super(ctx);
}
getConfig() {
return this.app.config.news;
}

/**
* request hacker-news api
* @param api - Api name
Expand All @@ -34,7 +27,7 @@ export class HackerNews extends Service {
timeout: ['30s', '30s'],
}, opts);

const result = await this.ctx.curl(`${this.getConfig().serverUrl}/${api}`, options);
const result = await this.ctx.curl(`${this.config.news.serverUrl}/${api}`, options);
return result.data;
}

Expand All @@ -45,7 +38,7 @@ export class HackerNews extends Service {
*/
public async getTopStories(page?: number, pageSize?: number): Promise<number[]> {
page = page || 1;
pageSize = pageSize || this.getConfig().pageSize;
pageSize = pageSize || this.config.news.pageSize;

try {
const result = await this.request('topstories.json', {
Expand Down
6 changes: 0 additions & 6 deletions hackernews-async-ts/app/service/index.d.ts

This file was deleted.

32 changes: 32 additions & 0 deletions hackernews-async-ts/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
import { EggAppConfig } from 'egg';
import * as fs from 'fs';
import * as path from 'path';

export default (appInfo: EggAppConfig) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EggAppInfo 的那个改改,这里不对

return {
keys: appInfo.name + '123456',
siteFile: {
'/favicon.ico': fs.readFileSync(
path.join(appInfo.baseDir, 'app/public/favicon.png'),
),
},
view: {
defaultViewEngine: 'nunjucks',
mapping: {
'.tpl': 'nunjucks',
},
},
news: {
/**
* page size
*/
pageSize: 30,

/**
* hacker news server url
*/
serverUrl: 'https://hacker-news.firebaseio.com/v0',
},
};
};
3 changes: 3 additions & 0 deletions hackernews-async-ts/config/config.local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const info = {
msg: 'hello',
};
5 changes: 5 additions & 0 deletions hackernews-async-ts/config/config.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
info: {
msg: 'hello',
},
};
26 changes: 0 additions & 26 deletions hackernews-async-ts/config/config.ts

This file was deleted.

14 changes: 0 additions & 14 deletions hackernews-async-ts/config/defaultConfig.ts

This file was deleted.

23 changes: 23 additions & 0 deletions hackernews-async-ts/config/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { EggAppConfig } from 'egg';
import defaultConfig from './config.default';
import * as localConfig from './config.local';
import prodConfig from './config.prod';

type DefaultConfig = ReturnType<typeof defaultConfig>;
type LocalConfig = typeof localConfig;
type ProdConfig = typeof prodConfig;
type NewAppConfig = EggAppConfig & DefaultConfig & LocalConfig & ProdConfig;

declare module 'egg' {
interface Application {
config: NewAppConfig;
}

interface Controller {
config: NewAppConfig;
}

interface Service {
config: NewAppConfig;
}
}
37 changes: 21 additions & 16 deletions hackernews-async-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"version": "1.0.0",
"description": "hackernews showcase using async/await for egg",
"private": true,
"egg": {
"typescript": true
},
"dependencies": {
"egg": "^2.0.0",
"egg-view-nunjucks": "^2.1.4",
"moment": "^2.19.4",
"source-map-support": "^0.5.0",
"tslib": "^1.8.1",
"typescript": "^2.6.2"
"egg": "^2.5.0",
"egg-scripts": "^2.5.1",
"egg-view-nunjucks": "^2.2.0",
"moment": "^2.21.0",
"source-map-support": "^0.5.4"
},
"devDependencies": {
"@types/cheerio": "^0.22.1",
Expand All @@ -19,25 +21,28 @@
"autod": "^3.0.1",
"autod-egg": "^1.1.0",
"cheerio": "^1.0.0-rc.2",
"egg-bin": "^4.3.7",
"egg-mock": "^3.14.0",
"egg-bin": "^4.6.0",
"egg-mock": "^3.16.0",
"egg-ts-helper": "^1.1.3",
"rimraf": "^2.6.1",
"tslint": "^4.0.0"
"tslib": "^1.8.1",
"tslint": "^4.0.0",
"typescript": "^2.8.1"
},
"engines": {
"node": ">=8.9.0"
},
"scripts": {
"clean": "rimraf app/**/*.{js,map} test/**/*.{js,map} config/**/*.{js,map}",
"tsc": "tsc -p tsconfig.json",
"tsc:w": "tsc -p tsconfig.json -w",
"debug": "egg-bin debug",
"dev": "egg-bin dev",
"test": "npm run tsc && npm run test-local",
"test-local": "egg-bin test",
"start": "egg-scripts start",
"dev": "egg-bin dev -r 'egg-ts-helper/register'",
"tsc": "ets && tsc -p tsconfig.json",
"clean": "ets clean",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test -r 'egg-ts-helper/register'",
"cov": "egg-bin cov",
"lint": "tslint .",
"ci": "npm run lint && npm run cov",
"debug": "egg-bin debug",
"autod": "autod"
}
}
12 changes: 1 addition & 11 deletions hackernews-async-ts/test/app/controller/news.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
'use strict';

import * as assert from 'assert';
import * as cheerio from 'cheerio';
import mm from 'egg-mock';
import { app, assert } from 'egg-mock/bootstrap';

describe('test/app/controller/news.test.ts', () => {
const app = mm.app();
before(async () => {
await app.ready();
});

after(() => app.close());

afterEach(mm.restore);

it('should GET /news', async () => {
const result = await app.httpRequest().get('/news').expect(200);
const $ = cheerio.load(result.text);
Expand Down
8 changes: 1 addition & 7 deletions hackernews-async-ts/test/app/service/HackerNews.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
'use strict';

import * as assert from 'assert';
import { Context } from 'egg';
import mm from 'egg-mock';
import { app, assert } from 'egg-mock/bootstrap';

describe('test/app/service/HackerNews.test.js', () => {
const app = mm.app();
let ctx: Context;

before(async () => {
await app.ready();
ctx = app.mockContext();
});

after(() => app.close());
afterEach(mm.restore);

it('getTopStories', async () => {
const list = await ctx.service.hackerNews.getTopStories();
assert(list.length === 30);
Expand Down
Loading