Skip to content

Commit d8f0a24

Browse files
authored
Merge pull request #55 from Zxilly/patch
feat: add custom header option
2 parents 19c343f + 936a8de commit d8f0a24

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/Authorizer.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Mode = "auto" | "cookies" | "manual"
1515
export class Authorizer {
1616
public mode!: Mode;
1717
public endpoint: string | undefined = undefined;
18+
public requestHeaders: StringKV | undefined = undefined;
1819
public permission: Permission | undefined;
1920
public cookieKey : string | undefined = undefined;
2021
public cacheExpiredTime = 60; // Seconds
@@ -27,17 +28,21 @@ export class Authorizer {
2728
* "auto": Specify the casbin server endpoint, and Casbin.js will load permission from it when the identity changes
2829
* "cookies": Casbin.js load the permission data from the cookie "access_perm" or the specified cookie key.
2930
* "manual": Load the permission mannually with "setPermission"
31+
* @param args
3032
* @param args.endpoint Casbin service endpoint, REQUIRED when mode == "auto"
3133
* @param args.cacheExpiredTime The expired time of local cache, Unit: seconds, Default: 60s, activated when mode == "auto"
3234
* @param args.cookieKey The cookie key when loading permission, activated when mode == "cookies"
3335
*/
34-
constructor(mode: Mode = "manual", args: {endpoint?: string, /*cookieKey?: string,*/ cacheExpiredTime?: number} = {}) {
36+
constructor(mode: Mode = 'manual', args: { endpoint?: string; cacheExpiredTime?: number; requestHeaders?: StringKV } = {}) {
3537
if (mode == 'auto') {
3638
if (!args.endpoint) {
3739
throw new Error("Specify the endpoint when initializing casbin.js with mode == 'auto'");
3840
} else {
3941
this.mode = mode;
4042
this.endpoint = args.endpoint;
43+
if (args.requestHeaders) {
44+
this.requestHeaders = args.requestHeaders;
45+
}
4146
if (args.cacheExpiredTime !== null && args.cacheExpiredTime !== undefined) {
4247
this.cacheExpiredTime = args.cacheExpiredTime;
4348
}
@@ -100,7 +105,9 @@ export class Authorizer {
100105
if (this.endpoint === undefined || this.endpoint === null) {
101106
throw Error("Endpoint is null or not specified.");
102107
}
103-
const resp = await axios.get<BaseResponse>(`${this.endpoint}?subject=${this.user}`);
108+
const resp = await axios.get<BaseResponse>(`${this.endpoint}?subject=${this.user}`, {
109+
headers: this.requestHeaders,
110+
});
104111
return resp.data.data;
105112
}
106113

0 commit comments

Comments
 (0)