mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-12-23 03:11:25 +00:00
fix wrapped http client (#2267)
wrappedhttpclient has two bugs: 1. setHeaders becomes async with errors gobbled up. this is probably not intentional and the result is not awaited at the call site 2. wrappedhttpclient overrides methods in the prototype and not the instance. this means that when a new instance is created, from then on all methdos on all wrappedhttpclients call the inner methods on the newly created client instead of their respective clients this PR tries to fix both. it is untested so idk if it works related: #2243
This commit is contained in:
parent
659d5f78d8
commit
03fcaa0815
2 changed files with 7 additions and 7 deletions
|
@ -38,7 +38,7 @@ export default async (req: Request, res: Response) => {
|
||||||
|
|
||||||
if (auth) {
|
if (auth) {
|
||||||
headers["Authorization"] = `Bearer ${auth}`;
|
headers["Authorization"] = `Bearer ${auth}`;
|
||||||
client.setHeaders(headers);
|
client.rawClient.setHeaders(headers);
|
||||||
}
|
}
|
||||||
const { path, url, query } = req;
|
const { path, url, query } = req;
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ export type RequestState<T> =
|
||||||
| FailedRequestState
|
| FailedRequestState
|
||||||
| SuccessRequestState<T>;
|
| SuccessRequestState<T>;
|
||||||
|
|
||||||
export type WrappedLemmyHttp = {
|
export type WrappedLemmyHttp = WrappedLemmyHttpClient & {
|
||||||
[K in keyof LemmyHttp]: LemmyHttp[K] extends (...args: any[]) => any
|
[K in keyof LemmyHttp]: LemmyHttp[K] extends (...args: any[]) => any
|
||||||
? ReturnType<LemmyHttp[K]> extends Promise<infer U>
|
? ReturnType<LemmyHttp[K]> extends Promise<infer U>
|
||||||
? (...args: Parameters<LemmyHttp[K]>) => Promise<RequestState<U>>
|
? (...args: Parameters<LemmyHttp[K]>) => Promise<RequestState<U>>
|
||||||
|
@ -47,18 +47,18 @@ export type WrappedLemmyHttp = {
|
||||||
};
|
};
|
||||||
|
|
||||||
class WrappedLemmyHttpClient {
|
class WrappedLemmyHttpClient {
|
||||||
#client: LemmyHttp;
|
rawClient: LemmyHttp;
|
||||||
|
|
||||||
constructor(client: LemmyHttp, silent = false) {
|
constructor(client: LemmyHttp, silent = false) {
|
||||||
this.#client = client;
|
this.rawClient = client;
|
||||||
|
|
||||||
for (const key of Object.getOwnPropertyNames(
|
for (const key of Object.getOwnPropertyNames(
|
||||||
Object.getPrototypeOf(this.#client),
|
Object.getPrototypeOf(this.rawClient),
|
||||||
)) {
|
)) {
|
||||||
if (key !== "constructor") {
|
if (key !== "constructor") {
|
||||||
WrappedLemmyHttpClient.prototype[key] = async (...args) => {
|
this[key] = async (...args) => {
|
||||||
try {
|
try {
|
||||||
const res = await this.#client[key](...args);
|
const res = await this.rawClient[key](...args);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data: res,
|
data: res,
|
||||||
|
|
Loading…
Reference in a new issue