Skip to content

Commit

Permalink
removes all arrow functions in recipe as per supertokens/supertokens-…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh committed Oct 28, 2021
1 parent d74fbb2 commit da9865b
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 119 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.0.1] - 2021-10-28

### Changes
- Uses non arrow functions in api and recipe interface impl to allow for "true" inheritance in override: https://github.com/supertokens/supertokens-node/issues/199
- Uses `bind(this)` when calling original implementation

## [3.0.0] - 2021-10-22
### Breaking changes
- `getJWTPayloadSecurely` has been renamed to `getAccessTokenPayloadSecurely` to be more accurate to the functionality
Expand Down
5 changes: 2 additions & 3 deletions lib/build/fetch.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/fetch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 2 additions & 9 deletions lib/build/recipeImplementation.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 22 additions & 17 deletions lib/build/recipeImplementation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/version.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/ts/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { supported_fdi } from "./version";
import AntiCSRF from "./antiCsrf";
import IdRefreshToken from "./idRefreshToken";
import getLock from "./locking";
import { IdRefreshTokenType, InputType, NormalisedInputType } from "./types";
import { IdRefreshTokenType, InputType, NormalisedInputType, RecipeInterface } from "./types";
import { shouldDoInterceptionBasedOnUrl, validateAndNormaliseInputOrThrowError } from "./utils";
import FrontToken from "./frontToken";
import RecipeImplementation from "./recipeImplementation";
Expand All @@ -35,7 +35,7 @@ export default class AuthHttpRequest {
static initCalled = false;
static rid: string;
static env: any;
static recipeImpl: RecipeImplementation;
static recipeImpl: RecipeInterface;
static config: NormalisedInputType;

static init(options: InputType) {
Expand All @@ -53,7 +53,7 @@ export default class AuthHttpRequest {
// even if the init function is called more than once (maybe across JS scripts),
// things will not get created multiple times.
AuthHttpRequest.env.__supertokensOriginalFetch = AuthHttpRequest.env.fetch.bind(AuthHttpRequest.env);
AuthHttpRequest.env.__supertokensSessionRecipe = config.override.functions(new RecipeImplementation());
AuthHttpRequest.env.__supertokensSessionRecipe = config.override.functions(RecipeImplementation());
AuthHttpRequest.env.fetch = AuthHttpRequest.env.__supertokensSessionRecipe.addFetchInterceptorsAndReturnModifiedFetch(
AuthHttpRequest.env.__supertokensOriginalFetch,
config
Expand Down
164 changes: 83 additions & 81 deletions lib/ts/recipeImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,101 +5,103 @@ import { supported_fdi } from "./version";
import IdRefreshToken from "./idRefreshToken";
import { interceptorFunctionRequestFulfilled, responseErrorInterceptor, responseInterceptor } from "./axios";

export default class RecipeImplementation implements RecipeInterface {
addFetchInterceptorsAndReturnModifiedFetch = (originalFetch: any, _: NormalisedInputType): typeof fetch => {
return async (url: RequestInfo, config?: RequestInit): Promise<Response> => {
return await AuthHttpRequest.doRequest(
(config?: RequestInit) => {
return originalFetch(url, {
...config
});
},
config,
url
);
};
};
export default function RecipeImplementation(): RecipeInterface {
return {
addFetchInterceptorsAndReturnModifiedFetch: function(originalFetch: any, _: NormalisedInputType): typeof fetch {
return async function(url: RequestInfo, config?: RequestInit): Promise<Response> {
return await AuthHttpRequest.doRequest(
(config?: RequestInit) => {
return originalFetch(url, {
...config
});
},
config,
url
);
};
},

addAxiosInterceptors = (axiosInstance: any, _: NormalisedInputType): void => {
// we first check if this axiosInstance already has our interceptors.
let requestInterceptors = axiosInstance.interceptors.request;
for (let i = 0; i < requestInterceptors.handlers.length; i++) {
if (requestInterceptors.handlers[i].fulfilled === interceptorFunctionRequestFulfilled) {
return;
addAxiosInterceptors: function(axiosInstance: any, _: NormalisedInputType): void {
// we first check if this axiosInstance already has our interceptors.
let requestInterceptors = axiosInstance.interceptors.request;
for (let i = 0; i < requestInterceptors.handlers.length; i++) {
if (requestInterceptors.handlers[i].fulfilled === interceptorFunctionRequestFulfilled) {
return;
}
}
}
// Add a request interceptor
axiosInstance.interceptors.request.use(interceptorFunctionRequestFulfilled, async function(error: any) {
throw error;
});
// Add a request interceptor
axiosInstance.interceptors.request.use(interceptorFunctionRequestFulfilled, async function(error: any) {
throw error;
});

// Add a response interceptor
axiosInstance.interceptors.response.use(
responseInterceptor(axiosInstance),
responseErrorInterceptor(axiosInstance)
);
};
// Add a response interceptor
axiosInstance.interceptors.response.use(
responseInterceptor(axiosInstance),
responseErrorInterceptor(axiosInstance)
);
},

getUserId = async (config: NormalisedInputType): Promise<string> => {
let tokenInfo = await FrontToken.getTokenInfo();
if (tokenInfo === undefined) {
throw new Error("No session exists");
}
return tokenInfo.uid;
};
getUserId: async function(config: NormalisedInputType): Promise<string> {
let tokenInfo = await FrontToken.getTokenInfo();
if (tokenInfo === undefined) {
throw new Error("No session exists");
}
return tokenInfo.uid;
},

getAccessTokenPayloadSecurely = async (config: NormalisedInputType): Promise<any> => {
let tokenInfo = await FrontToken.getTokenInfo();
if (tokenInfo === undefined) {
throw new Error("No session exists");
}
getAccessTokenPayloadSecurely: async function(config: NormalisedInputType): Promise<any> {
let tokenInfo = await FrontToken.getTokenInfo();
if (tokenInfo === undefined) {
throw new Error("No session exists");
}

if (tokenInfo.ate < Date.now()) {
let retry = await AuthHttpRequest.attemptRefreshingSession();
if (retry) {
return await this.getAccessTokenPayloadSecurely(config);
} else {
throw new Error("Could not refresh session");
if (tokenInfo.ate < Date.now()) {
let retry = await AuthHttpRequest.attemptRefreshingSession();
if (retry) {
return await this.getAccessTokenPayloadSecurely(config);
} else {
throw new Error("Could not refresh session");
}
}
}
return tokenInfo.up;
};
return tokenInfo.up;
},

doesSessionExist = async (config: NormalisedInputType): Promise<boolean> => {
return (await IdRefreshToken.getIdRefreshToken(true)).status === "EXISTS";
};
doesSessionExist: async function(config: NormalisedInputType): Promise<boolean> {
return (await IdRefreshToken.getIdRefreshToken(true)).status === "EXISTS";
},

signOut = async (config: NormalisedInputType): Promise<void> => {
if (!(await this.doesSessionExist(config))) {
config.onHandleEvent({
action: "SIGN_OUT"
signOut: async function(config: NormalisedInputType): Promise<void> {
if (!(await this.doesSessionExist(config))) {
config.onHandleEvent({
action: "SIGN_OUT"
});
return;
}

let preAPIResult = await config.preAPIHook({
action: "SIGN_OUT",
requestInit: {
method: "post",
headers: {
"fdi-version": supported_fdi.join(","),
rid: AuthHttpRequest.rid
}
},
url: AuthHttpRequest.signOutUrl
});
return;
}

let preAPIResult = await config.preAPIHook({
action: "SIGN_OUT",
requestInit: {
method: "post",
headers: {
"fdi-version": supported_fdi.join(","),
rid: AuthHttpRequest.rid
}
},
url: AuthHttpRequest.signOutUrl
});
let resp = await fetch(preAPIResult.url, preAPIResult.requestInit);

let resp = await fetch(preAPIResult.url, preAPIResult.requestInit);
if (resp.status === config.sessionExpiredStatusCode) {
// refresh must have already sent session expiry event
return;
}

if (resp.status === config.sessionExpiredStatusCode) {
// refresh must have already sent session expiry event
return;
}
if (resp.status >= 300) {
throw resp;
}

if (resp.status >= 300) {
throw resp;
// we do not send an event here since it's triggered in setIdRefreshToken area.
}

// we do not send an event here since it's triggered in setIdRefreshToken area.
};
}
2 changes: 1 addition & 1 deletion lib/ts/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
export const package_version = "3.0.0";
export const package_version = "3.0.1";

export const supported_fdi = ["1.8", "1.9"];
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "supertokens-react-native",
"version": "3.0.0",
"version": "3.0.1",
"description": "React Native SDK for SuperTokens",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit da9865b

Please sign in to comment.