如何扩展 Opine 中的 Response(Deno 框架)?
How to extend the Response in Opine (Deno framework)?
这里是第一个问题。
有谁知道如何扩展 Opine(Deno 框架)中的响应以便我可以创建自定义响应?
例如我想创建:
res.success(message)
这样我就不需要每次都像这样设置 HTTP 代码了:
res.setStatus(200).json({data: "success" });
我尝试像这里那样扩展响应:
https://deno.land/x/opine@2.1.5/test/units/app.response.test.ts
这是我的代码:
import { opine } from "https://deno.land/x/opine@2.1.4/mod.ts";
const app = opine();
(app.response as any).shout = function (str: string) {
this.send(str.toUpperCase());
};
app.get("/", (req, res) => {
res.shout("hello")
})
app.listen(3000);
console.log("Opine started on port 3000");
export { app };
但是当我 运行 我得到的程序时:
error: TS2339 [ERROR]: Property 'shout' does not exist on type 'OpineResponse<any>'.
res.shout("hello")
~~~~~
谢谢。
如果不分叉意见和修改库的核心函数和方法,就没有真正“干净”的方法。
您可以通过在调用站点断言类型来满足编译器的要求(例如 by using any
just like in the test file to which you linked). Another approach is to use an assertion function 就像下面代码的示例重构:
so-71990454.ts
:
import { assert } from "https://deno.land/std@0.136.0/testing/asserts.ts";
import {
type Opine,
opine,
type OpineResponse,
} from "https://deno.land/x/opine@2.1.5/mod.ts";
// Define the type extensions here
type ExtendedOpineResponse = {
shout(body: string): void;
};
// Implement the extensions here
function extendOpineApp(app: Opine): void {
// deno-lint-ignore no-explicit-any
(app.response as any).shout = function (str: string) {
this.send(str.toUpperCase());
};
}
// Assert feature tests for each one in this function
function assertIsExtendedResponse<T extends OpineResponse>(
response: T,
): asserts response is T & ExtendedOpineResponse {
assert(
// deno-lint-ignore no-explicit-any
typeof (response as any).shout === "function",
'Method "shout" not found on response',
);
}
export const app = opine();
// Invoke the extending function after creating the app
extendOpineApp(app);
app.get("/", (_req, res) => {
assertIsExtendedResponse(res);
res.shout("hello");
});
app.listen(3000);
console.log("Opine started on port 3000");
您可以看到type-checking模块没有产生诊断错误:
$ deno --version
deno 1.21.0 (release, x86_64-unknown-linux-gnu)
v8 10.0.139.17
typescript 4.6.2
$ deno check so-71990454.ts
$ echo $?
0
这里是第一个问题。
有谁知道如何扩展 Opine(Deno 框架)中的响应以便我可以创建自定义响应?
例如我想创建:
res.success(message)
这样我就不需要每次都像这样设置 HTTP 代码了:
res.setStatus(200).json({data: "success" });
我尝试像这里那样扩展响应: https://deno.land/x/opine@2.1.5/test/units/app.response.test.ts
这是我的代码:
import { opine } from "https://deno.land/x/opine@2.1.4/mod.ts";
const app = opine();
(app.response as any).shout = function (str: string) {
this.send(str.toUpperCase());
};
app.get("/", (req, res) => {
res.shout("hello")
})
app.listen(3000);
console.log("Opine started on port 3000");
export { app };
但是当我 运行 我得到的程序时:
error: TS2339 [ERROR]: Property 'shout' does not exist on type 'OpineResponse<any>'.
res.shout("hello")
~~~~~
谢谢。
如果不分叉意见和修改库的核心函数和方法,就没有真正“干净”的方法。
您可以通过在调用站点断言类型来满足编译器的要求(例如 by using any
just like in the test file to which you linked). Another approach is to use an assertion function 就像下面代码的示例重构:
so-71990454.ts
:
import { assert } from "https://deno.land/std@0.136.0/testing/asserts.ts";
import {
type Opine,
opine,
type OpineResponse,
} from "https://deno.land/x/opine@2.1.5/mod.ts";
// Define the type extensions here
type ExtendedOpineResponse = {
shout(body: string): void;
};
// Implement the extensions here
function extendOpineApp(app: Opine): void {
// deno-lint-ignore no-explicit-any
(app.response as any).shout = function (str: string) {
this.send(str.toUpperCase());
};
}
// Assert feature tests for each one in this function
function assertIsExtendedResponse<T extends OpineResponse>(
response: T,
): asserts response is T & ExtendedOpineResponse {
assert(
// deno-lint-ignore no-explicit-any
typeof (response as any).shout === "function",
'Method "shout" not found on response',
);
}
export const app = opine();
// Invoke the extending function after creating the app
extendOpineApp(app);
app.get("/", (_req, res) => {
assertIsExtendedResponse(res);
res.shout("hello");
});
app.listen(3000);
console.log("Opine started on port 3000");
您可以看到type-checking模块没有产生诊断错误:
$ deno --version
deno 1.21.0 (release, x86_64-unknown-linux-gnu)
v8 10.0.139.17
typescript 4.6.2
$ deno check so-71990454.ts
$ echo $?
0