2022-05-19 00:40:55 +08:00
|
|
|
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
|
|
import qs from 'qs';
|
|
|
|
|
|
|
|
export interface ActionParams {
|
|
|
|
filterByTk?: any;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceActionOptions<P = any> = {
|
|
|
|
resource?: string;
|
|
|
|
resourceOf?: any;
|
|
|
|
action?: string;
|
|
|
|
params?: P;
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface IResource {
|
|
|
|
list?: (params?: ActionParams) => Promise<any>;
|
|
|
|
get?: (params?: ActionParams) => Promise<any>;
|
|
|
|
create?: (params?: ActionParams) => Promise<any>;
|
|
|
|
update?: (params?: ActionParams) => Promise<any>;
|
|
|
|
destroy?: (params?: ActionParams) => Promise<any>;
|
|
|
|
[key: string]: (params?: ActionParams) => Promise<any>;
|
|
|
|
}
|
|
|
|
|
2022-05-27 00:00:59 +08:00
|
|
|
export class Auth {
|
2022-05-19 00:40:55 +08:00
|
|
|
protected api: APIClient;
|
2022-05-27 00:00:59 +08:00
|
|
|
|
2023-03-10 19:16:00 +08:00
|
|
|
protected NOCOBASE_LOCALE_KEY = 'NOCOBASE_LOCALE';
|
|
|
|
|
|
|
|
protected NOCOBASE_ROLE_KEY = 'NOCOBASE_ROLE';
|
|
|
|
|
2022-05-27 00:00:59 +08:00
|
|
|
protected options = {
|
|
|
|
token: null,
|
|
|
|
locale: null,
|
|
|
|
role: null,
|
|
|
|
};
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
constructor(api: APIClient) {
|
|
|
|
this.api = api;
|
2023-03-10 19:16:00 +08:00
|
|
|
this.initKeys();
|
2022-05-27 00:00:59 +08:00
|
|
|
this.locale = this.getLocale();
|
|
|
|
this.role = this.getRole();
|
|
|
|
this.token = this.getToken();
|
|
|
|
this.api.axios.interceptors.request.use(this.middleware.bind(this));
|
|
|
|
}
|
|
|
|
|
2023-03-10 19:16:00 +08:00
|
|
|
initKeys() {
|
|
|
|
if (!window) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const match = window.location.pathname.match(/^\/apps\/([^/]*)\//);
|
|
|
|
if (!match) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const appName = match[1];
|
|
|
|
this.NOCOBASE_LOCALE_KEY = `${appName.toUpperCase()}_NOCOBASE_LOCALE`;
|
|
|
|
this.NOCOBASE_ROLE_KEY = `${appName.toUpperCase()}_NOCOBASE_ROLE`;
|
|
|
|
}
|
|
|
|
|
2022-05-27 00:00:59 +08:00
|
|
|
get locale() {
|
|
|
|
return this.getLocale();
|
|
|
|
}
|
|
|
|
|
|
|
|
get role() {
|
|
|
|
return this.getRole();
|
|
|
|
}
|
|
|
|
|
|
|
|
get token() {
|
|
|
|
return this.getToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
set locale(value) {
|
|
|
|
this.setLocale(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
set role(value) {
|
|
|
|
this.setRole(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
set token(value) {
|
|
|
|
this.setToken(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware(config: AxiosRequestConfig) {
|
|
|
|
if (this.locale) {
|
|
|
|
config.headers['X-Locale'] = this.locale;
|
|
|
|
}
|
|
|
|
if (this.role) {
|
|
|
|
config.headers['X-Role'] = this.role;
|
|
|
|
}
|
|
|
|
if (this.token) {
|
|
|
|
config.headers['Authorization'] = `Bearer ${this.token}`;
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
getLocale() {
|
2023-03-10 19:16:00 +08:00
|
|
|
return this.api.storage.getItem(this.NOCOBASE_LOCALE_KEY);
|
2022-05-27 00:00:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setLocale(locale: string) {
|
|
|
|
this.options.locale = locale;
|
2023-03-10 19:16:00 +08:00
|
|
|
this.api.storage.setItem(this.NOCOBASE_LOCALE_KEY, locale || '');
|
2022-05-27 00:00:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getToken() {
|
|
|
|
return this.api.storage.getItem('NOCOBASE_TOKEN');
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setToken(token: string) {
|
2022-05-27 00:00:59 +08:00
|
|
|
this.options.token = token;
|
|
|
|
this.api.storage.setItem('NOCOBASE_TOKEN', token || '');
|
|
|
|
if (!token) {
|
|
|
|
this.setRole(null);
|
2022-08-20 18:06:12 +08:00
|
|
|
// this.setLocale(null);
|
2022-05-27 00:00:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getRole() {
|
2023-03-10 19:16:00 +08:00
|
|
|
return this.api.storage.getItem(this.NOCOBASE_ROLE_KEY);
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setRole(role: string) {
|
2022-05-27 00:00:59 +08:00
|
|
|
this.options.role = role;
|
2023-03-10 19:16:00 +08:00
|
|
|
this.api.storage.setItem(this.NOCOBASE_ROLE_KEY, role || '');
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:06:12 +08:00
|
|
|
async signIn(values, authenticator: string = 'password'): Promise<AxiosResponse<any>> {
|
2022-05-19 00:40:55 +08:00
|
|
|
const response = await this.api.request({
|
|
|
|
method: 'post',
|
|
|
|
url: 'users:signin',
|
|
|
|
data: values,
|
2022-08-20 18:06:12 +08:00
|
|
|
params: {
|
2023-01-04 19:27:37 +08:00
|
|
|
authenticator,
|
|
|
|
},
|
2022-05-19 00:40:55 +08:00
|
|
|
});
|
|
|
|
const data = response?.data?.data;
|
2022-05-27 00:00:59 +08:00
|
|
|
this.setToken(data?.token);
|
|
|
|
return response;
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async signOut() {
|
|
|
|
await this.api.request({
|
|
|
|
method: 'post',
|
|
|
|
url: 'users:signout',
|
|
|
|
});
|
2022-05-27 00:00:59 +08:00
|
|
|
this.setToken(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class Storage {
|
|
|
|
abstract clear(): void;
|
|
|
|
abstract getItem(key: string): string | null;
|
|
|
|
abstract removeItem(key: string): void;
|
|
|
|
abstract setItem(key: string, value: string): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MemoryStorage extends Storage {
|
|
|
|
items = new Map();
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.items.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
getItem(key: string) {
|
|
|
|
return this.items.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
setItem(key: string, value: string) {
|
|
|
|
return this.items.set(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeItem(key: string) {
|
|
|
|
return this.items.delete(key);
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 00:00:59 +08:00
|
|
|
interface ExtendedOptions {
|
|
|
|
authClass?: any;
|
|
|
|
storageClass?: any;
|
|
|
|
}
|
|
|
|
|
2022-05-19 00:40:55 +08:00
|
|
|
export class APIClient {
|
|
|
|
axios: AxiosInstance;
|
|
|
|
auth: Auth;
|
2022-05-27 00:00:59 +08:00
|
|
|
storage: Storage;
|
2022-05-19 00:40:55 +08:00
|
|
|
|
2022-05-27 00:00:59 +08:00
|
|
|
constructor(instance?: AxiosInstance | (AxiosRequestConfig & ExtendedOptions)) {
|
2022-05-19 00:40:55 +08:00
|
|
|
if (typeof instance === 'function') {
|
|
|
|
this.axios = instance;
|
|
|
|
} else {
|
2022-05-27 00:00:59 +08:00
|
|
|
const { authClass, storageClass, ...others } = instance || {};
|
|
|
|
this.axios = axios.create(others);
|
|
|
|
this.initStorage(storageClass);
|
|
|
|
if (authClass) {
|
|
|
|
this.auth = new authClass(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!this.storage) {
|
|
|
|
this.initStorage();
|
|
|
|
}
|
|
|
|
if (!this.auth) {
|
|
|
|
this.auth = new Auth(this);
|
|
|
|
}
|
2022-06-24 21:19:57 +08:00
|
|
|
this.interceptors();
|
2022-05-27 00:00:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private initStorage(storage?: any) {
|
|
|
|
if (storage) {
|
|
|
|
this.storage = new storage(this);
|
2022-07-15 20:45:43 +08:00
|
|
|
} else if (typeof localStorage !== 'undefined') {
|
2022-05-27 00:00:59 +08:00
|
|
|
this.storage = localStorage;
|
|
|
|
} else {
|
|
|
|
this.storage = new MemoryStorage();
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 21:19:57 +08:00
|
|
|
interceptors() {
|
2022-05-19 00:40:55 +08:00
|
|
|
this.axios.interceptors.request.use((config) => {
|
|
|
|
config.paramsSerializer = (params) => {
|
|
|
|
return qs.stringify(params, {
|
|
|
|
strictNullHandling: true,
|
|
|
|
arrayFormat: 'brackets',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return config;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D> | ResourceActionOptions): Promise<R> {
|
|
|
|
const { resource, resourceOf, action, params } = config as any;
|
|
|
|
if (resource) {
|
|
|
|
return this.resource(resource, resourceOf)[action](params);
|
|
|
|
}
|
|
|
|
return this.axios.request<T, R, D>(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
resource(name: string, of?: any): IResource {
|
|
|
|
const target = {};
|
|
|
|
const handler = {
|
|
|
|
get: (_: any, actionName: string) => {
|
|
|
|
let url = name.split('.').join(`/${of || '_'}/`);
|
|
|
|
url += `:${actionName}`;
|
|
|
|
const config: AxiosRequestConfig = { url };
|
|
|
|
if (['get', 'list'].includes(actionName)) {
|
|
|
|
config['method'] = 'get';
|
|
|
|
} else {
|
|
|
|
config['method'] = 'post';
|
|
|
|
}
|
2022-06-28 19:51:14 +08:00
|
|
|
return async (params?: ActionParams, opts?: any) => {
|
2022-05-19 00:40:55 +08:00
|
|
|
const { values, filter, ...others } = params || {};
|
|
|
|
config['params'] = others;
|
|
|
|
if (filter) {
|
|
|
|
if (typeof filter === 'string') {
|
|
|
|
config['params']['filter'] = filter;
|
|
|
|
} else {
|
|
|
|
config['params']['filter'] = JSON.stringify(filter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (config.method !== 'get') {
|
|
|
|
config['data'] = values || {};
|
|
|
|
}
|
2022-06-28 19:51:14 +08:00
|
|
|
return await this.request({
|
|
|
|
...config,
|
|
|
|
...opts,
|
|
|
|
});
|
2022-05-19 00:40:55 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return new Proxy(target, handler);
|
|
|
|
}
|
|
|
|
}
|