feat(client): add the setBearerToken method to the APIClient

This commit is contained in:
chenos 2022-02-23 18:20:45 +08:00
parent 872ee79146
commit 57c9524f34
3 changed files with 15 additions and 3 deletions

View File

@ -4,6 +4,8 @@ const apiClient = new APIClient({
baseURL: `http://localhost:3000/api/`, baseURL: `http://localhost:3000/api/`,
}); });
apiClient.setBearerToken();
// mock(apiClient); // mock(apiClient);
export default apiClient; export default apiClient;

View File

@ -37,6 +37,16 @@ export class APIClient {
} }
} }
setBearerToken(key = 'nocobaseToken') {
this.axios.interceptors.request.use((config) => {
const token = localStorage.getItem(key);
if (token) {
config.headers['Authorization'] = `Bearer ${token}`;
}
return config;
});
}
service(uid: string): Result<any, any> { service(uid: string): Result<any, any> {
return this.services[uid]; return this.services[uid];
} }
@ -74,4 +84,4 @@ export class APIClient {
}; };
return new Proxy(target, handler); return new Proxy(target, handler);
} }
} }

View File

@ -1,6 +1,6 @@
import React from 'react'; import { APIClient, APIClientProvider, compose, useRequest } from '@nocobase/client';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import { APIClient, APIClientProvider, useRequest, compose } from '@nocobase/client'; import React from 'react';
const apiClient = new APIClient(); const apiClient = new APIClient();