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: remove is-type-of and debug deps #17

Merged
merged 1 commit into from
Jan 11, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: CI for 2.x

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release
name: Release for 2.x

on:
push:
Expand Down
10 changes: 5 additions & 5 deletions lib/egg_router.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

const is = require('is-type-of');
const Router = require('./router');
const utility = require('utility');
const inflection = require('inflection');
const assert = require('assert');
const utils = require('./utils');
const Router = require('./router');

const METHODS = [ 'head', 'options', 'get', 'put', 'patch', 'post', 'delete' ];

Expand Down Expand Up @@ -138,6 +137,7 @@ class EggRouter extends Router {
* @return {Router} return route object.
* @since 1.0.0
*/

resources(...args) {
const splited = spliteAndResolveRouterParams({ args, app: this.app });
const middlewares = splited.middlewares;
Expand Down Expand Up @@ -198,7 +198,7 @@ class EggRouter extends Router {
const args = params;
let url = route.path;

assert(!is.regExp(url), `Can't get the url for regExp ${url} for by name '${name}'`);
assert(!(url instanceof RegExp), `Can't get the url for regExp ${url} for by name '${name}'`);

const queries = [];
if (typeof args === 'object' && args !== null) {
Expand Down Expand Up @@ -263,7 +263,7 @@ class EggRouter extends Router {
function spliteAndResolveRouterParams({ args, app }) {
let prefix;
let middlewares;
if (args.length >= 3 && (is.string(args[1]) || is.regExp(args[1]))) {
if (args.length >= 3 && (typeof args[1] === 'string' || args[1] instanceof RegExp)) {
// app.get(name, url, [...middleware], controller)
prefix = args.slice(0, 2);
middlewares = args.slice(2);
Expand All @@ -285,7 +285,7 @@ function spliteAndResolveRouterParams({ args, app }) {
* @return {Function} controller function
*/
function resolveController(controller, app) {
if (is.string(controller)) {
if (typeof controller === 'string') {
const actions = controller.split('.');
let obj = app.controller;
actions.forEach(key => {
Expand Down
2 changes: 1 addition & 1 deletion lib/layer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const debug = require('debug')('egg-router:layer');
const debug = require('util').debuglog('egg-router:layer');
const pathToRegExp = require('path-to-regexp');
const uri = require('urijs');
const utility = require('utility');
Expand Down
2 changes: 1 addition & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* RESTful resource routing middleware for eggjs.
*/

const debug = require('debug')('egg-router');
const debug = require('util').debuglog('egg-router');
const compose = require('koa-compose');
const HttpError = require('http-errors');
const methods = require('methods');
Expand Down Expand Up @@ -58,7 +58,7 @@
this.stack = [];
}

/**

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 10)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 10)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 12)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 12)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 8)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 8)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

@param "path" does not match an existing function parameter

Check warning on line 61 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

@param "path" does not match an existing function parameter
* Use given middleware.
*
* Middleware run in the order they are defined by `.use()`. They are invoked
Expand Down Expand Up @@ -293,7 +293,7 @@
};
}

/**

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 10)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 10)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 12)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 12)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 8)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 8)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

@param "middleware" does not match an existing function parameter

Check warning on line 296 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

@param "middleware" does not match an existing function parameter
* Register route with all methods.
*
* @param {String} name Optional.
Expand Down Expand Up @@ -428,7 +428,7 @@
return false;
}

/**

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 10)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 10)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 12)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 12)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 8)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 8)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

@param "params" does not match an existing function parameter

Check warning on line 431 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

@param "params" does not match an existing function parameter
* Generate URL for route. Takes a route name and map of named `params`.
*
* @example
Expand Down Expand Up @@ -700,7 +700,7 @@
// Alias for `router.delete()` because delete is a reserved word
Router.prototype.del = Router.prototype.delete;

/**

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 10)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 10)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 12)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 12)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 8)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 8)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

@param "params" does not match an existing function parameter

Check warning on line 703 in lib/router.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

@param "params" does not match an existing function parameter
* Generate URL from url pattern and given `params`.
*
* @example
Expand Down
24 changes: 20 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
'use strict';

const convert = require('koa-convert');
const is = require('is-type-of');
const co = require('co');

function isGeneratorFunction(obj) {
return obj
&& obj.constructor
&& obj.constructor.name === 'GeneratorFunction';
}
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

module.exports = {
async callFn(fn, args, ctx) {
args = args || [];
if (!is.function(fn)) return;
if (is.generatorFunction(fn)) fn = co.wrap(fn);
if (typeof fn !== 'function') return;
if (isGeneratorFunction(fn)) fn = co.wrap(fn);
return ctx ? fn.call(ctx, ...args) : fn(...args);
},

middleware(fn) {
return is.generatorFunction(fn) ? convert(fn) : fn;
return isGeneratorFunction(fn) ? convert(fn) : fn;
},

isGeneratorFunction,
isAsyncFunction(obj) {
return obj
&& obj.constructor
&& obj.constructor.name === 'AsyncFunction';
},
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
isPromise(obj) {
return obj
&& typeof obj.then === 'function';
},
};
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@
],
"dependencies": {
"co": "^4.6.0",
"debug": "^3.1.0",
"http-errors": "^1.3.1",
"inflection": "^1.12.0",
"is-type-of": "^1.2.1",
"koa-compose": "^3.0.0",
"koa-convert": "^1.2.0",
"methods": "^1.0.1",
"path-to-regexp": "^1.1.1",
"path-to-regexp": "^1.9.0",
"urijs": "^1.19.0",
"utility": "^1.15.0"
},
Expand Down
14 changes: 7 additions & 7 deletions test/lib/egg_router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const EggRouter = require('../../').EggRouter;
const assert = require('assert');
const is = require('is-type-of');
const { isGeneratorFunction, isAsyncFunction } = require('../../lib/utils');

describe('test/lib/egg_router.test.js', () => {
it('creates new router with egg app', function() {
Expand Down Expand Up @@ -126,16 +126,16 @@ describe('test/lib/egg_router.test.js', () => {
assert(router.stack[0].path === '/foo');
assert.deepEqual(router.stack[0].methods, [ 'HEAD', 'GET' ]);
assert(router.stack[0].stack.length === 4);
assert(!is.generatorFunction(router.stack[0].stack[0]));
assert(is.asyncFunction(router.stack[0].stack[1]));
assert(!is.generatorFunction(router.stack[0].stack[3]));
assert(!isGeneratorFunction(router.stack[0].stack[0]));
assert(isAsyncFunction(router.stack[0].stack[1]));
assert(!isGeneratorFunction(router.stack[0].stack[3]));
assert(router.stack[1].name === 'hello');
assert(router.stack[1].path === '/hello/world');
assert.deepEqual(router.stack[1].methods, [ 'POST' ]);
assert(router.stack[1].stack.length === 4);
assert(!is.generatorFunction(router.stack[1].stack[0]));
assert(is.asyncFunction(router.stack[1].stack[1]));
assert(!is.generatorFunction(router.stack[1].stack[3]));
assert(!isGeneratorFunction(router.stack[1].stack[0]));
assert(isAsyncFunction(router.stack[1].stack[1]));
assert(!isGeneratorFunction(router.stack[1].stack[3]));
});

it('should app.resource() work', () => {
Expand Down
17 changes: 8 additions & 9 deletions test/lib/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
'use strict';

const utils = require('../../lib/utils');
const is = require('is-type-of');
const assert = require('assert');
const utils = require('../../lib/utils');

describe('test/lib/utils.test.js', () => {
describe('callFn', () => {
it('should not function return same', () => {
const res = utils.callFn('foo');
assert(is.promise(res));
assert(utils.isPromise(res));
return res.then(result => assert(result === undefined));
});

it('should async function return promise', () => {
const res = utils.callFn(async (foo, bar) => {
return foo + bar;
}, [ 1, 2 ]);
assert(is.promise(res));
assert(utils.isPromise(res));
return res.then(result => assert(result === 3));
});

it('should generator function return promise', () => {
const res = utils.callFn(function* (foo, bar) {
return foo + bar;
}, [ 1, 2 ]);
assert(is.promise(res));
assert(utils.isPromise(res));
return res.then(result => assert(result === 3));
});

it('should common function return promise', () => {
const res = utils.callFn((foo, bar) => {
return foo + bar;
}, [ 1, 2 ]);
assert(is.promise(res));
assert(utils.isPromise(res));
return res.then(result => assert(result === 3));
});

it('should work with ctx', () => {
const res = utils.callFn(async function(bar) {
return this.foo + bar;
}, [ 2 ], { foo: 1 });
assert(is.promise(res));
assert(utils.isPromise(res));
return res.then(result => assert(result === 3));
});
});

describe('middleware', () => {
it('should work with async function', () => {
const res = utils.middleware(async () => {});
assert(is.asyncFunction(res));
assert(utils.isAsyncFunction(res));
});

it('should work with generator function', () => {
const res = utils.middleware(function* () {
return;
});
assert(!is.generatorFunction(res));
assert(!utils.isGeneratorFunction(res));
});
});
});
Loading