From 6e5f14792e0d39fd15e7bc19f6b02ebd52fdb57f Mon Sep 17 00:00:00 2001
From: simeng-li <simeng@silverhand.io>
Date: Wed, 27 Apr 2022 18:36:26 +0800
Subject: [PATCH] refactor(ui): update api lang header (#680)

update api lang header
---
 packages/ui/src/apis/api.ts      | 12 ++++++++++++
 packages/ui/src/apis/consent.ts  |  4 ++--
 packages/ui/src/apis/register.ts | 12 ++++++------
 packages/ui/src/apis/settings.ts |  2 +-
 packages/ui/src/apis/sign-in.ts  | 13 ++++++-------
 packages/ui/src/apis/social.ts   | 12 ++++++------
 6 files changed, 33 insertions(+), 22 deletions(-)
 create mode 100644 packages/ui/src/apis/api.ts

diff --git a/packages/ui/src/apis/api.ts b/packages/ui/src/apis/api.ts
new file mode 100644
index 000000000..fb1044b49
--- /dev/null
+++ b/packages/ui/src/apis/api.ts
@@ -0,0 +1,12 @@
+import i18next from 'i18next';
+import ky from 'ky';
+
+export default ky.extend({
+  hooks: {
+    beforeRequest: [
+      (request) => {
+        request.headers.set('Accept-Language', i18next.language);
+      },
+    ],
+  },
+});
diff --git a/packages/ui/src/apis/consent.ts b/packages/ui/src/apis/consent.ts
index 18749e32a..a1d5ddb88 100644
--- a/packages/ui/src/apis/consent.ts
+++ b/packages/ui/src/apis/consent.ts
@@ -1,9 +1,9 @@
-import ky from 'ky';
+import api from './api';
 
 export const consent = async () => {
   type Response = {
     redirectTo: string;
   };
 
-  return ky.post('/api/session/consent').json<Response>();
+  return api.post('/api/session/consent').json<Response>();
 };
diff --git a/packages/ui/src/apis/register.ts b/packages/ui/src/apis/register.ts
index 017452be5..0e408b556 100644
--- a/packages/ui/src/apis/register.ts
+++ b/packages/ui/src/apis/register.ts
@@ -1,11 +1,11 @@
-import ky from 'ky';
+import api from './api';
 
 export const register = async (username: string, password: string) => {
   type Response = {
     redirectTo: string;
   };
 
-  return ky
+  return api
     .post('/api/session/register/username-password', {
       json: {
         username,
@@ -16,7 +16,7 @@ export const register = async (username: string, password: string) => {
 };
 
 export const sendRegisterSmsPasscode = async (phone: string) => {
-  return ky
+  return api
     .post('/api/session/register/passwordless/sms/send-passcode', {
       json: {
         phone,
@@ -30,7 +30,7 @@ export const verifyRegisterSmsPasscode = async (phone: string, passcode: string)
     redirectTo: string;
   };
 
-  return ky
+  return api
     .post('/api/session/register/passwordless/sms/verify-passcode', {
       json: {
         phone,
@@ -41,7 +41,7 @@ export const verifyRegisterSmsPasscode = async (phone: string, passcode: string)
 };
 
 export const sendRegisterEmailPasscode = async (email: string) => {
-  return ky
+  return api
     .post('/api/session/register/passwordless/email/send-passcode', {
       json: {
         email,
@@ -55,7 +55,7 @@ export const verifyRegisterEmailPasscode = async (email: string, passcode: strin
     redirectTo: string;
   };
 
-  return ky
+  return api
     .post('/api/session/register/passwordless/email/verify-passcode', {
       json: {
         email,
diff --git a/packages/ui/src/apis/settings.ts b/packages/ui/src/apis/settings.ts
index 7b7426718..24ed1942b 100644
--- a/packages/ui/src/apis/settings.ts
+++ b/packages/ui/src/apis/settings.ts
@@ -1,6 +1,6 @@
 /**
  * Used to get and general sign-in experience settings.
- * The API will be depreated in the future once SSR is implemented.
+ * The API will be deprecated in the future once SSR is implemented.
  */
 
 import { SignInExperience } from '@logto/schemas';
diff --git a/packages/ui/src/apis/sign-in.ts b/packages/ui/src/apis/sign-in.ts
index e337cada3..80c3f06f8 100644
--- a/packages/ui/src/apis/sign-in.ts
+++ b/packages/ui/src/apis/sign-in.ts
@@ -1,5 +1,4 @@
-import ky from 'ky';
-
+import api from './api';
 import { bindSocialAccount } from './social';
 
 export const signInBasic = async (username: string, password: string, socialToBind?: string) => {
@@ -7,7 +6,7 @@ export const signInBasic = async (username: string, password: string, socialToBi
     redirectTo: string;
   };
 
-  const result = await ky
+  const result = await api
     .post('/api/session/sign-in/username-password', {
       json: {
         username,
@@ -24,7 +23,7 @@ export const signInBasic = async (username: string, password: string, socialToBi
 };
 
 export const sendSignInSmsPasscode = async (phone: string) => {
-  return ky
+  return api
     .post('/api/session/sign-in/passwordless/sms/send-passcode', {
       json: {
         phone,
@@ -42,7 +41,7 @@ export const verifySignInSmsPasscode = async (
     redirectTo: string;
   };
 
-  const result = await ky
+  const result = await api
     .post('/api/session/sign-in/passwordless/sms/verify-passcode', {
       json: {
         phone,
@@ -59,7 +58,7 @@ export const verifySignInSmsPasscode = async (
 };
 
 export const sendSignInEmailPasscode = async (email: string) => {
-  return ky
+  return api
     .post('/api/session/sign-in/passwordless/email/send-passcode', {
       json: {
         email,
@@ -77,7 +76,7 @@ export const verifySignInEmailPasscode = async (
     redirectTo: string;
   };
 
-  const result = await ky
+  const result = await api
     .post('/api/session/sign-in/passwordless/email/verify-passcode', {
       json: {
         email,
diff --git a/packages/ui/src/apis/social.ts b/packages/ui/src/apis/social.ts
index 05072da1d..4c30ac057 100644
--- a/packages/ui/src/apis/social.ts
+++ b/packages/ui/src/apis/social.ts
@@ -1,4 +1,4 @@
-import ky from 'ky';
+import api from './api';
 
 export const invokeSocialSignIn = async (
   connectorId: string,
@@ -9,7 +9,7 @@ export const invokeSocialSignIn = async (
     redirectTo: string;
   };
 
-  return ky
+  return api
     .post('/api/session/sign-in/social', {
       json: {
         connectorId,
@@ -30,7 +30,7 @@ export const signInWithSocial = async (parameters: {
     redirectTo: string;
   };
 
-  return ky
+  return api
     .post('/api/session/sign-in/social', {
       json: parameters,
     })
@@ -42,7 +42,7 @@ export const bindSocialAccount = async (connectorId: string) => {
     redirectTo: string;
   };
 
-  return ky
+  return api
     .post('/api/session/sign-in/bind-social', {
       json: {
         connectorId,
@@ -56,7 +56,7 @@ export const bindSocialRelatedUser = async (connectorId: string) => {
     redirectTo: string;
   };
 
-  return ky
+  return api
     .post('/api/session/sign-in/bind-social-related-user', {
       json: {
         connectorId,
@@ -70,7 +70,7 @@ export const registerWithSocial = async (connectorId: string) => {
     redirectTo: string;
   };
 
-  return ky
+  return api
     .post('/api/session/register/social', {
       json: {
         connectorId,