Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Организация работы с API на Vue.js, Виталий Копачёв

189 views

Published on

В процессе работы из проекта в проект я тащу свой «велосипед», постоянного его подтачиваю или модифицирую под конкретные нужды. Хотел бы рассказать о нем, его плюсах и минусах. Кроме этого, поговорим о том, какие «велосипеды» для работы с API существуют.

Published in: Technology
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Организация работы с API на Vue.js, Виталий Копачёв

  1. 1. Организация работы с API на Vue.js Копачёв Виталий
  2. 2. В чем проблема? ● Пишут запросы в компонентах ● Простыня кода ● Сложное тестирование ● Сложно поддерживать
  3. 3. Немного кода
  4. 4. export default { name: 'AllCoursesPanel', data() { return { loadingState: false}}, computed: { ...mapState('dashboard', ['availableCourses']) }, methods: { ...mapActions('dashboard', [ 'getAvailableCourses' ]) }, created() { this.loadingState = true axios.get('api/v1/user_courses/') .then((resp) => { this.$store .commit('setAvailableCourses', resp.data) }) .catch((error) => { if (error.response.status === httpBadRequest) { this.$store .dispatch( 'common/errorMessage', 'errors.fetchCoursesError' ) } this.$store .dispatch('common/errorMessage', 'errors.CommonError') }) .finally(() => { this.loadingState = false }) } } export default { name: 'AllCoursesPanel', data() { return { loadingState: false}}, computed: { ...mapState('dashboard', ['availableCourses']) }, methods: { ...mapActions('dashboard', [ 'getAvailableCourses' ]) }, created() { this.loadingState = true this.getAvailableCourses().finally(() => { this.loadingState = false }) } } Я Сын маминой...
  5. 5. export default { name: 'AllCoursesPanel', data() { return { loadingState: false}}, computed: { ...mapState('dashboard', ['availableCourses']) }, methods: { ...mapActions('dashboard', [ 'getAvailableCourses' ]) }, created() { this.loadingState = true axios.get('api/v1/user_courses/') .then((resp) => { this.$store .commit('setAvailableCourses', resp.data) }) .catch((error) => { if (error.response.status === httpBadRequest) { this.$store .dispatch( 'common/errorMessage', 'errors.fetchCoursesError' ) } this.$store .dispatch('common/errorMessage', 'errors.CommonError') }) .finally(() => { this.loadingState = false }) } } export default { name: 'AllCoursesPanel', data() { return { loadingState: false}}, computed: { ...mapState('dashboard', ['availableCourses']) }, methods: { ...mapActions('dashboard', [ 'getAvailableCourses' ]) }, created() { this.loadingState = true this.getAvailableCourses().finally(() => { this.loadingState = false }) } } Получение и отображение смешаны Логика отделена от реализации
  6. 6. Чего хотим? ● Понятный код ● Управление кодом ● Удобное тестирование ● Параллельная работа с кодом ● Конфигурация “в одном месте”
  7. 7. ● Три уровня абстракции ● Разделение зон ответственности ● Инкапсуляция логики ● Упрощение взаимодействия К чему пришли
  8. 8. Запросы к API META информация Data Recieving Отслеживание Post Processing Pre Processing Уровень сервисов
  9. 9. Абстракции бизнес-логикиАбстракции бизнес-логики
  10. 10. Взаимосвязи уровней
  11. 11. Сервисы Получение сырых данных. Обращение к внешним АПИ. Бизнес-логика. Сущности. Валидация уровня предметной области.Вызов методов Данные АПИ Взаимосвязи уровней
  12. 12. /** * Получение текущего курса. */ export async function getCurrentCourse({rootGetters, commit, dispatch}, courseId) { try { const data = rootGetters.api.getCourse(courseId) commit('setCurrentCourse', data) } catch (err) { dispatch('common/errorMessage', 'errors.fetchCoursesError', {root: true}) throw Error(err) } } /** * Установка текущего курса */ export function setCurrentCourse(state, rawCourse) { state.currentCourse = Course.produceCourse(rawCourse) } /** * Модели дашборда */ export class Course { constructor ({ uid, name, description = '', image_1 = '', destinationUrl }) { this.uid = uid this.name = name this.description = description this.image_1 = image_1 this.destinationUrl = destinationUrl } static produceCourse (rawData) { if (rawData.uid === undefined || rawData.uid === null) { throw new Error('Bad course data: no data') } if (rawData.name === undefined || rawData.name === null) { throw new Error('Bad course data: no name') } if (rawData.destinationUrl === undefined && rawData.destinationUrl === null) { throw new Error('Bad course data: no destinationUrl') } return new this(rawData) } } Взаимосвязи уровней
  13. 13. - Уровень рендеринга. - Уровень компонентов. Уровень отображения
  14. 14. Бизнес-логика. Взаимодействие. Поведение. Алгоритмы. Отображение. Внешний вид конкретных данных. Логика отображения.Вызов действий. Данные для отображения Взаимосвязи уровней
  15. 15. <template> <div class="UserCoursesPanel"> <q-card v-for="course in availableCourses"> <q-card-section> <div class="text-h6">{{ course.name }}</div> </q-card-section>` <q-card-section>{{ course.description }}</q-card-section> <q-card-actions> <q-btn color="info" @click="getCurrentCourse(course.uid)">Получить этот курс</q-btn> </q-card-actions> </q-card> <q-btn color="info" @click="getCourses">Посмотреть курсы</q-btn> </div> </template> <script> import {mapActions, mapState} from 'vuex' export default { name: 'UserCoursesPanel', computed: { ...mapState('dashboard', 'availableCourses')}, methods: { ...mapActions({ getCourses: 'dashboard/getUserCourses', getCurrentCourse: 'dashboard/getCurrentCourse' }) } } </script> Уровень отображения
  16. 16. Сервисы “Это уже не хамство. Однако все еще не сервис” Довлатов С.
  17. 17. /** * Получение списка доступных курсов пользователя */ export async function getAvailableCourses({rootGetters, commit, dispatch}) { try { const data = rootGetters.api.getCourses() commit('setAvailableCourses', data) } catch (err) { dispatch('common/errorMessage', 'errors.fetchCoursesError', {root: true}) throw Error(err) } } Единый интерфейс общения с АПИ
  18. 18. /** * Получение списка доступных курсов пользователя */ export async function getAvailableCourses({rootGetters, commit, dispatch}) { try { const data = rootGetters.api.getCourses() commit('setAvailableCourses', data) } catch (err) { dispatch('common/errorMessage', 'errors.fetchCoursesError', {root: true}) throw Error(err) } } Единый интерфейс общения с АПИ
  19. 19. /** * Получение списка доступных курсов пользователя */ export async function getAvailableCourses({rootGetters, commit, dispatch}) { try { const data = rootGetters.api.getCourses() commit('setAvailableCourses', data) } catch (err) { dispatch('common/errorMessage', 'errors.fetchCoursesError', {root: true}) throw Error(err) } } Единый интерфейс общения с АПИ
  20. 20. Инкапсулированный функционал Инициализация конфигурации. Установка url ресурсов. Подготовка текущего запроса. Добавление хедеров. Внутренняя обработки ответов на отдельные запросы. Общая логика выполнения запросов. Сервис АПИ
  21. 21. /** * Объект-обертка над клиентом. * Реализация REST */ class ApiClientClass { constructor(options = {}) { this.defaultHeaders = options.headers || { 'Accept': 'application/json', 'Content-Type': 'application/json' } // Создание экземпляра клиента. this.client = options.client || axios.create({ baseURL: process.env.API_URL ? process.env.API_URL : '', headers: this.defaultHeaders }); } } Инициализация конфигурации
  22. 22. /** * Объект-обертка над клиентом. * Реализация REST */ class ApiClientClass { constructor(options = {}) { this.defaultHeaders = options.headers || { 'Accept': 'application/json', 'Content-Type': 'application/json' } // Создание экземпляра клиента. this.client = options.client || axios.create({ baseURL: process.env.API_URL ? process.env.API_URL : '', headers: this.defaultHeaders }); } } Инициализация конфигурации
  23. 23. this.client.interceptors.request.use( /** * Подготовка запроса */ config => { if (!localStorage.getItem('tAccess')) { return config } const newHeaders = { ...this.defaultHeaders, Authorization: `Bearer ${localStorage.getItem('tAccess')}` } return { ...config, headers: newHeaders } }, e => Promise.reject(e) ) Подготовка текущего запроса
  24. 24. подготовка текущего запроса this.client.interceptors.request.use( /** * Подготовка запроса */ config => { if (!localStorage.getItem('tAccess')) { return config } const newHeaders = { ...this.defaultHeaders, Authorization: `Bearer ${localStorage.getItem('tAccess')}` } return { ...config, headers: newHeaders } }, e => Promise.reject(e) ) Подготовка текущего запроса
  25. 25. пост-обработка ответа this.client.interceptors.response.use( r => r, async error => { if (error.response && error.response.status === httpForbidden) { this.removeTokens() throw USER_UNAUTHORIZED } if (error.response && error.response.status === httpUnauthorized && !error.config.retry) { try { const {data} = await this.createRefreshRequest() this.setTokens({ tAccess: data.access, tRefresh: data.token }) const newRequest = { ...error.config, retry: true } return this.client(newRequest) } catch (err) { console.warn('') throw err } finally { this.refreshRequest = null } } throw error } ) Пост-обработка ответа
  26. 26. this.client.interceptors.response.use( r => r, async error => { if (error.response && error.response.status === httpForbidden) { this.removeTokens() throw USER_UNAUTHORIZED } if (error.response && error.response.status === httpUnauthorized && !error.config.retry) { try { const {data} = await this.createRefreshRequest() this.setTokens({ tAccess: data.access, tRefresh: data.token }) const newRequest = { ...error.config, retry: true } return this.client(newRequest) } catch (err) { console.warn('') throw err } finally { this.refreshRequest = null } } throw error } ) Реакция на ошибку доступа
  27. 27. - Обработка неудачных запросов. - Отдельная обработка для разных групп запросов. - Повторное выполнение запросов после выполнения некоторых действий Что еще можно делать
  28. 28. конфигурация интерфейса. /** * URL'ы ресурсов бекенда */ export const BACKEND_ENDPOINTS = { createToken: {method: 'post', url: 'api/v1/create_token/'}, updateToken: {method: 'put', url: 'api/v1/update_token/{token}/'}, refreshToken: {method: 'post', url: 'api/v1/refresh_token/'}, userCourses: {method: 'get', url: 'api/v1/user_courses/'}, verifyToken: {method: 'post', url: 'api/v1/verify_token/'}, getCourses: {method: 'get', url: 'api/v1/courses/'}, getCourse: {method: 'get', url: 'api/v1/courses/{courseId}'}, addCourse: {method: 'post', url: 'api/v1/courses'}, modifyCourse: {method: 'patch', url: 'api/v1/courses/{courseId}'}, fetchPracticePages: {method: 'get', url: 'api/v1/practice_pages/'}, attemptsLeft: {method: 'get', url: 'api/v1/quest_stat/attempts_left/'}, currentGitQuestAttempt: {method: 'get', url: 'api/v1/quest_stat/current/'}, startGitNewAttemptQuest: {method: 'post', url: 'api/v1/quest_stat/new_attempt/'}, finishGitNewAttemptQuest: {method: 'put', url: 'api/v1/quest_stat/finish/'} } Конфигурация интерфейса
  29. 29. /** * URL'ы ресурсов бекенда */ export const BACKEND_ENDPOINTS = { createToken: {method: 'post', url: 'api/v1/create_token/'}, updateToken: {method: 'put', url: 'api/v1/update_token/{token}/'}, refreshToken: {method: 'post', url: 'api/v1/refresh_token/'}, userCourses: {method: 'get', url: 'api/v1/user_courses/'}, verifyToken: {method: 'post', url: 'api/v1/verify_token/'}, getCourses: {method: 'get', url: 'api/v1/courses/'}, getCourse: {method: 'get', url: 'api/v1/courses/{courseId}'}, addCourse: {method: 'post', url: 'api/v1/courses'}, modifyCourse: {method: 'patch', url: 'api/v1/courses/{courseId}'}, fetchPracticePages: {method: 'get', url: 'api/v1/practice_pages/'}, attemptsLeft: {method: 'get', url: 'api/v1/quest_stat/attempts_left/'}, currentGitQuestAttempt: {method: 'get', url: 'api/v1/quest_stat/current/'}, startGitNewAttemptQuest: {method: 'post', url: 'api/v1/quest_stat/new_attempt/'}, finishGitNewAttemptQuest: {method: 'put', url: 'api/v1/quest_stat/finish/'} } Конфигурация интерфейса
  30. 30. - В теле запроса. - В параметрах запроса. В url ресурса. - Как часть URL. Идентификаторы ресурсов. где передаются параметры запроса. Параметры запроса
  31. 31. внешний код абстрагирован от этой логики. getCourse: {method: 'get', url: 'api/v1/courses/{courseId}'}, /** * Получение текущего курса. */ export async function getCurrentCourse({rootGetters, commit, dispatch}, courseId) { try { const data = rootGetters.api.getCourse({params: {courseId}}) commit('setCurrentCourse', data) } catch (err) { dispatch('common/errorMessage', 'errors.fetchCoursesError', {root: true}) throw Error(err) } } Внешний код абстрагирован
  32. 32. addCourse: {method: 'post', url: 'api/v1/courses'}, /** * Добавление нового курса. */ export async function createNewCourse({rootGetters, commit, dispatch}, courseData) { try { const data = rootGetters.api.addCourse({data: courseData}) } catch (err) { dispatch('common/errorMessage', 'errors.addCourseError', {root: true}) throw Error(err) } } Внешний код абстрагирован
  33. 33. /** * Прокси объект для динамического вызова функций апи. */ export default new Proxy( new ApiClientClass(), { get: function (target, name) { if (BACKEND_ENDPOINTS[name] !== undefined) { return ({params = {}, data = {}, args = {}} = {}) => { return target.client({ method: BACKEND_ENDPOINTS[name].method, url: target.urlFormat(BACKEND_ENDPOINTS[name].url, args), data: data, params: params }) .then((serverResponse) => {data: serverResponse.data}) .catch((error) => { if (error.response.status === httpBadRequest) new BadDataError('Bad request error') throw new Error('Server response error') }) } } else { // Если вызов не относиться к вызову стандартного API вызываем его напрямую из объекта return target[name] } } } ) Универсальный метод
  34. 34. /** * Прокси объект для динамического вызова функций апи. */ export default new Proxy( new ApiClientClass(), { get: function (target, name) { if (BACKEND_ENDPOINTS[name] !== undefined) { return ({params = {}, data = {}, args = {}} = {}) => { return target.client({ method: BACKEND_ENDPOINTS[name].method, url: target.urlFormat(BACKEND_ENDPOINTS[name].url, args), data: data, params: params }) .then((serverResponse) => {data: serverResponse.data}) .catch((error) => { if (error.response.status === httpBadRequest) new BadDataError('Bad request error') throw new Error('Server response error') }) } } else { // Если вызов не относиться к вызову стандартного API вызываем его напрямую из объекта return target[name] } } } ) Универсальный метод
  35. 35. /** * Прокси объект для динамического вызова функций апи. */ export default new Proxy( new ApiClientClass(), { get: function (target, name) { if (BACKEND_ENDPOINTS[name] !== undefined) { return ({params = {}, data = {}, args = {}} = {}) => { return target.client({ method: BACKEND_ENDPOINTS[name].method, url: target.urlFormat(BACKEND_ENDPOINTS[name].url, args), data: data, params: params }) .then((serverResponse) => {data: serverResponse.data}) .catch((error) => { if (error.response.status === httpBadRequest) new BadDataError('Bad request error') throw new Error('Server response error') }) } } else { // Если вызов не относиться к вызову стандартного API вызываем его напрямую из объекта return target[name] } } } ) Универсальный метод
  36. 36. /** * Прокси объект для динамического вызова функций апи. */ export default new Proxy( new ApiClientClass(), { get: function (target, name) { if (BACKEND_ENDPOINTS[name] !== undefined) { return ({params = {}, data = {}, args = {}} = {}) => { return target.client({ method: BACKEND_ENDPOINTS[name].method, url: target.urlFormat(BACKEND_ENDPOINTS[name].url, args), data: data, params: params }) .then((serverResponse) => {data: serverResponse.data}) .catch((error) => { if (error.response.status === httpBadRequest) new BadDataError('Bad request error') throw new Error('Server response error') }) } } else { // Если вызов не относиться к вызову стандартного API вызываем его напрямую из объекта return target[name] } } } ) Универсальный метод
  37. 37. реализация универсального метода /** * Прокси объект для динамического вызова функций апи. */ export default new Proxy( new ApiClientClass(), { get: function (target, name) { if (BACKEND_ENDPOINTS[name] !== undefined) { return ({params = {}, data = {}, args = {}} = {}) => { return target.client({ method: BACKEND_ENDPOINTS[name].method, url: target.urlFormat(BACKEND_ENDPOINTS[name].url, args), data: data, params: params }) .then((serverResponse) => {data: serverResponse.data}) .catch((error) => { if (error.response.status === httpBadRequest) new BadDataError('Bad request error') throw new Error('Server response error') }) } } else { // Если вызов не относиться к вызову стандартного API вызываем его напрямую из объекта return target[name] } } } ) Универсальный метод
  38. 38. /** * Прокси объект для динамического вызова функций апи. */ export default new Proxy( new ApiClientClass(), { get: function (target, name) { if (BACKEND_ENDPOINTS[name] !== undefined) { return ({params = {}, data = {}, args = {}} = {}) => { return target.client({ method: BACKEND_ENDPOINTS[name].method, url: target.urlFormat(BACKEND_ENDPOINTS[name].url, args), data: data, params: params }) .then((serverResponse) => {data: serverResponse.data}) .catch((error) => { if (error.response.status === httpBadRequest) new BadDataError('Bad request error') throw new Error('Server response error') }) } } else { // Если вызов не относиться к вызову стандартного API вызываем его напрямую из объекта return target[name] } } } ) Универсальный метод
  39. 39. /** * Прокси объект для динамического вызова функций апи. */ export default new Proxy( new ApiClientClass(), { get: function (target, name) { if (BACKEND_ENDPOINTS[name] !== undefined) { return ({params = {}, data = {}, args = {}} = {}) => { return target.client({ method: BACKEND_ENDPOINTS[name].method, url: target.urlFormat(BACKEND_ENDPOINTS[name].url, args), data: data, params: params }) .then((serverResponse) => {data: serverResponse.data}) .catch((error) => { if (error.response.status === httpBadRequest) new BadDataError('Bad request error') throw new Error('Server response error') }) } } else { // Если вызов не относиться к вызову стандартного API вызываем его напрямую из объекта return target[name] } } } ) Универсальный метод
  40. 40. // Получение токенов при помощи авторизационных данных пользователя. async loginByToken ({ urlArguments }) { try { const result = await this.client({ method: BACKEND_ENDPOINTS.updateToken.method, url: this.urlFormat(BACKEND_ENDPOINTS.updateToken.url, urlArguments) }) const { data } = result this.setTokens({ tAccess: data.jwt_token.access, tRefresh: data.jwt_token.refresh }) return true } catch (err) { if (err.response.status === httpNotFound) { // Пробуем определить является ли ответ бизнес ответом или это просто не валидный урл if (err.response.data.result) { console.warn('Token on server not found err.response = ', err.response) return false } throw new Error(err) } throw new Error(err) } } Специальные методы
  41. 41. async logout () { try { const result = await this.client({ ...{ method: BACKEND_ENDPOINTS.updateToken.method, url: `${BACKEND_ENDPOINTS.updateToken.url}${token}/` } }) } catch (error) { if (error !== USER_UNAUTHORIZED) { const respStatus = error.response.status if (![httpForbidden, httpUnauthorized].includes(respStatus)) { throw new Error(error.response) } } } finally { this.removeTokens() } } Специальные методы
  42. 42. setTokens ({tAccess = '', tRefresh = ''}) { localStorage.setItem('tAccess', tAccess) localStorage.setItem('tRefresh', tRefresh) window.dispatchEvent(new StorageEvent('storage', {key: 'tAccess'})) window.dispatchEvent(new StorageEvent('storage', {key: 'tRefresh'})) } removeTokens() { localStorage.removeItem('tAccess') localStorage.removeItem('tRefresh') window.dispatchEvent(new StorageEvent('storage', {key: 'tAccess'})) window.dispatchEvent(new StorageEvent('storage', {key: 'tRefresh'})) } Хелперы
  43. 43. setTokens ({tAccess = '', tRefresh = ''}) { localStorage.setItem('tAccess', tAccess) localStorage.setItem('tRefresh', tRefresh) window.dispatchEvent(new StorageEvent('storage', {key: 'tAccess'})) window.dispatchEvent(new StorageEvent('storage', {key: 'tRefresh'})) } removeTokens() { localStorage.removeItem('tAccess') localStorage.removeItem('tRefresh') window.dispatchEvent(new StorageEvent('storage', {key: 'tAccess'})) window.dispatchEvent(new StorageEvent('storage', {key: 'tRefresh'})) } Хелперы
  44. 44. REST-like JSON-RPC
  45. 45. - Обработки ответов опирается на JSON-RPC коды - Упрощенная логика формирования запроса - Упрощенная логика обработки ошибок - Возможность множественного вызова JSON-RPC vs REST-like
  46. 46. JSON-RPC реализация /** * Объект-обертка над клиентом. * Реализация REST */ class ApiClientClass { constructor(options = {}) { this.defaultHeaders = options.headers || { 'Accept': 'application/json', 'Content-Type': 'application/json' } // Создание экземпляра клиента. this.client = options.client || axios.create({ baseURL: process.env.API_URL ? process.env.API_URL : '', headers: this.defaultHeaders }) } } /** * Объект-обертка над клиентом. * Реализация RPC интерфейса. */ class ApiClientClass { constructor(options = {}) { this.defaultHeaders = options.headers || { 'Accept': 'application/json', 'Content-Type': 'application/json' } const customRequestsUri = process.env.API_URL + process.env.API_URN // Создание экземпляра клиента. this.client = options.client || axios.create({ baseURL: customRequestsUri || 'rpc/', headers: this.defaultHeaders }) } } JSON - rpc реализация
  47. 47. this.client.interceptors.response.use( r => r, async error => { if (error.response && error.response.status === httpForbidden) { this.removeTokens() throw USER_UNAUTHORIZED } if (error.response && error.response.status === httpUnauthorized && !error.config.retry) { try { const {data} = await this.createRefreshRequest() this.setTokens({ tAccess: data.access, tRefresh: data.token }) const newRequest = { ...error.config, retry: true } return this.client(newRequest) } catch (err) { console.warn('') throw err } finally { this.refreshRequest = null } } throw error } ) this.client.interceptors.response.use( async response => { /** * Перехватчик неавторизованных запросов при вызове * одиночного метода Если при попытке доступа к ресурсу * мы получили код 32001 это означает что нужно * предпринять попытку повторного запроса токена */ const {data} = response if (data.error && data.error.code === RPCUnauthorizedException && !response.config.retry ) { try { await this.createRefreshRequest() const newRequest = { ...response.config, retry: true } return this.client(newRequest) } catch (err) { console.warn( 'Error when requesting refresh err', err ) } } return response }, error => error ) Обработка по JSON - rpc кодам
  48. 48. this.client.interceptors.response.use( r => r, async error => { if (error.response && error.response.status === httpForbidden) { this.removeTokens() throw USER_UNAUTHORIZED } if (error.response && error.response.status === httpUnauthorized && !error.config.retry) { try { const {data} = await this.createRefreshRequest() this.setTokens({ tAccess: data.access, tRefresh: data.token }) const newRequest = { ...error.config, retry: true } return this.client(newRequest) } catch (err) { console.warn('') throw err } finally { this.refreshRequest = null } } throw error } ) this.client.interceptors.response.use( async response => { /** * Перехватчик неавторизованных запросов при вызове * одиночного метода Если при попытке доступа к ресурсу * мы получили код 32001 это означает что нужно * предпринять попытку повторного запроса токена */ const {data} = response if (data.error && data.error.code === RPCUnauthorizedException && !response.config.retry ) { try { await this.createRefreshRequest() const newRequest = { ...response.config, retry: true } return this.client(newRequest) } catch (err) { console.warn( 'Error when requesting refresh err', err ) } } return response }, error => error ) JSON - rpc реализация
  49. 49. async callFunction (payload) { let response = null try { response = await this.client({ method: 'post', data: payload }) } catch (err) { console.warn(err) throw new Error('Not found end point or Server error') } const { data } = response if (Array.isArray(data)) { return data.map( rawResultData => rawResultData.error ? { error: rawResultData.error.message, id: rawResultData.id } : { result: rawResultData.result, id: rawResultData.id } ) } if (data.error) { throw new Error(data.error.message) } return data.result } /** * Прокси объект для динамического вызова функций апи. */ export default new Proxy( new ApiClientClass(), { get: function (target, name) { if (BACKEND_ENDPOINTS[name] !== undefined) { return ({params = {}, data = {}, args = {}} = {}) => { return target.client({ method: BACKEND_ENDPOINTS[name].method, url: target.urlFormat(BACKEND_ENDPOINTS[name].url, args), data: data, params: params }) .then((serverResponse) => {data: serverResponse.data}) .catch((error) => { if (error.response.status === httpBadRequest) new BadDataError('Bad request error') throw new Error('Server response error') }) } } else { // Если вызов не относиться к вызову стандартного API вызываем его напрямую из объекта return target[name] } } } ) Обработка ошибок
  50. 50. const API_METHODS = { getCourses: {transport: 'http', url: 'course.getCourses'}, getCourse: {transport: 'http', url: 'course.getCourses'}, addCourse: {transport: 'http', url: 'course.addCourse'}, modifyCourse: {transport: 'http', url: 'course.modifyCourse'}, refreshToken: {transport: 'http', methodName: REFRESH_TOKEN_METHOD_NAME}, verifyCodeByPhoneAndGetToken: {transport: 'http', methodName: VERIFY_SMS_CODE}, createToken: {transport: 'http', methodName: CREATE_TOKEN_METHOD_NAME}, logout: {transport: 'http', methodName: 'logout'}, registerPushNotification: {transport: 'http', methodName: REGISTER_PUSH_NOTIFICATION}, updateCustomerProfile: {transport: 'http', methodName: 'user.updateCustomerProfile'}, getCustomerProfile: {transport: 'http', methodName: 'user.getCustomerProfile'}, getTokenForDownloadFile: {transport: 'http', methodName: 'common.getTokenForDownloadFile'}, personalUserStatistic: {transport: 'http', methodName: 'statistic.personalUserStatistic'} } Конфигурация
  51. 51. const API_METHODS = { getCourses: {transport: 'http', url: 'course.getCourses'}, getCourse: {transport: 'http', url: 'course.getCourses'}, addCourse: {transport: 'http', url: 'course.addCourse'}, modifyCourse: {transport: 'http', url: 'course.modifyCourse'}, refreshToken: {transport: 'http', methodName: REFRESH_TOKEN_METHOD_NAME}, verifyCodeByPhoneAndGetToken: {transport: 'http', methodName: VERIFY_SMS_CODE}, createToken: {transport: 'http', methodName: CREATE_TOKEN_METHOD_NAME}, logout: {transport: 'http', methodName: 'logout'}, registerPushNotification: {transport: 'http', methodName: REGISTER_PUSH_NOTIFICATION}, updateCustomerProfile: {transport: 'http', methodName: 'user.updateCustomerProfile'}, getCustomerProfile: {transport: 'http', methodName: 'user.getCustomerProfile'}, getTokenForDownloadFile: {transport: 'http', methodName: 'common.getTokenForDownloadFile'}, personalUserStatistic: {transport: 'http', methodName: 'statistic.personalUserStatistic'} } Конфигурация
  52. 52. /** * Генератор маршрутизатора транспорта RPC методов бекенда */ Object.keys(API_METHODS) .forEach(methodName => { BACKEND_METHODS[methodName] = { transport: API_METHODS[methodName].transport, requestPayloadConfig: (params = []) => { return { ...RPC_20_DATA_STRUCTURE(), ...{ params, method: API_METHODS[methodName].methodName } } } } }) /** * Регистрация метода API */ registerMethod (methodName) { this[`${methodName}Constructor`] = BACKEND_METHODS[methodName] this[methodName] = async (...params) => { try { const result = await this .transports[this[`${methodName}Constructor`].transport] .callFunction( this[`${methodName}Constructor`].requestPayloadConfig(pa rams) ) return result } catch (Err) { throw new Error(`errors.${Err.message}`) } } } export const RPC_20_DATA_STRUCTURE = () => ({ jsonrpc: '2.0', method: 'test_example_echo_method', params: [], id: uuidv4() }) Формирование запроса
  53. 53. async callFunction (payload) { let response = null try { response = await this.client({ method: 'post', data: payload }) } catch (err) { console.warn(err) throw new Error('Not found end point or Server error') } const { data } = response if (Array.isArray(data)) { return data.map( rawResultData => rawResultData.error ? { error: rawResultData.error.message, id: rawResultData.id } : { result: rawResultData.result, id: rawResultData.id } ) } if (data.error) { throw new Error(data.error.message) } return data.result } /** * Регистрация метода API */ registerMethod (methodName) { this[`${methodName}Constructor`] = BACKEND_METHODS[methodName] this[methodName] = async (...params) => { try { const result = await this .transports[this[`${methodName}Constructor`].transport] .callFunction( this[`${methodName}Constructor`].requestPayloadConfig(pa rams) ) return result } catch (Err) { throw new Error(`errors.${Err.message}`) } } } Формирование запроса
  54. 54. /** * Получение текущего курса. */ export async function getCurrentCourse({rootGetters, commit, dispatch}, courseId) { try { const data = rootGetters.api.getCourse({args: {courseId}}) commit('setCurrentCourse', data) } catch (err) { dispatch('common/errorMessage', 'errors.fetchCoursesError', {root: true}) throw Error(err) } } /** * Получение текущего курса. */ export async function getCurrentCourse({rootGetters, commit, dispatch}, courseId) { try { const data = rootGetters.api.getCourse(courseId) commit('setCurrentCourse', data) } catch (err) { dispatch('common/errorMessage', 'errors.fetchCoursesError', {root: true}) throw Error(err) } } Интерфейс не изменился
  55. 55. /** * Получение текущего курса. */ export async function getCurrentCourse({rootGetters, commit, dispatch}, courseId) { try { const data = rootGetters.api.getCourse({args: {courseId}}) commit('setCurrentCourse', data) } catch (err) { dispatch('common/errorMessage', 'errors.fetchCoursesError', {root: true}) throw Error(err) } } /** * Получение текущего курса. */ export async function getCurrentCourse({rootGetters, commit, dispatch}, courseId) { try { const data = rootGetters.api.getCourse(courseId) commit('setCurrentCourse', data) } catch (err) { dispatch('common/errorMessage', 'errors.fetchCoursesError', {root: true}) throw Error(err) } } Интерфейс не изменился
  56. 56. /** * Отладка мульти-вызова. * TODO Действие для отладки сервиса АПИ. */ export async function sendMulticallMethods ({ rootGetters }) { try { const result = await rootGetters.api.callMultiple([ { methodName: 'testExampleProtectedEchoMethod', params: [1, 2] }, { methodName: 'testExampleEchoMethod', params: [2, 3] }, { methodName: 'testExampleProtectedEchoMethod', params: [3, 4] }, { methodName: 'testExampleEchoMethod', params: [4, 5] }, ]) return result } catch (err) { console.warn('sendMulticallMethods error err =', err) } } Мультивызовы
  57. 57. JSON-RPC реализация на Proxy
  58. 58. формирование запроса с применением Proxy export const apiClientFactory = (options = {}) => new Proxy( new ApiClientClass(options), { get: function (target, name) { return (...args) => { const preparedData = { ...RPC_20_DATA_STRUCTURE({ inputParams: args, method: name }) } // Обращение к Апи. return target.client({ data: preparedData }).then(({ data }) => { if (data.error) { logger('data.error', data) throw new Error(data.error) } return data.result }).catch((error) => { throw new Error(error) }) } } } ) Применение Proxy
  59. 59. export const apiClientFactory = (options = {}) => new Proxy( new ApiClientClass(options), { get: function (target, name) { return (...args) => { const preparedData = { ...RPC_20_DATA_STRUCTURE({ inputParams: args, method: name }) } // Обращение к Апи. return target.client({ data: preparedData }).then(({ data }) => { if (data.error) { logger('data.error', data) throw new Error(data.error) } return data.result }).catch((error) => { throw new Error(error) }) } } } ) Применение Proxy
  60. 60. export const apiClientFactory = (options = {}) => new Proxy( new ApiClientClass(options), { get: function (target, name) { return (...args) => { const preparedData = { ...RPC_20_DATA_STRUCTURE({ inputParams: args, method: name }) } // Обращение к Апи. return target.client({ data: preparedData }).then(({ data }) => { if (data.error) { logger('data.error', data) throw new Error(data.error) } return data.result }).catch((error) => { throw new Error(error) }) } } } ) Применение Proxy
  61. 61. export const apiClientFactory = (options = {}) => new Proxy( new ApiClientClass(options), { get: function (target, name) { return (...args) => { const preparedData = { ...RPC_20_DATA_STRUCTURE({ inputParams: args, method: name }) } // Обращение к Апи. return target.client({ data: preparedData }).then(({ data }) => { if (data.error) { logger('data.error', data) throw new Error(data.error) } return data.result }).catch((error) => { throw new Error(error) }) } } } ) Применение Proxy
  62. 62. /** * Получение текущего курса. */ export async function getCurrentCourse({rootGetters, commit, dispatch}, courseId) { try { const data = rootGetters.api.getCourse(courseId) commit('setCurrentCourse', data) } catch (err) { dispatch('common/errorMessage', 'errors.fetchCoursesError', {root: true}) throw Error(err) } } Интерфейс не изменился
  63. 63. Сервер
  64. 64. Как это выглядит на сервере
  65. 65. как это выглядит на сервере Как это выглядит на сервере
  66. 66. как это выглядит на сервере Как это выглядит на сервере
  67. 67. Как это выглядит на сервере
  68. 68. Копачёв Виталий tg: @vetos_88 Спасибо за внимание
  69. 69. внутренняя логика. Выделение токенов

×