Skip to content

Commit 8205a91

Browse files
committed
added option to setHeader in plugin context
1 parent e837238 commit 8205a91

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

packages/altair-app/src/app/modules/altair/services/plugin/context/plugin-context.service.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ describe('PluginContextService', () => {
107107
});
108108
});
109109

110+
describe('setHeader', () => {
111+
it('should dispatch set header action', async () => {
112+
const ctx = createContext();
113+
const windowState = await ctx.app.getCurrentWindowState();
114+
const currentWindowId = windowState!.windowId;
115+
await ctx.app.setHeader(currentWindowId, 'x-test-header', 'test-value');
116+
expect(mockStore.dispatch).toHaveBeenCalledWith(
117+
expect.objectContaining({
118+
payload: {
119+
headers: [
120+
{ enabled: true, key: '', value: '' },
121+
{ enabled: true, key: 'x-test-header', value: 'test-value' },
122+
],
123+
},
124+
type: 'SET_HEADERS',
125+
windowId: 'def-456',
126+
})
127+
);
128+
});
129+
});
130+
110131
describe('createPanel', () => {
111132
it('should return new panel', () => {
112133
const ctx = createContext();

packages/altair-app/src/app/modules/altair/services/plugin/context/plugin-context.service.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Store } from '@ngrx/store';
33
import {
44
CreateActionOptions,
55
CreatePanelOptions,
6+
PluginContext,
67
PluginContextGenerator,
78
PluginWindowState,
89
} from 'altair-graphql-core/build/plugin/context/context.interface';
@@ -24,6 +25,7 @@ import * as fromRoot from '../../../store';
2425

2526
import * as queryActions from '../../../store/query/query.action';
2627
import * as variablesActions from '../../../store/variables/variables.action';
28+
import * as headersActions from '../../../store/headers/headers.action';
2729
import * as localActions from '../../../store/local/local.action';
2830
import * as settingsActions from '../../../store/settings/settings.action';
2931

@@ -32,6 +34,7 @@ import { first, take } from 'rxjs/operators';
3234
import { ThemeRegistryService } from '../../../services/theme/theme-registry.service';
3335
import { NotifyService } from '../../../services/notify/notify.service';
3436
import { SubscriptionProviderRegistryService } from '../../subscriptions/subscription-provider-registry.service';
37+
import { headerListToMap, headerMapToList } from '../../../utils/headers';
3538
import {
3639
AltairPanel,
3740
AltairPanelLocation,
@@ -54,7 +57,7 @@ export class PluginContextService implements PluginContextGenerator {
5457
private notifyService: NotifyService
5558
) {}
5659

57-
createContext(pluginName: string, plugin: AltairPlugin) {
60+
createContext(pluginName: string, plugin: AltairPlugin): PluginContext {
5861
const self = this;
5962
const log = (msg: string) => debug.log(`PLUGIN[${pluginName}]: ${msg}`);
6063
const eventBus = this.pluginEventService.group();
@@ -161,6 +164,20 @@ export class PluginContextService implements PluginContextGenerator {
161164
new queryActions.SendIntrospectionQueryRequestAction(windowId)
162165
);
163166
},
167+
async setHeader(windowId, key, value) {
168+
log(`setting header: ${key}`);
169+
const state = await this.getWindowState(windowId);
170+
const headers = state?.headers ?? [];
171+
const obj = headerListToMap(headers);
172+
173+
obj[key] = value;
174+
self.store.dispatch(
175+
new headersActions.SetHeadersAction(
176+
{ headers: headerMapToList(obj) },
177+
windowId
178+
)
179+
);
180+
},
164181
addSubscriptionProvider(providerData: SubscriptionProviderData) {
165182
log(`adding subscription provider: ${providerData.id}`);
166183
self.subscriptionProviderRegistryService.addProviderData(

packages/altair-core/src/plugin/context/context.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export interface PluginContext {
6767
createWindow(data: ExportWindowState): void;
6868
setQuery(windowId: string, query: string): void;
6969
setVariables(windowId: string, variables: string): void;
70+
setHeader(windowId: string, key: string, value: string): void;
7071
setEndpoint(windowId: string, url: string): void;
7172
addSubscriptionProvider(providerData: SubscriptionProviderData): void;
7273
executeCommand(): void; // TODO: To be defined

0 commit comments

Comments
 (0)