-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support cjs and esm both by tshy (#7)
BREAKING CHANGE: drop Node.js < 18.19.0 support part of eggjs/egg#3644 eggjs/egg#5257
- Loading branch information
Showing
14 changed files
with
217 additions
and
137 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,3 +1,6 @@ | ||
{ | ||
"extends": "eslint-config-egg" | ||
"extends": [ | ||
"eslint-config-egg/typescript", | ||
"eslint-config-egg/lib/rules/enforce-node-prefix" | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
Job: | ||
name: Node.js | ||
uses: node-modules/github-actions/.github/workflows/node-test.yml@master | ||
with: | ||
os: 'ubuntu-latest' | ||
version: '18.19.0, 18, 20, 22' | ||
secrets: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: Release | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
|
||
jobs: | ||
release: | ||
name: Node.js | ||
uses: eggjs/github-actions/.github/workflows/node-release.yml@master | ||
secrets: | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
GIT_TOKEN: ${{ secrets.GIT_TOKEN }} |
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 |
---|---|---|
|
@@ -14,3 +14,7 @@ node_modules | |
npm-debug.log | ||
coverage/ | ||
.nyc_output/ | ||
.tshy* | ||
.eslintcache | ||
dist | ||
coverage |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,7 +1,8 @@ | ||
This software is licensed under the MIT License. | ||
|
||
Copyright (c) node-modules and other contributors | ||
Copyright (c) 2015 fengmk2 <[email protected]> and other contributors | ||
Copyright (c) 2024-present eggjs and other contributors | ||
Copyright (c) 2015-2024 node-modules and other contributors | ||
Copyright (c) 2015 fengmk2 <[email protected]> and other contributors | ||
Copyright (c) 2014 Jonathan Ong <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
|
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 was deleted.
Oops, something went wrong.
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,38 +1,35 @@ | ||
{ | ||
"name": "koa-override", | ||
"version": "3.0.0", | ||
"description": "method override middleware for koa", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"test": "egg-bin test", | ||
"cov": "egg-bin cov", | ||
"lint": "eslint test *.js", | ||
"ci": "npm run lint && npm run cov", | ||
"autod": "autod -w --prefix '^'" | ||
}, | ||
"dependencies": { | ||
"methods": "^1.1.2" | ||
"engines": { | ||
"node": ">= 18.19.0" | ||
}, | ||
"description": "method override middleware for koa", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"autod": "*", | ||
"egg-bin": "^4.3.5", | ||
"egg-ci": "^1.7.0", | ||
"eslint": "^4.0.0", | ||
"eslint-config-egg": "^4.2.1", | ||
"koa": "2", | ||
"@arethetypeswrong/cli": "^0.15.3", | ||
"@eggjs/koa": "^2.18.3", | ||
"@eggjs/tsconfig": "1", | ||
"@types/koa-bodyparser": "^4.3.12", | ||
"@types/mocha": "10", | ||
"@types/node": "20", | ||
"@types/supertest": "^6.0.2", | ||
"egg-bin": "6", | ||
"eslint": "8", | ||
"eslint-config-egg": "13", | ||
"koa-bodyparser": "4", | ||
"supertest": "3" | ||
"supertest": "7", | ||
"tshy": "1", | ||
"tshy-after": "1", | ||
"typescript": "5" | ||
}, | ||
"homepage": "https://github.com/node-modules/koa-override", | ||
"homepage": "https://github.com/eggjs/koa-override", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/node-modules/koa-override.git" | ||
"url": "git://github.com/eggjs/koa-override.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/node-modules/koa-override/issues" | ||
"url": "https://github.com/eggjs/koa-override/issues" | ||
}, | ||
"keywords": [ | ||
"koa-override", | ||
|
@@ -41,12 +38,40 @@ | |
"override", | ||
"rewrite" | ||
], | ||
"engines": { | ||
"node": ">= 8.0.0" | ||
"author": "fengmk2 <[email protected]> (https://github.com/fengmk2)", | ||
"license": "MIT", | ||
"scripts": { | ||
"lint": "eslint --cache src test --ext .ts", | ||
"test": "npm run lint -- --fix && egg-bin test", | ||
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly && attw --pack", | ||
"prepublishOnly": "tshy && tshy-after" | ||
}, | ||
"ci": { | ||
"version": "8, 9" | ||
"type": "module", | ||
"tshy": { | ||
"exports": { | ||
".": "./src/index.ts", | ||
"./package.json": "./package.json" | ||
} | ||
}, | ||
"author": "fengmk2 <[email protected]> (http://fengmk2.com)", | ||
"license": "MIT" | ||
"exports": { | ||
".": { | ||
"import": { | ||
"source": "./src/index.ts", | ||
"types": "./dist/esm/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"require": { | ||
"source": "./src/index.ts", | ||
"types": "./dist/commonjs/index.d.ts", | ||
"default": "./dist/commonjs/index.js" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"types": "./dist/commonjs/index.d.ts", | ||
"main": "./dist/commonjs/index.js" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { METHODS } from 'node:http'; | ||
|
||
const methods = METHODS.map(method => { | ||
return method.toUpperCase(); | ||
}); | ||
|
||
export interface OverrideMiddlewareOptions { | ||
/** methods allow to override, default is `[ 'POST' ]` */ | ||
allowedMethods?: string[]; | ||
} | ||
|
||
export type Next = () => Promise<void>; | ||
|
||
export default (options: OverrideMiddlewareOptions = {}) => { | ||
options.allowedMethods = options.allowedMethods ?? [ 'POST' ]; | ||
|
||
return function overrideMethod(ctx: any, next: Next) { | ||
const originalMethod = ctx.request.method; | ||
if (!options.allowedMethods?.includes(originalMethod)) { | ||
return next(); | ||
} | ||
|
||
let method: string | undefined; | ||
// body support | ||
const body = ctx.request.body; | ||
if (body?._method && typeof body._method === 'string') { | ||
method = body._method.toUpperCase(); | ||
} else { | ||
// header support | ||
const header = ctx.get('x-http-method-override'); | ||
if (header) { | ||
method = header.toUpperCase(); | ||
} | ||
} | ||
|
||
if (method) { | ||
// only allow supported methods | ||
// if you want to support other methods, | ||
// just create your own utility! | ||
if (!methods.includes(method)) { | ||
ctx.throw(400, `invalid override method: "${method}"`); | ||
} | ||
ctx.request.method = method; | ||
} | ||
|
||
return next(); | ||
}; | ||
}; |
Oops, something went wrong.