Skip to content

Commit

Permalink
feat: add helper get() and post() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 12, 2021
1 parent 3964f7a commit 29f4876
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { Credentials } from "./helper/api/creds";
import type { Token } from "./helper/accessToken";
import type { Data } from "./helper/types";
import type { OauthOpts } from "./helper/api/oauth";
import type { Query } from "./helper/api/core";
import * as anon from "./helper/api/anon";
import * as oauth from "./helper/api/oauth";
import refreshToken from "./helper/accessToken";

export interface UsernameAuth {
Expand Down Expand Up @@ -30,6 +35,21 @@ export default class Client {
this.userAgent = opts.userAgent;
}

async get<T>(path: string, query: Query = {}): Promise<T> {
if (this.creds) {
return oauth.get(await this.oAuth(), path, query);
} else {
return anon.get(this.userAgent, path, query);
}
}
async post<T>(path: string, data: Data, query: Query = {}): Promise<T> {
if (this.creds) {
return oauth.post(await this.oAuth(), path, data, query);
} else {
return anon.post(this.userAgent, path, data, query);
}
}

protected async updateAccessToken(): Promise<void> {
if (!this.auth) throw "No auth";
if (!this.creds) throw "No creds";
Expand All @@ -41,4 +61,12 @@ export default class Client {
this.userAgent
);
}

protected async oAuth(): Promise<OauthOpts> {
await this.updateAccessToken();
return {
token: this.token!.access,
userAgent: this.userAgent,
};
}
}

0 comments on commit 29f4876

Please sign in to comment.