반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 리눅스
- androidstudio
- jest
- webpack
- PYTHON
- VirtualBox
- react
- build
- unittest
- xcode
- linux
- node
- qunit
- Chrome
- TensorFlow
- ReactNative
- 맥
- 네트워크
- localserver
- avds
- centos
- IOS
- 센토스
- MachineLearning
- picker
- vsCode
- Android
- MAC
- 개발
Archives
- Today
- Total
로메오의 블로그
[VUE + TYPESCRIPT] Rest Api & Cors 처리 본문
반응형
RestApi 클래스
$ npm install axios
.env
VUE_APP_API_PUBLIC_URL='http://localhost:8888/v1/'
VUE_APP_API_BASE_URL='http://localhost:8888/v1/'
utils/restApi.ts
import axios, { AxiosInstance, AxiosResponse } from 'axios'
export enum ServiceUrl {
BASE = process.env.VUE_APP_API_BASE_URL
}
export default abstract class HttpClient {
public apiUrl?: string;
protected readonly instance: AxiosInstance;
public constructor (
serviceUrl?: ServiceUrl,
apiUrl?: string
) {
this.apiUrl = apiUrl
this.instance = axios.create({
baseURL: serviceUrl || process.env.VUE_APP_API_PUBLIC_URL,
headers: {
'Cache-Control': 'no-cache',
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json; charset = utf-8'
}
})
this._requestInterceptor()
this._responseInterceptor()
}
/**
* 인증처리
*/
public setAxiosAuth = () => {
// console.log('setAxiosAuth')
}
/**
* 요청 Interceptor
*/
private _requestInterceptor = () => {
this.instance.interceptors.request.use(this._handleRequest)
};
private _handleRequest = (req: any) => {
// loading 추가
return req
};
/**
* 응답 Interceptor
*/
private _responseInterceptor = () => {
this.instance.interceptors.response.use(
this._handleResponse,
this._handleError
)
};
private _handleResponse = (res: AxiosResponse) => {
// loading 제거
return res.data
};
/**
* 오류 처리
*/
protected _handleError = (error: any) => {
console.error(error)
}
}
/**
* RestApi 객체
*/
export class RestApi<T> extends HttpClient {
public apiUrl: string;
private _type!: { new(): T}
public constructor (
apiUrl: string,
_type: { new(): T}
) {
super(undefined, apiUrl)
this.apiUrl = apiUrl
this._type = _type
}
/**
* Get 조회합니다.
*/
public get = async () => {
await this.setAxiosAuth()
const res: any = await this.instance.get<T>(`${this.apiUrl}`)
return this.castType(res)
}
/**
* Post 전송합니다.
*/
public post = async (item: any) => {
await this.setAxiosAuth()
const res: any = await this.instance.post<T>(`${this.apiUrl}`, item)
return this.castType(res)
}
castType = (item: any) => Object.assign(new this._type(), item);
}
통신하기
<template>
<div>
<v-btn @click="getApi">Get Api</v-btn>
<v-btn @click="postApi">Post Api</v-btn>
</div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import { RestApi } from '@/utils/restApi'
/**
* 데이터 객체를 정의한다.
*/
export class DataType {
date?: string;
number?: string;
}
@Component
export default class Test1View extends Vue {
testApi = new RestApi('/api/path', DataType)
async postApi () {
const data = {
date: '20201221',
number: 'S002'
}
const res = await this.testApi.post(data)
console.log(res)
}
async getApi () {
const res: DataType = await this.testApi.get()
console.log(res)
}
}
</script>
Spring Boot CORS 처리
interceptor/SecurityConfig.java
package com.sk.aibril.tsrassistant.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SecurityConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST");
}
}
반응형
'Frontend > Vue' 카테고리의 다른 글
[VUE + TYPESCRIPT] unit test - jest (0) | 2022.03.17 |
---|---|
[VUE + TYPESCRIPT] Life Cycle (0) | 2022.03.15 |
[VUE + TYPESCRIPT] 환경변수 설정 (0) | 2022.03.14 |
[VUE + TYPESCRIPT] store module 분리하기 (0) | 2021.02.12 |
[VUE + TYPESCRIPT] Vuex Interface를 구현하는 Store (0) | 2021.02.12 |
Comments