Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/runtime/composables/gql-async-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { hashPasswords } from '../functions/graphql-meta';
import { useAuthState } from '../states/auth';
import { useAuth } from './use-auth';
import { useHelper } from './use-helper';
import { useRequestOptions } from './use-request-options';

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

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

const requestHeaders: Record<string, string> = {
authorization: `Bearer ${accessTokenState.value}`,
...getHeaders(),
...(options.headers || {}),
authorization: `Bearer ${accessTokenState.value}`,
};

let result = await $graphql.default.request(documentNode, variables, requestHeaders);
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/composables/gql-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import type { IGraphQLOptions } from '../interfaces/graphql-options.interface';
import { hashPasswords } from '../functions/graphql-meta';
import { useAuthState } from '../states/auth';
import { useAuth } from './use-auth';
import { useRequestOptions } from './use-request-options';

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

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

const requestHeaders: Record<string, string> = {
authorization: `Bearer ${method === 'refreshToken' ? refreshTokenState.value : accessTokenState.value}`,
...getHeaders(),
...(options.headers || {}),
authorization: `Bearer ${method === 'refreshToken' ? refreshTokenState.value : accessTokenState.value}`,
};

let data;
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/composables/gql-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import type { IGraphQLOptions } from '../interfaces/graphql-options.interface';
import { hashPasswords } from '../functions/graphql-meta';
import { useAuthState } from '../states/auth';
import { useAuth } from './use-auth';
import { useRequestOptions } from './use-request-options';

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

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

const requestHeaders: Record<string, string> = {
authorization: `Bearer ${accessTokenState.value}`,
...getHeaders(),
...(options.headers || {}),
authorization: `Bearer ${accessTokenState.value}`,
};

let data = null;
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/composables/use-auth-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useRuntimeConfig } from 'nuxt/app';

import { useAuthState } from '../states/auth';
import { useAuth } from './use-auth';
import { useRequestOptions } from './use-request-options';

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

// @ts-expect-error - because of nice types from ofetch <3
Expand All @@ -23,6 +25,7 @@ export function useAuthFetch<
async onRequest(data: any) {
if (accessTokenState.value) {
data.options.headers = {
...getHeaders(),
...data.options.headers,
Authorization: `Bearer ${accessTokenState.value}`,
};
Expand Down
14 changes: 14 additions & 0 deletions src/runtime/composables/use-request-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function useRequestOptions() {
let headers: Record<string, string> = {};
const setHeaders = (newHeaders: Record<string, string>) => {
headers = newHeaders;
};
const getHeaders = () => {
return headers;
};

return {
getHeaders,
setHeaders,
};
}
4 changes: 4 additions & 0 deletions src/runtime/plugins/ws.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ import { createClient } from 'graphql-ws';
import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from 'nuxt/app';
import { reactive } from 'vue';

import { useRequestOptions } from '../composables/use-request-options';

export const wsHeaders = reactive<Record<string, string>>({});

export default defineNuxtPlugin({
name: 'ws',
async setup() {
const nuxtApp = useNuxtApp();
const { wsUrl } = useRuntimeConfig().public;
const { getHeaders } = useRequestOptions();

const client = createClient({
connectionParams: async () => {
const { accessTokenState } = useAuthState();
return {
Authorization: 'Bearer ' + accessTokenState.value,
...getHeaders(),
...wsHeaders,
};
},
Expand Down