reactiverse / es4x

🚀 fast JavaScript 4 Eclipse Vert.x
https://reactiverse.io/es4x/
Apache License 2.0
884 stars 75 forks source link

CorsHandler does not work on .allowedMethod() #365

Closed Pakho closed 4 years ago

Pakho commented 4 years ago

I cannot create a CorsHandler as mentioned in the docs on both vertx and es4x.

Here is my code:

// <reference types="es4x" />
// @ts-checknp

import { Router } from '@vertx/web';
import { CorsHandler } from '@vertx/web';

var server = vertx.createHttpServer();
var router = Router.router(vertx);

// method 1 on vertx js docs: 
router.route().handler(CorsHandler.create("vertx\\.io").allowedMethod("GET"));
// method 2 on es4x docs give error HttpMethod is undefined. 
//router.route().handler(CorsHandler.create("vertx\\.io").allowedMethod(HttpMethod.GET));

router.get("/").handler(function (ctx) {
  // This handler will be called for every request
  var response = ctx.response();
  response.putHeader("content-type", "text/plain");
  // Write to the response and end it
  response.end("Hello World from Vert.x-Web!");
});

server.requestHandler(router.handle).listen(port, "ip");

It gives the following error:

Failed in deploying verticle caused by TypeError: invokeMember (allowedMethod) on io.vertx.ext.web.handler.impl.CorsHandlerImpl@4a79cd2 failed due to: Cannot convert 'GET'(language: Java, type: java.lang.String) to Java type 'io.vertx.core.http.HttpMethod': Unsupported target type.

What am I missing?

pmlopes commented 4 years ago

Hi, this is a bug in the auto generated docs, the enum is loaded using:

import { HttpMethod } from '@vertx/core/enums';

// and the this should be right:
router.route().handler(CorsHandler.create("vertx\\.io").allowedMethod(HttpMethod.GET));
pmlopes commented 4 years ago

The new docs, avoid duplicate the official vert.x docs, instead they explain how the mapping works and point to the source.