-
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.
Merge pull request #346 from kure-kosen/fix/send-contact
send-contactの修正
- Loading branch information
Showing
17 changed files
with
817 additions
and
410 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
import RestClient from "./RestClient"; | ||
|
||
export interface IContactEnum { | ||
corners: { [key: string]: number }; | ||
departments: { [key: string]: number }; | ||
grades: { [key: string]: number }; | ||
} | ||
|
||
export default class ContactApi { | ||
public restClient: RestClient; | ||
|
||
constructor(restClient: RestClient) { | ||
this.restClient = restClient; | ||
} | ||
|
||
public saveContact(json: object, succussed: (res: object) => void, errored: (err: object) => void, always = () => {}) { | ||
public saveContact( | ||
json: object, | ||
succussed: (res: object) => void, | ||
errored: (err: object) => void, | ||
always = () => {} | ||
) { | ||
return this.restClient.post("/api/v1/contacts", json, succussed, errored, always); | ||
} | ||
|
||
public fetchContactEnum() { | ||
return this.restClient.get<IContactEnum>("/api/v1/contacts/enum"); | ||
} | ||
} |
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,83 +1,94 @@ | ||
import axios, { AxiosInstance } from "axios"; | ||
|
||
export default class RestClient { | ||
public axios: any; | ||
public axios: AxiosInstance; | ||
|
||
constructor(baseUrl: string) { | ||
const axiosBase = require("axios"); | ||
const csrfToken = (document.querySelector("meta[name=csrf-token]") as HTMLMetaElement).content; | ||
|
||
this.axios = axiosBase.create({ | ||
this.axios = axios.create({ | ||
baseURL: baseUrl, | ||
timeout: 1000, | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-CSRF-TOKEN": csrfToken | ||
} | ||
}); | ||
|
||
// TODO: Productionではログを流さないようにする | ||
this.axios.interceptors.response.use( | ||
response => { | ||
const { config, data, status } = response; | ||
const { method, params, url } = config; | ||
|
||
console.group(`${method ? method.toUpperCase() : "undefined method"}:${status} - ${url}`); | ||
if (params) console.table(params); | ||
console.log(data); | ||
console.groupEnd(); | ||
|
||
return response; | ||
}, | ||
error => { | ||
console.log(error); | ||
return Promise.reject(error); | ||
} | ||
); | ||
} | ||
|
||
public get( | ||
public async get<T>( | ||
path: string, | ||
params: object, | ||
successed: (res: object) => void, | ||
errored: (res: object) => void, | ||
always = () => {} | ||
params?: object, | ||
successed?: (res: object) => void, | ||
errored?: (res: object) => void, | ||
always: () => any = () => {} | ||
) { | ||
return this.axios | ||
.get(path, { params }) | ||
.then((result: object) => { | ||
console.log(`GET ${this.axios.baseURL}/${path}`); | ||
console.log(`result: ${JSON.stringify(result)}`); | ||
successed(result); | ||
}) | ||
.catch((error: object) => { | ||
console.log(`ERROR! GET ${this.axios.baseURL}/${path}`); | ||
console.log(`error: ${JSON.stringify(error)}`); | ||
errored(error); | ||
}) | ||
.then(always()); | ||
try { | ||
const response = await this.axios.get<T>(path, { params }); | ||
if (successed) successed(response); | ||
return response; | ||
} catch (error) { | ||
if (errored) errored(error); | ||
throw error; | ||
} finally { | ||
always(); | ||
} | ||
} | ||
|
||
public post( | ||
public async post<T>( | ||
path: string, | ||
params: object, | ||
successed: (res: object) => void, | ||
errored: (res: object) => void, | ||
always = () => {} | ||
params?: object, | ||
successed?: (res: object) => void, | ||
errored?: (res: object) => void, | ||
always: () => any = () => {} | ||
) { | ||
return this.axios | ||
.post(path, params) | ||
.then((result: object) => { | ||
console.log(`POST ${this.axios.baseURL}/${path}`); | ||
console.log(`result: ${result}`); | ||
successed(result); | ||
}) | ||
.catch((error: object) => { | ||
console.log(`ERROR! POST ${this.axios.baseURL}/${path}`); | ||
console.log(`error: ${error}`); | ||
errored(error); | ||
}) | ||
.then(always()); | ||
try { | ||
const response = await this.axios.post<T>(path, params); | ||
if (successed) successed(response); | ||
return response; | ||
} catch (error) { | ||
if (errored) errored(error); | ||
throw error; | ||
} finally { | ||
always(); | ||
} | ||
} | ||
|
||
public delete( | ||
public async delete<T>( | ||
path: string, | ||
params: object, | ||
successed: (res: object) => void, | ||
errored: (res: object) => void, | ||
always = () => {} | ||
params?: object, | ||
successed?: (res: object) => void, | ||
errored?: (res: object) => void, | ||
always: () => any = () => {} | ||
) { | ||
return this.axios | ||
.delete(path, { data: { params } }) | ||
.then((result: object) => { | ||
console.log(`DELETE ${this.axios.baseURL}/${path}`); | ||
console.log(`result: ${result}`); | ||
successed(result); | ||
}) | ||
.catch((error: object) => { | ||
console.log(`ERROR! DELETE ${this.axios.baseURL}/${path}`); | ||
console.log(`error: ${error}`); | ||
errored(error); | ||
}) | ||
.then(always()); | ||
try { | ||
const response = await this.axios.delete<T>(path, { data: { params } }); | ||
if (successed) successed(response); | ||
return response; | ||
} catch (error) { | ||
if (errored) errored(error); | ||
throw error; | ||
} finally { | ||
always(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.