Skip to content

added option to setHeader in plugin context #2135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2023
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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