Skip to content

Commit

Permalink
feat: 🎸 update delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Josper committed Sep 23, 2024
1 parent c9fefe8 commit 08cc908
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 44 deletions.
26 changes: 2 additions & 24 deletions packages/http-svc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,8 @@
- 弃用的功能或特性 -->

## [v1.0.0-rc.8] - 2024-05-07

### Changed

- Readme doc link

## [v1.0.0-rc.7] - 2024-04-18

### Added

- Bundle update

## [v1.0.0-rc.6] - 2024-03-19

### Added

- Types update

### Fixed

- AssembleCtrl compose bug

## [v1.0.0-rc.1] - 2024-01-11
## [v1.0.0] - 2024-07-18

### Added

- Release RC version
- release v1.0.0
14 changes: 14 additions & 0 deletions packages/http-svc/src/__examples__/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,17 @@ http
.catch((error) => {
console.error(error)
})
http
.request({
url: '/delete',
method: 'delete',
data: {
test_key: 2
}
})
.then((res) => {
console.log(res)
})
.catch((error) => {
console.error(error)
})
24 changes: 11 additions & 13 deletions packages/http-svc/src/built-in/init-ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,19 @@ const initCtx: IMiddlewareHandler = async function (ctx, next) {
ctx.request.headers = {
...(headers || {})
}
if (method === 'POST' || method === 'PUT') {
if (data) {
if (typeof data === 'object') {
if (typeof FormData !== 'undefined' && data instanceof FormData) {
const form = new FormData()
for (const [key, value] of data.entries()) {
form.append(key, value)
}
ctx.request.data = form
} else if (Object.keys(data)) {
ctx.request.data = JSON.parse(JSON.stringify(data))
if (data) {
if (typeof data === 'object') {
if (typeof FormData !== 'undefined' && data instanceof FormData) {
const form = new FormData()
for (const [key, value] of data.entries()) {
form.append(key, value)
}
} else {
ctx.request.data = data
ctx.request.data = form
} else if (Object.keys(data)) {
ctx.request.data = JSON.parse(JSON.stringify(data))
}
} else {
ctx.request.data = data
}
}

Expand Down
34 changes: 34 additions & 0 deletions packages/middleware/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# CHANGELOG

<!-- 所有重要的更改和版本更新将在此文档中进行记录。
格式
每个版本的更改应该以以下格式呈现:
## [版本号] - 发布日期
### Added(⭐️)
- 新增的功能和特性
### Fixed(🐞)
- 修复的 Bug
### Changed(✍🏻)
- 对已有功能或特性的修改
### Removed(🗑)
- 移除的功能或特性
### Deprecated (🚗)
- 弃用的功能或特性 -->

## [v1.0.0] - 2024-07-18

### Added

- release v1.0.0
6 changes: 4 additions & 2 deletions packages/middleware/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http-svc/middleware",
"version": "1.0.0-rc.4",
"version": "1.0.0",
"description": "HTTP Service base middleware",
"repository": "https://github.com/bilibili/http-service",
"private": false,
Expand All @@ -9,12 +9,13 @@
"types": "types/index.d.ts",
"type": "module",
"exports": {
"./types": "./types/index.d.ts",
".": {
"types": "./types/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./legacy": {
"types": "./types/index.d.ts",
"import": "./dist/index.legacy.esm.js",
"require": "./dist/index.legacy.js"
}
Expand All @@ -28,6 +29,7 @@
"dist",
"index.ts",
"types",
"CHANGELOG.md",
"README.md"
],
"keywords": [
Expand Down
19 changes: 14 additions & 5 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Koa from 'koa'
import Router from 'koa-router'
import bodyParser from 'koa-body'
import bodyParser, { HttpMethodEnum } from 'koa-body'
import cors from '@koa/cors'
const app = new Koa()
const router = new Router()

app.use(bodyParser())
app.use(
bodyParser({
parsedMethods: [HttpMethodEnum.POST, HttpMethodEnum.PUT, HttpMethodEnum.DELETE]
})
)
app.use(
cors({
origin: 'http://localhost',
Expand All @@ -15,17 +19,22 @@ app.use(

// 创建 GET 接口
router.get('/get', (ctx) => {
console.log(ctx.request.url) // 打印请求体
console.log('get:', ctx.request.url) // 打印请求体
ctx.body = { message: 'GET 请求成功' }
})
// 创建 GET 接口
router.post('/post', (ctx) => {
console.log(ctx.request.body) // 打印请求体
console.log('post:', ctx.request.body) // 打印请求体
ctx.body = { message: 'POST 请求成功' }
})
// 创建 DELETE 接口
router.delete('/delete', (ctx) => {
console.log('delete:', ctx.request.body) // 打印请求体
ctx.body = { message: 'DELETE 请求成功' }
})
// 创建 PUT 接口
router.put('/put', (ctx) => {
console.log(ctx.request.body) // 打印请求体
console.log('put:', ctx.request.body) // 打印请求体
ctx.body = { message: 'PUT 请求成功' }
})

Expand Down

0 comments on commit 08cc908

Please sign in to comment.