-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Axios Http into generated Vue Webapp
- Loading branch information
1 parent
6b535a1
commit 03970d4
Showing
11 changed files
with
251 additions
and
2 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
118 changes: 118 additions & 0 deletions
118
src/main/resources/generator/client/vue/test/spec/http/AxiosHttp.spec.ts.mustache
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,118 @@ | ||
import { AxiosResponse } from 'axios'; | ||
import { AxiosHttp } from '@/http/AxiosHttp'; | ||
import { dataAxiosResponse, stubAxiosInstance } from './AxiosStub'; | ||
|
||
interface Payload { | ||
payload: string; | ||
} | ||
|
||
const fakePayload = (): Payload => ({ | ||
payload: 'content', | ||
}); | ||
|
||
interface Result { | ||
result: string; | ||
} | ||
|
||
const fakeResult = (): Result => ({ | ||
result: 'content', | ||
}); | ||
|
||
const responseResult = (): AxiosResponse => dataAxiosResponse(fakeResult()); | ||
|
||
const expectForQuerying = (uri: string, result: AxiosResponse<Result>) => { | ||
expect(result.data).toEqual(fakeResult()); | ||
expect(uri).toBe('/uri'); | ||
}; | ||
|
||
const expectForSendingAndQuerying = (uri: string, payload: Payload, result: AxiosResponse<Result>) => { | ||
expect(payload).toEqual<Payload>(fakePayload()); | ||
expectForQuerying(uri, result); | ||
}; | ||
|
||
describe('axiosHttp', () => { | ||
describe('GET', () => { | ||
it('should get content', async () => { | ||
const axiosInstance = stubAxiosInstance(); | ||
axiosInstance.get.resolves(responseResult()); | ||
const axiosHttp = new AxiosHttp(axiosInstance); | ||
const result = await axiosHttp.get<Result>('/uri'); | ||
const [uri] = axiosInstance.get.getCall(0).args; | ||
expectForQuerying(uri, result); | ||
}); | ||
|
||
it('should get content with params', async () => { | ||
const axiosInstance = stubAxiosInstance(); | ||
axiosInstance.get.resolves(responseResult()); | ||
const axiosHttp = new AxiosHttp(axiosInstance); | ||
await axiosHttp.get<Result>('/uri', { params: { beer: 'chips' } }); | ||
|
||
const [, config] = axiosInstance.get.getCall(0).args; | ||
expect(config.params.beer).toBe('chips'); | ||
}); | ||
}); | ||
|
||
describe('PUT', () => { | ||
it('should only get content', async () => { | ||
const axiosInstance = stubAxiosInstance(); | ||
axiosInstance.put.resolves(responseResult()); | ||
const axiosHttp = new AxiosHttp(axiosInstance); | ||
const result = await axiosHttp.put<Result>('/uri'); | ||
const [uri] = axiosInstance.put.getCall(0).args; | ||
expectForQuerying(uri, result); | ||
}); | ||
|
||
it('should send and get content', async () => { | ||
const axiosInstance = stubAxiosInstance(); | ||
axiosInstance.put.resolves(responseResult()); | ||
const axiosHttp = new AxiosHttp(axiosInstance); | ||
const result = await axiosHttp.put<Result, Payload>('/uri', fakePayload()); | ||
const [uri, payload] = axiosInstance.put.getCall(0).args; | ||
expectForSendingAndQuerying(uri, payload, result); | ||
}); | ||
}); | ||
|
||
describe('POST', () => { | ||
it('should only get content', async () => { | ||
const axiosInstance = stubAxiosInstance(); | ||
axiosInstance.post.resolves(responseResult()); | ||
const axiosHttp = new AxiosHttp(axiosInstance); | ||
const result = await axiosHttp.post<Result>('/uri'); | ||
const [uri] = axiosInstance.post.getCall(0).args; | ||
expectForQuerying(uri, result); | ||
}); | ||
|
||
it('should send and get content', async () => { | ||
const axiosInstance = stubAxiosInstance(); | ||
axiosInstance.post.resolves(responseResult()); | ||
const axiosHttp = new AxiosHttp(axiosInstance); | ||
const result = await axiosHttp.post<Result, Payload>('/uri', fakePayload()); | ||
const [uri, payload] = axiosInstance.post.getCall(0).args; | ||
expectForSendingAndQuerying(uri, payload, result); | ||
}); | ||
}); | ||
|
||
describe('DELETE', () => { | ||
it('should get content', async () => { | ||
const axiosInstance = stubAxiosInstance(); | ||
axiosInstance.delete.resolves(responseResult()); | ||
const axiosHttp = new AxiosHttp(axiosInstance); | ||
const result = await axiosHttp.delete<Result>('/uri'); | ||
const [uri] = axiosInstance.delete.getCall(0).args; | ||
expectForQuerying(uri, result); | ||
}); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
src/main/resources/generator/client/vue/test/spec/http/AxiosHttpStub.ts.mustache
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,22 @@ | ||
import sinon, { SinonStub } from 'sinon'; | ||
import { AxiosHttp, AxiosHttpResponse } from '@/http/AxiosHttp'; | ||
|
||
export interface AxiosHttpStub extends AxiosHttp { | ||
get: SinonStub; | ||
post: SinonStub; | ||
delete: SinonStub; | ||
put: SinonStub; | ||
} | ||
|
||
export const stubAxiosHttp = (): AxiosHttpStub => | ||
({ | ||
get: sinon.stub(), | ||
post: sinon.stub(), | ||
delete: sinon.stub(), | ||
put: sinon.stub(), | ||
} as AxiosHttpStub); | ||
|
||
export const dataBackendResponse = <T>(data: T): AxiosHttpResponse<T> => | ||
({ | ||
data, | ||
} as AxiosHttpResponse<T>); |
22 changes: 22 additions & 0 deletions
22
src/main/resources/generator/client/vue/test/spec/http/AxiosStub.ts.mustache
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,22 @@ | ||
import { AxiosInstance, AxiosResponse } from 'axios'; | ||
import sinon, { SinonStub } from 'sinon'; | ||
|
||
export interface AxiosStubInstance extends AxiosInstance { | ||
get: SinonStub; | ||
put: SinonStub; | ||
post: SinonStub; | ||
delete: SinonStub; | ||
} | ||
|
||
export const stubAxiosInstance = (): AxiosStubInstance => | ||
({ | ||
get: sinon.stub(), | ||
put: sinon.stub(), | ||
post: sinon.stub(), | ||
delete: sinon.stub(), | ||
} as AxiosStubInstance); | ||
|
||
export const dataAxiosResponse = <T>(data: T): AxiosResponse<T> => | ||
({ | ||
data, | ||
} as AxiosResponse<T>); |
23 changes: 23 additions & 0 deletions
23
src/main/resources/generator/client/vue/webapp/app/http/AxiosHttp.ts.mustache
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,23 @@ | ||
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
|
||
export type AxiosHttpResponse<T> = AxiosResponse<T>; | ||
|
||
export class AxiosHttp { | ||
constructor(private axiosInstance: AxiosInstance) {} | ||
|
||
async get<Result>(uri: string, config: AxiosRequestConfig = {}): Promise<AxiosResponse<Result>> { | ||
return this.axiosInstance.get<Result>(uri, config); | ||
} | ||
|
||
async put<Result, Payload = never>(uri: string, data?: Payload): Promise<AxiosResponse<Result>> { | ||
return this.axiosInstance.put<Result>(uri, data); | ||
} | ||
|
||
async post<Result, Payload = never>(uri: string, data?: Payload, config?: AxiosRequestConfig): Promise<AxiosResponse<Result>> { | ||
return this.axiosInstance.post<Result>(uri, data, config); | ||
} | ||
|
||
async delete<Result>(uri: string): Promise<AxiosResponse<Result>> { | ||
return this.axiosInstance.delete<Result>(uri); | ||
} | ||
} |
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