How to transform response from API request into class instance with `class-transformer` in TypeScript?

Baterka

I have normal method Request(method: HttpMethod, url: string, ...) for calling API. I am using TypeScript.

I need to transform response from this API Request into class instance with class-transformer (or without it :D).

Example:

class UserResponse {
  id: number;l
  foo: string;
  bar: string;
}
const user = await Request(HttpMEthod.GET, '/user/1');
// `user` should be class instance `UserResponse`

I know I cannot use generics like so:

const Request = <T>(method: HttpMethod, url: string, ...) => {
  // axios or something else...
  return plainToClass(T, res);
}

const user = await Request<UserResponse>(HttpMEthod.GET, '/user/1');

Generics do not work that way, but I can do something like:

const Request = <T>(method: HttpMethod, url: string, ...,  transformTo?: { new (): T }) => {
  // axios or something else...
  return plainToClass(transformTo, res);
}

const user = await Request(HttpMEthod.GET, '/user/1', ... , new UserResponse());

But this also not working. I am still getting user type:

const user: unknown

What I am doing wrong?

mwilson

First and foremost, what is Request? I am assuming this returns a Promise.

You have a couple of issues:

  1. You never actually map your response into a new UserResponse
  2. Generics in TypeScript will not new up a response. It acts more like an interface where it's more a data contract rather than a full initialized class.

Here is how you need to do this:

class UserResponse {
  public id: number;
  public foo: string;
  public bar: string;
  constructor(user: UserResponse) {
    Object.assign(this, user); // or set each prop individually
  }
}
const response = await Request(HttpMEthod.GET, '/user/1');
const user = new UserResponse(response);

Basically, you need to add a constructor to your class (otherwise, just use an interface). Once you've got the constructor, then you can new up a new UserResponse class from the response.

Also, if Request looks like:

const Request = <T>(method: HttpMethod, url: string, ...,  transformTo?: { new (): T }) => {
  // axios or something else...
  return plainToClass(transformTo, res);
}

I think you want to do this instead: const user = await Request(HttpMEthod.GET, '/user/1', ... , UserResponse);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在TypeScript中使用class-transformer将API请求的响应转换为类实例?

来自分类Dev

Access class variable from instance

来自分类Dev

Call derived class method from base class instance

来自分类Dev

How do I parse a RestSharp response into a class?

来自分类Dev

Does this point to the class or a class instance? (6)

来自分类Dev

How can I copy a templated instance with a virtual base class?

来自分类Dev

How can I request an Image from a URL when the Response is 302?

来自分类Dev

How to return a simple QSqlQueryModel from a class to another class?

来自分类Dev

How to access static members from instance methods in typescript?

来自分类Dev

Static Instance Base/Derived class

来自分类Dev

create an instance of class in python 2.7

来自分类Dev

获取Javascript中元素的transform class属性

来自分类Dev

获取Javascript中元素的transform class属性

来自分类Dev

AngularJS & Typescript: How to assign a class/type as a directive's controller?

来自分类Dev

为什么在使用class-transformer时不使用enableImplicitConversion?

来自分类Dev

typescript: make class objects iterable

来自分类Dev

<class 'requests.models.Response'> to Json

来自分类Dev

Is it true that every inner class requires an enclosing instance?

来自分类Dev

WM_CLASS与WM_INSTANCE?

来自分类Dev

Static function returning a static instance of the class - shouldn't the instance be the same?

来自分类Dev

Pull Properties from Class

来自分类Dev

populate dropdown from class

来自分类Dev

fireEvent from Custom class

来自分类Dev

Accessing static methods from instance in Typescript

来自分类Dev

'Transform' : 'class' OpenGL 3D 类型重新定义

来自分类Dev

How to typehint mixins if the target class for the mixin inherits from a metaclass?

来自分类Dev

How to get name of the object's from a different class in Java

来自分类Dev

Swift: How to call a category or class method from Objective-C

来自分类Dev

Can I create a class [] operator in Typescript

Related 相关文章

  1. 1

    如何在TypeScript中使用class-transformer将API请求的响应转换为类实例?

  2. 2

    Access class variable from instance

  3. 3

    Call derived class method from base class instance

  4. 4

    How do I parse a RestSharp response into a class?

  5. 5

    Does this point to the class or a class instance? (6)

  6. 6

    How can I copy a templated instance with a virtual base class?

  7. 7

    How can I request an Image from a URL when the Response is 302?

  8. 8

    How to return a simple QSqlQueryModel from a class to another class?

  9. 9

    How to access static members from instance methods in typescript?

  10. 10

    Static Instance Base/Derived class

  11. 11

    create an instance of class in python 2.7

  12. 12

    获取Javascript中元素的transform class属性

  13. 13

    获取Javascript中元素的transform class属性

  14. 14

    AngularJS & Typescript: How to assign a class/type as a directive's controller?

  15. 15

    为什么在使用class-transformer时不使用enableImplicitConversion?

  16. 16

    typescript: make class objects iterable

  17. 17

    <class 'requests.models.Response'> to Json

  18. 18

    Is it true that every inner class requires an enclosing instance?

  19. 19

    WM_CLASS与WM_INSTANCE?

  20. 20

    Static function returning a static instance of the class - shouldn't the instance be the same?

  21. 21

    Pull Properties from Class

  22. 22

    populate dropdown from class

  23. 23

    fireEvent from Custom class

  24. 24

    Accessing static methods from instance in Typescript

  25. 25

    'Transform' : 'class' OpenGL 3D 类型重新定义

  26. 26

    How to typehint mixins if the target class for the mixin inherits from a metaclass?

  27. 27

    How to get name of the object's from a different class in Java

  28. 28

    Swift: How to call a category or class method from Objective-C

  29. 29

    Can I create a class [] operator in Typescript

热门标签

归档