Skip to content

Commit f4e1e07

Browse files
committed
feat(DEV-248): added RequestOptions composable to set headers globally
1 parent 780b189 commit f4e1e07

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

src/runtime/composables/gql-async-query.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import { hashPasswords } from '../functions/graphql-meta';
88
import { useAuthState } from '../states/auth';
99
import { useAuth } from './use-auth';
1010
import { useHelper } from './use-helper';
11+
import { useRequestOptions } from './use-request-options';
1112

1213
export async function gqlAsyncQuery<T = any>(method: string, options: IGraphQLOptions = {}): Promise<AsyncData<T, Error>> {
1314
const { $graphql, _meta } = useNuxtApp();
1415
const _nuxtApp = useNuxtApp();
1516
const { accessTokenState } = useAuthState();
1617
const { checkTokenAndRenew } = useAuth();
1718
const { generateUniqueHash } = useHelper();
19+
const { getHeaders } = useRequestOptions();
1820

1921
// Check parameters
2022
if (!method) {
@@ -123,8 +125,9 @@ export async function gqlAsyncQuery<T = any>(method: string, options: IGraphQLOp
123125
await callWithNuxt(_nuxtApp, checkTokenAndRenew);
124126

125127
const requestHeaders: Record<string, string> = {
126-
authorization: `Bearer ${accessTokenState.value}`,
128+
...getHeaders(),
127129
...(options.headers || {}),
130+
authorization: `Bearer ${accessTokenState.value}`,
128131
};
129132

130133
let result = await $graphql.default.request(documentNode, variables, requestHeaders);

src/runtime/composables/gql-mutation.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import type { IGraphQLOptions } from '../interfaces/graphql-options.interface';
88
import { hashPasswords } from '../functions/graphql-meta';
99
import { useAuthState } from '../states/auth';
1010
import { useAuth } from './use-auth';
11+
import { useRequestOptions } from './use-request-options';
1112

1213
export async function gqlMutation<T = any>(method: string, options: IGraphQLOptions = {}): Promise<{ data: T; error: GraphqlError }> {
1314
const { $graphql, _meta } = useNuxtApp();
1415
const _nuxtApp = useNuxtApp();
1516
const { accessTokenState, refreshTokenState } = useAuthState();
1617
const { checkTokenAndRenew } = useAuth();
18+
const { getHeaders } = useRequestOptions();
1719

1820
// Check parameters
1921
if (!method) {
@@ -133,8 +135,9 @@ export async function gqlMutation<T = any>(method: string, options: IGraphQLOpti
133135
}
134136

135137
const requestHeaders: Record<string, string> = {
136-
authorization: `Bearer ${method === 'refreshToken' ? refreshTokenState.value : accessTokenState.value}`,
138+
...getHeaders(),
137139
...(options.headers || {}),
140+
authorization: `Bearer ${method === 'refreshToken' ? refreshTokenState.value : accessTokenState.value}`,
138141
};
139142

140143
let data;

src/runtime/composables/gql-query.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import type { IGraphQLOptions } from '../interfaces/graphql-options.interface';
88
import { hashPasswords } from '../functions/graphql-meta';
99
import { useAuthState } from '../states/auth';
1010
import { useAuth } from './use-auth';
11+
import { useRequestOptions } from './use-request-options';
1112

1213
export async function gqlQuery<T = any>(method: string, options: IGraphQLOptions = {}): Promise<{ data: T; error: GraphqlError | null }> {
1314
const { $graphql, _meta } = useNuxtApp();
1415
const _nuxtApp = useNuxtApp();
1516
const { accessTokenState } = useAuthState();
1617
const { checkTokenAndRenew } = useAuth();
18+
const { getHeaders } = useRequestOptions();
1719

1820
// Check parameters
1921
if (!method) {
@@ -122,8 +124,9 @@ export async function gqlQuery<T = any>(method: string, options: IGraphQLOptions
122124
await callWithNuxt(_nuxtApp, checkTokenAndRenew);
123125

124126
const requestHeaders: Record<string, string> = {
125-
authorization: `Bearer ${accessTokenState.value}`,
127+
...getHeaders(),
126128
...(options.headers || {}),
129+
authorization: `Bearer ${accessTokenState.value}`,
127130
};
128131

129132
let data = null;

src/runtime/composables/use-auth-fetch.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useRuntimeConfig } from 'nuxt/app';
44

55
import { useAuthState } from '../states/auth';
66
import { useAuth } from './use-auth';
7+
import { useRequestOptions } from './use-request-options';
78

89
export function useAuthFetch<
910
DefaultT = unknown,
@@ -14,6 +15,7 @@ export function useAuthFetch<
1415
>(request: R, opts?: O): Promise<TypedInternalResponse<R, T, ExtractedRouteMethod<R, O>>> {
1516
const { requestNewToken } = useAuth();
1617
const { accessTokenState } = useAuthState();
18+
const { getHeaders } = useRequestOptions();
1719
const config = useRuntimeConfig();
1820

1921
// @ts-expect-error - because of nice types from ofetch <3
@@ -23,6 +25,7 @@ export function useAuthFetch<
2325
async onRequest(data: any) {
2426
if (accessTokenState.value) {
2527
data.options.headers = {
28+
...getHeaders(),
2629
...data.options.headers,
2730
Authorization: `Bearer ${accessTokenState.value}`,
2831
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function useRequestOptions() {
2+
let headers: Record<string, string> = {};
3+
const setHeaders = (newHeaders: Record<string, string>) => {
4+
headers = newHeaders;
5+
};
6+
const getHeaders = () => {
7+
return headers;
8+
};
9+
10+
return {
11+
getHeaders,
12+
setHeaders,
13+
};
14+
}

src/runtime/plugins/ws.client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ import { createClient } from 'graphql-ws';
33
import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from 'nuxt/app';
44
import { reactive } from 'vue';
55

6+
import { useRequestOptions } from '../composables/use-request-options';
7+
68
export const wsHeaders = reactive<Record<string, string>>({});
79

810
export default defineNuxtPlugin({
911
name: 'ws',
1012
async setup() {
1113
const nuxtApp = useNuxtApp();
1214
const { wsUrl } = useRuntimeConfig().public;
15+
const { getHeaders } = useRequestOptions();
1316

1417
const client = createClient({
1518
connectionParams: async () => {
1619
const { accessTokenState } = useAuthState();
1720
return {
1821
Authorization: 'Bearer ' + accessTokenState.value,
22+
...getHeaders(),
1923
...wsHeaders,
2024
};
2125
},

0 commit comments

Comments
 (0)