Skip to content

Commit

Permalink
feat: Add custom header to fetch instance
Browse files Browse the repository at this point in the history
  • Loading branch information
trandaison committed Jul 29, 2024
1 parent b06924a commit 5af577d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
8 changes: 6 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,24 @@ export default defineNuxtModule<AuthOptions>({
url: "/login",
method: "POST",
property: "",
headers: undefined,
},
logout: { url: "/logout", method: "DELETE" },
logout: { url: "/logout", method: "DELETE", headers: undefined },
refresh: {
url: "/refresh_tokens",
method: "POST",
property: "",
headers: undefined,
},
user: {
url: "/me",
method: "GET",
property: "",
headers: undefined,
},
signup: { url: "/signup", method: "POST" },
signup: { url: "/signup", method: "POST", headers: undefined },
},
headers: undefined,
token: {
headerName: "Authorization",
type: "Bearer",
Expand Down
12 changes: 8 additions & 4 deletions src/runtime/services/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export class Auth implements AuthService {
this.loginPromise ??= this.httpService.call<T>(
this.config.endpoints.login.method,
this.config.endpoints.login.url,
credentials
credentials,
{ headers: this.config.endpoints.login.headers }
);
const res = await this.loginPromise;
const data = this.getProperty(res, "login");
Expand All @@ -114,7 +115,7 @@ export class Auth implements AuthService {
this.config.endpoints.user.method,
this.config.endpoints.user.url,
undefined,
{ auth: true }
{ auth: true, headers: this.config.endpoints.user.headers }
);
const res = await this.fetchUserPromise;
const data = this.getProperty(res, "user");
Expand All @@ -133,7 +134,9 @@ export class Auth implements AuthService {
if (callApi) {
this.logoutPromise ??= this.httpService.call<unknown>(
this.config.endpoints.logout.method,
this.config.endpoints.logout.url
this.config.endpoints.logout.url,
undefined,
{ headers: this.config.endpoints.logout.headers }
);
await this.logoutPromise;
}
Expand All @@ -150,7 +153,8 @@ export class Auth implements AuthService {
this.refreshTokensPromise ??= this.httpService.call(
this.config.endpoints.refresh.method,
this.config.endpoints.refresh.url,
{ [this.config.refreshToken.paramName]: this.storage.refreshToken() }
{ [this.config.refreshToken.paramName]: this.storage.refreshToken() },
{ headers: this.config.endpoints.refresh.headers }
);
const res = await this.refreshTokensPromise;
const data = this.getProperty(res, "refresh");
Expand Down
17 changes: 8 additions & 9 deletions src/runtime/services/HttpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ export default class HttpService {
this.$fetch = $fetch.create({
baseURL: baseUrl,

headers: {
Accept: "application/json",
},

onRequest: this.onRequest.bind(this),

onResponse: async (context) => {
Expand All @@ -54,12 +50,11 @@ export default class HttpService {
}

const { token } = await this.$auth.refreshTokens().catch(() => ({

token: undefined
}))
token: undefined,
}));

if (!token) {
this.onAuthFailure(AuthStatus.Expired)
this.onAuthFailure(AuthStatus.Expired);
return;
}

Expand Down Expand Up @@ -91,7 +86,11 @@ export default class HttpService {
}

private onRequest({ options }: FetchContext) {
options.headers = new Headers(options.headers);
options.headers = new Headers({
Accept: "application/json",
...this.$configs.headers,
...options.headers,
});
const authOption = options.auth ?? true;
const token = this.$auth.accessToken();
if (authOption !== false && token) {
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface EndpointOption {
url: string;
method?: string;
property?: string;
headers?: Record<string, any>;
}

export interface AuthEndpointOptions {
Expand Down Expand Up @@ -69,6 +70,7 @@ export interface RedirectOptions {

export interface AuthConfig {
endpoints: AuthEndpointOptions;
headers?: Record<string, any>;
token: TokenOptions;
refreshToken: RefreshTokenOptions;
redirect: RedirectOptions;
Expand All @@ -84,6 +86,7 @@ export interface AuthConfig {

export interface AuthOptions {
endpoints?: Partial<AuthEndpointOptions>;
headers?: Record<string, any>;
token?: Partial<TokenOptions>;
refreshToken?: Partial<RefreshTokenOptions>;
cookie?: Partial<CookieOptions>;
Expand Down

0 comments on commit 5af577d

Please sign in to comment.