Skip to content

Commit

Permalink
feat: support cjs and esm both by tshy (#7)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257
  • Loading branch information
fengmk2 authored Jun 23, 2024
1 parent 1ed7010 commit f021708
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 137 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
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"
]
}
17 changes: 17 additions & 0 deletions .github/workflows/nodejs.yml
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 }}
13 changes: 13 additions & 0 deletions .github/workflows/release.yml
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 }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ node_modules
npm-debug.log
coverage/
.nyc_output/
.tshy*
.eslintcache
dist
coverage
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

File renamed without changes.
5 changes: 3 additions & 2 deletions LICENSE
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
Expand Down
34 changes: 16 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
koa-override
=======
# koa-override

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]
[![CI](https://github.com/eggjs/koa-override/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/koa-override/actions?query=branch%3Amaster)
[![Coverage](https://img.shields.io/codecov/c/github/eggjs/koa-override.svg?style=flat-square)](https://codecov.io/gh/eggjs/koa-override)

[npm-image]: https://img.shields.io/npm/v/koa-override.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-override
[travis-image]: https://img.shields.io/travis/node-modules/koa-override.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/koa-override
[coveralls-image]: https://img.shields.io/coveralls/node-modules/koa-override.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/node-modules/koa-override?branch=master
[david-image]: https://img.shields.io/david/node-modules/koa-override.svg?style=flat-square
[david-url]: https://david-dm.org/node-modules/koa-override
[download-image]: https://img.shields.io/npm/dm/koa-override.svg?style=flat-square
[download-url]: https://npmjs.org/package/koa-override

Expand All @@ -26,17 +18,17 @@ Refactor from [koa-override-method#5](https://github.com/koajs/override-method/p
## Install

```bash
$ npm install koa-override --save
npm install koa-override --save
```

## Usage

```js
const bodyParser = require('koa-bodyparser')
const override = require('koa-override')
```ts
import bodyParser from 'koa-bodyparser';
import override from 'koa-override';

app.use(bodyParser())
app.use(override())
app.use(bodyParser());
app.use(override());
```

## API
Expand All @@ -53,4 +45,10 @@ You shouldn't use this unless you know you're using override.

## License

[MIT](./LICENSE)
[MIT](LICENSE)

## Contributors

[![Contributors](https://contrib.rocks/image?repo=eggjs/koa-override)](https://github.com/eggjs/koa-override/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).
15 changes: 0 additions & 15 deletions appveyor.yml

This file was deleted.

40 changes: 0 additions & 40 deletions index.js

This file was deleted.

85 changes: 55 additions & 30 deletions package.json
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",
Expand All @@ -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"
}
48 changes: 48 additions & 0 deletions src/index.ts
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();
};
};
Loading

0 comments on commit f021708

Please sign in to comment.