Skip to content

Commit 08cc908

Browse files
author
Josper
committed
feat: 🎸 update delete
1 parent c9fefe8 commit 08cc908

File tree

6 files changed

+79
-44
lines changed

6 files changed

+79
-44
lines changed

packages/http-svc/CHANGELOG.md

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,8 @@
2727
2828
- 弃用的功能或特性 -->
2929

30-
## [v1.0.0-rc.8] - 2024-05-07
31-
32-
### Changed
33-
34-
- Readme doc link
35-
36-
## [v1.0.0-rc.7] - 2024-04-18
37-
38-
### Added
39-
40-
- Bundle update
41-
42-
## [v1.0.0-rc.6] - 2024-03-19
43-
44-
### Added
45-
46-
- Types update
47-
48-
### Fixed
49-
50-
- AssembleCtrl compose bug
51-
52-
## [v1.0.0-rc.1] - 2024-01-11
30+
## [v1.0.0] - 2024-07-18
5331

5432
### Added
5533

56-
- Release RC version
34+
- release v1.0.0

packages/http-svc/src/__examples__/normal.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,17 @@ http
3434
.catch((error) => {
3535
console.error(error)
3636
})
37+
http
38+
.request({
39+
url: '/delete',
40+
method: 'delete',
41+
data: {
42+
test_key: 2
43+
}
44+
})
45+
.then((res) => {
46+
console.log(res)
47+
})
48+
.catch((error) => {
49+
console.error(error)
50+
})

packages/http-svc/src/built-in/init-ctx.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,19 @@ const initCtx: IMiddlewareHandler = async function (ctx, next) {
3131
ctx.request.headers = {
3232
...(headers || {})
3333
}
34-
if (method === 'POST' || method === 'PUT') {
35-
if (data) {
36-
if (typeof data === 'object') {
37-
if (typeof FormData !== 'undefined' && data instanceof FormData) {
38-
const form = new FormData()
39-
for (const [key, value] of data.entries()) {
40-
form.append(key, value)
41-
}
42-
ctx.request.data = form
43-
} else if (Object.keys(data)) {
44-
ctx.request.data = JSON.parse(JSON.stringify(data))
34+
if (data) {
35+
if (typeof data === 'object') {
36+
if (typeof FormData !== 'undefined' && data instanceof FormData) {
37+
const form = new FormData()
38+
for (const [key, value] of data.entries()) {
39+
form.append(key, value)
4540
}
46-
} else {
47-
ctx.request.data = data
41+
ctx.request.data = form
42+
} else if (Object.keys(data)) {
43+
ctx.request.data = JSON.parse(JSON.stringify(data))
4844
}
45+
} else {
46+
ctx.request.data = data
4947
}
5048
}
5149

packages/middleware/CHANGELOG.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# CHANGELOG
2+
3+
<!-- 所有重要的更改和版本更新将在此文档中进行记录。
4+
5+
格式
6+
每个版本的更改应该以以下格式呈现:
7+
8+
## [版本号] - 发布日期
9+
10+
### Added(⭐️)
11+
12+
- 新增的功能和特性
13+
14+
### Fixed(🐞)
15+
16+
- 修复的 Bug
17+
18+
### Changed(✍🏻)
19+
20+
- 对已有功能或特性的修改
21+
22+
### Removed(🗑)
23+
24+
- 移除的功能或特性
25+
26+
### Deprecated (🚗)
27+
28+
- 弃用的功能或特性 -->
29+
30+
## [v1.0.0] - 2024-07-18
31+
32+
### Added
33+
34+
- release v1.0.0

packages/middleware/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@http-svc/middleware",
3-
"version": "1.0.0-rc.4",
3+
"version": "1.0.0",
44
"description": "HTTP Service base middleware",
55
"repository": "https://github.com/bilibili/http-service",
66
"private": false,
@@ -9,12 +9,13 @@
99
"types": "types/index.d.ts",
1010
"type": "module",
1111
"exports": {
12-
"./types": "./types/index.d.ts",
1312
".": {
13+
"types": "./types/index.d.ts",
1414
"import": "./dist/index.mjs",
1515
"require": "./dist/index.cjs"
1616
},
1717
"./legacy": {
18+
"types": "./types/index.d.ts",
1819
"import": "./dist/index.legacy.esm.js",
1920
"require": "./dist/index.legacy.js"
2021
}
@@ -28,6 +29,7 @@
2829
"dist",
2930
"index.ts",
3031
"types",
32+
"CHANGELOG.md",
3133
"README.md"
3234
],
3335
"keywords": [

server/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import Koa from 'koa'
22
import Router from 'koa-router'
3-
import bodyParser from 'koa-body'
3+
import bodyParser, { HttpMethodEnum } from 'koa-body'
44
import cors from '@koa/cors'
55
const app = new Koa()
66
const router = new Router()
77

8-
app.use(bodyParser())
8+
app.use(
9+
bodyParser({
10+
parsedMethods: [HttpMethodEnum.POST, HttpMethodEnum.PUT, HttpMethodEnum.DELETE]
11+
})
12+
)
913
app.use(
1014
cors({
1115
origin: 'http://localhost',
@@ -15,17 +19,22 @@ app.use(
1519

1620
// 创建 GET 接口
1721
router.get('/get', (ctx) => {
18-
console.log(ctx.request.url) // 打印请求体
22+
console.log('get:', ctx.request.url) // 打印请求体
1923
ctx.body = { message: 'GET 请求成功' }
2024
})
2125
// 创建 GET 接口
2226
router.post('/post', (ctx) => {
23-
console.log(ctx.request.body) // 打印请求体
27+
console.log('post:', ctx.request.body) // 打印请求体
2428
ctx.body = { message: 'POST 请求成功' }
2529
})
30+
// 创建 DELETE 接口
31+
router.delete('/delete', (ctx) => {
32+
console.log('delete:', ctx.request.body) // 打印请求体
33+
ctx.body = { message: 'DELETE 请求成功' }
34+
})
2635
// 创建 PUT 接口
2736
router.put('/put', (ctx) => {
28-
console.log(ctx.request.body) // 打印请求体
37+
console.log('put:', ctx.request.body) // 打印请求体
2938
ctx.body = { message: 'PUT 请求成功' }
3039
})
3140

0 commit comments

Comments
 (0)