-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove is-type-of and debug deps (#17)
force path-to-regexp to >=1.9.0 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Added `resources` method to router for enhanced RESTful routing capabilities - Introduced new utility functions for function type checking - **Dependency Updates** - Updated `path-to-regexp` dependency - Removed `debug` and `is-type-of` dependencies - **Performance Improvements** - Replaced external debugging library with Node.js native `util.debuglog` - Streamlined type checking with native JavaScript methods - **Maintenance** - Refactored type checking across multiple files - Updated GitHub Actions workflows for 2.x branch <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
9 changed files
with
45 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: CI | ||
name: CI for 2.x | ||
|
||
on: | ||
push: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Release | ||
name: Release for 2.x | ||
|
||
on: | ||
push: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
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'; | ||
}, | ||
isPromise(obj) { | ||
return obj | ||
&& typeof obj.then === 'function'; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); | ||
}); |