Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ikepu-tp committed Sep 26, 2023
1 parent d5b805e commit 3473378
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ikepu-tp/react-mvc",
"type": "module",
"version": "0.8.0",
"version": "0.8.1",
"description": "React MVC is a library for supporting MVC-like implementations in JavaScript projects.",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
38 changes: 11 additions & 27 deletions src/Send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default class Send<defaultResponse = SuccessOrFailedResponseResource> {
Accept: 'application/json',
'Content-Type': 'application/json',
};
protected count: number = 0;
protected sendProps: SendProps | undefined;
protected responseHeader: Headers | undefined;
protected responseBody: any;
Expand All @@ -64,49 +63,35 @@ export default class Send<defaultResponse = SuccessOrFailedResponseResource> {
this.default_headers = { ...{}, ...headers };
}

protected countUp(): void {
++this.count;
}

protected countDown(): void {
--this.count;
}

protected resetCount(): void {
this.count = 0;
}

protected beforeSend(): void {
this.default_headers['X-XSRF-TOKEN'] = Function.cookie('XSRF-TOKEN', '');
this.default_headers['X-NONCE'] = Function.createKey();
}

protected afterSend(): void {
if (this.responseBody === null) {
this.resetCount();
return;
}
if (!this.checkNonce()) this.unexpectedResponse();
this.resetCount();
protected afterSend(next: boolean = false): void {
if (this.responseBody === null) return;
if (!this.checkNonce()) this.unexpectedResponse(next);
}

protected checkNonce(): boolean {
if (!this.responseHeader) return false;
return this.responseHeader.get('Request-Nonce') === this.default_headers['X-NONCE'];
}

protected async unexpectedResponse(): Promise<void> {
if (this.count > 1) throw new Error('Unexpected response.');
protected async unexpectedResponse(next: boolean = false): Promise<void> {
if (!this.sendProps) throw new Error('Unexpected response and failed');
await this.send(this.sendProps);
if (next) await this.send(this.sendProps, false);
throw new Error('Unexpected response.');
}

protected setSendProps<Param = ParamType>(props: SendProps<Param>): void {
this.sendProps = { ...{}, ...props } as SendProps;
}

public async send<R = defaultResponse, Param = ParamType>(props: SendProps<Param>): Promise<R | null> {
this.countUp();
public async send<R = defaultResponse, Param = ParamType>(
props: SendProps<Param>,
next: boolean = true
): Promise<R | null> {
this.setSendProps<Param>(props);

this.beforeSend();
Expand All @@ -127,8 +112,7 @@ export default class Send<defaultResponse = SuccessOrFailedResponseResource> {

if (_response.status === 204) {
this.responseBody = null;
this.afterSend();
this.resetCount();
this.afterSend(next);
return null;
}

Expand Down

0 comments on commit 3473378

Please sign in to comment.