如何扩展Opine(DENO框架)中的响应?

发布于 2025-01-23 19:03:08 字数 1240 浏览 2 评论 0 原文

这里的第一个问题。

有谁知道如何扩展Opine(DENO框架)中的响应,以便我可以创建自定义响应?

例如,我想创建:

res.success(message)

这样我就不需要每次设置HTTP代码:

res.setStatus(200).json({data: "success" });

我尝试像在此处完成的响应扩展: https://deno.land/x/ [email  prectioned] /span>/test/test/units/app.response.test.test.ts.ts

这是我的代码:

import { opine } from "https://deno.land/x/[email protected]/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")
        ~~~~~

谢谢。

first question here.

Does anyone know how to extend the response in Opine (Deno framework) so that I can create custom responses?

For example I would like to create:

res.success(message)

So that I don't need to set HTTP codes every time like this:

res.setStatus(200).json({data: "success" });

I tried extending the response like it's done here:
https://deno.land/x/[email protected]/test/units/app.response.test.ts

This is my code:

import { opine } from "https://deno.land/x/[email protected]/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 };

But when I run the program I get:

error: TS2339 [ERROR]: Property 'shout' does not exist on type 'OpineResponse<any>'.
    res.shout("hello")
        ~~~~~

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼痛 2025-01-30 19:03:08

没有真正的“干净”方法,而无需分叉opine并修改库核心的功能和方法。

您可以通过在调用站点上断言类型来满足编译器(例如通过使用任何,就像您链接到的测试文件中的任何一样)。另一种方法是使用喜欢在下面的代码的示例重构中:

so-71990454.ts

import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
import {
  type Opine,
  opine,
  type OpineResponse,
} from "https://deno.land/x/[email protected]/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");

您可以看到该类型检查模块不会产生任何诊断错误:

$ 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

There's not really a "clean" way of doing this without forking opine and modifying the functions and methods which are core to the library.

You can satisfy the compiler by asserting the types at the invocation sites (e.g. by using any just like in the test file to which you linked). Another approach is to use an assertion function like in the example refactor of your code below:

so-71990454.ts:

import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
import {
  type Opine,
  opine,
  type OpineResponse,
} from "https://deno.land/x/[email protected]/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");

You can see that type-checking the module produces no diagnostic errors:

$ 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文