feat(client): add the setBearerToken method to the APIClient
This commit is contained in:
parent
872ee79146
commit
57c9524f34
@ -4,6 +4,8 @@ const apiClient = new APIClient({
|
||||
baseURL: `http://localhost:3000/api/`,
|
||||
});
|
||||
|
||||
apiClient.setBearerToken();
|
||||
|
||||
// mock(apiClient);
|
||||
|
||||
export default apiClient;
|
||||
|
@ -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> {
|
||||
return this.services[uid];
|
||||
}
|
||||
@ -74,4 +84,4 @@ export class APIClient {
|
||||
};
|
||||
return new Proxy(target, handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import React from 'react';
|
||||
import { APIClient, APIClientProvider, compose, useRequest } from '@nocobase/client';
|
||||
import MockAdapter from 'axios-mock-adapter';
|
||||
import { APIClient, APIClientProvider, useRequest, compose } from '@nocobase/client';
|
||||
import React from 'react';
|
||||
|
||||
const apiClient = new APIClient();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user