airtasker/spot

Accept variable as decorator's input

janainascal opened this issue · 3 comments

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.

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?

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

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.