mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-10 20:42:06 +10:00
feat: lazy init igdb
This commit is contained in:
@ -129,10 +129,10 @@ interface IGDBGameFull extends IGDBSearchStub {
|
|||||||
|
|
||||||
// Api Docs: https://api-docs.igdb.com/
|
// Api Docs: https://api-docs.igdb.com/
|
||||||
export class IGDBProvider implements MetadataProvider {
|
export class IGDBProvider implements MetadataProvider {
|
||||||
private client_id: string;
|
private clientId: string;
|
||||||
private client_secret: string;
|
private clientSecret: string;
|
||||||
private access_token: string;
|
private accessToken: string;
|
||||||
private access_token_expire: moment.Moment;
|
private accessTokenExpiry: moment.Moment;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const client_id = process.env.IGDB_CLIENT_ID;
|
const client_id = process.env.IGDB_CLIENT_ID;
|
||||||
@ -145,18 +145,18 @@ export class IGDBProvider implements MetadataProvider {
|
|||||||
this.name()
|
this.name()
|
||||||
);
|
);
|
||||||
|
|
||||||
this.client_id = client_id;
|
this.clientId = client_id;
|
||||||
this.client_secret = client_secret;
|
this.clientSecret = client_secret;
|
||||||
|
|
||||||
this.access_token = "";
|
this.accessToken = "";
|
||||||
this.access_token_expire = moment();
|
this.accessTokenExpiry = moment(new Date(0));
|
||||||
this.authWithTwitch();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async authWithTwitch() {
|
private async authWithTwitch() {
|
||||||
|
console.log("authorizing with twitch");
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
client_id: this.client_id,
|
client_id: this.clientId,
|
||||||
client_secret: this.client_secret,
|
client_secret: this.clientSecret,
|
||||||
grant_type: "client_credentials",
|
grant_type: "client_credentials",
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -166,18 +166,16 @@ export class IGDBProvider implements MetadataProvider {
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
});
|
});
|
||||||
|
|
||||||
this.access_token = response.data.access_token;
|
this.accessToken = response.data.access_token;
|
||||||
this.access_token_expire = moment().add(
|
this.accessTokenExpiry = moment().add(response.data.expires_in, "seconds");
|
||||||
response.data.expires_in,
|
|
||||||
"seconds"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async refreshCredentials() {
|
private async refreshCredentials() {
|
||||||
const futureTime = moment().add(1, "day");
|
const futureTime = moment().add(1, "day");
|
||||||
|
|
||||||
// if the token expires before this future time (aka soon), refresh
|
// if the token expires in less than a day
|
||||||
if (this.access_token_expire.isBefore(futureTime)) this.authWithTwitch();
|
if (this.accessTokenExpiry.isBefore(futureTime))
|
||||||
|
await this.authWithTwitch();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async request<T extends Object>(
|
private async request<T extends Object>(
|
||||||
@ -188,7 +186,7 @@ export class IGDBProvider implements MetadataProvider {
|
|||||||
await this.refreshCredentials();
|
await this.refreshCredentials();
|
||||||
|
|
||||||
// prevent calling api before auth is complete
|
// prevent calling api before auth is complete
|
||||||
if (this.access_token.length <= 0)
|
if (this.accessToken.length <= 0)
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"IGDB either failed to authenticate, or has not done so yet"
|
"IGDB either failed to authenticate, or has not done so yet"
|
||||||
);
|
);
|
||||||
@ -202,8 +200,8 @@ export class IGDBProvider implements MetadataProvider {
|
|||||||
data: body,
|
data: body,
|
||||||
headers: {
|
headers: {
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
"Client-ID": this.client_id,
|
"Client-ID": this.clientId,
|
||||||
Authorization: `Bearer ${this.access_token}`,
|
Authorization: `Bearer ${this.accessToken}`,
|
||||||
"content-type": "text/plain",
|
"content-type": "text/plain",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user