1、TypeScript簡介html
1.定義node
(1)Typescript = JavaScript + type + ( some other stuff )git
(2)Typescript須要先被編譯成Javascript,然後才能使用github
2.demotypescript
安裝typescript:npm
- npm install typescript –g
- tsc index.ts
index.ts:json
var fn = () => ‘response’
command line:api
tsc init
修改tsconfig.json的outDir和rootDir,並增添out目錄與src目錄
修改package.json的scripts爲"start": "tsc && node out/index.js"
運行npm start網站
結果以下:this
此時,src裏的index.ts已經被編譯至index.js中
index.js:
var fn = function () { return ‘response’; }
2、項目實現
1.需求
在node環境下,使用ts語言,借用gituhub的api實現對用戶的關鍵信息查詢功能
2.目錄
3.安裝庫
(1)request庫
npm i @types/request --save-dev npm i request –save
(2)lodash庫(用於排序)
npm i @types/lodash --save-dev npm install lodash --save-dev
4.內容
index.ts
import { GithubApiService } from './GithubApiService' import { User } from './User' import { Repo } from './Repo'; import * as ld from 'lodash'; // node.js內置功能,用於獲取第三個參數內容 console.log(process.argv[2]) let svc: GithubApiService = new GithubApiService(); if (process.argv.length < 3) { console.log("請輸入用戶名"); } else { svc.getUserInfo("bbcfive", (user: User) => { svc.getRepos(user.login, (repos: Repo[]) => { // 按照fork的數量由小到大排列 ps:返回值 * -1 表明由大到小排列 let sortedRepos = ld.sortBy(repos, [(repo: Repo) => repo.forks_count]); user.repos = sortedRepos; console.log(user); }) }); }
GithubApiService.ts
import * as request from 'request' import { User } from './User'; import { Repo } from './Repo'; const options = { headers: { 'User-Agent': 'request' }, json: true } export class GithubApiService { getUserInfo(userName:string, callBack: (user: User) => any) { request.get("http://api.github.com/users/" + userName, options, (error: any, response: any, body: any) => { // 寫法一 // let user = new User(JSON.parse(body)); // typeof body == object // 寫法二 let user: User = new User(body); // typeof body == string callBack(user); }) } getRepos(userName:string, callBack: (repos: Repo[]) => any) { request.get("http://api.github.com/users/" + userName + "/repos", options, (error: any, response: any, body: any) => { let repos: Repo[] = body.map((repo: any) => new Repo(repo)); callBack(repos); }) } }
Repo.ts
export class Repo { name: string; description: string; url: string; size: number; forks_count: number; constructor (repo: any) { this.name = repo.name; this.description = repo.description; this.url = repo.html_url; this.size = repo.size; this.forks_count = repo.forks_count; } }
User.js
import { Repo } from './Repo'; export class User { login: string; fullName: string; repoCount: number; followerCount: number; repos: Repo[] = []; constructor (userResponse: any) { this.login = userResponse.login; this.fullName = userResponse.name; this.repoCount = userResponse.public_repos; this.followerCount = userResponse.followers; } }
5.運行
由於在index.ts裏已經使用nodeJs自帶的api,process.argv對npm輸入命令作了判斷,因此只須要在命令行輸入
npm start bbcfive // "bbcfive"是個人github用戶名
終端打印結果:
能夠清晰的看到此用戶的github關鍵信息。
3、其餘延展功能
給定必定量的github用戶名列表,能夠經過此方法循環獲取每一個用戶的關鍵信息,並做篩選,來得到目標範圍用戶。
適用於如下場景:
1.hr招人
2.找一些很讚的項目
3.社交(畢竟是全球最大的同性交友網站~~)
—— 完 ——