Ich begann Typescript für mein Nodejs-Projekt zu verwenden. Für den Zugriff auf eine externe API verwende ich node-fetch, um Anfragen zu stellen. Während eine PUT-Anforderung einrichten, erscheint ein Fehler auf und sagte, dass die gegebene Körper nicht belegbar ist RequestInit
eingeben:Wie kann der Anfragetext mit RequestInit oder BodyInit kompatibel gemacht werden, wenn node-fetch verwendet wird?
Der Fehler:
Error:(176, 28) TS2345:Argument of type '{ headers: Headers; method: string; body: MyClass; }' is not assignable to parameter of type 'RequestInit'.
Types of property 'body' are incompatible.
Type 'MyClass' is not assignable to type 'BodyInit'.
Type 'MyClass' is not assignable to type 'ReadableStream'.
Property 'readable' is missing in type 'MyClass'.
MyClass:
class MyClass {
configId: string;
adapterType: string;
address: string;
constructor(configId: string, adapterType: string, address: string) {
this.configId = configId;
this.adapterType = adapterType;
this.address = address;
}
// some methods
}
Invocation:
let body = new MyClass("a", "b", "c")
let url = config.url + "/dialog/" + dialogId
let headers = new Headers()
// Append several headers
let params = {
headers: headers,
method: "PUT",
body: body
}
return new Promise((resolve, reject) => {
Fetch(url, params) // <-- error is shown for params variable
.then(res => {
// Do stuff
resolve(/*somevalue*/)
})
}
Wie soll ich das Körperobjekt kompatibel machen?