Created
June 3, 2019 10:46
-
-
Save hw0k/06bdae8e1bf706f0056da5f25bbac561 to your computer and use it in GitHub Desktop.
react-sudoku 3.4
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
import axios from 'axios'; | |
const baseURI: string = 'https://sugoku.herokuapp.com'; | |
type Tuple9<T> = [T, T, T, T, T, T, T, T, T]; | |
export type Board = Tuple9<Tuple9<number>>; | |
export type Difficulty = 'easy' | 'medium' | 'hard' | 'random'; | |
export type Status = 'solved' | 'unsolved'; | |
export type BoardResponse = { | |
board: Board; | |
}; | |
export type SolveResponse = { | |
difficulty: Difficulty; | |
status: Status; | |
solution: Board; | |
}; | |
export type ValidateResponse = { | |
status: Status; | |
}; | |
const handleAPIError = (err: Error) => { | |
console.error('An API Error occured.'); | |
console.error(err); | |
}; | |
export const getBoard = async (difficulty: Difficulty) => { | |
try { | |
const { data } = await axios.get(`${baseURI}/board?difficulty=${difficulty}`); | |
return data; | |
} | |
catch (err) { | |
handleAPIError(err); | |
} | |
}; | |
export const solveBoard = async (board: Board) => { | |
try { | |
const { data } = await axios.post(`${baseURI}/solve`, {board: board}), { | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
}); | |
return data; | |
} | |
catch (err) { | |
handleAPIError(err); | |
} | |
}; | |
export const validateBoard = async (board: Board) => { | |
try { | |
const { data } = await axios.post(`${baseURI}/validate`, {board: board}), { | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
}); | |
return data; | |
} | |
catch (err) { | |
handleAPIError(err); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment