-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
99 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
const axios = require('axios') | ||
const MockAdapter = require('axios-mock-adapter') | ||
|
||
// All requests using this instance will have a 2 seconds delay: | ||
// All requests using this instance will have a 2 seconds delay. | ||
const mock = new MockAdapter(axios, { delayResponse: 2000 }) | ||
|
||
// Mock GET request to /users when param `searchText` is 'John' | ||
// arguments for reply are (status, data, headers) | ||
mock.onGet('/api/getDemo').reply(200, { | ||
message: '/api/getDemo success!' | ||
}) | ||
|
||
mock.onGet('/api/delay').reply(200, { | ||
message: 'delay message' | ||
mock.onGet('/api/cancel-request-test').reply(200, { | ||
message: '/api/cancel-request-test SUCCESS' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
import Axios, { AxiosRequestConfig, Canceler } from 'axios' | ||
|
||
export const CancelToken = Axios.CancelToken | ||
export const pendingRequestList: PendingRequest[] = [] | ||
|
||
/** | ||
* Pending Request | ||
* @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com | ||
* @date 1/6/20 2:26 PM | ||
*/ | ||
export class PendingRequest { | ||
/** | ||
* Request token. | ||
* | ||
* Format: [URL]::[HTTP Method]::[Stringified Request Params] | ||
* | ||
* Sample: /api/cancel-request-test::get::undefined | ||
*/ | ||
requestToken!: string | ||
|
||
/** | ||
* Cancel executor | ||
*/ | ||
cancelExecutor!: Canceler | ||
|
||
constructor (requestToken: string, cancelExecutor: Canceler) { | ||
this.requestToken = requestToken | ||
this.cancelExecutor = cancelExecutor | ||
} | ||
} | ||
|
||
export const cancelAndRemoveSamePendingRequest = (axiosRequestConfig: AxiosRequestConfig): void => { | ||
pendingRequestList.forEach((pendingRequest, index) => { | ||
const requestToken = `${axiosRequestConfig?.url?.split('?')[0]}::${axiosRequestConfig.method}::${JSON.stringify(axiosRequestConfig.params)}` | ||
if (pendingRequest.requestToken === requestToken) { | ||
// Execute cancellation of this pending request. | ||
pendingRequest.cancelExecutor(`Cancelled Axios request. Request token: ${requestToken}`) | ||
// Remove this pending request from list. | ||
pendingRequestList.splice(index, 1) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import * as Axios from '@/plugins/axios' | ||
// eslint-disable-next-line no-unused-vars | ||
/* eslint-disable no-unused-vars */ | ||
import { GetDemoPayload } from '@/requests/vuetify-demo/payload/get-demo-payload' | ||
import { CancelRequestTestPayload } from '@/requests/vuetify-demo/payload/cancel-request-test-payload' | ||
|
||
export const vuetifyDemoApi = { | ||
getDemo: (getDemoPayload: GetDemoPayload) => Axios.get('/api/getDemo', getDemoPayload), | ||
delay: (delayTime: number) => Axios.get('/api/delay', delayTime) | ||
cancelRequestTest: (cancelRequestTestPayload: CancelRequestTestPayload) => Axios.get('/api/cancel-request-test', cancelRequestTestPayload) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/requests/vuetify-demo/payload/cancel-request-test-payload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Allow } from 'class-validator' | ||
|
||
export class CancelRequestTestPayload { | ||
/** | ||
* ID. | ||
*/ | ||
@Allow() | ||
id!: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters