From 28f91d2bf529c8ca5a1b6997e8aba93e09638085 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 6 Feb 2020 21:54:35 +1100 Subject: [PATCH] Axios can't cors. --- src/public/package.json | 3 +-- src/public/src/api/APIRequest.tsx | 14 ++++++++++++++ src/public/src/api/Client.tsx | 5 ----- src/public/src/api/SendMail.tsx | 4 ++-- 4 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 src/public/src/api/APIRequest.tsx delete mode 100644 src/public/src/api/Client.tsx diff --git a/src/public/package.json b/src/public/package.json index fdadf92..50b31dd 100644 --- a/src/public/package.json +++ b/src/public/package.json @@ -7,7 +7,7 @@ "develop": "gatsby develop", "start": "npm run develop", "serve": "gatsby serve", - "deploy": "serverless client deploy", + "deploy": "serverless client deploy --no-confirm", "clean": "gatsby clean" }, "repository": { @@ -27,7 +27,6 @@ }, "homepage": "https://domsplace.com", "dependencies": { - "axios": "^0.19.2", "babel-plugin-styled-components": "^1.10.7", "gatsby": "^2.18.12", "gatsby-image": "^2.2.39", diff --git a/src/public/src/api/APIRequest.tsx b/src/public/src/api/APIRequest.tsx new file mode 100644 index 0000000..dbc02c3 --- /dev/null +++ b/src/public/src/api/APIRequest.tsx @@ -0,0 +1,14 @@ +export const APIRequest = (url:string, body?:object) => { + return fetch(`https://api.domsplace.com/v1/${url}`, { + mode: 'no-cors', + method: body ? 'POST' : 'GET', + body: body ? JSON.stringify(body) : null, + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } + }); +} + + +(globalThis as any)['APIRequest'] = APIRequest; \ No newline at end of file diff --git a/src/public/src/api/Client.tsx b/src/public/src/api/Client.tsx deleted file mode 100644 index 0f7d81c..0000000 --- a/src/public/src/api/Client.tsx +++ /dev/null @@ -1,5 +0,0 @@ -import axios from 'axios'; - -export const Client = axios.create({ - baseURL: 'https://api.domsplace.com/v1/' -}); diff --git a/src/public/src/api/SendMail.tsx b/src/public/src/api/SendMail.tsx index 117dfbf..9250706 100644 --- a/src/public/src/api/SendMail.tsx +++ b/src/public/src/api/SendMail.tsx @@ -1,6 +1,6 @@ -import { Client } from "./Client"; +import { APIRequest } from "./APIRequest"; -export const sendMail = (name:string, email:string, message:string) => Client.post('mail/send', { +export const sendMail = (name:string, email:string, message:string) => APIRequest('mail/send', { name, email, message });