Skip to content

Commit

Permalink
added option to setHeader in plugin context
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Mar 1, 2023
1 parent e837238 commit 5722446
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Expand Up @@ -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();
Expand Down
Expand Up @@ -3,6 +3,7 @@ import { Store } from '@ngrx/store';
import {
CreateActionOptions,
CreatePanelOptions,
PluginContext,
PluginContextGenerator,
PluginWindowState,
} from 'altair-graphql-core/build/plugin/context/context.interface';
Expand All @@ -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';

Expand All @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit 5722446

Please sign in to comment.