KnisterPeter/pretend

Is there any way to define what is being returned from the service (wrapping it into a class)?

caiobiodere opened this issue ยท 18 comments

Hey everyone first of all thanks for creating this, I was thinking about doing something like this for a project but you guys got it covered! second here comes my question I still don't know if it is possible to define a response type like a class being created from what is returned from the service. I would probably also want to define which path of the object should be pointed to this class this could be a decorator. What do you guys think about all of this? I might be interested in helping this project it is really what I have been doing with Swift using Sourcery :)

Hi @caiobiodere,
I'm not sure if I understand what you are aiming at. What do you mean by 'return like a typescript class'?
Do you want to have the returned data wrapped in a class which is created based on the method which was called?

Would be nice to see some pseudocode you have in mind if you can write that down.

@KnisterPeter yeah that's exactly what I meant sorry english is not one of my best things yet, still getting in there :) yes sure can do that give me a couple of days will figure out how to do that

@caiobiodere no need to say sorry ๐Ÿ˜ƒ We all are non native speakers. You're welcome.

Before start coding a rough scribble of pseudo code might help me to understand your requirements. Maybe what you try to achieve is already possible, not sure.
Please just explain it here in a few more sentences and a sketch.

@KnisterPeter thanks for understanding mate!

getting into it, the idea behind all of this would be something similar to this:

type UserType {
   id?: number
}

class User: UserType {
   id?: number

    constructor(id: number) {
        this.id = id;
    }
}

@Headers('Accept: application/json')
@Get('/user/{id}', true)
@Response(type: User | User[], keyPath: "userObject" |  null) (the new decorator I mentioned before)
public async get(id: string, parameters: any) : User | User[] {}

Hi @caiobiodere,
this looks nice. Can you explain a bit what you need the keyPath for? The type is pretty obvious.

In general I could think of adding this feature, I like it.

Hi mate @KnisterPeter,
basically keyPath would be the path that it should extract the object to be wrapped from, lets say that we have a response like this:

{
  user: {
    id: 1
  }
}

I could use keyPath as "user" and tell the decorator to wrap into the User class from it, this would save a lot of scenarios I can tell that because most of the time API's are not in the way you really want them to be

Instead I would propose to allow this to be a function. Then if the reponse is not matching it could be updated by that. Like this:

@Response(type: User, transform: (input) => input.user)
public async get(id: string, parameters: any) : User {}

I like your idea better, do you want me to help with that? I really want this to be part of my current working project

You can give it a try. I would accept a PR if you like.
But I'm as well a bit biased and have an idea how I would implement this. Lets see, maybe I like your idea as well. ๐Ÿ˜ƒ

Lets get this running, will be watching and forking this project from now on, do you have an idea of how much time would it take to you to get a first version of this done?

if you think you can finish this in a couple of days I can wait mate, that would be no problem at all

@caiobiodere I'll definitely build this this week I think.

@KnisterPeter hey mate I saw you started it already, how is it going do you have an estimated time?

The PR could be reviewed by you, it's already done.
The tests should hold some examples.
Please give some feedback.

@caiobiodere Any comments or thoughts? I'd like to make a release out of this.

These are the docs in the README

Released in v3.1.0

thanks mate! I think that's good enough regarding it also fixes my case :)