-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(social): commente un bouquet #180
Closed
+144
−13
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,7 +3,9 @@ import { toast } from 'vue3-toastify' | |||||
import { useLoading } from 'vue-loading-overlay' | ||||||
|
||||||
import config from '@/config' | ||||||
import type { Response } from '@/model' | ||||||
|
||||||
// FIXME: the client should not depend be aware of the store. | ||||||
import { useUserStore } from '../../store/UserStore' | ||||||
|
||||||
const $loading = useLoading() | ||||||
|
@@ -21,7 +23,7 @@ instance.interceptors.request.use( | |||||
|
||||||
return config | ||||||
}, | ||||||
(error) => Promise.reject(error) | ||||||
async (error) => await Promise.reject(error) | ||||||
) | ||||||
|
||||||
/** | ||||||
|
@@ -73,14 +75,16 @@ export default class DatagouvfrAPI { | |||||
*/ | ||||||
async makeRequestAndHandleResponse(url, method = 'get', params = {}) { | ||||||
const loader = $loading.show() | ||||||
return this.request(url, method, params) | ||||||
return await this.request(url, method, params) | ||||||
.catch((error) => { | ||||||
if (error && error.message) { | ||||||
if (error?.message) { | ||||||
toast(error.message, { type: 'error', autoClose: false }) // TODO: Refacto to handle the error | ||||||
return error.response | ||||||
} | ||||||
}) | ||||||
.finally(() => loader.hide()) | ||||||
.finally(() => { | ||||||
loader.hide() | ||||||
}) | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -141,6 +145,23 @@ export default class DatagouvfrAPI { | |||||
) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Base function for HTTP calls (without error handling) | ||||||
* | ||||||
* @todo Remove this function once all API calls are all handled this way: | ||||||
* leaving the error handling to the caller (store). | ||||||
* | ||||||
* @param {string} url | ||||||
* @param {object} data | ||||||
* @returns {Promise<Response>} | ||||||
*/ | ||||||
async _create(url: string, data = {}): Promise<Response> { | ||||||
return await this.httpClient.post(url, data).then( | ||||||
(response) => response, | ||||||
(error) => this.#handleError(error) | ||||||
) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Update an entity (PUT) | ||||||
* | ||||||
|
@@ -160,17 +181,19 @@ export default class DatagouvfrAPI { | |||||
* Delete an entity (DELETE) | ||||||
* | ||||||
* @param {string} entityId - A UUID entity id | ||||||
* @returns {Promise} | ||||||
* @returns {Promise<Response>} | ||||||
*/ | ||||||
async delete(entityId) { | ||||||
return this.httpClient.delete(`${this.url()}/${entityId}/`).then( | ||||||
async delete(entityId: string): Promise<Response> { | ||||||
return await this.httpClient.delete(`${this.url()}/${entityId}/`).then( | ||||||
(response) => response, | ||||||
(error) => this.#handleError(error) | ||||||
) | ||||||
} | ||||||
|
||||||
#handleError({ response, message }) { | ||||||
if (response) return { status: response.status } | ||||||
return { message } | ||||||
#handleError({ response, message }): Response { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @edelagnier vu que c'est du TypeScript maintenant, ne pourrait-on utiliser
Suggested change
|
||||||
return { | ||||||
...(response && { status: response.status }), | ||||||
...(message && { error: { message } }) | ||||||
} | ||||||
} | ||||||
} |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import axios from 'axios' | ||
import { HttpResponse, http } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
import { createPinia, setActivePinia } from 'pinia' | ||
import { | ||
afterAll, | ||
afterEach, | ||
beforeAll, | ||
beforeEach, | ||
expect, | ||
test | ||
} from 'vitest' | ||
|
||
import DiscussionsAPI from '@/services/api/resources/DiscussionsAPI' | ||
|
||
const baseUrl = 'https://example.lol' | ||
const version = '1234' | ||
const endpoint = 'discussions' | ||
|
||
const discussionRequest = { | ||
title: 'Title of the discussion', | ||
comment: 'This is a discussion.', | ||
subject: { | ||
class: 'Topic', | ||
id: 'id123' | ||
} | ||
} | ||
|
||
const server = setupServer( | ||
http.post(`${baseUrl}/${version}/${endpoint}/`, () => { | ||
return HttpResponse.json(discussionRequest, { status: 200 }) | ||
}) | ||
) | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
beforeEach(async (context) => { | ||
const httpClient = axios.create() | ||
httpClient.defaults.proxy = false | ||
setActivePinia(createPinia()) | ||
context.client = new DiscussionsAPI({ | ||
baseUrl, | ||
version, | ||
httpClient | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
test('create a discussion', async ({ client }) => { | ||
const { data } = await client.create(discussionRequest) | ||
expect(data.title).toEqual(discussionRequest.title) | ||
}) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Le
await
est nécessaire ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@edelagnier ? (Il me semble que c'est le
npm run format
qui rajoute ça automatiquement).