如何在 TypeScript 中将 api 数据转换为 DTO(数据传输对象)
How to transform api data to DTO (Data Transfer Object) in TypeScript
我有一系列车辆,如下所示。
export const fetchDataFromApi = () => {
return [
{ vehicleId: 1, vehicleType: 'car', seats: 4, wheelType: 'summer', updatedAt: new Date().toISOString },
{ vehicleId: 2, vehicleType: 'plane', seats: 200, maxAltitude: 8000, updatedAt: new Date().toISOString },
{ vehicleId: 3, vehicleType: 'train', seats: 1200, railType: 'whatever', updatedAt: new Date().toISOString },
];
};
我已经创建了交通工具、汽车、飞机和火车 class。但我不确定,那些也可能是接口。
此 API 响应应使用定义响应类型的 DTO 进行处理,并允许将响应转换为 DTO 类型。
那么如何将数据转换为类型化的 DTO?下面是我的 class 定义,但我不确定如何实现它..
class Vehicle {
vehicleId: number;
vehicleType: string;
seats: number;
updatedAt: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string) {
this.vehicleId = vehicleId;
this.vehicleType = vehicleType;
this.seats = seats;
this.updatedAt = updatedAt;
}
}
class Car extends Vehicle {
wheelType: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, wheelType: string) {
super(vehicleId, vehicleType, seats, updatedAt);
this.wheelType = wheelType;
}
}
class Plane extends Vehicle {
maxAltitude: number;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, maxAltitude: number) {
super(vehicleId, vehicleType, seats, updatedAt);
this.maxAltitude = maxAltitude;
}
}
class Train extends Vehicle {
railType: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, railType: string) {
super(vehicleId, vehicleType, seats, updatedAt);
this.railType = railType;
}
}
您只需要创建一个映射器来转换数据集。
const fetchDataFromApi = (): Vehicle[] => {
return [
{ vehicleId: 1, vehicleType: 'car', seats: 4, wheelType: 'summer', updatedAt: new Date().toISOString },
{ vehicleId: 2, vehicleType: 'plane', seats: 200, maxAltitude: 8000, updatedAt: new Date().toISOString },
{ vehicleId: 3, vehicleType: 'train', seats: 1200, railType: 'whatever', updatedAt: new Date().toISOString },
];
};
class Vehicle {
vehicleId: number;
vehicleType: string;
seats: number;
updatedAt: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string) {
this.vehicleId = vehicleId;
this.vehicleType = vehicleType;
this.seats = seats;
this.updatedAt = updatedAt;
}
}
class Car extends Vehicle {
wheelType: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, wheelType: string) {
super(vehicleId, vehicleType, seats, updatedAt);
this.wheelType = wheelType;
}
}
class Plane extends Vehicle {
maxAltitude: number;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, maxAltitude: number) {
super(vehicleId, vehicleType, seats, updatedAt);
this.maxAltitude = maxAltitude;
}
}
class Train extends Vehicle {
railType: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, railType: string) {
super(vehicleId, vehicleType, seats, updatedAt);
this.railType = railType;
}
}
const vehicleMapper = (vehicle: Vehicle) => {
const {vehicleId, vehicleType, seats, updatedAt} = vehicle;
switch(vehicle.vehicleType) {
case 'car':
const {wheelType} = vehicle as Car;
return new Car(vehicleId, vehicleType, seats, updatedAt, wheelType);
case 'plane':
const {maxAltitude} = vehicle as Plane;
return new Plane(vehicleId, vehicleType, seats, updatedAt, maxAltitude);
case 'train':
const {railType} = vehicle as Train;
return new Train(vehicleId, vehicleType, seats, updatedAt, railType);
default:
return new Vehicle(vehicleId, vehicleType, seats, updatedAt);
}
}
const data = fetchDataFromApi();
const vehicles = data.map(vehicleMapper);
我有一系列车辆,如下所示。
export const fetchDataFromApi = () => {
return [
{ vehicleId: 1, vehicleType: 'car', seats: 4, wheelType: 'summer', updatedAt: new Date().toISOString },
{ vehicleId: 2, vehicleType: 'plane', seats: 200, maxAltitude: 8000, updatedAt: new Date().toISOString },
{ vehicleId: 3, vehicleType: 'train', seats: 1200, railType: 'whatever', updatedAt: new Date().toISOString },
];
};
我已经创建了交通工具、汽车、飞机和火车 class。但我不确定,那些也可能是接口。
此 API 响应应使用定义响应类型的 DTO 进行处理,并允许将响应转换为 DTO 类型。
那么如何将数据转换为类型化的 DTO?下面是我的 class 定义,但我不确定如何实现它..
class Vehicle {
vehicleId: number;
vehicleType: string;
seats: number;
updatedAt: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string) {
this.vehicleId = vehicleId;
this.vehicleType = vehicleType;
this.seats = seats;
this.updatedAt = updatedAt;
}
}
class Car extends Vehicle {
wheelType: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, wheelType: string) {
super(vehicleId, vehicleType, seats, updatedAt);
this.wheelType = wheelType;
}
}
class Plane extends Vehicle {
maxAltitude: number;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, maxAltitude: number) {
super(vehicleId, vehicleType, seats, updatedAt);
this.maxAltitude = maxAltitude;
}
}
class Train extends Vehicle {
railType: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, railType: string) {
super(vehicleId, vehicleType, seats, updatedAt);
this.railType = railType;
}
}
您只需要创建一个映射器来转换数据集。
const fetchDataFromApi = (): Vehicle[] => {
return [
{ vehicleId: 1, vehicleType: 'car', seats: 4, wheelType: 'summer', updatedAt: new Date().toISOString },
{ vehicleId: 2, vehicleType: 'plane', seats: 200, maxAltitude: 8000, updatedAt: new Date().toISOString },
{ vehicleId: 3, vehicleType: 'train', seats: 1200, railType: 'whatever', updatedAt: new Date().toISOString },
];
};
class Vehicle {
vehicleId: number;
vehicleType: string;
seats: number;
updatedAt: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string) {
this.vehicleId = vehicleId;
this.vehicleType = vehicleType;
this.seats = seats;
this.updatedAt = updatedAt;
}
}
class Car extends Vehicle {
wheelType: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, wheelType: string) {
super(vehicleId, vehicleType, seats, updatedAt);
this.wheelType = wheelType;
}
}
class Plane extends Vehicle {
maxAltitude: number;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, maxAltitude: number) {
super(vehicleId, vehicleType, seats, updatedAt);
this.maxAltitude = maxAltitude;
}
}
class Train extends Vehicle {
railType: string;
constructor(vehicleId: number, vehicleType: string, seats: number, updatedAt: string, railType: string) {
super(vehicleId, vehicleType, seats, updatedAt);
this.railType = railType;
}
}
const vehicleMapper = (vehicle: Vehicle) => {
const {vehicleId, vehicleType, seats, updatedAt} = vehicle;
switch(vehicle.vehicleType) {
case 'car':
const {wheelType} = vehicle as Car;
return new Car(vehicleId, vehicleType, seats, updatedAt, wheelType);
case 'plane':
const {maxAltitude} = vehicle as Plane;
return new Plane(vehicleId, vehicleType, seats, updatedAt, maxAltitude);
case 'train':
const {railType} = vehicle as Train;
return new Train(vehicleId, vehicleType, seats, updatedAt, railType);
default:
return new Vehicle(vehicleId, vehicleType, seats, updatedAt);
}
}
const data = fetchDataFromApi();
const vehicles = data.map(vehicleMapper);