Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept variable as decorator's input #1319

Open
janainascal opened this issue Mar 18, 2021 · 3 comments
Open

Accept variable as decorator's input #1319

janainascal opened this issue Mar 18, 2021 · 3 comments

Comments

@janainascal
Copy link

Problem

I can't use a variable in decorators

 Error: expected decorator factory configuration argument to be an object literal

I'm developing in typescript on my front (AngularJS) and my back (Adonis v5), and I would like to import API Configuration directly API package. Although I could set this twice (inside decorators and in a const), I like the Spot main idea: Single Point Of Truth.
My code is organize in 3 folders:
frontend:
|
Angular project
api
|
Spot project
__ backend
|__ Adonis V5 project

Solution I'd like

Describe the solution you'd like
A clear and concise description of what you want to happen.

const endPoindConfig: EndpointConfig = {
  method: "POST",
  path: "/users"
}
/** CreateUser endpoint */
@endpoint(endPoindConfig)
class CreateUser {
}

Alternatives

  • I could declare it twice, as I said above, or
  • I could crate an openapi specification and use OpenApi Generator to generate code for both ends, but it seems too many translations.
@lfportal
Copy link
Contributor

Hi @janainascal, can you provide a bit more context on with regard to "import API Configuration directly API package". Do you have a more complete example that might help me understand?

@janainascal
Copy link
Author

Hi @lfportal , I change SPOT's initial api.ts to what I want:

import { api, body, endpoint, request, response, String } from "@airtasker/spot";

@api({ name: "my-api" })
class Api {}

const CreateUserEndpoint={
  method: "POST",
  path: "/users"
}
@endpoint(CreateUserEndpoint)
class CreateUser {
  @request
  request(
    @body body: CreateUserRequest
  ) {}

  @response({ status: 201 })
  successfulResponse(
    @body body: CreateUserResponse
  ) {}
}

interface CreateUserRequest {
  firstName: String;
  lastName: String;
}

interface CreateUserResponse {
  firstName: String;
  lastName: String;
  role: String;
}

export{CreateUserEndpoint, CreateUserRequest, CreateUserResponse}

My wish is to generate a object and interfaces (this is already there) describing my api to use on my client and server using this api

@janainascal
Copy link
Author

For now, I'm duplicating it:

const CreateUserEndpoint = {
  method: "POST",
  path: "/users"
}
@endpoint({
  method: "POST",
  path: "/users"
})
class CreateUser {
  [...]
}

But, I realy like the idea of Single Point Of Truth ...

Anyway, this is a wish, and I understand if that would make SPOT development too complicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants