Socket
Socket
Sign inDemoInstall

twitch-api-typescript

Package Overview
Dependencies
12
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.5.4 to 0.5.5

build/exceptions.d.ts

71

build/token-handler.d.ts
export declare class TokenHandler {
private readonly _clientId;
private readonly _clientSecret;
private _clientSecret?;
private _appAccessToken?;

@@ -9,19 +9,53 @@ private _refreshToken?;

private _appAccessTokenInterval?;
private readonly _writeOut?;
constructor(config: {
clientId: string;
clientSecret: string;
private readonly _initialUserAccessTokenRefresh;
private readonly _initialAppAccessTokenRefresh;
/**
* Tokenhandler that makes token management easy
* @param clientId The client identifier of the application that should be used for interaction with the Twitch API
* @param [tokens] Possible tokens at initialization
* @param [tokens.userAccessToken] An already existing User Access Token
* @param [tokens.appAccessToken] An already existing App Access Token
* @param [tokens.refreshToken] A refresh token for auto User Access Token renewal/refresh
* @param [options] Additional options
* @param [options.clientSecret] The applications secrets for auto App Access Token renewal/refresh
* @param [options.refreshAppAccessToken] A Boolean that determines whether the App Access Token should be refreshed automatically
* @param [options.clientSecret] A Boolean that determines whether the User Access Token should be refreshed automatically
*/
constructor(clientId: string, tokens?: {
userAccessToken?: string;
appAccessToken?: string;
refreshToken?: string;
writeOut?: {
path: string;
userToken?: boolean;
appToken?: boolean;
refreshToken?: boolean;
};
}, options?: {
clientSecret?: string;
refreshAppAccessToken?: boolean;
refreshUserAccessToken?: boolean;
});
/**
* Initializes the handler with the provided information inside the constructor
*/
init(): Promise<void>;
/**
* Stops the User Access Token refresh interval, if it is running
*/
stopUserTokenRefresh(): void;
/**
* Starts the User Access Token refresh interval or restarts it, if it is already running
*/
startUserTokenRefresh(): Promise<void>;
/**
* Stops the App Access Token refresh interval, if it is running
*/
stopAppTokenRefresh(): void;
/**
* Starts the App Access Token refresh interval or restarts it, if it is already running
*/
startAppTokenRefresh(): Promise<void>;
/**
* Renews/refreshes the User Access Token once (with the initial/current internal client secret)
*/
renewAppAccessToken(): Promise<void>;
/**
* Renews/refreshes the User Access Token once (with the initial/current internal token information)
*/
renewUserAccessToken(): Promise<void>;
get appAccessToken(): string | undefined;

@@ -32,7 +66,18 @@ get userAccessToken(): string | undefined;

get refreshToken(): string | undefined;
set userToken(token: string);
set appAccessToken(token: string | undefined);
set userAccessToken(token: string | undefined);
set refreshToken(token: string | undefined);
set clientSecret(secret: string | undefined);
/**
* Refreshes the App Access Token once or starts the refresh interval after the first renewal/refresh
* @param refreshOnce A boolean that determines whether the App Access token should only be renewed/refreshed once
* @private
*/
private refreshAppAccessToken;
/**
* Refreshes the User Access Token once or starts the refresh interval after the first renewal/refresh
* @param refreshOnce A boolean that determines whether the User Access token should only be renewed/refreshed once
* @private
*/
private refreshUserAccessToken;
private writeOutFile;
}

174

build/token-handler.js

@@ -9,34 +9,96 @@ "use strict";

const zod_1 = require("zod");
const fs_1 = require("fs");
const utils_1 = require("./utils");
class TokenHandler {
constructor(config) {
config = constructorValidator.parse(config);
this._clientId = config.clientId;
this._clientSecret = config.clientSecret;
this._refreshToken = config.refreshToken;
if (!(0, utils_1.isUndefined)(config.writeOut))
this._writeOut = config.writeOut;
/**
* Tokenhandler that makes token management easy
* @param clientId The client identifier of the application that should be used for interaction with the Twitch API
* @param [tokens] Possible tokens at initialization
* @param [tokens.userAccessToken] An already existing User Access Token
* @param [tokens.appAccessToken] An already existing App Access Token
* @param [tokens.refreshToken] A refresh token for auto User Access Token renewal/refresh
* @param [options] Additional options
* @param [options.clientSecret] The applications secrets for auto App Access Token renewal/refresh
* @param [options.refreshAppAccessToken] A Boolean that determines whether the App Access Token should be refreshed automatically
* @param [options.clientSecret] A Boolean that determines whether the User Access Token should be refreshed automatically
*/
constructor(clientId, tokens, options) {
this._clientId = zod_1.z.string().min(10).parse(clientId);
if ((0, utils_1.isUndefined)(tokens?.appAccessToken, options?.clientSecret, tokens?.userAccessToken, tokens?.refreshToken)) {
console.error("You did not provide any token or the client secret! You wont be able to make any API calls except those that require a JWT (see Extensions calls)");
}
else {
if ((0, utils_1.isUndefined)(tokens?.appAccessToken, options?.clientSecret))
console.info("No App Access Token or client secret provided. You will not be able to make API calls that only accept App Access Tokens!");
if ((0, utils_1.isUndefined)(tokens?.userAccessToken, tokens?.refreshToken))
console.info("No User Access Token or Refresh Token provided. You will not be able to make API calls that only accept User Access Tokens!");
}
this._clientSecret = options?.clientSecret;
this._refreshToken = tokens?.refreshToken;
this._userAccessToken = tokens?.userAccessToken;
this._appAccessToken = tokens?.appAccessToken;
this._initialUserAccessTokenRefresh = (0, utils_1.isDefined)(tokens?.refreshToken) && ((0, utils_1.isDefined)(options?.refreshUserAccessToken) && options.refreshUserAccessToken);
this._initialAppAccessTokenRefresh = (0, utils_1.isDefined)(options?.clientSecret) && ((0, utils_1.isDefined)(options?.refreshAppAccessToken) && options.refreshAppAccessToken);
}
/**
* Initializes the handler with the provided information inside the constructor
*/
async init() {
await this.refreshAppAccessToken();
await this.refreshUserAccessToken();
if (this._initialAppAccessTokenRefresh)
await this.refreshAppAccessToken();
if (this._initialUserAccessTokenRefresh)
await this.refreshUserAccessToken();
}
// #region API
//#region API
/**
* Stops the User Access Token refresh interval, if it is running
*/
stopUserTokenRefresh() {
clearInterval(this._refreshTokenInterval);
if ((0, utils_1.isDefined)(this._refreshTokenInterval))
clearInterval(this._refreshTokenInterval);
}
/**
* Starts the User Access Token refresh interval or restarts it, if it is already running
*/
async startUserTokenRefresh() {
if (!(0, utils_1.isUndefined)(this._refreshTokenInterval))
if ((0, utils_1.isDefined)(this._refreshTokenInterval))
clearInterval(this._refreshTokenInterval);
await this.refreshUserAccessToken();
if ((0, utils_1.isDefined)(this._refreshToken)) {
await this.refreshUserAccessToken();
}
else {
console.error("Failed to start User Access Token refresh interval due to missing refresh token!");
}
}
/**
* Stops the App Access Token refresh interval, if it is running
*/
stopAppTokenRefresh() {
clearInterval(this._appAccessTokenInterval);
if ((0, utils_1.isDefined)(this._appAccessTokenInterval))
clearInterval(this._appAccessTokenInterval);
}
/**
* Starts the App Access Token refresh interval or restarts it, if it is already running
*/
async startAppTokenRefresh() {
if (!(0, utils_1.isUndefined)(this._appAccessTokenInterval))
if ((0, utils_1.isDefined)(this._appAccessTokenInterval))
clearInterval(this._appAccessTokenInterval);
await this.refreshAppAccessToken();
if ((0, utils_1.isDefined)(this._clientSecret)) {
await this.refreshAppAccessToken();
}
else {
console.error("Failed to start App Access Token refresh interval due to missing client secret!");
}
}
/**
* Renews/refreshes the User Access Token once (with the initial/current internal client secret)
*/
async renewAppAccessToken() {
await this.refreshAppAccessToken(true);
}
/**
* Renews/refreshes the User Access Token once (with the initial/current internal token information)
*/
async renewUserAccessToken() {
await this.refreshUserAccessToken(true);
}
get appAccessToken() {

@@ -57,3 +119,6 @@ return this._appAccessToken;

}
set userToken(token) {
set appAccessToken(token) {
this._appAccessToken = token;
}
set userAccessToken(token) {
this._userAccessToken = token;

@@ -63,16 +128,38 @@ }

if (!(0, utils_1.isUndefined)(token)) {
if (!(0, utils_1.isUndefined)(this._refreshTokenInterval))
clearInterval(this._refreshTokenInterval);
this.stopUserTokenRefresh();
this._refreshToken = undefined;
return;
}
const tokenWasSet = (0, utils_1.isUndefined)(this._refreshToken);
//If refresh interval was already running, restart interval
const tokenWasSet = (0, utils_1.isDefined)(this._refreshToken);
this._refreshToken = token;
if (!tokenWasSet) {
if (tokenWasSet) {
this.refreshUserAccessToken();
}
}
// #endregion
async refreshAppAccessToken() {
set clientSecret(secret) {
if (!(0, utils_1.isUndefined)(secret)) {
this.stopAppTokenRefresh();
this._clientSecret = undefined;
return;
}
//If refresh interval was already running, restart interval
const secretWasSet = (0, utils_1.isDefined)(this._clientSecret);
this._clientSecret = secret;
if (secretWasSet) {
this.refreshAppAccessToken();
}
}
//endregion
/**
* Refreshes the App Access Token once or starts the refresh interval after the first renewal/refresh
* @param refreshOnce A boolean that determines whether the App Access token should only be renewed/refreshed once
* @private
*/
async refreshAppAccessToken(refreshOnce) {
try {
if ((0, utils_1.isUndefined)(this._clientSecret)) {
console.error("Unable to start App Access token refresh interval due to missing client secret!");
return;
}
const response = await axios_1.default.post("https://id.twitch.tv/oauth2/token", {

@@ -84,4 +171,4 @@ client_id: this._clientId,

this._appAccessToken = response.data.access_token;
console.log(`New app access token: ${this.appAccessToken}`);
this.writeOutFile();
if ((0, utils_1.isDefined)(refreshOnce))
return;
if (this._appAccessTokenInterval)

@@ -95,5 +182,11 @@ clearInterval(this._appAccessTokenInterval);

}
async refreshUserAccessToken() {
/**
* Refreshes the User Access Token once or starts the refresh interval after the first renewal/refresh
* @param refreshOnce A boolean that determines whether the User Access token should only be renewed/refreshed once
* @private
*/
async refreshUserAccessToken(refreshOnce) {
try {
if ((0, utils_1.isUndefined)(this._refreshToken)) {
console.error("Unable to start User Access token refresh interval due to missing refresh token!");
return;

@@ -108,3 +201,4 @@ }

this._userAccessToken = response.data.access_token;
this.writeOutFile();
if ((0, utils_1.isDefined)(refreshOnce))
return;
const expiresIn = response.data.expires_in * 1000;

@@ -119,27 +213,3 @@ if (this._refreshTokenInterval)

}
writeOutFile() {
if (this._writeOut != null) {
const writeOutTokens = {};
if (this._writeOut.appToken === true)
writeOutTokens.appAccessToken = this._appAccessToken;
if (this._writeOut.userToken === true)
writeOutTokens.userAccessToken = this._appAccessToken;
if (this._writeOut.refreshToken === true)
writeOutTokens.refreshToken = this._appAccessToken;
(0, fs_1.writeFileSync)(this._writeOut.path, JSON.stringify(writeOutTokens, null, 4));
}
}
}
exports.TokenHandler = TokenHandler;
const constructorValidator = zod_1.z.object({
//Length of 20 is the lowest ive found
clientId: zod_1.z.string().min(20),
clientSecret: zod_1.z.string().min(20),
refreshToken: zod_1.z.string().min(20).optional(),
writeOut: zod_1.z.object({
path: zod_1.z.string().min(2),
userToken: zod_1.z.boolean().optional(),
appToken: zod_1.z.boolean().optional(),
refreshToken: zod_1.z.boolean().optional()
}).optional()
});
{
"name": "twitch-api-typescript",
"version": "0.5.4",
"version": "0.5.5",
"description": "Fully typed TwitchAPI wrapper that makes working with TwitchAPI as easy as possible",

@@ -9,3 +9,4 @@ "main": "build/index.js",

"scripts": {
"dev": "npx nodemon src/index.ts",
"dev": "npx nodemon src/Testground.ts",
"test": "npx mocha -r ts-node/register 'tests/**/*.ts'",
"prepublish": "rm -R build/* && tsc",

@@ -33,2 +34,6 @@ "npmpublish": "npm run prepublish && npm publish"

"@tsconfig/node16": "^1.0.3",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"chai": "^4.3.7",
"mocha": "^10.2.0",
"nodemon": "^2.0.20",

@@ -35,0 +40,0 @@ "ts-node": "^10.9.1",

@@ -17,30 +17,83 @@ # TwitchTS

#### ❗IMPORTANT❗: In the current version, TwitchTS automatically updates the UserAccessToken and AppAccessToken used. Therefore, it is necessary to specify the client secret (for AppAccessToken) and refresh token (for UserAccessToken). The necessity will be made optional in the next version: one-time specification of the tokens will be allowed and an interface will be provided to refresh them externally afterwards.
### Tokenmanagement
TwitchTS always tries to use the UserAccessToken first for API calls. If this is not defined, the AppAccessToken is used.
So if you know that you don't use any functionality that requires a UserAccessToken, then it is sufficient to specify only the client id and the client secret.
TwitchTS always tries to use the user access token first for API calls that can be called using the app access token or user access token. So if you know that you only make calls where the app access token is enough, then it is enough to just specify that.
##### AppAccessToken only:
During initialization, several pieces of optional information can be specified for token management:
1. user access token
* If this token was specified at initialization, it will be used for all API calls where a user token is required (or for those where a simple app access token is required)
2. app access token
* If this token was specified at initialization, it will be used for all API calls where an app access token is required.
3. application secret
* If the client secret is specified during initialization, then TwitchTS will automatically renew the app access token. It is necessary to specify an app access token if a client secret is specified
4. refresh token
* If the refresh token is specified during initialization, then TwitchTS will automatically renew the user access token (about 1 hour before expiration). It is necessary to specify a user access token when a refresh token is specified
<br/>
#### The following are a few examples of how the individual tokens/secret can be specified
###### User access token & app access token:
```typescript
const apiClient = new TwitchAPI({
clientId: "{YOUR-CLIENT-ID}",
clientSecret: "{YOUR-CLIENT-SECRET}",
});
tokens:{
userToken: "{YOUR-USER-ACCESS-TOKEN}",
appToken: "{YOUR-APP-ACCESS-TOKEN}"
}
})
await apiClient.init();
```
<br/>
apiClient.init();
###### Refresh token only:
```typescript
const apiClient = new TwitchAPI({
clientId: "{YOUR-CLIENT-ID}",
tokens:{
refreshToken: "{YOUR-REFRESH-TOKEN}"
}
})
await apiClient.init();
```
##### UserAccessToken with AppAccessToken as a fallback:
###### Client secret only:
```typescript
const apiClient = new TwitchAPI({
clientId: "{YOUR-CLIENT-ID}",
clientSecret: "{YOUR-CLIENT-SECRET}",
refreshToken: "{YOUR-REFRESH_TOKEN}"
});
apiClient.init();
clientSecret: "{YOUR-CLIENT-SECRET}"
})
await apiClient.init();
```
<br/><br/>
At runtime, all settings related to tokens can be changed. TwitchTS offers a public interface for this purpose:
```typescript
const apiClient = new TwitchAPI({
clientId: "{YOUR-CLIENT-ID}",
...
})
//Setting tokens/secret to a new value directly.
apiClient._tokenHandler.userAccessToken = "{NEW-USER-ACCESS-TOKEN}"
apiClient._tokenHandler.appAccessToken = "{NEW-APP-ACCESS-TOKEN}"
apiClient._tokenHandler.refreshToken = "{NEW-REFRESH-TOKEN}"
apiClient._tokenHandler.clientSecret = "{NEW-CLIENT-SECRET}"
//Manually trigger a refresh of the respective token
await apiClient._tokenHandler.renewAppAccessToken();
await apiClient._tokenHandler.renewUserAccessToken();
//Stop the refresh interval of the respective token refresh routine
await apiClient._tokenHandler.stopAppTokenRefresh();
await apiClient._tokenHandler.stopUserTokenRefresh();
//Start/Restart the refresh interval of the respective token refresh routine
await apiClient._tokenHandler.startAppTokenRefresh();
await apiClient._tokenHandler.stopUserTokenRefresh();
```
### 📝 NOT IMPLEMENTED YET

@@ -62,4 +115,4 @@ - Update Drops Entitlements

- [ ] Finish code documentation
- [ ] Make refresh token & user secret optional at initialization
- [ ] Custom exceptions
- [x] Make refresh token & user secret optional at initialization
- [x] Custom exceptions
- [ ] User input validation with ZOD

@@ -66,0 +119,0 @@ - [ ] Proper tests

import axios from "axios";
import {z as zod} from "zod";
import {writeFileSync} from "fs";
import { isUndefined } from "./utils";
import {isDefined, isUndefined} from "./utils";

@@ -9,3 +8,3 @@ export class TokenHandler{

private readonly _clientId: string
private readonly _clientSecret: string
private _clientSecret?: string

@@ -20,44 +19,116 @@ private _appAccessToken?: string;

private readonly _writeOut?: {path: string, userToken?: boolean, appToken?: boolean, refreshToken?: boolean};
private readonly _initialUserAccessTokenRefresh: boolean;
private readonly _initialAppAccessTokenRefresh: boolean;
constructor(config: {clientId: string, clientSecret: string, refreshToken?: string, writeOut?: {path: string, userToken?: boolean, appToken?: boolean, refreshToken?: boolean}}){
config = constructorValidator.parse(config)
/**
* Tokenhandler that makes token management easy
* @param clientId The client identifier of the application that should be used for interaction with the Twitch API
* @param [tokens] Possible tokens at initialization
* @param [tokens.userAccessToken] An already existing User Access Token
* @param [tokens.appAccessToken] An already existing App Access Token
* @param [tokens.refreshToken] A refresh token for auto User Access Token renewal/refresh
* @param [options] Additional options
* @param [options.clientSecret] The applications secrets for auto App Access Token renewal/refresh
* @param [options.refreshAppAccessToken] A Boolean that determines whether the App Access Token should be refreshed automatically
* @param [options.clientSecret] A Boolean that determines whether the User Access Token should be refreshed automatically
*/
constructor(clientId: string, tokens?: {userAccessToken?: string, appAccessToken?: string, refreshToken?: string}, options?: {clientSecret?: string, refreshAppAccessToken?: boolean, refreshUserAccessToken?: boolean}){
this._clientId = zod.string().min(10).parse(clientId)
this._clientId = config.clientId
this._clientSecret = config.clientSecret
this._refreshToken = config.refreshToken
if(isUndefined(tokens?.appAccessToken, options?.clientSecret, tokens?.userAccessToken, tokens?.refreshToken)){
console.error("You did not provide any token or the client secret! You wont be able to make any API calls except those that require a JWT (see Extensions calls)");
}else{
if(isUndefined(tokens?.appAccessToken, options?.clientSecret))
console.info("No App Access Token or client secret provided. You will not be able to make API calls that only accept App Access Tokens!");
if(!isUndefined(config.writeOut))
this._writeOut = config.writeOut
if(isUndefined(tokens?.userAccessToken, tokens?.refreshToken))
console.info("No User Access Token or Refresh Token provided. You will not be able to make API calls that only accept User Access Tokens!");
}
this._clientSecret = options?.clientSecret;
this._refreshToken = tokens?.refreshToken;
this._userAccessToken = tokens?.userAccessToken;
this._appAccessToken = tokens?.appAccessToken;
this._initialUserAccessTokenRefresh = isDefined(tokens?.refreshToken) && (isDefined(options?.refreshUserAccessToken) && options!.refreshUserAccessToken!)
this._initialAppAccessTokenRefresh = isDefined(options?.clientSecret) && (isDefined(options?.refreshAppAccessToken) && options!.refreshAppAccessToken!)
}
/**
* Initializes the handler with the provided information inside the constructor
*/
public async init(){
public async init(){
await this.refreshAppAccessToken();
await this.refreshUserAccessToken();
if(this._initialAppAccessTokenRefresh)
await this.refreshAppAccessToken();
if(this._initialUserAccessTokenRefresh)
await this.refreshUserAccessToken();
}
// #region API
//#region API
/**
* Stops the User Access Token refresh interval, if it is running
*/
public stopUserTokenRefresh(){
clearInterval(this._refreshTokenInterval);
if(isDefined(this._refreshTokenInterval))
clearInterval(this._refreshTokenInterval)
}
/**
* Starts the User Access Token refresh interval or restarts it, if it is already running
*/
public async startUserTokenRefresh(){
if(!isUndefined(this._refreshTokenInterval))
if(isDefined(this._refreshTokenInterval))
clearInterval(this._refreshTokenInterval)
await this.refreshUserAccessToken();
if(isDefined(this._refreshToken)){
await this.refreshUserAccessToken();
}else{
console.error("Failed to start User Access Token refresh interval due to missing refresh token!")
}
}
/**
* Stops the App Access Token refresh interval, if it is running
*/
public stopAppTokenRefresh(){
clearInterval(this._appAccessTokenInterval);
if(isDefined(this._appAccessTokenInterval))
clearInterval(this._appAccessTokenInterval)
}
/**
* Starts the App Access Token refresh interval or restarts it, if it is already running
*/
public async startAppTokenRefresh(){
if(!isUndefined(this._appAccessTokenInterval))
if(isDefined(this._appAccessTokenInterval))
clearInterval(this._appAccessTokenInterval)
await this.refreshAppAccessToken();
if(isDefined(this._clientSecret)){
await this.refreshAppAccessToken();
}else{
console.error("Failed to start App Access Token refresh interval due to missing client secret!")
}
}
/**
* Renews/refreshes the User Access Token once (with the initial/current internal client secret)
*/
public async renewAppAccessToken(){
await this.refreshAppAccessToken(true);
}
/**
* Renews/refreshes the User Access Token once (with the initial/current internal token information)
*/
public async renewUserAccessToken(){
await this.refreshUserAccessToken(true);
}
get appAccessToken(): string | undefined{

@@ -76,3 +147,3 @@ return this._appAccessToken;

get clientSecret(): string{
return this._clientSecret;
return <string>this._clientSecret;
}

@@ -84,3 +155,7 @@

set userToken(token: string){
set appAccessToken(token: string | undefined){
this._appAccessToken = token;
}
set userAccessToken(token: string | undefined){
this._userAccessToken = token;

@@ -92,4 +167,3 @@ }

if(!isUndefined(token)){
if(!isUndefined(this._refreshTokenInterval))
clearInterval(this._refreshTokenInterval)
this.stopUserTokenRefresh()
this._refreshToken = undefined;

@@ -99,16 +173,44 @@ return;

//If refresh interval was already running, restart interval
const tokenWasSet = isDefined(this._refreshToken);
const tokenWasSet = isUndefined(this._refreshToken);
this._refreshToken = token;
if(!tokenWasSet){
if(tokenWasSet){
this.refreshUserAccessToken();
}
}
// #endregion
set clientSecret(secret: string | undefined){
private async refreshAppAccessToken(){
if(!isUndefined(secret)){
this.stopAppTokenRefresh()
this._clientSecret = undefined;
return;
}
//If refresh interval was already running, restart interval
const secretWasSet = isDefined(this._clientSecret);
this._clientSecret = secret;
if(secretWasSet){
this.refreshAppAccessToken();
}
}
//endregion
/**
* Refreshes the App Access Token once or starts the refresh interval after the first renewal/refresh
* @param refreshOnce A boolean that determines whether the App Access token should only be renewed/refreshed once
* @private
*/
private async refreshAppAccessToken(refreshOnce?: boolean){
try{
if(isUndefined(this._clientSecret)){
console.error("Unable to start App Access token refresh interval due to missing client secret!")
return;
}
const response = await axios.post("https://id.twitch.tv/oauth2/token",{

@@ -121,7 +223,6 @@ client_id: this._clientId,

this._appAccessToken = response.data.access_token;
console.log(`New app access token: ${this.appAccessToken}`)
this.writeOutFile();
if(isDefined(refreshOnce))
return;
if(this._appAccessTokenInterval)

@@ -136,5 +237,11 @@ clearInterval(this._appAccessTokenInterval);

private async refreshUserAccessToken(){
/**
* Refreshes the User Access Token once or starts the refresh interval after the first renewal/refresh
* @param refreshOnce A boolean that determines whether the User Access token should only be renewed/refreshed once
* @private
*/
private async refreshUserAccessToken(refreshOnce?: boolean){
try{
if(isUndefined(this._refreshToken)){
console.error("Unable to start User Access token refresh interval due to missing refresh token!")
return;

@@ -152,6 +259,8 @@ }

this.writeOutFile();
if(isDefined(refreshOnce))
return;
const expiresIn = response.data.expires_in * 1000;
if(this._refreshTokenInterval)

@@ -166,33 +275,5 @@ clearInterval(this._refreshTokenInterval);

private writeOutFile(){
if(this._writeOut != null){
const writeOutTokens: {appAccessToken?: string, userAccessToken?: string, refreshToken?: string} = {}
if(this._writeOut.appToken === true)
writeOutTokens.appAccessToken = this._appAccessToken;
if(this._writeOut.userToken === true)
writeOutTokens.userAccessToken = this._appAccessToken;
if(this._writeOut.refreshToken === true)
writeOutTokens.refreshToken = this._appAccessToken;
writeFileSync(this._writeOut.path, JSON.stringify(writeOutTokens, null, 4));
}
}
}
const constructorValidator = zod.object({
//Length of 20 is the lowest ive found
clientId: zod.string().min(20),
clientSecret: zod.string().min(20),
refreshToken: zod.string().min(20).optional(),
writeOut: zod.object({
path: zod.string().min(2),
userToken: zod.boolean().optional(),
appToken: zod.boolean().optional(),
refreshToken: zod.boolean().optional()
}).optional()
})

@@ -7,3 +7,4 @@ {

"skipLibCheck": true,
"declaration": true
"declaration": true,
"forceConsistentCasingInFileNames": false
},

@@ -10,0 +11,0 @@ "files": [

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc