Skip to content

Commit

Permalink
feat: add proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
skyunBoss committed Mar 28, 2022
1 parent e49f9cf commit de8c6d4
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 277 deletions.
6 changes: 3 additions & 3 deletions example/config/config.development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export default app => {
},

// mock配置
mock: {
prefix: '',
},
// mock: {
// prefix: '', // api前缀
// },

// ioredis 配置,使用之前,需要开启redis-server
// redis: {
Expand Down
4 changes: 3 additions & 1 deletion example/controller/goods/getinfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export default {
method: 'GET',
handler: async (ctx) => {
ctx.body = "this is koa book."
const result = await ctx.proxy('https://registry.npmjs.com/diudiu/latest');

ctx.body = result;
}
}
2 changes: 1 addition & 1 deletion example/mock/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default {
}
],
'GET /goods/getinfo': (ctx) => {
console.log(ctx);
// console.log(ctx);
return {
id: 1,
goodname: 'book',
Expand Down
2 changes: 1 addition & 1 deletion example/mock/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
id: 1,
},
],
'GET /user/info': (ctx) => {
'GET /user/getinfo': (ctx) => {
return {
id: 1,
username: 'sky',
Expand Down
266 changes: 1 addition & 265 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/core/hooks/mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import glob from "glob";
import path from "path";
import glob from 'glob';
import path from 'path';

export default async (app) => {
const mockConfig = app.config.mock || {};
Expand All @@ -23,7 +23,7 @@ export default async (app) => {
});
});

app.use(async (ctx, next) => {
app.use((ctx, next) => {
const { method, path } = ctx;
// 拼接mock的key
const key = `${method} ${mockConfig.prefix}${path}`;
Expand Down
23 changes: 23 additions & 0 deletions lib/core/hooks/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';

export default async (app) => {
// 数据代理请求方法
const proxy = async (config: AxiosRequestConfig) => {
const { data, headers, status } = (await axios(config)) as AxiosResponse;

// 返回 RESTful 规范
return {
code: status,
status: status === 200 ? 'success' : 'fail',
headers,
data,
};
};

app.use((ctx, next) => {
// 将proxy挂载在ctx上
ctx.proxy = proxy;

return next();
});
}
2 changes: 1 addition & 1 deletion lib/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Koa from 'koa';
import path from 'path';
import { getHooks, deepMerge } from './utils'
import { Hook, App, DiudiuProcess } from './types';
const hooks = ['formData', 'log', 'redis', 'mysql', 'elasticsearch', 'static', 'view', 'bodyparser', 'login', 'custom-middlewares', 'cors', 'router', 'lift', 'mock'];
const hooks = ['formData', 'log', 'proxy', 'mock', 'redis', 'mysql', 'elasticsearch', 'static', 'view', 'bodyparser', 'login', 'custom-middlewares', 'cors', 'router', 'lift'];

type Params = {
appPath: string;
Expand Down
13 changes: 13 additions & 0 deletions lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit de8c6d4

Please sign in to comment.