diff --git a/packages/altair-app/src/app/modules/altair/services/plugin/context/plugin-context.service.spec.ts b/packages/altair-app/src/app/modules/altair/services/plugin/context/plugin-context.service.spec.ts index b5278339d4..ac06eafa6f 100644 --- a/packages/altair-app/src/app/modules/altair/services/plugin/context/plugin-context.service.spec.ts +++ b/packages/altair-app/src/app/modules/altair/services/plugin/context/plugin-context.service.spec.ts @@ -107,6 +107,27 @@ describe('PluginContextService', () => { }); }); + describe('setHeader', () => { + it('should dispatch set header action', async () => { + const ctx = createContext(); + const windowState = await ctx.app.getCurrentWindowState(); + const currentWindowId = windowState!.windowId; + await ctx.app.setHeader(currentWindowId, 'x-test-header', 'test-value'); + expect(mockStore.dispatch).toHaveBeenCalledWith( + expect.objectContaining({ + payload: { + headers: [ + { enabled: true, key: '', value: '' }, + { enabled: true, key: 'x-test-header', value: 'test-value' }, + ], + }, + type: 'SET_HEADERS', + windowId: 'def-456', + }) + ); + }); + }); + describe('createPanel', () => { it('should return new panel', () => { const ctx = createContext(); diff --git a/packages/altair-app/src/app/modules/altair/services/plugin/context/plugin-context.service.ts b/packages/altair-app/src/app/modules/altair/services/plugin/context/plugin-context.service.ts index 4c134e0c2b..d0f96e8c4a 100644 --- a/packages/altair-app/src/app/modules/altair/services/plugin/context/plugin-context.service.ts +++ b/packages/altair-app/src/app/modules/altair/services/plugin/context/plugin-context.service.ts @@ -3,6 +3,7 @@ import { Store } from '@ngrx/store'; import { CreateActionOptions, CreatePanelOptions, + PluginContext, PluginContextGenerator, PluginWindowState, } from 'altair-graphql-core/build/plugin/context/context.interface'; @@ -24,6 +25,7 @@ import * as fromRoot from '../../../store'; import * as queryActions from '../../../store/query/query.action'; import * as variablesActions from '../../../store/variables/variables.action'; +import * as headersActions from '../../../store/headers/headers.action'; import * as localActions from '../../../store/local/local.action'; import * as settingsActions from '../../../store/settings/settings.action'; @@ -32,6 +34,7 @@ import { first, take } from 'rxjs/operators'; import { ThemeRegistryService } from '../../../services/theme/theme-registry.service'; import { NotifyService } from '../../../services/notify/notify.service'; import { SubscriptionProviderRegistryService } from '../../subscriptions/subscription-provider-registry.service'; +import { headerListToMap, headerMapToList } from '../../../utils/headers'; import { AltairPanel, AltairPanelLocation, @@ -54,7 +57,7 @@ export class PluginContextService implements PluginContextGenerator { private notifyService: NotifyService ) {} - createContext(pluginName: string, plugin: AltairPlugin) { + createContext(pluginName: string, plugin: AltairPlugin): PluginContext { const self = this; const log = (msg: string) => debug.log(`PLUGIN[${pluginName}]: ${msg}`); const eventBus = this.pluginEventService.group(); @@ -161,6 +164,20 @@ export class PluginContextService implements PluginContextGenerator { new queryActions.SendIntrospectionQueryRequestAction(windowId) ); }, + async setHeader(windowId, key, value) { + log(`setting header: ${key}`); + const state = await this.getWindowState(windowId); + const headers = state?.headers ?? []; + const obj = headerListToMap(headers); + + obj[key] = value; + self.store.dispatch( + new headersActions.SetHeadersAction( + { headers: headerMapToList(obj) }, + windowId + ) + ); + }, addSubscriptionProvider(providerData: SubscriptionProviderData) { log(`adding subscription provider: ${providerData.id}`); self.subscriptionProviderRegistryService.addProviderData( diff --git a/packages/altair-core/src/plugin/context/context.interface.ts b/packages/altair-core/src/plugin/context/context.interface.ts index 34f6536ead..d344787889 100644 --- a/packages/altair-core/src/plugin/context/context.interface.ts +++ b/packages/altair-core/src/plugin/context/context.interface.ts @@ -67,6 +67,7 @@ export interface PluginContext { createWindow(data: ExportWindowState): void; setQuery(windowId: string, query: string): void; setVariables(windowId: string, variables: string): void; + setHeader(windowId: string, key: string, value: string): void; setEndpoint(windowId: string, url: string): void; addSubscriptionProvider(providerData: SubscriptionProviderData): void; executeCommand(): void; // TODO: To be defined