BackendStack21 / restana

Restana is a lightweight and fast Node.js framework for building RESTful APIs.
MIT License
467 stars 27 forks source link

Problem with typescript types (http2 and https server) #40

Closed Julien-Broyard closed 5 years ago

Julien-Broyard commented 5 years ago

While trying to use restana with http2/https with typescript I've got some errors.

Here's the code :

import http2 from "http2";
import pem from "pem";
import restana from 'restana'

pem.createCertificate({ days: 1, selfSigned: true }, async (err, keys) => {
  try {
    if (err) console.error(err);

    const service = await restana({
      server: http2.createSecureServer({
        cert: keys.certificate,
        key: keys.clientKey
      })
    });

    service.get("/", (req, res) => {
      res.send(req.url);
    });

    await service.start(8080);
  } catch (error) {
    console.error(error);
  }
});

Here's the error :

Type 'Http2SecureServer' is missing the following properties from type 'Server': maxHeadersCount, timeout, headersTimeout, keepAliveTimeoutts(2739)

index.d.ts(116, 5): The expected type comes from property 'server' which is declared here on type 'Options<Protocol.HTTP>'

Here's the code for the https :

import https from "https";
import pem from "pem";
import restana from "restana";

pem.createCertificate({ days: 1, selfSigned: true }, async (err, keys) => {
  try {
    if (err) console.error(err);

    const service = await restana({
      server: https.createServer({
        cert: keys.certificate,
        key: keys.clientKey
      })
    });

    service.get("/", (req, res) => {
      res.send(req.url);
    });

    await service.start(8080);
  } catch (error) {
    console.error(error);
  }
});

Here's the error :

Argument of type '{ server: Server; }' is not assignable to parameter of type 'Options<Protocol.HTTP>'.
  Property 'errorHandler' is missing in type '{ server: Server; }' but required in type 'Options<Protocol.HTTP>'.ts(2345)

index.d.ts(124, 5): 'errorHandler' is declared here.

Here's my tsconfig :

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "alwaysStrict": true,
    "baseUrl": "src",
    "esModuleInterop": true,
    "lib": ["dom", "esnext"],
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist",
    "pretty": true,
    "removeComments": true,
    "resolveJsonModule": true,
    "target": "esnext"
  },
  "exclude": ["node_modules", "tests/**/*.ts", "dist/**/*.ts"],
  "include": ["src/**/*.ts"]
}
jkyberneees commented 5 years ago

Hi @tomc974, can we kindly ask for your support here?

Best Regards.

tomc974 commented 5 years ago

@Julien-Broyard Hi! When calling restana you have to specify which protocol you are using. So if you change your function call to restana<restana.Protocol.HTTP2>, it should work.

Julien-Broyard commented 5 years ago

Hi !

I've tried it however it don't work

import https from "https";
import pem from "pem";
import restana from "restana";

pem.createCertificate({ days: 1, selfSigned: true }, async (err, keys) => {
  try {
    if (err) console.error(err);

    const service = await restana<restana.Protocol.HTTPS>({
      server: https.createServer({
        cert: keys.certificate,
        key: keys.clientKey
      })
    });

    service.get("/", (req, res) => {
      res.send(req.url);
    });

    await service.start(8080);
  } catch (error) {
    console.error(error);
  }
});
Argument of type '{ server: Server; }' is not assignable to parameter of type 'Options<Protocol.HTTPS>'.
  Property 'errorHandler' is missing in type '{ server: Server; }' but required in type 'Options<Protocol.HTTPS>'.ts(2345)
index.d.ts(124, 5): 'errorHandler' is declared here.
import http2 from "http2";
import pem from "pem";
import restana from "restana";

pem.createCertificate({ days: 1, selfSigned: true }, async (err, keys) => {
  try {
    if (err) console.error(err);

    const service = await restana<restana.Protocol.HTTP2>({
      server: http2.createSecureServer({
        cert: keys.certificate,
        key: keys.clientKey
      })
    });

    service.get("/", (req, res) => {
      res.send(req.url);
    });

    await service.start(8080);
  } catch (error) {
    console.error(error);
  }
});
Argument of type '{ server: Http2SecureServer; }' is not assignable to parameter of type 'Options<Protocol.HTTP2>'.
  Property 'errorHandler' is missing in type '{ server: Http2SecureServer; }' but required in type 'Options<Protocol.HTTP2>'.ts(2345)
index.d.ts(124, 5): 'errorHandler' is declared here.
Julien-Broyard commented 5 years ago

My bad I've just realized that I needed to add the errorHandler property to restana options !

jkyberneees commented 5 years ago

My bad I've just realized that I needed to add the errorHandler property to restana options !

The errorHandler property should be optional. Will fix it ASAP.

Anyway glad that you could move forward. Thanks @tomc974 for your support!

jkyberneees commented 5 years ago

Hi @Julien-Broyard, errorHandler config is not required anymore in restana@3.0.2. You should be fine now.

Regards and thanks.