Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
How to extend the Response in Opine (Deno framework)?

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/opine@2.1.5/test/units/app.response.test.ts

This is my code:

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 };

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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/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");

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda