Skip to content

Commit

Permalink
feat(projects): add request language
Browse files Browse the repository at this point in the history
  • Loading branch information
paynezhuang committed Aug 22, 2024
1 parent 723c821 commit b6f597d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/axios/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** request id key */
export const REQUEST_ID_KEY = 'X-Request-Id';
export const REQUEST_ID_KEY = 'P-Request-Id';

/** request language key */
export const REQUEST_LANGUAGE = 'P-Language';

/** the backend error code key */
export const BACKEND_ERROR_CODE = 'BACKEND_ERROR';
4 changes: 2 additions & 2 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AxiosResponse, CreateAxiosDefaults, InternalAxiosRequestConfig } f
import axiosRetry from 'axios-retry';
import { nanoid } from '@sa/utils';
import { createAxiosConfig, createDefaultOptions, createRetryOptions } from './options';
import { BACKEND_ERROR_CODE, REQUEST_ID_KEY } from './constant';
import { BACKEND_ERROR_CODE, REQUEST_ID_KEY, REQUEST_LANGUAGE } from './constant';
import type { CustomAxiosRequestConfig, FlatRequestInstance, MappedType, RequestInstance, RequestOption, ResponseType } from './type';

function createCommonRequest<ResponseData = any>(axiosConfig?: CreateAxiosDefaults, options?: Partial<RequestOption<ResponseData>>) {
Expand Down Expand Up @@ -159,6 +159,6 @@ export function createFlatRequest<ResponseData = any, State = Record<string, unk
return flatRequest;
}

export { BACKEND_ERROR_CODE, REQUEST_ID_KEY };
export { BACKEND_ERROR_CODE, REQUEST_ID_KEY, REQUEST_LANGUAGE };
export type * from './type';
export type { CreateAxiosDefaults, AxiosError };
6 changes: 5 additions & 1 deletion src/service/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AxiosResponse } from 'axios';
import { BACKEND_ERROR_CODE, createFlatRequest, createRequest } from '@sa/axios';
import { BACKEND_ERROR_CODE, REQUEST_LANGUAGE, createFlatRequest, createRequest } from '@sa/axios';
import { useAuthStore } from '@/store/modules/auth';
import { $t } from '@/locales';
import { localStg } from '@/utils/storage';
Expand All @@ -22,6 +22,10 @@ export const request = createFlatRequest<App.Service.Response, RequestInstanceSt
const token = localStg.get('token');
const Authorization = token ? `Bearer ${token}` : null;

// set language
const language = localStg.get('lang') || 'zh-CN';
headers.set(REQUEST_LANGUAGE, language);

// set refresh nonce
if (request.state.isRefreshingToken) {
// Use the refresh nonce code returned by the backend to refresh the token.
Expand Down

0 comments on commit b6f597d

Please sign in to comment.