Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

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

Closed
caiobiodere opened this issue Jun 14, 2020 · 18 comments · Fixed by #530

Comments

@caiobiodere
Copy link

caiobiodere commented Jun 14, 2020

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 :)

@caiobiodere caiobiodere changed the title is there any way to define what is being returned like a typescript class? Is there any way to define what is being returned like a typescript class? Jun 14, 2020
@KnisterPeter
Copy link
Owner

KnisterPeter commented Jun 14, 2020

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.

@caiobiodere caiobiodere changed the title Is there any way to define what is being returned like a typescript class? Is there any way to define what is being returned like folding it into a typescript class? Jun 14, 2020
@caiobiodere
Copy link
Author

@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 caiobiodere changed the title Is there any way to define what is being returned like folding it into a typescript class? Is there any way to define what is being returned from the service (wrapping it into a class)? Jun 14, 2020
@KnisterPeter
Copy link
Owner

@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.

@caiobiodere
Copy link
Author

caiobiodere commented Jun 14, 2020

@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[] {}

@KnisterPeter
Copy link
Owner

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.

@caiobiodere
Copy link
Author

caiobiodere commented Jun 15, 2020

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

@KnisterPeter
Copy link
Owner

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 {}

@caiobiodere
Copy link
Author

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

@KnisterPeter
Copy link
Owner

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. 😃

@caiobiodere
Copy link
Author

caiobiodere commented Jun 15, 2020

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?

@caiobiodere
Copy link
Author

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

@KnisterPeter
Copy link
Owner

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

KnisterPeter added a commit that referenced this issue Jun 15, 2020
This implements data mappers to create classes from responses with
optional transform step.

Closes #515
@caiobiodere
Copy link
Author

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

@KnisterPeter
Copy link
Owner

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

@KnisterPeter
Copy link
Owner

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

@KnisterPeter
Copy link
Owner

KnisterPeter commented Jun 21, 2020

These are the docs in the README

KnisterPeter added a commit that referenced this issue Jun 22, 2020
This implements data mappers to create classes from responses with
optional transform step.

Closes #515
@KnisterPeter
Copy link
Owner

Released in v3.1.0

@caiobiodere
Copy link
Author

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

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

Successfully merging a pull request may close this issue.

2 participants